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 | 60 | return mailman.user_lists(self) |
61 | 61 | |
62 | 62 | def update_subscription(self, email, lists): |
63 | - mailman.update_subscription(email, lists) | |
63 | + return mailman.update_subscription(email, lists) | |
64 | 64 | |
65 | 65 | def save(self, *args, **kwargs): |
66 | 66 | ... | ... |
colab/accounts/utils/mailman.py
... | ... | @@ -4,6 +4,7 @@ import requests |
4 | 4 | import logging |
5 | 5 | |
6 | 6 | from django.conf import settings |
7 | +from django.contrib import messages | |
7 | 8 | |
8 | 9 | TIMEOUT = 1 |
9 | 10 | |
... | ... | @@ -20,11 +21,13 @@ def get_url(listname=None): |
20 | 21 | def subscribe(listname, address): |
21 | 22 | url = get_url(listname) |
22 | 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 | 27 | except: |
25 | 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 | 33 | def unsubscribe(listname, address): |
... | ... | @@ -39,6 +42,7 @@ def unsubscribe(listname, address): |
39 | 42 | |
40 | 43 | def update_subscription(address, lists): |
41 | 44 | current_lists = mailing_lists(address=address) |
45 | + error_messages = [] | |
42 | 46 | |
43 | 47 | for maillist in current_lists: |
44 | 48 | if maillist not in lists: |
... | ... | @@ -46,7 +50,11 @@ def update_subscription(address, lists): |
46 | 50 | |
47 | 51 | for maillist in lists: |
48 | 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 | 60 | def mailing_lists(**kwargs): | ... | ... |
colab/accounts/views.py
... | ... | @@ -144,7 +144,9 @@ class ManageUserSubscriptionsView(UserProfileBaseMixin, DetailView): |
144 | 144 | user = self.get_object() |
145 | 145 | for email in user.emails.values_list('address', flat=True): |
146 | 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 | 151 | return redirect('user_profile', username=user.username) |
150 | 152 | ... | ... |