Commit 8c7fed81df7a19a1030ab2cccbe6618bd6d247f0

Authored by Matheus de Sousa Faria
1 parent baa4b74f

Widget system as a Django App

colab/accounts/views.py
@@ -15,7 +15,7 @@ from colab.super_archives.models import (EmailAddress, @@ -15,7 +15,7 @@ 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.plugins.utils.widget_manager import WidgetManager 18 +from colab.widgets.widget_manager import WidgetManager
19 19
20 from .forms import (UserCreationForm, ListsForm, UserUpdateForm) 20 from .forms import (UserCreationForm, ListsForm, UserUpdateForm)
21 from .utils import mailman 21 from .utils import mailman
colab/plugins/utils/widget_manager.py
@@ -1,53 +0,0 @@ @@ -1,53 +0,0 @@
1 -from django.utils.safestring import mark_safe  
2 -  
3 -class Widget:  
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  
colab/widgets/__init__.py 0 → 100644
colab/widgets/admin.py 0 → 100644
@@ -0,0 +1,3 @@ @@ -0,0 +1,3 @@
  1 +from django.contrib import admin
  2 +
  3 +# Register your models here.
colab/widgets/migrations/__init__.py 0 → 100644
colab/widgets/models.py 0 → 100644
@@ -0,0 +1,3 @@ @@ -0,0 +1,3 @@
  1 +from django.db import models
  2 +
  3 +# Create your models here.
colab/widgets/tests.py 0 → 100644
@@ -0,0 +1,3 @@ @@ -0,0 +1,3 @@
  1 +from django.test import TestCase
  2 +
  3 +# Create your tests here.
colab/widgets/views.py 0 → 100644
@@ -0,0 +1,3 @@ @@ -0,0 +1,3 @@
  1 +from django.shortcuts import render
  2 +
  3 +# Create your views here.
colab/widgets/widget_manager.py 0 → 100644
@@ -0,0 +1,53 @@ @@ -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