Commit e659f77c9826b6d692b0447937cbbcdabe09c9df

Authored by Gust
1 parent d28b5b08

Refactored collaboration models.

Signed-off-by: Gustavo Jaruga <darksshades@gmail.com>
Signed-off-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
colab/accounts/views.py
1 -#!/usr/bin/env python  
2 # encoding: utf-8 1 # encoding: utf-8
3 from collections import OrderedDict 2 from collections import OrderedDict
4 3
5 -from haystack.exceptions import SearchBackendError  
6 -  
7 from django.conf import settings 4 from django.conf import settings
8 from django.contrib import messages 5 from django.contrib import messages
9 from django.db import transaction 6 from django.db import transaction
@@ -20,7 +17,7 @@ from conversejs.models import XMPPAccount @@ -20,7 +17,7 @@ from conversejs.models import XMPPAccount
20 17
21 from colab.super_archives.models import (EmailAddress, Message, 18 from colab.super_archives.models import (EmailAddress, Message,
22 EmailAddressValidation) 19 EmailAddressValidation)
23 -from colab.search.utils import trans, get_collaboration_data 20 +from colab.search.utils import get_collaboration_data
24 21
25 from .forms import (UserCreationForm, UserForm, ListsForm, 22 from .forms import (UserCreationForm, UserForm, ListsForm,
26 UserUpdateForm, ChangeXMPPPasswordForm) 23 UserUpdateForm, ChangeXMPPPasswordForm)
@@ -70,7 +67,7 @@ class UserProfileDetailView(UserProfileBaseMixin, DetailView): @@ -70,7 +67,7 @@ class UserProfileDetailView(UserProfileBaseMixin, DetailView):
70 67
71 # TODO: remove when mailman becomes a proxied plugin 68 # TODO: remove when mailman becomes a proxied plugin
72 messages = Message.objects.filter(from_address__user__pk=user.pk) 69 messages = Message.objects.filter(from_address__user__pk=user.pk)
73 - count_types[trans('thread')] = messages.count() 70 + count_types[_('Emails')] = messages.count()
74 71
75 collaborations, count_types_extras = get_collaboration_data(user) 72 collaborations, count_types_extras = get_collaboration_data(user)
76 collaborations.extend(messages) 73 collaborations.extend(messages)
colab/home/views.py
@@ -2,7 +2,7 @@ from django.conf import settings @@ -2,7 +2,7 @@ from django.conf import settings
2 from django.shortcuts import render 2 from django.shortcuts import render
3 from django.http import HttpResponse, Http404 3 from django.http import HttpResponse, Http404
4 4
5 -from colab.search.utils import trans, get_collaboration_data 5 +from colab.search.utils import get_collaboration_data
6 from colab.super_archives.models import Thread 6 from colab.super_archives.models import Thread
7 7
8 8
@@ -23,9 +23,6 @@ def dashboard(request): @@ -23,9 +23,6 @@ def dashboard(request):
23 latest_results.extend(messages) 23 latest_results.extend(messages)
24 latest_results.sort(key=lambda elem: elem.modified, reverse=True) 24 latest_results.sort(key=lambda elem: elem.modified, reverse=True)
25 25
26 - for key in count_types.keys():  
27 - count_types[trans(key)] = count_types.pop(key)  
28 -  
29 context = { 26 context = {
30 'hottest_threads': hottest_threads[:6], 27 'hottest_threads': hottest_threads[:6],
31 'latest_threads': latest_threads[:6], 28 'latest_threads': latest_threads[:6],
colab/proxy/gitlab/apps.py
@@ -5,37 +5,6 @@ from ..utils.apps import ColabProxiedAppConfig @@ -5,37 +5,6 @@ from ..utils.apps import ColabProxiedAppConfig
5 5
6 6
7 class ProxyGitlabAppConfig(ColabProxiedAppConfig): 7 class ProxyGitlabAppConfig(ColabProxiedAppConfig):
8 - '''  
9 - You can define a collaboration_models list to tell colab which  
10 - models and what values should be displayed as collaborations.  
11 -  
12 - See the example bellow:  
13 -  
14 - Field model refers to the model to be displayed.  
15 - Field model_verbose is the human name to be displayed in charts.  
16 - Field collaborator_username tells which user(username) is  
17 - associated with this collaboration.  
18 -  
19 - The value of the hashes maps the attribute or method of the model  
20 - to be put in those positions.  
21 -  
22 - collaboration_models = [  
23 - {  
24 - 'model' : 'User',  
25 - 'model_verbose' : 'User',  
26 - 'tag' : '',  
27 - 'title' : 'username',  
28 - 'description' : 'get_full_name',  
29 - 'fullname' : '',  
30 - 'modified' : 'modified',  
31 - 'modified_by' : '',  
32 - 'modified_by_url' : '',  
33 - 'url' : '',  
34 - 'type' : '',  
35 - 'collaborator_username' : 'username',  
36 - },  
37 - ]  
38 - '''  
39 name = 'colab.proxy.gitlab' 8 name = 'colab.proxy.gitlab'
40 verbose_name = 'Gitlab Proxy' 9 verbose_name = 'Gitlab Proxy'
41 10
@@ -54,6 +23,3 @@ class ProxyGitlabAppConfig(ColabProxiedAppConfig): @@ -54,6 +23,3 @@ class ProxyGitlabAppConfig(ColabProxiedAppConfig):
54 23
55 ), 24 ),
56 } 25 }
57 -  
58 - collaboration_models = []  
59 -  
colab/proxy/jenkins/apps.py
@@ -14,5 +14,3 @@ class ProxyJenkinsAppConfig(ColabProxiedAppConfig): @@ -14,5 +14,3 @@ class ProxyJenkinsAppConfig(ColabProxiedAppConfig):
14 (_('Continuos Integration'), ''), 14 (_('Continuos Integration'), ''),
15 ), 15 ),
16 } 16 }
17 -  
18 - collaboration_models = []  
colab/proxy/noosfero/apps.py
@@ -19,5 +19,3 @@ class ProxyNoosferoAppConfig(ColabProxiedAppConfig): @@ -19,5 +19,3 @@ class ProxyNoosferoAppConfig(ColabProxiedAppConfig):
19 (_('Control panel'), 'myprofile'), 19 (_('Control panel'), 'myprofile'),
20 ), 20 ),
21 } 21 }
22 -  
23 - collaboration_models = []  
colab/proxy/redmine/apps.py
@@ -5,5 +5,3 @@ from ..utils.apps import ColabProxiedAppConfig @@ -5,5 +5,3 @@ from ..utils.apps import ColabProxiedAppConfig
5 class ProxyRedmineAppConfig(ColabProxiedAppConfig): 5 class ProxyRedmineAppConfig(ColabProxiedAppConfig):
6 name = 'colab.proxy.redmine' 6 name = 'colab.proxy.redmine'
7 verbose_name = 'Redmine Proxy' 7 verbose_name = 'Redmine Proxy'
8 -  
9 - collaboration_models = []  
colab/proxy/trac/apps.py
@@ -22,5 +22,3 @@ class ProxyTracAppConfig(ColabProxiedAppConfig): @@ -22,5 +22,3 @@ class ProxyTracAppConfig(ColabProxiedAppConfig):
22 (_('New Wiki Page'), 'wiki/WikiNewPage'), 22 (_('New Wiki Page'), 'wiki/WikiNewPage'),
23 ), 23 ),
24 } 24 }
25 -  
26 - collaboration_models = []  
colab/proxy/utils/models.py 0 → 100644
@@ -0,0 +1,59 @@ @@ -0,0 +1,59 @@
  1 +from django.db import models
  2 +from django.conf import settings
  3 +from colab.accounts.models import User
  4 +
  5 +
  6 +class CollaborationModel(models.Model):
  7 + '''
  8 + Class to define the fields of the collaboration block
  9 + that are displayed at dashboard and profile pages.
  10 + '''
  11 +
  12 + @property
  13 + def verbose_name(self):
  14 + raise NotImplemented
  15 +
  16 + @property
  17 + def tag(self):
  18 + return None
  19 +
  20 + @property
  21 + def title(self):
  22 + raise NotImplemented
  23 +
  24 + @property
  25 + def description(self):
  26 + return None
  27 +
  28 + @property
  29 + def url(self):
  30 + return None
  31 +
  32 + @property
  33 + def modified(self):
  34 + return None
  35 +
  36 + @property
  37 + def modified_by(self):
  38 + if self.user:
  39 + return self.user.get_full_name()
  40 + return None
  41 +
  42 + @property
  43 + def modified_by_url(self):
  44 + if self.user:
  45 + return self.user.get_absolute_url()
  46 + return None
  47 +
  48 + @property
  49 + def type(self):
  50 + return None
  51 +
  52 + user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True,
  53 + on_delete=models.SET_NULL)
  54 +
  55 + def update_user(self, user_name):
  56 + self.user = User.objects.filter(username=user_name).last()
  57 +
  58 + class Meta:
  59 + abstract = True
colab/search/preview_block.py
@@ -1,19 +0,0 @@ @@ -1,19 +0,0 @@
1 -class PreviewBlock():  
2 - '''  
3 - Class to define the fields of the collaboration block  
4 - that are displayed at dashboard and profile pages.  
5 - '''  
6 - tag = None  
7 - title = None  
8 - description = None  
9 - fullname = None  
10 - modified = None  
11 - modified_by = None  
12 - url = None  
13 - type = None  
14 - modified_by_url = None  
15 - collaborator_username = None  
16 -  
17 - def __init__(self, **kwargs):  
18 - for key, value in kwargs.items():  
19 - setattr(self, key, value)  
colab/search/utils.py
@@ -5,36 +5,12 @@ from collections import OrderedDict @@ -5,36 +5,12 @@ from collections import OrderedDict
5 5
6 from django.core.cache import cache 6 from django.core.cache import cache
7 from django.utils.translation import ugettext as _ 7 from django.utils.translation import ugettext as _
8 -from django.apps import apps  
9 from django.conf import settings 8 from django.conf import settings
10 from colab.super_archives.models import Thread 9 from colab.super_archives.models import Thread
11 -from colab.search.preview_block import PreviewBlock  
12 -  
13 -  
14 -def trans(key):  
15 - translations = {  
16 - 'wiki': _('Wiki'),  
17 - 'thread': _('Emails'),  
18 - 'changeset': _('Code'),  
19 - 'ticket': _('Tickets'),  
20 - 'attachment': _('Attachments'),  
21 - }  
22 -  
23 - app_names = settings.PROXIED_APPS.keys()  
24 -  
25 - for app_name in app_names:  
26 - collaboration_models = \  
27 - apps.get_app_config(app_name).collaboration_models  
28 -  
29 - for collaboration in collaboration_models:  
30 - translations[collaboration['model'].lower()] = \  
31 - collaboration['model_verbose']  
32 -  
33 - return translations.get(key, key) 10 +from colab.proxy.utils.models import CollaborationModel
34 11
35 12
36 def get_collaboration_data(filter_by_user=None): 13 def get_collaboration_data(filter_by_user=None):
37 -  
38 latest_results = [] 14 latest_results = []
39 count_types = cache.get('home_chart') 15 count_types = cache.get('home_chart')
40 populate_count_types = False 16 populate_count_types = False
@@ -42,59 +18,38 @@ def get_collaboration_data(filter_by_user=None): @@ -42,59 +18,38 @@ def get_collaboration_data(filter_by_user=None):
42 if count_types is None: 18 if count_types is None:
43 populate_count_types = True 19 populate_count_types = True
44 count_types = OrderedDict() 20 count_types = OrderedDict()
45 - count_types['thread'] = Thread.objects.count() 21 + count_types[_('Emails')] = Thread.objects.count()
46 22
47 app_names = settings.PROXIED_APPS.keys() 23 app_names = settings.PROXIED_APPS.keys()
48 24
49 for app_name in app_names: 25 for app_name in app_names:
50 - collaboration_models = \  
51 - apps.get_app_config(app_name).collaboration_models 26 + module = importlib
  27 + module = \
  28 + module.import_module('colab.proxy.{}.models'.format(app_name))
52 29
53 - for collaboration in collaboration_models:  
54 - module = importlib  
55 - module = \  
56 - module.import_module('colab.proxy.{}.models'.format(app_name)) 30 + for module_item_name in dir(module):
  31 + module_item = getattr(module, module_item_name)
  32 + if not inspect.isclass(module_item):
  33 + continue
  34 + if not issubclass(module_item, CollaborationModel):
  35 + continue
  36 + if module_item == CollaborationModel:
  37 + continue
57 38
58 - module = eval("module." + collaboration['model'])  
59 - elements = module.objects 39 + elements = module_item.objects
60 40
61 if filter_by_user: 41 if filter_by_user:
62 - dic = {}  
63 - dic[collaboration['collaborator_username']] = filter_by_user  
64 - elements = elements.filter(**dic) 42 + elements = elements.filter(
  43 + user__username=filter_by_user)
65 else: 44 else:
66 elements = elements.all() 45 elements = elements.all()
67 46
68 - latest_results.extend(parsePreviewBlock(elements, collaboration)) 47 + latest_results.extend(elements)
69 48
70 if populate_count_types: 49 if populate_count_types:
71 - count_types[collaboration['model'].lower()] = elements.count() 50 + count_types[module_item().verbose_name] = elements.count()
72 51
73 if populate_count_types: 52 if populate_count_types:
74 cache.set('home_chart', count_types, 30) 53 cache.set('home_chart', count_types, 30)
75 54
76 - for key in count_types.keys():  
77 - count_types[trans(key)] = count_types.pop(key)  
78 -  
79 return latest_results, count_types 55 return latest_results, count_types
80 -  
81 -  
82 -def parsePreviewBlock(elements, collaboration):  
83 - results = []  
84 - for element in elements:  
85 - previewblock = PreviewBlock()  
86 - attributes = collaboration.keys()  
87 -  
88 - for keyname in attributes:  
89 - if keyname == 'model' or keyname == 'model_verbose' \  
90 - or len(collaboration[keyname].strip()) == 0:  
91 - continue  
92 - value = getattr(element, collaboration[keyname])  
93 - if(inspect.ismethod(value)):  
94 - setattr(previewblock, keyname, value())  
95 - else:  
96 - setattr(previewblock, keyname, value)  
97 -  
98 - results.append(previewblock)  
99 -  
100 - return results