From e18873115aadd44622bf874e2ab8c01293a05295 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Wed, 27 Oct 2021 14:02:14 +0200 Subject: [PATCH] =?UTF-8?q?Catch=20errors=20while=20sending=20K-F=C3=AAt?= =?UTF-8?q?=20reminder=20emails?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kfet/models.py | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/kfet/models.py b/kfet/models.py index 7a0ec777..024b1b29 100644 --- a/kfet/models.py +++ b/kfet/models.py @@ -1,4 +1,6 @@ +import logging import re +import smtplib from django.conf import settings from django.contrib.auth.models import User @@ -20,6 +22,8 @@ from .auth.models import GenericTeamToken # noqa from .config import kfet_config from .utils import to_ukf +logger = logging.getLogger(__name__) + class AccountManager(models.Manager): """Manager for Account Model.""" @@ -298,23 +302,29 @@ class AccountNegative(models.Model): Envoie un mail de rappel signalant que la personne est en négatif. """ # On envoie le mail - send_mail( - "Compte K-Psul négatif", - loader.render_to_string( - "kfet/mails/rappel.txt", - context={ - "account": self.account, - "neg_amount": -self.account.balance, - "start_date": self.start, - }, - ), - settings.MAIL_DATA["rappel_negatif"]["FROM"], - [self.account.email], - ) - # On enregistre le fait que l'envoi a bien eu lieu - self.last_rappel = timezone.now() - self.save() - return + try: + send_mail( + "Compte K-Psul négatif", + loader.render_to_string( + "kfet/mails/rappel.txt", + context={ + "account": self.account, + "neg_amount": -self.account.balance, + "start_date": self.start, + }, + ), + settings.MAIL_DATA["rappel_negatif"]["FROM"], + [self.account.email], + ) + # On enregistre le fait que l'envoi a bien eu lieu + self.last_rappel = timezone.now() + self.save() + except smtplib.SMTPException: + logger.warning( + "L'envoi du mail de rappel pour le négatif de {} a échoué".format( + self.account + ) + ) class CheckoutQuerySet(models.QuerySet):