Commit d99d8ebc0075922ea1ebb4b1b9772e7ed7520595
Exists in
master
and in
4 other branches
Merge pull request #103 from colab/improve_widget
Improve widget
Showing
9 changed files
with
84 additions
and
11 deletions
Show diff stats
colab/accounts/templates/accounts/user_update_form.html
1 | {% extends "base.html" %} | 1 | {% extends "base.html" %} |
2 | -{% load i18n gravatar plugins %} | 2 | +{% load i18n gravatar plugins widgets_tag %} |
3 | + | ||
4 | +{% block html %} | ||
5 | + {% import_widgets 'profile' %} | ||
6 | + {{ block.super }} | ||
7 | +{% endblock %} | ||
3 | 8 | ||
4 | {% block head_js %} | 9 | {% block head_js %} |
5 | <script> | 10 | <script> |
@@ -105,7 +110,7 @@ $(function() { | @@ -105,7 +110,7 @@ $(function() { | ||
105 | {% block head %} | 110 | {% block head %} |
106 | {{ block.super }} | 111 | {{ block.super }} |
107 | 112 | ||
108 | - {% for widget in widgets %} | 113 | + {% for widget in widgets_profile %} |
109 | {{ widget.get_header }} | 114 | {{ widget.get_header }} |
110 | {% endfor %} | 115 | {% endfor %} |
111 | 116 | ||
@@ -129,7 +134,7 @@ $(function() { | @@ -129,7 +134,7 @@ $(function() { | ||
129 | <!-- Start of navs --> | 134 | <!-- Start of navs --> |
130 | <ul class="nav nav-tabs"> | 135 | <ul class="nav nav-tabs"> |
131 | <li class="active"><a data-toggle="pill" href="#profile">Profile</a></li> | 136 | <li class="active"><a data-toggle="pill" href="#profile">Profile</a></li> |
132 | - {% for widget in widgets %} | 137 | + {% for widget in widgets_profile %} |
133 | <li> | 138 | <li> |
134 | <a data-toggle="pill" href="#{{ widget.identifier }}">{{ widget.name }}</a> | 139 | <a data-toggle="pill" href="#{{ widget.identifier }}">{{ widget.name }}</a> |
135 | </li> | 140 | </li> |
@@ -229,7 +234,7 @@ $(function() { | @@ -229,7 +234,7 @@ $(function() { | ||
229 | </div> | 234 | </div> |
230 | </form> | 235 | </form> |
231 | </div> | 236 | </div> |
232 | - {% for widget in widgets %} | 237 | + {% for widget in widgets_profile %} |
233 | <div id="{{ widget.identifier }}" class="tab-pane fade"> | 238 | <div id="{{ widget.identifier }}" class="tab-pane fade"> |
234 | <h2>{{ widget.name }}</h2> | 239 | <h2>{{ widget.name }}</h2> |
235 | {{ widget.get_body }} | 240 | {{ widget.get_body }} |
colab/accounts/views.py
@@ -15,7 +15,6 @@ from colab.super_archives.models import (EmailAddress, | @@ -15,7 +15,6 @@ from colab.super_archives.models import (EmailAddress, | ||
15 | EmailAddressValidation) | 15 | EmailAddressValidation) |
16 | from colab.search.utils import get_collaboration_data, get_visible_threads | 16 | from colab.search.utils import get_collaboration_data, get_visible_threads |
17 | from colab.accounts.models import User | 17 | from colab.accounts.models import User |
18 | -from colab.widgets.widget_manager import WidgetManager | ||
19 | 18 | ||
20 | from .forms import (UserCreationForm, ListsForm, UserUpdateForm) | 19 | from .forms import (UserCreationForm, ListsForm, UserUpdateForm) |
21 | from .utils import mailman | 20 | from .utils import mailman |
@@ -43,12 +42,6 @@ class UserProfileUpdateView(UserProfileBaseMixin, UpdateView): | @@ -43,12 +42,6 @@ class UserProfileUpdateView(UserProfileBaseMixin, UpdateView): | ||
43 | 42 | ||
44 | return obj | 43 | return obj |
45 | 44 | ||
46 | - def get_context_data(self, **kwargs): | ||
47 | - context = {} | ||
48 | - context['widgets'] = WidgetManager.get_widgets('profile', self.request) | ||
49 | - context.update(kwargs) | ||
50 | - return super(UserProfileUpdateView, self).get_context_data(**context) | ||
51 | - | ||
52 | 45 | ||
53 | class UserProfileDetailView(UserProfileBaseMixin, DetailView): | 46 | class UserProfileDetailView(UserProfileBaseMixin, DetailView): |
54 | template_name = 'accounts/user_detail.html' | 47 | template_name = 'accounts/user_detail.html' |
colab/settings.py
@@ -53,6 +53,7 @@ INSTALLED_APPS = ( | @@ -53,6 +53,7 @@ INSTALLED_APPS = ( | ||
53 | 'colab', | 53 | 'colab', |
54 | 'colab.home', | 54 | 'colab.home', |
55 | 'colab.plugins', | 55 | 'colab.plugins', |
56 | + 'colab.widgets', | ||
56 | 'colab.super_archives', | 57 | 'colab.super_archives', |
57 | 'colab.rss', | 58 | 'colab.rss', |
58 | 'colab.search', | 59 | 'colab.search', |
colab/templates/base.html
@@ -2,6 +2,7 @@ | @@ -2,6 +2,7 @@ | ||
2 | {% load i18n gravatar plugins %} | 2 | {% load i18n gravatar plugins %} |
3 | {% load static from staticfiles %} | 3 | {% load static from staticfiles %} |
4 | 4 | ||
5 | +{% block html %} | ||
5 | <html> | 6 | <html> |
6 | <head> | 7 | <head> |
7 | {% block head %} | 8 | {% block head %} |
@@ -101,3 +102,4 @@ | @@ -101,3 +102,4 @@ | ||
101 | {% block footer_js %}{% endblock %} | 102 | {% block footer_js %}{% endblock %} |
102 | </body> | 103 | </body> |
103 | </html> | 104 | </html> |
105 | +{% endblock %} |
@@ -0,0 +1,14 @@ | @@ -0,0 +1,14 @@ | ||
1 | +from django import template | ||
2 | +from colab.widgets.widget_manager import WidgetManager | ||
3 | + | ||
4 | + | ||
5 | +register = template.Library() | ||
6 | + | ||
7 | + | ||
8 | +@register.simple_tag(takes_context=True) | ||
9 | +def import_widgets(context, area_id, widget_var=None): | ||
10 | + if not widget_var: | ||
11 | + widget_var = "widgets_{}".format(area_id) | ||
12 | + context[widget_var] = WidgetManager.get_widgets(area_id, | ||
13 | + context['request']) | ||
14 | + return "" |
@@ -0,0 +1,29 @@ | @@ -0,0 +1,29 @@ | ||
1 | +import unittest | ||
2 | +from mock import patch | ||
3 | + | ||
4 | +from colab.widgets.templatetags.widgets_tag import import_widgets | ||
5 | +from colab.widgets.widget_manager import WidgetManager | ||
6 | + | ||
7 | + | ||
8 | +class WidgetsTest(unittest.TestCase): | ||
9 | + @patch.object(WidgetManager, 'get_widgets') | ||
10 | + def test_import_widgets_tag(self, get_widgets): | ||
11 | + return_list = [1, 2, 3] | ||
12 | + get_widgets.return_value = return_list | ||
13 | + | ||
14 | + context = {'request': ""} | ||
15 | + import_widgets(context, 'area') | ||
16 | + | ||
17 | + self.assertIn('widgets_area', context) | ||
18 | + self.assertEquals(context['widgets_area'], return_list) | ||
19 | + | ||
20 | + @patch.object(WidgetManager, 'get_widgets') | ||
21 | + def test_import_widgets_tag_with_named_var(self, get_widgets): | ||
22 | + return_list = [1, 2, 3] | ||
23 | + get_widgets.return_value = return_list | ||
24 | + | ||
25 | + context = {'request': ""} | ||
26 | + import_widgets(context, 'area', 'var') | ||
27 | + | ||
28 | + self.assertIn('var', context) | ||
29 | + self.assertEquals(context['var'], return_list) |
docs/source/dev.rst
@@ -48,3 +48,32 @@ Example Widget: | @@ -48,3 +48,32 @@ Example Widget: | ||
48 | self.content = processed_content | 48 | self.content = processed_content |
49 | 49 | ||
50 | To add the widget in a view check the Widgets section in User Documentation. | 50 | To add the widget in a view check the Widgets section in User Documentation. |
51 | +To use a widget in the templates, you have to use the ``import_widget`` tag inside the ``html`` block. | ||
52 | +You can also set the variable that the widgets of an area will be imported. | ||
53 | +Or you can use the default name, which is ``widgets_area_name``. | ||
54 | +For example, in the ``profile`` area the variable name is ``widgets_profile``. | ||
55 | +This variable will be inserted directly in the page ``context``. | ||
56 | + | ||
57 | +.. code-block:: python | ||
58 | + | ||
59 | + {% load widgets_tag %} | ||
60 | + | ||
61 | + {% block html %} | ||
62 | + {% import_widgets 'profile' %} | ||
63 | + {{ block.super }} | ||
64 | + {% endblock %} | ||
65 | + | ||
66 | + {# example of how to use #} | ||
67 | + {% block head %} | ||
68 | + {{ block.super }} | ||
69 | + | ||
70 | + {% for widget in widgets_profile %} | ||
71 | + {{ widget.get_header }} | ||
72 | + {% endfor %} | ||
73 | + | ||
74 | + {% endblock %} | ||
75 | + | ||
76 | + | ||
77 | +.. warning:: | ||
78 | + | ||
79 | + Warning! Remember to use the tag ``{{ block.super }}`` inside the html block. Otherwise, the page will appear blank. |