Commit 5b139c69ea03e31d32a4e300c100124c5213db1e
Exists in
master
and in
4 other branches
Merge pull request #105 from colab/improve_widget
Refactored widgets to receive **kwargs
Showing
3 changed files
with
6 additions
and
6 deletions
Show diff stats
colab/widgets/templatetags/widgets_tag.py
... | ... | @@ -9,6 +9,5 @@ register = template.Library() |
9 | 9 | def import_widgets(context, area_id, widget_var=None): |
10 | 10 | if not widget_var: |
11 | 11 | widget_var = "widgets_{}".format(area_id) |
12 | - context[widget_var] = WidgetManager.get_widgets(area_id, | |
13 | - context['request']) | |
12 | + context[widget_var] = WidgetManager.get_widgets(area_id, context=context) | |
14 | 13 | return "" | ... | ... |
colab/widgets/widget_manager.py
... | ... | @@ -28,7 +28,7 @@ class Widget(object): |
28 | 28 | head = self.content[start + len('<head>'):end] |
29 | 29 | return mark_safe(head) |
30 | 30 | |
31 | - def generate_content(self, request=None): | |
31 | + def generate_content(self, **kwarg): | |
32 | 32 | self.content = '' |
33 | 33 | |
34 | 34 | |
... | ... | @@ -50,11 +50,11 @@ class WidgetManager(object): |
50 | 50 | WidgetManager.widget_categories[category].remove(widget) |
51 | 51 | |
52 | 52 | @staticmethod |
53 | - def get_widgets(category, request=None): | |
53 | + def get_widgets(category, **kargs): | |
54 | 54 | if category not in WidgetManager.widget_categories: |
55 | 55 | return [] |
56 | 56 | |
57 | 57 | widgets = WidgetManager.widget_categories[category][:] |
58 | 58 | for widget in widgets: |
59 | - widget.generate_content(request) | |
59 | + widget.generate_content(**kargs) | |
60 | 60 | return widgets | ... | ... |
docs/source/dev.rst
... | ... | @@ -43,12 +43,13 @@ Example Widget: |
43 | 43 | class MyWidget(Widget): |
44 | 44 | identifier = 'my_widget_id' |
45 | 45 | name = 'My Widget' |
46 | - def generate_content(self, request): | |
46 | + def generate_content(self, **kwargs): | |
47 | 47 | # process HTML content |
48 | 48 | self.content = processed_content |
49 | 49 | |
50 | 50 | To add the widget in a view check the Widgets section in User Documentation. |
51 | 51 | To use a widget in the templates, you have to use the ``import_widget`` tag inside the ``html`` block. |
52 | +This way, the kwargs parameter will have a ``context`` key from the ``html``. | |
52 | 53 | You can also set the variable that the widgets of an area will be imported. |
53 | 54 | Or you can use the default name, which is ``widgets_area_name``. |
54 | 55 | For example, in the ``profile`` area the variable name is ``widgets_profile``. | ... | ... |