Commit b086563abd5092e4e01019dc53bed74d1b184f3c

Authored by Carlos Coêlho
Committed by Charles Oliveira
1 parent b216ed6c

Fixed Mailman API v2 usage

Fixed test calls in order to adapt it to mailman-api v2

Signed-off-by: Carlos Oliveira <carlospecter@gmail.com>
colab/accounts/utils/mailman.py
@@ -117,8 +117,9 @@ def get_list_description(listname, lists=None): @@ -117,8 +117,9 @@ def get_list_description(listname, lists=None):
117 if not lists: 117 if not lists:
118 lists = all_lists() 118 lists = all_lists()
119 119
120 - desc = "".join(mlist.get('description') for mlist in lists\  
121 - if isinstance(mlist, dict) and mlist.get('listname') == listname) 120 + desc = "".join(mlist.get('description') for mlist in lists
  121 + if isinstance(mlist, dict) and
  122 + mlist.get('listname') == listname)
122 123
123 return desc 124 return desc
124 125
@@ -147,3 +148,10 @@ def get_user_mailinglists(user): @@ -147,3 +148,10 @@ def get_user_mailinglists(user):
147 lists_for_user.extend(mailing_lists(address=email)) 148 lists_for_user.extend(mailing_lists(address=email))
148 149
149 return lists_for_user 150 return lists_for_user
  151 +
  152 +
  153 +def extract_listname_from_list(lists):
  154 + try:
  155 + return [mlist.get('listname') for mlist in lists]
  156 + except ValueError:
  157 + return []
colab/home/views.py
@@ -10,9 +10,10 @@ from colab.accounts.models import User @@ -10,9 +10,10 @@ from colab.accounts.models import User
10 10
11 def get_user_threads(threads, lists_for_user, key): 11 def get_user_threads(threads, lists_for_user, key):
12 visible_threads = [] 12 visible_threads = []
  13 + listnames_for_user = mailman.extract_listname_from_list(lists_for_user)
13 for t in threads: 14 for t in threads:
14 if not t.mailinglist.is_private or \ 15 if not t.mailinglist.is_private or \
15 - t.mailinglist.name in lists_for_user: 16 + t.mailinglist.name in listnames_for_user:
16 visible_threads.append(key(t)) 17 visible_threads.append(key(t))
17 18
18 return visible_threads 19 return visible_threads
colab/search/utils.py
@@ -15,11 +15,12 @@ from colab.accounts.utils import mailman @@ -15,11 +15,12 @@ from colab.accounts.utils import mailman
15 15
16 def get_visible_threads_queryset(logged_user): 16 def get_visible_threads_queryset(logged_user):
17 queryset = Thread.objects 17 queryset = Thread.objects
18 - lists_for_user = [] 18 + listnames_for_user = []
19 if logged_user: 19 if logged_user:
20 lists_for_user = mailman.get_user_mailinglists(logged_user) 20 lists_for_user = mailman.get_user_mailinglists(logged_user)
  21 + listnames_for_user = mailman.extract_listname_from_list(lists_for_user)
21 22
22 - user_lists = Condition(mailinglist__name__in=lists_for_user) 23 + user_lists = Condition(mailinglist__name__in=listnames_for_user)
23 public_lists = Condition(mailinglist__is_private=False) 24 public_lists = Condition(mailinglist__is_private=False)
24 queryset = Thread.objects.filter(user_lists | public_lists) 25 queryset = Thread.objects.filter(user_lists | public_lists)
25 26
@@ -28,6 +29,7 @@ def get_visible_threads_queryset(logged_user): @@ -28,6 +29,7 @@ def get_visible_threads_queryset(logged_user):
28 29
29 def get_visible_threads(logged_user, filter_by_user=None): 30 def get_visible_threads(logged_user, filter_by_user=None):
30 thread_qs = get_visible_threads_queryset(logged_user) 31 thread_qs = get_visible_threads_queryset(logged_user)
  32 +
31 if filter_by_user: 33 if filter_by_user:
32 message_qs = Message.objects.filter(thread__in=thread_qs) 34 message_qs = Message.objects.filter(thread__in=thread_qs)
33 messages = message_qs.filter( 35 messages = message_qs.filter(
colab/super_archives/fixtures/mailinglistdata.json
@@ -6,23 +6,23 @@ @@ -6,23 +6,23 @@
6 "twitter": null, 6 "twitter": null,
7 "is_staff": false, 7 "is_staff": false,
8 "user_permissions": [ 8 "user_permissions": [
9 - 9 +
10 ], 10 ],
11 "date_joined": "2015-02-24T21:10:35.004Z", 11 "date_joined": "2015-02-24T21:10:35.004Z",
12 "google_talk": null, 12 "google_talk": null,
13 - "first_name": "Gust", 13 + "first_name": "John",
14 "is_superuser": false, 14 "is_superuser": false,
15 "last_login": "2015-02-26T17:56:13.378Z", 15 "last_login": "2015-02-26T17:56:13.378Z",
16 "verification_hash": null, 16 "verification_hash": null,
17 "role": null, 17 "role": null,
18 - "email": "gustmax@hotmail.com",  
19 - "username": "gustmax", 18 + "email": "johndoe@example.com",
  19 + "username": "johndoe",
20 "bio": null, 20 "bio": null,
21 "needs_update": false, 21 "needs_update": false,
22 "is_active": true, 22 "is_active": true,
23 "facebook": null, 23 "facebook": null,
24 "groups": [ 24 "groups": [
25 - 25 +
26 ], 26 ],
27 "password": "pbkdf2_sha256$12000$ez83ccNOUQZk$vYT/QcYMukXZ7D7L1qQPyYlzCUEEEF20J7/Xjef0Rqg=", 27 "password": "pbkdf2_sha256$12000$ez83ccNOUQZk$vYT/QcYMukXZ7D7L1qQPyYlzCUEEEF20J7/Xjef0Rqg=",
28 "institution": null, 28 "institution": null,
@@ -37,7 +37,7 @@ @@ -37,7 +37,7 @@
37 "real_name": "", 37 "real_name": "",
38 "user": 1, 38 "user": 1,
39 "md5": "ed8f47ae6048f8d4456c0554578f53ff", 39 "md5": "ed8f47ae6048f8d4456c0554578f53ff",
40 - "address": "gustmax@hotmail.com" 40 + "address": "johndoe@example.com"
41 }, 41 },
42 "model": "super_archives.emailaddress", 42 "model": "super_archives.emailaddress",
43 "pk": 1 43 "pk": 1
colab/super_archives/tests/test_privatelist.py
@@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
2 import mock 2 import mock
3 3
4 from colab.accounts.utils import mailman 4 from colab.accounts.utils import mailman
5 -from django.test import TestCase, Client 5 +from django.test import TestCase, Client
6 6
7 7
8 class ArchivesViewTest(TestCase): 8 class ArchivesViewTest(TestCase):
@@ -13,11 +13,14 @@ class ArchivesViewTest(TestCase): @@ -13,11 +13,14 @@ class ArchivesViewTest(TestCase):
13 self.client = Client() 13 self.client = Client()
14 14
15 def authenticate_user(self): 15 def authenticate_user(self):
16 - self.client.login(username='gustmax', password='1234') 16 + self.client.login(username='johndoe', password='1234')
17 17
18 def test_see_only_private_list_if_member(self): 18 def test_see_only_private_list_if_member(self):
19 mailman.get_user_mailinglists = mock.Mock( 19 mailman.get_user_mailinglists = mock.Mock(
  20 + return_value="[{'listname': 'privatelist'}]")
  21 + mailman.extract_listname_from_list = mock.Mock(
20 return_value="['privatelist']") 22 return_value="['privatelist']")
  23 + mailman.list_users = mock.Mock(return_value="['johndoe@example.com']")
21 24
22 self.authenticate_user() 25 self.authenticate_user()
23 request = self.client.get('/archives/thread/') 26 request = self.client.get('/archives/thread/')
@@ -26,7 +29,7 @@ class ArchivesViewTest(TestCase): @@ -26,7 +29,7 @@ class ArchivesViewTest(TestCase):
26 29
27 self.assertEqual('lista', list_data[0][0]) 30 self.assertEqual('lista', list_data[0][0])
28 self.assertEqual('privatelist', list_data[1][0]) 31 self.assertEqual('privatelist', list_data[1][0])
29 - self.assertEqual(2, len(list_data)) 32 + self.assertEqual(2, len(list_data))
30 33
31 def test_see_only_public_if_not_logged_in(self): 34 def test_see_only_public_if_not_logged_in(self):
32 request = self.client.get('/archives/thread/') 35 request = self.client.get('/archives/thread/')
@@ -34,10 +37,12 @@ class ArchivesViewTest(TestCase): @@ -34,10 +37,12 @@ class ArchivesViewTest(TestCase):
34 list_data = request.context['lists'] 37 list_data = request.context['lists']
35 38
36 self.assertEqual('lista', list_data[0][0]) 39 self.assertEqual('lista', list_data[0][0])
37 - self.assertEqual(1, len(list_data)) 40 + self.assertEqual(1, len(list_data))
38 41
39 def test_see_private_thread_in_dashboard_if_member(self): 42 def test_see_private_thread_in_dashboard_if_member(self):
40 mailman.get_user_mailinglists = mock.Mock( 43 mailman.get_user_mailinglists = mock.Mock(
  44 + return_value="[{'listname': 'privatelist'}]")
  45 + mailman.extract_listname_from_list = mock.Mock(
41 return_value="['privatelist']") 46 return_value="['privatelist']")
42 47
43 self.authenticate_user() 48 self.authenticate_user()
@@ -59,7 +64,7 @@ class ArchivesViewTest(TestCase): @@ -59,7 +64,7 @@ class ArchivesViewTest(TestCase):
59 self.assertEqual(1, len(hottest_threads)) 64 self.assertEqual(1, len(hottest_threads))
60 65
61 def test_dont_see_private_threads_in_profile_if_logged_out(self): 66 def test_dont_see_private_threads_in_profile_if_logged_out(self):
62 - request = self.client.get('/account/gustmax') 67 + request = self.client.get('/account/johndoe')
63 68
64 emails = request.context['emails'] 69 emails = request.context['emails']
65 70
colab/super_archives/views.py
@@ -47,8 +47,8 @@ class ThreadView(View): @@ -47,8 +47,8 @@ class ThreadView(View):
47 user = User.objects.get(username=request.user) 47 user = User.objects.get(username=request.user)
48 emails = user.emails.values_list('address', flat=True) 48 emails = user.emails.values_list('address', flat=True)
49 lists_for_user = mailman.get_user_mailinglists(user) 49 lists_for_user = mailman.get_user_mailinglists(user)
50 - listnames_for_user = [mlist.get("listname")  
51 - for mlist in lists_for_user] 50 + listnames_for_user = mailman.extract_listname_from_list(
  51 + lists_for_user)
52 if thread.mailinglist.name not in listnames_for_user: 52 if thread.mailinglist.name not in listnames_for_user:
53 raise PermissionDenied 53 raise PermissionDenied
54 54
@@ -151,13 +151,16 @@ class ThreadDashboardView(View): @@ -151,13 +151,16 @@ class ThreadDashboardView(View):
151 151
152 context['lists'] = [] 152 context['lists'] = []
153 153
154 - lists_for_user = [] 154 + listnames_for_user = []
155 if request.user.is_authenticated(): 155 if request.user.is_authenticated():
156 user = User.objects.get(username=request.user) 156 user = User.objects.get(username=request.user)
157 lists_for_user = mailman.get_user_mailinglists(user) 157 lists_for_user = mailman.get_user_mailinglists(user)
  158 + listnames_for_user = mailman.extract_listname_from_list(
  159 + lists_for_user)
158 160
159 for list_ in MailingList.objects.order_by('name'): 161 for list_ in MailingList.objects.order_by('name'):
160 - if list_.name not in all_privates or list_.name in lists_for_user: 162 + if list_.name not in all_privates\
  163 + or list_.name in listnames_for_user:
161 context['lists'].append(( 164 context['lists'].append((
162 list_.name, 165 list_.name,
163 mailman.get_list_description(list_.name), 166 mailman.get_list_description(list_.name),