Commit 0f909e81959cea3bd02f38edbabab83c1b6ed093
1 parent
975fe726
Exists in
master
and in
28 other branches
Added code errors for subscription errors
Showing
2 changed files
with
22 additions
and
7 deletions
Show diff stats
colab/accounts/utils/mailman.py
| ... | ... | @@ -10,6 +10,22 @@ TIMEOUT = 1 |
| 10 | 10 | |
| 11 | 11 | LOGGER = logging.getLogger('colab.mailman') |
| 12 | 12 | |
| 13 | +S = 'success' | |
| 14 | +I = 'info' | |
| 15 | +E = 'error' | |
| 16 | + | |
| 17 | +MAILMAN_MSGS = { | |
| 18 | + 0: (S, '%s: Success!'), | |
| 19 | + 1: (S, '%s: An email confirmation was sent to you, please check your inbox.'), | |
| 20 | + 2: (I, '%s: Your subscription was sent successfully! Please wait for the list\'s admin approval.'), | |
| 21 | + 3: (I, '%s: You are already a member of this list.'), | |
| 22 | + 4: (E, '%s: You are banned from this list!'), | |
| 23 | + 5: (E, '%s: You appear to have an invalid email address.'), | |
| 24 | + 6: (E, '%s: Your email address is considered to be hostile.'), | |
| 25 | + 7: (E, '%s: You are not a member of this list.'), | |
| 26 | + 8: (E, 'Missing information: `email_from`, `subject` and `body` are mandatory.'), | |
| 27 | +} | |
| 28 | + | |
| 13 | 29 | |
| 14 | 30 | def get_url(listname=None): |
| 15 | 31 | if listname: |
| ... | ... | @@ -22,10 +38,11 @@ def subscribe(listname, address): |
| 22 | 38 | url = get_url(listname) |
| 23 | 39 | try: |
| 24 | 40 | result = requests.put(url, timeout=TIMEOUT, data={'address': address}) |
| 25 | - return True, '%s: %s' % (listname, result.json()) | |
| 41 | + msg_type, message = MAILMAN_MSGS[result.json()] | |
| 42 | + return msg_type, message % listname | |
| 26 | 43 | except: |
| 27 | 44 | LOGGER.exception('Unable to subscribe user') |
| 28 | - return False, 'Error: Unable to subscribe user' | |
| 45 | + return E, 'Error: Unable to subscribe user' | |
| 29 | 46 | |
| 30 | 47 | |
| 31 | 48 | def unsubscribe(listname, address): | ... | ... |
colab/accounts/views.py
| ... | ... | @@ -145,11 +145,9 @@ class ManageUserSubscriptionsView(UserProfileBaseMixin, DetailView): |
| 145 | 145 | for email in user.emails.values_list('address', flat=True): |
| 146 | 146 | lists = self.request.POST.getlist(email) |
| 147 | 147 | info_messages = user.update_subscription(email, lists) |
| 148 | - for operation_status, message in info_messages: | |
| 149 | - if operation_status: | |
| 150 | - messages.success(request, _(message)) | |
| 151 | - else: | |
| 152 | - messages.error(request, _(message)) | |
| 148 | + for msg_type, message in info_messages: | |
| 149 | + show_message = getattr(messages, msg_type) | |
| 150 | + show_message(request, _(message)) | |
| 153 | 151 | |
| 154 | 152 | |
| 155 | 153 | return redirect('user_profile', username=user.username) | ... | ... |