From e659f77c9826b6d692b0447937cbbcdabe09c9df Mon Sep 17 00:00:00 2001 From: Gust Date: Tue, 27 Jan 2015 18:06:37 -0200 Subject: [PATCH] Refactored collaboration models. --- colab/accounts/views.py | 7 ++----- colab/home/views.py | 5 +---- colab/proxy/gitlab/apps.py | 34 ---------------------------------- colab/proxy/jenkins/apps.py | 2 -- colab/proxy/noosfero/apps.py | 2 -- colab/proxy/redmine/apps.py | 2 -- colab/proxy/trac/apps.py | 2 -- colab/proxy/utils/models.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ colab/search/preview_block.py | 19 ------------------- colab/search/utils.py | 81 ++++++++++++++++++--------------------------------------------------------------- 10 files changed, 80 insertions(+), 133 deletions(-) create mode 100644 colab/proxy/utils/models.py delete mode 100644 colab/search/preview_block.py diff --git a/colab/accounts/views.py b/colab/accounts/views.py index 282ab74..232cdc7 100644 --- a/colab/accounts/views.py +++ b/colab/accounts/views.py @@ -1,9 +1,6 @@ -#!/usr/bin/env python # encoding: utf-8 from collections import OrderedDict -from haystack.exceptions import SearchBackendError - from django.conf import settings from django.contrib import messages from django.db import transaction @@ -20,7 +17,7 @@ from conversejs.models import XMPPAccount from colab.super_archives.models import (EmailAddress, Message, EmailAddressValidation) -from colab.search.utils import trans, get_collaboration_data +from colab.search.utils import get_collaboration_data from .forms import (UserCreationForm, UserForm, ListsForm, UserUpdateForm, ChangeXMPPPasswordForm) @@ -70,7 +67,7 @@ class UserProfileDetailView(UserProfileBaseMixin, DetailView): # TODO: remove when mailman becomes a proxied plugin messages = Message.objects.filter(from_address__user__pk=user.pk) - count_types[trans('thread')] = messages.count() + count_types[_('Emails')] = messages.count() collaborations, count_types_extras = get_collaboration_data(user) collaborations.extend(messages) diff --git a/colab/home/views.py b/colab/home/views.py index fdf59b4..9661748 100644 --- a/colab/home/views.py +++ b/colab/home/views.py @@ -2,7 +2,7 @@ from django.conf import settings from django.shortcuts import render from django.http import HttpResponse, Http404 -from colab.search.utils import trans, get_collaboration_data +from colab.search.utils import get_collaboration_data from colab.super_archives.models import Thread @@ -23,9 +23,6 @@ def dashboard(request): latest_results.extend(messages) latest_results.sort(key=lambda elem: elem.modified, reverse=True) - for key in count_types.keys(): - count_types[trans(key)] = count_types.pop(key) - context = { 'hottest_threads': hottest_threads[:6], 'latest_threads': latest_threads[:6], diff --git a/colab/proxy/gitlab/apps.py b/colab/proxy/gitlab/apps.py index fb253b6..8719018 100644 --- a/colab/proxy/gitlab/apps.py +++ b/colab/proxy/gitlab/apps.py @@ -5,37 +5,6 @@ from ..utils.apps import ColabProxiedAppConfig class ProxyGitlabAppConfig(ColabProxiedAppConfig): - ''' - You can define a collaboration_models list to tell colab which - models and what values should be displayed as collaborations. - - See the example bellow: - - Field model refers to the model to be displayed. - Field model_verbose is the human name to be displayed in charts. - Field collaborator_username tells which user(username) is - associated with this collaboration. - - The value of the hashes maps the attribute or method of the model - to be put in those positions. - - collaboration_models = [ - { - 'model' : 'User', - 'model_verbose' : 'User', - 'tag' : '', - 'title' : 'username', - 'description' : 'get_full_name', - 'fullname' : '', - 'modified' : 'modified', - 'modified_by' : '', - 'modified_by_url' : '', - 'url' : '', - 'type' : '', - 'collaborator_username' : 'username', - }, - ] - ''' name = 'colab.proxy.gitlab' verbose_name = 'Gitlab Proxy' @@ -54,6 +23,3 @@ class ProxyGitlabAppConfig(ColabProxiedAppConfig): ), } - - collaboration_models = [] - diff --git a/colab/proxy/jenkins/apps.py b/colab/proxy/jenkins/apps.py index 15e1817..52acd2b 100644 --- a/colab/proxy/jenkins/apps.py +++ b/colab/proxy/jenkins/apps.py @@ -14,5 +14,3 @@ class ProxyJenkinsAppConfig(ColabProxiedAppConfig): (_('Continuos Integration'), ''), ), } - - collaboration_models = [] diff --git a/colab/proxy/noosfero/apps.py b/colab/proxy/noosfero/apps.py index 2f0c3da..bb9e75f 100644 --- a/colab/proxy/noosfero/apps.py +++ b/colab/proxy/noosfero/apps.py @@ -19,5 +19,3 @@ class ProxyNoosferoAppConfig(ColabProxiedAppConfig): (_('Control panel'), 'myprofile'), ), } - - collaboration_models = [] diff --git a/colab/proxy/redmine/apps.py b/colab/proxy/redmine/apps.py index 121dea2..1076304 100644 --- a/colab/proxy/redmine/apps.py +++ b/colab/proxy/redmine/apps.py @@ -5,5 +5,3 @@ from ..utils.apps import ColabProxiedAppConfig class ProxyRedmineAppConfig(ColabProxiedAppConfig): name = 'colab.proxy.redmine' verbose_name = 'Redmine Proxy' - - collaboration_models = [] diff --git a/colab/proxy/trac/apps.py b/colab/proxy/trac/apps.py index e5ea130..d54d8bb 100644 --- a/colab/proxy/trac/apps.py +++ b/colab/proxy/trac/apps.py @@ -22,5 +22,3 @@ class ProxyTracAppConfig(ColabProxiedAppConfig): (_('New Wiki Page'), 'wiki/WikiNewPage'), ), } - - collaboration_models = [] diff --git a/colab/proxy/utils/models.py b/colab/proxy/utils/models.py new file mode 100644 index 0000000..7232a26 --- /dev/null +++ b/colab/proxy/utils/models.py @@ -0,0 +1,59 @@ +from django.db import models +from django.conf import settings +from colab.accounts.models import User + + +class CollaborationModel(models.Model): + ''' + Class to define the fields of the collaboration block + that are displayed at dashboard and profile pages. + ''' + + @property + def verbose_name(self): + raise NotImplemented + + @property + def tag(self): + return None + + @property + def title(self): + raise NotImplemented + + @property + def description(self): + return None + + @property + def url(self): + return None + + @property + def modified(self): + return None + + @property + def modified_by(self): + if self.user: + return self.user.get_full_name() + return None + + @property + def modified_by_url(self): + if self.user: + return self.user.get_absolute_url() + return None + + @property + def type(self): + return None + + user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, + on_delete=models.SET_NULL) + + def update_user(self, user_name): + self.user = User.objects.filter(username=user_name).last() + + class Meta: + abstract = True diff --git a/colab/search/preview_block.py b/colab/search/preview_block.py deleted file mode 100644 index d5e9778..0000000 --- a/colab/search/preview_block.py +++ /dev/null @@ -1,19 +0,0 @@ -class PreviewBlock(): - ''' - Class to define the fields of the collaboration block - that are displayed at dashboard and profile pages. - ''' - tag = None - title = None - description = None - fullname = None - modified = None - modified_by = None - url = None - type = None - modified_by_url = None - collaborator_username = None - - def __init__(self, **kwargs): - for key, value in kwargs.items(): - setattr(self, key, value) diff --git a/colab/search/utils.py b/colab/search/utils.py index ad7e1e2..a0e6d93 100644 --- a/colab/search/utils.py +++ b/colab/search/utils.py @@ -5,36 +5,12 @@ from collections import OrderedDict from django.core.cache import cache from django.utils.translation import ugettext as _ -from django.apps import apps from django.conf import settings from colab.super_archives.models import Thread -from colab.search.preview_block import PreviewBlock - - -def trans(key): - translations = { - 'wiki': _('Wiki'), - 'thread': _('Emails'), - 'changeset': _('Code'), - 'ticket': _('Tickets'), - 'attachment': _('Attachments'), - } - - app_names = settings.PROXIED_APPS.keys() - - for app_name in app_names: - collaboration_models = \ - apps.get_app_config(app_name).collaboration_models - - for collaboration in collaboration_models: - translations[collaboration['model'].lower()] = \ - collaboration['model_verbose'] - - return translations.get(key, key) +from colab.proxy.utils.models import CollaborationModel def get_collaboration_data(filter_by_user=None): - latest_results = [] count_types = cache.get('home_chart') populate_count_types = False @@ -42,59 +18,38 @@ def get_collaboration_data(filter_by_user=None): if count_types is None: populate_count_types = True count_types = OrderedDict() - count_types['thread'] = Thread.objects.count() + count_types[_('Emails')] = Thread.objects.count() app_names = settings.PROXIED_APPS.keys() for app_name in app_names: - collaboration_models = \ - apps.get_app_config(app_name).collaboration_models + module = importlib + module = \ + module.import_module('colab.proxy.{}.models'.format(app_name)) - for collaboration in collaboration_models: - module = importlib - module = \ - module.import_module('colab.proxy.{}.models'.format(app_name)) + for module_item_name in dir(module): + module_item = getattr(module, module_item_name) + if not inspect.isclass(module_item): + continue + if not issubclass(module_item, CollaborationModel): + continue + if module_item == CollaborationModel: + continue - module = eval("module." + collaboration['model']) - elements = module.objects + elements = module_item.objects if filter_by_user: - dic = {} - dic[collaboration['collaborator_username']] = filter_by_user - elements = elements.filter(**dic) + elements = elements.filter( + user__username=filter_by_user) else: elements = elements.all() - latest_results.extend(parsePreviewBlock(elements, collaboration)) + latest_results.extend(elements) if populate_count_types: - count_types[collaboration['model'].lower()] = elements.count() + count_types[module_item().verbose_name] = elements.count() if populate_count_types: cache.set('home_chart', count_types, 30) - for key in count_types.keys(): - count_types[trans(key)] = count_types.pop(key) - return latest_results, count_types - - -def parsePreviewBlock(elements, collaboration): - results = [] - for element in elements: - previewblock = PreviewBlock() - attributes = collaboration.keys() - - for keyname in attributes: - if keyname == 'model' or keyname == 'model_verbose' \ - or len(collaboration[keyname].strip()) == 0: - continue - value = getattr(element, collaboration[keyname]) - if(inspect.ismethod(value)): - setattr(previewblock, keyname, value()) - else: - setattr(previewblock, keyname, value) - - results.append(previewblock) - - return results -- libgit2 0.21.2