Commit 345b273193a1be5569b653dd557d1e489d80eac5
1 parent
bdd65848
Exists in
master
and in
28 other branches
Added subscribe messages returned from Mailman
Signed-off-by: Charles Oliveira <18oliveira.charles@gmail.com> Signed-off-by: Matheus Fernandes <matheus.souza.fernandes@gmail.com>
Showing
3 changed files
with
16 additions
and
6 deletions
Show diff stats
colab/accounts/models.py
@@ -60,7 +60,7 @@ class User(AbstractUser): | @@ -60,7 +60,7 @@ class User(AbstractUser): | ||
60 | return mailman.user_lists(self) | 60 | return mailman.user_lists(self) |
61 | 61 | ||
62 | def update_subscription(self, email, lists): | 62 | def update_subscription(self, email, lists): |
63 | - mailman.update_subscription(email, lists) | 63 | + return mailman.update_subscription(email, lists) |
64 | 64 | ||
65 | def save(self, *args, **kwargs): | 65 | def save(self, *args, **kwargs): |
66 | 66 |
colab/accounts/utils/mailman.py
@@ -4,6 +4,7 @@ import requests | @@ -4,6 +4,7 @@ import requests | ||
4 | import logging | 4 | import logging |
5 | 5 | ||
6 | from django.conf import settings | 6 | from django.conf import settings |
7 | +from django.contrib import messages | ||
7 | 8 | ||
8 | TIMEOUT = 1 | 9 | TIMEOUT = 1 |
9 | 10 | ||
@@ -20,11 +21,13 @@ def get_url(listname=None): | @@ -20,11 +21,13 @@ def get_url(listname=None): | ||
20 | def subscribe(listname, address): | 21 | def subscribe(listname, address): |
21 | url = get_url(listname) | 22 | url = get_url(listname) |
22 | try: | 23 | try: |
23 | - requests.put(url, timeout=TIMEOUT, data={'address': address}) | 24 | + result = requests.put(url, timeout=TIMEOUT, data={'address': address}) |
25 | + if result.status_code is not 200: | ||
26 | + return False, '%s: %s' % (listname, result.json()) | ||
24 | except: | 27 | except: |
25 | LOGGER.exception('Unable to subscribe user') | 28 | LOGGER.exception('Unable to subscribe user') |
26 | - return False | ||
27 | - return True | 29 | + return False, 'Unable to subscribe user' |
30 | + return True, 'Success' | ||
28 | 31 | ||
29 | 32 | ||
30 | def unsubscribe(listname, address): | 33 | def unsubscribe(listname, address): |
@@ -39,6 +42,7 @@ def unsubscribe(listname, address): | @@ -39,6 +42,7 @@ def unsubscribe(listname, address): | ||
39 | 42 | ||
40 | def update_subscription(address, lists): | 43 | def update_subscription(address, lists): |
41 | current_lists = mailing_lists(address=address) | 44 | current_lists = mailing_lists(address=address) |
45 | + error_messages = [] | ||
42 | 46 | ||
43 | for maillist in current_lists: | 47 | for maillist in current_lists: |
44 | if maillist not in lists: | 48 | if maillist not in lists: |
@@ -46,7 +50,11 @@ def update_subscription(address, lists): | @@ -46,7 +50,11 @@ def update_subscription(address, lists): | ||
46 | 50 | ||
47 | for maillist in lists: | 51 | for maillist in lists: |
48 | if maillist not in current_lists: | 52 | if maillist not in current_lists: |
49 | - subscribe(maillist, address) | 53 | + subscribed, message = subscribe(maillist, address) |
54 | + if not subscribed: | ||
55 | + error_messages.append(message) | ||
56 | + | ||
57 | + return error_messages | ||
50 | 58 | ||
51 | 59 | ||
52 | def mailing_lists(**kwargs): | 60 | def mailing_lists(**kwargs): |
colab/accounts/views.py
@@ -144,7 +144,9 @@ class ManageUserSubscriptionsView(UserProfileBaseMixin, DetailView): | @@ -144,7 +144,9 @@ class ManageUserSubscriptionsView(UserProfileBaseMixin, DetailView): | ||
144 | user = self.get_object() | 144 | user = self.get_object() |
145 | for email in user.emails.values_list('address', flat=True): | 145 | for email in user.emails.values_list('address', flat=True): |
146 | lists = self.request.POST.getlist(email) | 146 | lists = self.request.POST.getlist(email) |
147 | - user.update_subscription(email, lists) | 147 | + info_messages = user.update_subscription(email, lists) |
148 | + for message in info_messages: | ||
149 | + messages.success(request, _(message)) | ||
148 | 150 | ||
149 | return redirect('user_profile', username=user.username) | 151 | return redirect('user_profile', username=user.username) |
150 | 152 |