Commit 4a22801da91a742387a86bcfd34d807e363e387d
Committed by
Macartur Sousa
1 parent
da128030
Exists in
master
and in
4 other branches
Edited generate_content default behavior
Signed-off-by: Luiz Oliveira <ziuloliveira@gmail.com> Signed-off-by: Macartur Sousa <macartur.sc@gmail.com>
Showing
4 changed files
with
63 additions
and
16 deletions
Show diff stats
colab/widgets/templatetags/widgets_tag.py
1 | 1 | from django import template |
2 | 2 | from colab.widgets.widget_manager import WidgetManager |
3 | -from django.template import Template | |
4 | 3 | |
5 | 4 | |
6 | 5 | register = template.Library() |
... | ... | @@ -13,7 +12,4 @@ def import_widgets(context, area_id, widget_var=None): |
13 | 12 | |
14 | 13 | context[widget_var] = WidgetManager.get_widgets(area_id, context=context) |
15 | 14 | |
16 | - for widget in context[widget_var]: | |
17 | - widget.content = Template(widget.content).render(context) | |
18 | - | |
19 | 15 | return "" | ... | ... |
colab/widgets/tests/test_widget_manager.py
... | ... | @@ -3,34 +3,54 @@ from django.test import TestCase |
3 | 3 | from colab.widgets.widget_manager import WidgetManager, Widget |
4 | 4 | |
5 | 5 | |
6 | +class WigetMock(Widget): | |
7 | + | |
8 | + def __init__(self, content=""): | |
9 | + self.content = content | |
10 | + | |
11 | + | |
6 | 12 | class WidgetManagerTest(TestCase): |
7 | 13 | |
8 | 14 | html_content = "<head><meta charset='UTF-8'></head><body><p>T</p></body>" |
9 | 15 | widget_area = 'profile' |
10 | 16 | widget_id = 'widget_id' |
11 | 17 | |
12 | - def custom_widget_instance(self, content): | |
18 | + def ovewrited_widget_instance(self, content): | |
13 | 19 | |
14 | - class CustomWidget(Widget): | |
20 | + class WidgetOverwrited(Widget): | |
15 | 21 | identifier = 'widget_id' |
16 | 22 | |
17 | 23 | def generate_content(self, request=None): |
18 | 24 | self.content = content |
19 | - return CustomWidget() | |
25 | + return WidgetOverwrited() | |
26 | + | |
27 | + def default_widget_instance(self): | |
28 | + | |
29 | + class WidgetDefault(Widget): | |
30 | + pass | |
31 | + | |
32 | + return WidgetDefault() | |
20 | 33 | |
21 | 34 | def setUp(self): |
22 | - custom_widget = self.custom_widget_instance(self.html_content) | |
35 | + custom_widget = self.ovewrited_widget_instance(self.html_content) | |
23 | 36 | WidgetManager.register_widget(self.widget_area, custom_widget) |
24 | 37 | |
25 | 38 | def tearDown(self): |
26 | 39 | WidgetManager.unregister_widget(self.widget_area, self.widget_id) |
27 | 40 | |
41 | + def test_widget_default_values(self): | |
42 | + widget = self.default_widget_instance() | |
43 | + self.assertEqual(widget.identifier, None) | |
44 | + self.assertEqual(widget.name, None) | |
45 | + self.assertEqual(widget.content, '') | |
46 | + self.assertEqual(widget.template, '') | |
47 | + | |
28 | 48 | def test_add_widgets_to_key_area(self): |
29 | 49 | self.assertEqual(len(WidgetManager.get_widgets(self.widget_area)), 1) |
30 | 50 | |
31 | 51 | def test_remove_widgets_in_key_area(self): |
32 | 52 | area = 'admin' |
33 | - widget_instance = self.custom_widget_instance(self.html_content) | |
53 | + widget_instance = self.ovewrited_widget_instance(self.html_content) | |
34 | 54 | |
35 | 55 | WidgetManager.register_widget(area, widget_instance) |
36 | 56 | WidgetManager.unregister_widget(area, self.widget_id) |
... | ... | @@ -38,17 +58,41 @@ class WidgetManagerTest(TestCase): |
38 | 58 | self.assertEqual(len(WidgetManager.get_widgets(area)), 0) |
39 | 59 | |
40 | 60 | def test_get_body(self): |
41 | - customWidget = self.custom_widget_instance(self.html_content) | |
61 | + custom_widget = self.ovewrited_widget_instance(self.html_content) | |
42 | 62 | |
43 | - customWidget.generate_content() | |
44 | - self.assertEqual(customWidget.get_body(), "<p>T</p>") | |
63 | + custom_widget.generate_content() | |
64 | + self.assertEqual(custom_widget.get_body(), "<p>T</p>") | |
45 | 65 | |
46 | 66 | def test_get_header(self): |
47 | - customWidget = self.custom_widget_instance(self.html_content) | |
67 | + custom_widget = self.ovewrited_widget_instance(self.html_content) | |
68 | + | |
69 | + custom_widget.generate_content() | |
70 | + self.assertEqual(custom_widget.get_header(), "<meta charset='UTF-8'>") | |
71 | + | |
72 | + def test_get_header_wrong(self): | |
73 | + widget = self.default_widget_instance() | |
74 | + widget.content = "<head> Teste <head>" | |
75 | + self.assertEqual(widget.get_header(), '') | |
48 | 76 | |
49 | - customWidget.generate_content() | |
50 | - self.assertEqual(customWidget.get_header(), "<meta charset='UTF-8'>") | |
77 | + def test_get_body_wrong(self): | |
78 | + widget = self.default_widget_instance() | |
79 | + widget.content = "<body> Teste <body>" | |
80 | + self.assertEqual(widget.get_body(), '') | |
51 | 81 | |
52 | 82 | def test_generate_content(self): |
53 | 83 | widgets = WidgetManager.get_widgets(self.widget_area) |
54 | 84 | self.assertEqual(widgets[0].content, self.html_content) |
85 | + | |
86 | + def test_widget_with_invalid_area(self): | |
87 | + self.assertEqual(WidgetManager.get_widgets("area"), []) | |
88 | + | |
89 | + def test_generate_content_without_template(self): | |
90 | + widget = self.default_widget_instance() | |
91 | + with self.assertRaises(Exception): | |
92 | + widget.generate_content() | |
93 | + | |
94 | + def test_generate_content_with_template(self): | |
95 | + widget = self.default_widget_instance() | |
96 | + widget.template = self.html_content | |
97 | + with self.assertRaises(Exception): | |
98 | + widget.generate_content() | ... | ... |
colab/widgets/tests/test_widgets.py
... | ... | @@ -7,11 +7,13 @@ from django.template import Context |
7 | 7 | |
8 | 8 | |
9 | 9 | class WigetMock(Widget): |
10 | + | |
10 | 11 | def __init__(self, content=""): |
11 | 12 | self.content = content |
12 | 13 | |
13 | 14 | |
14 | 15 | class WidgetsTest(unittest.TestCase): |
16 | + | |
15 | 17 | @patch.object(WidgetManager, 'get_widgets') |
16 | 18 | def test_import_widgets_tag(self, get_widgets): |
17 | 19 | return_list = [WigetMock(), WigetMock(), WigetMock()] | ... | ... |
colab/widgets/widget_manager.py
1 | 1 | from django.utils.safestring import mark_safe |
2 | +from django.template.loader import render_to_string | |
2 | 3 | |
3 | 4 | |
4 | 5 | class Widget(object): |
5 | 6 | identifier = None |
6 | 7 | name = None |
7 | 8 | content = '' |
9 | + template = '' | |
8 | 10 | |
9 | 11 | def get_body(self): |
10 | 12 | # avoiding regex in favor of performance |
... | ... | @@ -29,7 +31,10 @@ class Widget(object): |
29 | 31 | return mark_safe(head) |
30 | 32 | |
31 | 33 | def generate_content(self, **kwargs): |
32 | - self.content = '' | |
34 | + if not self.template: | |
35 | + class_name = self.__class__.__name__ | |
36 | + raise Exception("Template not defined in {}.".format(class_name)) | |
37 | + self.content = render_to_string(self.template, kwargs.get('context')) | |
33 | 38 | |
34 | 39 | |
35 | 40 | class WidgetManager(object): | ... | ... |