Commit 47ad4827152bf433934cc4745257fc91a17ebd6d

Authored by Charles Oliveira
1 parent 46a0d475

Added code errors for subscription errors

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
... ... @@ -140,11 +140,9 @@ class ManageUserSubscriptionsView(UserProfileBaseMixin, DetailView):
140 140 for email in user.emails.values_list('address', flat=True):
141 141 lists = self.request.POST.getlist(email)
142 142 info_messages = user.update_subscription(email, lists)
143   - for operation_status, message in info_messages:
144   - if operation_status:
145   - messages.success(request, _(message))
146   - else:
147   - messages.error(request, _(message))
  143 + for msg_type, message in info_messages:
  144 + show_message = getattr(messages, msg_type)
  145 + show_message(request, _(message))
148 146  
149 147  
150 148 return redirect('user_profile', username=user.username)
... ...