Commit d99d8ebc0075922ea1ebb4b1b9772e7ed7520595

Authored by Gustavo Jaruga Cruz
2 parents 7397ed19 dc49d1d7

Merge pull request #103 from colab/improve_widget

Improve widget
colab/accounts/templates/accounts/user_update_form.html
1 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 9 {% block head_js %}
5 10 <script>
... ... @@ -105,7 +110,7 @@ $(function() {
105 110 {% block head %}
106 111 {{ block.super }}
107 112  
108   - {% for widget in widgets %}
  113 + {% for widget in widgets_profile %}
109 114 {{ widget.get_header }}
110 115 {% endfor %}
111 116  
... ... @@ -129,7 +134,7 @@ $(function() {
129 134 <!-- Start of navs -->
130 135 <ul class="nav nav-tabs">
131 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 138 <li>
134 139 <a data-toggle="pill" href="#{{ widget.identifier }}">{{ widget.name }}</a>
135 140 </li>
... ... @@ -229,7 +234,7 @@ $(function() {
229 234 </div>
230 235 </form>
231 236 </div>
232   - {% for widget in widgets %}
  237 + {% for widget in widgets_profile %}
233 238 <div id="{{ widget.identifier }}" class="tab-pane fade">
234 239 <h2>{{ widget.name }}</h2>
235 240 {{ widget.get_body }}
... ...
colab/accounts/views.py
... ... @@ -15,7 +15,6 @@ from colab.super_archives.models import (EmailAddress,
15 15 EmailAddressValidation)
16 16 from colab.search.utils import get_collaboration_data, get_visible_threads
17 17 from colab.accounts.models import User
18   -from colab.widgets.widget_manager import WidgetManager
19 18  
20 19 from .forms import (UserCreationForm, ListsForm, UserUpdateForm)
21 20 from .utils import mailman
... ... @@ -43,12 +42,6 @@ class UserProfileUpdateView(UserProfileBaseMixin, UpdateView):
43 42  
44 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 46 class UserProfileDetailView(UserProfileBaseMixin, DetailView):
54 47 template_name = 'accounts/user_detail.html'
... ...
colab/accounts/widgets/__init__.py 0 → 100644
colab/settings.py
... ... @@ -53,6 +53,7 @@ INSTALLED_APPS = (
53 53 'colab',
54 54 'colab.home',
55 55 'colab.plugins',
  56 + 'colab.widgets',
56 57 'colab.super_archives',
57 58 'colab.rss',
58 59 'colab.search',
... ...
colab/templates/base.html
... ... @@ -2,6 +2,7 @@
2 2 {% load i18n gravatar plugins %}
3 3 {% load static from staticfiles %}
4 4  
  5 +{% block html %}
5 6 <html>
6 7 <head>
7 8 {% block head %}
... ... @@ -101,3 +102,4 @@
101 102 {% block footer_js %}{% endblock %}
102 103 </body>
103 104 </html>
  105 +{% endblock %}
... ...
colab/widgets/templatetags/__init__.py 0 → 100644
colab/widgets/templatetags/widgets_tag.py 0 → 100644
... ... @@ -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 ""
... ...
colab/widgets/tests/test_widgets.py 0 → 100644
... ... @@ -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 48 self.content = processed_content
49 49  
50 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.
... ...