Commit c57dd273ae6be8f59ce8521b9f3879690e92f59b
1 parent
f1e92a83
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
| ... | ... | @@ -139,7 +139,9 @@ class ManageUserSubscriptionsView(UserProfileBaseMixin, DetailView): |
| 139 | 139 | user = self.get_object() |
| 140 | 140 | for email in user.emails.values_list('address', flat=True): |
| 141 | 141 | lists = self.request.POST.getlist(email) |
| 142 | - user.update_subscription(email, lists) | |
| 142 | + info_messages = user.update_subscription(email, lists) | |
| 143 | + for message in info_messages: | |
| 144 | + messages.success(request, _(message)) | |
| 143 | 145 | |
| 144 | 146 | return redirect('user_profile', username=user.username) |
| 145 | 147 | ... | ... |