From 0f909e81959cea3bd02f38edbabab83c1b6ed093 Mon Sep 17 00:00:00 2001 From: Charles Oliveira <18oliveira.charles@gmail.com> Date: Mon, 27 Jul 2015 16:31:06 -0400 Subject: [PATCH] Added code errors for subscription errors --- colab/accounts/utils/mailman.py | 21 +++++++++++++++++++-- colab/accounts/views.py | 8 +++----- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/colab/accounts/utils/mailman.py b/colab/accounts/utils/mailman.py index 879cd82..183334d 100644 --- a/colab/accounts/utils/mailman.py +++ b/colab/accounts/utils/mailman.py @@ -10,6 +10,22 @@ TIMEOUT = 1 LOGGER = logging.getLogger('colab.mailman') +S = 'success' +I = 'info' +E = 'error' + +MAILMAN_MSGS = { + 0: (S, '%s: Success!'), + 1: (S, '%s: An email confirmation was sent to you, please check your inbox.'), + 2: (I, '%s: Your subscription was sent successfully! Please wait for the list\'s admin approval.'), + 3: (I, '%s: You are already a member of this list.'), + 4: (E, '%s: You are banned from this list!'), + 5: (E, '%s: You appear to have an invalid email address.'), + 6: (E, '%s: Your email address is considered to be hostile.'), + 7: (E, '%s: You are not a member of this list.'), + 8: (E, 'Missing information: `email_from`, `subject` and `body` are mandatory.'), +} + def get_url(listname=None): if listname: @@ -22,10 +38,11 @@ def subscribe(listname, address): url = get_url(listname) try: result = requests.put(url, timeout=TIMEOUT, data={'address': address}) - return True, '%s: %s' % (listname, result.json()) + msg_type, message = MAILMAN_MSGS[result.json()] + return msg_type, message % listname except: LOGGER.exception('Unable to subscribe user') - return False, 'Error: Unable to subscribe user' + return E, 'Error: Unable to subscribe user' def unsubscribe(listname, address): diff --git a/colab/accounts/views.py b/colab/accounts/views.py index f787b42..a35f4e9 100644 --- a/colab/accounts/views.py +++ b/colab/accounts/views.py @@ -145,11 +145,9 @@ class ManageUserSubscriptionsView(UserProfileBaseMixin, DetailView): for email in user.emails.values_list('address', flat=True): lists = self.request.POST.getlist(email) info_messages = user.update_subscription(email, lists) - for operation_status, message in info_messages: - if operation_status: - messages.success(request, _(message)) - else: - messages.error(request, _(message)) + for msg_type, message in info_messages: + show_message = getattr(messages, msg_type) + show_message(request, _(message)) return redirect('user_profile', username=user.username) -- libgit2 0.21.2