[damned-lies] [vertimus] Catch exceptions while sending mail



commit e0330362b29af13cd79ce1f1301d2f8b9317baa9
Author: Claude Paroz <claude 2xlibre net>
Date:   Sun Jun 23 11:58:02 2013 +0200

    [vertimus] Catch exceptions while sending mail

 vertimus/models.py |   20 +++++++++++++++-----
 vertimus/views.py  |    7 ++++++-
 2 files changed, 21 insertions(+), 6 deletions(-)
---
diff --git a/vertimus/models.py b/vertimus/models.py
index be47631..7b8b01c 100644
--- a/vertimus/models.py
+++ b/vertimus/models.py
@@ -1,7 +1,7 @@
 # -*- coding: utf-8 -*-
 #
 # Copyright (c) 2008-2009 Stéphane Raimbault <stephane raimbault gmail com>
-# Copyright (c) 2011-2012 Claude Paroz <claude 2xlibre net>
+# Copyright (c) 2011-2013 Claude Paroz <claude 2xlibre net>
 #
 # This file is part of Damned Lies.
 #
@@ -40,6 +40,11 @@ from people.models import Person
 
 from teams.models import Role
 
+
+class SendMailFailed(Exception):
+    """Somehting went wrong during sending message"""
+    pass
+
 #
 # States
 #
@@ -422,14 +427,15 @@ class Action(ActionAbstract):
         else:
             # Force updated state date to be updated
             self.state_db.save()
-        if send_to_ml:
-            self.send_mail_new_state(state, (state.language.team.mailing_list,))
 
         if self.target_state == StateCommitted:
             # Committed is the last state of the workflow, archive actions
             arch_action = self.new_by_name('AA', person=self.person)
             arch_action.apply_on(self.state_db, False)
 
+        if send_to_ml:
+            self.send_mail_new_state(state, (state.language.team.mailing_list,))
+
     def get_previous_action_with_po(self):
         """
         Return the previous Action with an uploaded file related to the
@@ -503,8 +509,12 @@ class Action(ActionAbstract):
         message += self.comment or ugettext("Without comment")
         message += "\n\n" + self.person.name
         message += "\n--\n" + ugettext(u"This is an automated message sent from %s.") % current_site.domain
-        mail.send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, recipient_list)
-        activate(current_lang)
+        try:
+            mail.send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, recipient_list)
+        except Exception as exc:
+            raise SendMailFailed("Sending message failed: %r" % exc)
+        finally:
+            activate(current_lang)
 
 
 def generate_archive_filename(instance, original_filename):
diff --git a/vertimus/views.py b/vertimus/views.py
index 5b4b0bb..81a8a8e 100644
--- a/vertimus/views.py
+++ b/vertimus/views.py
@@ -22,6 +22,7 @@
 import os
 
 from django.conf import settings
+from django.contrib import messages
 from django.core import urlresolvers
 from django.http import HttpResponseRedirect, Http404
 from django.shortcuts import render, get_object_or_404
@@ -105,7 +106,11 @@ def vertimus(request, branch, domain, language, stats=None, level="0"):
 
                 action = Action.new_by_name(action, person=person, comment=comment,
                     file=request.FILES.get('file', None))
-                action.apply_on(state, action_form.cleaned_data.get('send_to_ml'))
+                try:
+                    action.apply_on(state, action_form.cleaned_data.get('send_to_ml'))
+                except SendMailFailed:
+                    messages.error(request, 
+                        _("A problem occurred while sending mail, no mail have been sent"))
 
                 return HttpResponseRedirect(
                     urlresolvers.reverse('vertimus_by_names',


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]