Commit ee4a2fdeea1d8bd68c5f03bd619ee436d0ecd648

Authored by Matheus de Sousa Faria
2 parents 3ecd9d8a bcbde82d

Merge pull request #132 from colab/fix-dashboard

Moved collaboration to super_archives and plugins
colab/accounts/views.py
@@ -13,7 +13,8 @@ from django.http import Http404 @@ -13,7 +13,8 @@ from django.http import Http404
13 13
14 from colab.super_archives.models import (EmailAddress, 14 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.plugins.utils.collaborations import (get_collaboration_data,
  17 + get_visible_threads)
17 from colab.accounts.models import User 18 from colab.accounts.models import User
18 19
19 from .forms import (UserCreationForm, ListsForm, UserUpdateForm) 20 from .forms import (UserCreationForm, ListsForm, UserUpdateForm)
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 get_collaboration_data 5 +from colab.plugins.utils.collaborations import get_collaboration_data
6 from colab.super_archives.models import Thread 6 from colab.super_archives.models import Thread
7 from colab.accounts.utils import mailman 7 from colab.accounts.utils import mailman
8 from colab.accounts.models import User 8 from colab.accounts.models import User
colab/plugins/utils/collaborations.py 0 → 100644
@@ -0,0 +1,67 @@ @@ -0,0 +1,67 @@
  1 +import importlib
  2 +import inspect
  3 +
  4 +from collections import OrderedDict
  5 +
  6 +from django.core.cache import cache
  7 +from django.utils.translation import ugettext as _
  8 +from django.conf import settings
  9 +
  10 +from colab.plugins.utils.models import Collaboration
  11 +from colab.super_archives.utils.collaborations import (get_visible_threads,
  12 + count_threads)
  13 +
  14 +
  15 +def get_collaboration_data(logged_user, filter_by_user=None):
  16 + username = getattr(filter_by_user, 'username', '')
  17 + cache_key = 'home_chart-{}'.format(username)
  18 + count_types = cache.get(cache_key)
  19 +
  20 + latest_results = []
  21 + populate_count_types = False
  22 +
  23 + if count_types is None:
  24 + populate_count_types = True
  25 + count_types = OrderedDict()
  26 +
  27 + count_types[_('Emails')] = count_threads()
  28 +
  29 + messages = get_visible_threads(logged_user, filter_by_user)
  30 +
  31 + latest_results.extend(messages)
  32 +
  33 + for app in settings.COLAB_APPS.values():
  34 + module = importlib.import_module('{}.models'.format(app.get('name')))
  35 +
  36 + for module_item_name in dir(module):
  37 + module_item = getattr(module, module_item_name)
  38 + if not inspect.isclass(module_item):
  39 + continue
  40 + if not issubclass(module_item, Collaboration):
  41 + continue
  42 + if module_item == Collaboration:
  43 + continue
  44 +
  45 + queryset = module_item.objects
  46 +
  47 + if filter_by_user:
  48 + elements = queryset.filter(
  49 + user__username=filter_by_user)
  50 + else:
  51 + elements = queryset.all()
  52 +
  53 + latest_results.extend(elements)
  54 + elements_count = elements.count()
  55 +
  56 + if elements_count > 1:
  57 + verbose_name = module_item._meta.verbose_name_plural.title()
  58 + else:
  59 + verbose_name = module_item._meta.verbose_name.title()
  60 +
  61 + if populate_count_types:
  62 + count_types[verbose_name] = elements_count
  63 +
  64 + if populate_count_types:
  65 + cache.set(cache_key, count_types, 30)
  66 +
  67 + return latest_results, count_types
colab/search/utils.py
@@ -1,96 +0,0 @@ @@ -1,96 +0,0 @@
1 -import importlib  
2 -import inspect  
3 -  
4 -from collections import OrderedDict  
5 -  
6 -from django.core.cache import cache  
7 -from django.utils.translation import ugettext as _  
8 -from django.conf import settings  
9 -from django.db.models import Q as Condition  
10 -  
11 -from colab.super_archives.models import Thread, Message  
12 -from colab.plugins.utils.models import Collaboration  
13 -from colab.accounts.utils import mailman  
14 -  
15 -  
16 -def get_visible_threads_queryset(logged_user):  
17 - queryset = Thread.objects  
18 - listnames_for_user = []  
19 - if logged_user:  
20 - lists_for_user = mailman.get_user_mailinglists(logged_user)  
21 - listnames_for_user = mailman.extract_listname_from_list(lists_for_user)  
22 -  
23 - user_lists = Condition(mailinglist__name__in=listnames_for_user)  
24 - public_lists = Condition(mailinglist__is_private=False)  
25 - queryset = Thread.objects.filter(user_lists | public_lists)  
26 -  
27 - return queryset  
28 -  
29 -  
30 -def get_visible_threads(logged_user, filter_by_user=None):  
31 - thread_qs = get_visible_threads_queryset(logged_user)  
32 -  
33 - if filter_by_user:  
34 - message_qs = Message.objects.filter(thread__in=thread_qs)  
35 - messages = message_qs.filter(  
36 - from_address__user__pk=filter_by_user.pk)  
37 - else:  
38 - latest_threads = thread_qs.all()  
39 - messages = [t.latest_message for t in latest_threads]  
40 -  
41 - return messages  
42 -  
43 -  
44 -def get_collaboration_data(logged_user, filter_by_user=None):  
45 - username = getattr(filter_by_user, 'username', '')  
46 - cache_key = 'home_chart-{}'.format(username)  
47 - count_types = cache.get(cache_key)  
48 -  
49 - latest_results = []  
50 - populate_count_types = False  
51 -  
52 - if count_types is None:  
53 - populate_count_types = True  
54 - count_types = OrderedDict()  
55 - visible_threads = get_visible_threads(logged_user, filter_by_user)  
56 - count_types[_('Emails')] = len(visible_threads)  
57 -  
58 - messages = get_visible_threads(logged_user, filter_by_user)  
59 -  
60 - latest_results.extend(messages)  
61 -  
62 - for app in settings.COLAB_APPS.values():  
63 - module = importlib.import_module('{}.models'.format(app.get('name')))  
64 -  
65 - for module_item_name in dir(module):  
66 - module_item = getattr(module, module_item_name)  
67 - if not inspect.isclass(module_item):  
68 - continue  
69 - if not issubclass(module_item, Collaboration):  
70 - continue  
71 - if module_item == Collaboration:  
72 - continue  
73 -  
74 - queryset = module_item.objects  
75 -  
76 - if filter_by_user:  
77 - elements = queryset.filter(  
78 - user__username=filter_by_user)  
79 - else:  
80 - elements = queryset.all()  
81 -  
82 - latest_results.extend(elements)  
83 - elements_count = elements.count()  
84 -  
85 - if elements_count > 1:  
86 - verbose_name = module_item._meta.verbose_name_plural.title()  
87 - else:  
88 - verbose_name = module_item._meta.verbose_name.title()  
89 -  
90 - if populate_count_types:  
91 - count_types[verbose_name] = elements_count  
92 -  
93 - if populate_count_types:  
94 - cache.set(cache_key, count_types, 30)  
95 -  
96 - return latest_results, count_types  
colab/super_archives/utils/collaborations.py 0 → 100644
@@ -0,0 +1,37 @@ @@ -0,0 +1,37 @@
  1 +
  2 +from django.db.models import Q
  3 +
  4 +from colab.accounts.utils import mailman
  5 +from colab.super_archives.models import Thread, Message
  6 +
  7 +
  8 +def count_threads():
  9 + return Thread.objects.count()
  10 +
  11 +
  12 +def get_visible_threads_queryset(logged_user):
  13 + queryset = Thread.objects
  14 + listnames_for_user = []
  15 + if logged_user:
  16 + lists_for_user = mailman.get_user_mailinglists(logged_user)
  17 + listnames_for_user = mailman.extract_listname_from_list(lists_for_user)
  18 +
  19 + user_lists = Q(mailinglist__name__in=listnames_for_user)
  20 + public_lists = Q(mailinglist__is_private=False)
  21 + queryset = Thread.objects.filter(user_lists | public_lists)
  22 +
  23 + return queryset
  24 +
  25 +
  26 +def get_visible_threads(logged_user, filter_by_user=None):
  27 + thread_qs = get_visible_threads_queryset(logged_user)
  28 +
  29 + if filter_by_user:
  30 + message_qs = Message.objects.filter(thread__in=thread_qs)
  31 + messages = message_qs.filter(
  32 + from_address__user__pk=filter_by_user.pk)
  33 + else:
  34 + latest_threads = thread_qs.all()
  35 + messages = [t.latest_message for t in latest_threads]
  36 +
  37 + return messages