From f97d339a1c4a81b5a435b2e13028be8368a83d46 Mon Sep 17 00:00:00 2001 From: Tom Hubrecht Date: Wed, 14 Jun 2023 20:56:25 +0200 Subject: [PATCH] =?UTF-8?q?fix(kfet):=20R=C3=A9cup=C3=A8re=20lors=20d'une?= =?UTF-8?q?=20erreur=20due=20=C3=A0=20smtplib?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kfet/management/commands/sendrappelsnegatifs.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/kfet/management/commands/sendrappelsnegatifs.py b/kfet/management/commands/sendrappelsnegatifs.py index a1a8b7bc..981e2c44 100644 --- a/kfet/management/commands/sendrappelsnegatifs.py +++ b/kfet/management/commands/sendrappelsnegatifs.py @@ -2,6 +2,7 @@ Gestion en ligne de commande des mails de rappel K-Fet. """ +import smtplib from datetime import timedelta from django.core.management.base import BaseCommand @@ -10,6 +11,14 @@ from django.utils import timezone from kfet.models import AccountNegative +def send_mail(neg: AccountNegative, stdout) -> None: + try: + neg.send_rappel() + stdout.write(f"Mail de rappel pour {neg.account} envoyé avec succès.") + except smtplib.SMTPException: + stdout.write(f"Erreur lors de l'envoi du mail de rappel pour {neg.account}.") + + class Command(BaseCommand): help = ( "Envoie un mail de rappel aux personnes en négatif.\n" @@ -39,12 +48,10 @@ class Command(BaseCommand): ) for neg in accounts_first_mail: - neg.send_rappel() - self.stdout.write(f"Mail de rappel pour {neg.account} envoyé avec succès.") + send_mail(neg, self.stdout) for neg in accounts_periodic_mail: - neg.send_rappel() - self.stdout.write(f"Mail de rappel pour {neg.account} envoyé avec succès.") + send_mail(neg, self.stdout) if not (accounts_first_mail.exists() or accounts_periodic_mail.exists()): self.stdout.write("Aucun mail à envoyer.")