Compare View
Commits (4)
Showing
13 changed files
Show diff stats
colab/accounts/views.py
... | ... | @@ -15,7 +15,7 @@ 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.plugins.utils.widget_manager import WidgetManager | |
18 | +from colab.widgets.widget_manager import WidgetManager | |
19 | 19 | |
20 | 20 | from .forms import (UserCreationForm, ListsForm, UserUpdateForm) |
21 | 21 | from .utils import mailman | ... | ... |
colab/utils/conf.py
... | ... | @@ -142,30 +142,38 @@ def load_widgets_settings(): |
142 | 142 | '/etc/colab/widgets_settings.py') |
143 | 143 | settings_module = settings_file.split('.')[-2].split('/')[-1] |
144 | 144 | py_path = "/".join(settings_file.split('/')[:-1]) |
145 | - | |
146 | 145 | logger.info('Widgets Settings file: %s', settings_file) |
147 | 146 | |
148 | 147 | if not os.path.exists(py_path): |
149 | - msg = "The py file {} does not exist".format(py_path) | |
150 | - raise InaccessibleSettings(msg) | |
148 | + return | |
151 | 149 | |
152 | - py_settings = _load_py_file(settings_module, py_path) | |
150 | + original_path = sys.path | |
151 | + sys.path.append(py_path) | |
152 | + importlib.import_module(settings_module) | |
153 | 153 | |
154 | - # Read settings from settings.d | |
155 | - settings_dir = '/etc/colab/widgets.d' | |
154 | + # Read settings from widgets.d | |
155 | + settings_dir = os.getenv('COLAB_WIDGETS', | |
156 | + '/etc/colab/widgets.d') | |
156 | 157 | logger.info('Widgets Settings directory: %s', settings_dir) |
158 | + sys.path = original_path | |
157 | 159 | |
158 | 160 | if not os.path.exists(settings_dir): |
159 | - return py_settings | |
161 | + return | |
160 | 162 | |
161 | 163 | for file_name in os.listdir(settings_dir): |
162 | 164 | if not file_name.endswith('.py'): |
163 | 165 | continue |
164 | 166 | |
167 | + original_path = sys.path | |
168 | + sys.path.append(settings_dir) | |
169 | + | |
165 | 170 | file_module = file_name.split('.')[0] |
166 | - _load_py_file(file_module, settings_dir) | |
171 | + importlib.import_module(file_module) | |
167 | 172 | logger.info('Loaded %s/%s', settings_dir, file_name) |
168 | 173 | |
174 | + sys.path = original_path | |
175 | + | |
176 | + | |
169 | 177 | def validate_database(database_dict, default_db, debug): |
170 | 178 | db_name = database_dict.get('default', {}).get('NAME') |
171 | 179 | if not debug and db_name == default_db: | ... | ... |
... | ... | @@ -0,0 +1,12 @@ |
1 | +from django.test import TestCase | |
2 | + | |
3 | +from colab.widgets.widget_manager import WidgetManager, Widget | |
4 | + | |
5 | +class WidgetManagerTest(TestCase): | |
6 | + | |
7 | + def test_add_widgets_to_key_area(self): | |
8 | + area = 'profile' | |
9 | + WidgetManager.register_widget(area, Widget()) | |
10 | + | |
11 | + self.assertEqual(len(WidgetManager.get_widgets(area)), 1) | |
12 | + | ... | ... |
... | ... | @@ -0,0 +1,53 @@ |
1 | +from django.utils.safestring import mark_safe | |
2 | + | |
3 | +class Widget(object): | |
4 | + identifier = None | |
5 | + name = None | |
6 | + default_url = None | |
7 | + content = '' | |
8 | + | |
9 | + def get_body(self): | |
10 | + # avoiding regex in favor of performance | |
11 | + start = self.content.find('<body>') | |
12 | + end = self.content.find('</body>') | |
13 | + | |
14 | + if -1 in [start, end]: | |
15 | + return '' | |
16 | + | |
17 | + body = self.content[start + len('<body>'):end] | |
18 | + return mark_safe(body) | |
19 | + | |
20 | + def get_header(self): | |
21 | + # avoiding regex in favor of performance | |
22 | + start = self.content.find('<head>') | |
23 | + end = self.content.find('</head>') | |
24 | + | |
25 | + if -1 in [start, end]: | |
26 | + return '' | |
27 | + | |
28 | + head = self.content[start + len('<head>'):end] | |
29 | + return mark_safe(head) | |
30 | + | |
31 | + def generate_content(self, request=None): | |
32 | + self.content = '' | |
33 | + | |
34 | + | |
35 | +class WidgetManager(object): | |
36 | + widget_categories = {} | |
37 | + | |
38 | + @staticmethod | |
39 | + def register_widget(category, widget): | |
40 | + if not WidgetManager.widget_categories.has_key(category): | |
41 | + WidgetManager.widget_categories[category] = [] | |
42 | + | |
43 | + WidgetManager.widget_categories[category].append(widget) | |
44 | + | |
45 | + @staticmethod | |
46 | + def get_widgets(category, request=None): | |
47 | + if not WidgetManager.widget_categories.has_key(category): | |
48 | + return [] | |
49 | + | |
50 | + widgets = WidgetManager.widget_categories[category] | |
51 | + for widget in widgets: | |
52 | + widget.generate_content(request) | |
53 | + return widgets | ... | ... |
tests/run.py
... | ... | @@ -5,7 +5,9 @@ import sys |
5 | 5 | |
6 | 6 | os.environ['DJANGO_SETTINGS_MODULE'] = 'colab.settings' |
7 | 7 | os.environ['COLAB_SETTINGS'] = 'tests/colab_settings.py' |
8 | +os.environ['COLAB_WIDGETS_SETTINGS'] = 'tests/widgets_settings.py' | |
8 | 9 | os.environ['COLAB_PLUGINS'] = 'tests/plugins.d' |
10 | +os.environ['COLAB_WIDGETS'] = 'tests/widgets.d' | |
9 | 11 | os.environ['COVERAGE_PROCESS_START'] = '.coveragerc' |
10 | 12 | |
11 | 13 | ... | ... |