forked from frappe/lms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidgets.py
63 lines (51 loc) · 1.73 KB
/
widgets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""The widgets provides access to HTML widgets
provided in each frappe module.
Widgets are simple moduler templates that can reused
in multiple places. These are like macros, but accessing
them will be a lot easier.
The widgets will be provided
"""
import frappe
from frappe.utils.jinja import get_jenv
# search path for widgets.
# When {{widgets.SomeWidget()}} is called, it looks for
# widgets/SomeWidgets.html in each of these modules.
MODULES = [
"lms",
]
def update_website_context(context):
"""Adds widgets to the context.
Called from hooks.
"""
context.widgets = Widgets()
class Widgets:
"""The widget collection.
This is just a placeholder object and returns the appropriate
widget when accessed using attribute.
>>> widgets = Widgets()
>>> widgets.HelloWorld(name="World!")
'<div>Hello, World!</div>'
"""
def __getattr__(self, name):
widget_globals = {"widgets": self}
if not name.startswith("__"):
return Widget(name, widget_globals)
else:
raise AttributeError(name)
class Widget:
"""The Widget class renders a widget.
Widget is a reusable template defined in widgets/ directory in
each frappe module.
>>> w = Widget("HelloWorld")
>>> w(name="World!")
'<div>Hello, World!</div>'
"""
def __init__(self, name, widget_globals={}):
self.widget_globals = widget_globals
self.name = name
def __call__(self, **kwargs):
# the widget could be in any of the modules
paths = [f"{module}/widgets/{self.name}.html" for module in MODULES]
env = get_jenv()
kwargs.update(self.widget_globals)
return env.get_or_select_template(paths).render(kwargs)