From 12c10847e7a648dbbd1b4da14f63b70668e12005 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Wed, 23 Jan 2019 18:19:54 +0100 Subject: [PATCH] use send_mass_custom_mail_wrapper for bda notifications --- bda/admin.py | 34 +++++++++++++++++++++++----------- bda/models.py | 39 +++++++++++++++++++++++---------------- 2 files changed, 46 insertions(+), 27 deletions(-) diff --git a/bda/admin.py b/bda/admin.py index b32144f1..f4710350 100644 --- a/bda/admin.py +++ b/bda/admin.py @@ -1,6 +1,5 @@ from datetime import timedelta -from custommail.shortcuts import send_mass_custom_mail from dal.autocomplete import ModelSelect2 from django import forms from django.contrib import admin @@ -20,6 +19,8 @@ from bda.models import ( Tirage, ) +from utils.mailutils import send_mass_custom_mail_wrapper, SendMassMailRecipientRefused + class ReadOnlyMixin(object): readonly_fields_update = () @@ -142,17 +143,28 @@ class ParticipantAdmin(ReadOnlyMixin, admin.ModelAdmin): context["places"] = attribs print(context) datatuple.append((shortname, context, "bda@ens.fr", [member.user.email])) - send_mass_custom_mail(datatuple) count = len(queryset.all()) - if count == 1: - message_bit = "1 membre a" - plural = "" - else: - message_bit = "%d membres ont" % count - plural = "s" - self.message_user( - request, "%s été informé%s avec succès." % (message_bit, plural) - ) + try: + send_mass_custom_mail_wrapper(datatuple) + except SendMassMailRecipientRefused as exn: + errors, = exn.args + faulty_emails = ", ".join(errors.keys()) + count -= len(errors) + self.message_user( + request, + "Les emails suivant causent des erreurs: {}".format(faulty_emails) + ) + raise + finally: + if count == 1: + message_bit = "[1/{}] membre a".format(len(queryset.all())) + plural = "" + else: + message_bit = "[{}/{}] membres ont".format(count, queryset.all()) + plural = "s" + self.message_user( + request, "%s été informé%s avec succès." % (message_bit, plural) + ) send_attribs.short_description = "Envoyer les résultats par mail" diff --git a/bda/models.py b/bda/models.py index 9ac38a41..5814221c 100644 --- a/bda/models.py +++ b/bda/models.py @@ -3,7 +3,6 @@ import random from datetime import timedelta from custommail.models import CustomMail -from custommail.shortcuts import send_mass_custom_mail from django.conf import settings from django.contrib.auth.models import User from django.contrib.sites.models import Site @@ -12,6 +11,8 @@ from django.db import models from django.db.models import Count from django.utils import formats, timezone +from utils.mailutils import send_mass_custom_mail_wrapper + def get_generic_user(): generic, _ = User.objects.get_or_create( @@ -125,10 +126,12 @@ class Spectacle(models.Model): ) for member in members ] - send_mass_custom_mail(datatuple) - # On enregistre le fait que l'envoi a bien eu lieu - self.rappel_sent = timezone.now() - self.save() + try: + send_mass_custom_mail_wrapper(datatuple) + finally: + # On enregistre le fait que l'envoi a bien eu lieu + self.rappel_sent = timezone.now() + self.save() # On renvoie la liste des destinataires return members @@ -343,10 +346,12 @@ class SpectacleRevente(models.Model): ) for participant in inscrits ] - send_mass_custom_mail(datatuple) - self.notif_sent = True - self.notif_time = timezone.now() - self.save() + try: + send_mass_custom_mail_wrapper(datatuple) + finally: + self.notif_sent = True + self.notif_time = timezone.now() + self.save() def mail_shotgun(self): """ @@ -367,13 +372,15 @@ class SpectacleRevente(models.Model): ) for participant in inscrits ] - send_mass_custom_mail(datatuple) - self.notif_sent = True - self.notif_time = timezone.now() - # Flag inutile, sauf si l'horloge interne merde - self.tirage_done = True - self.shotgun = True - self.save() + try: + send_mass_custom_mail_wrapper(datatuple) + finally: + self.notif_sent = True + self.notif_time = timezone.now() + # Flag inutile, sauf si l'horloge interne merde + self.tirage_done = True + self.shotgun = True + self.save() def tirage(self, send_mails=True): """