From 87f383bef1698b1317f3a2d42682a7794427afc9 Mon Sep 17 00:00:00 2001 From: Tom Hubrecht Date: Sat, 18 Dec 2021 18:06:27 +0100 Subject: [PATCH] =?UTF-8?q?On=20n'envoie=20des=20mails=20de=20rappel=20que?= =?UTF-8?q?=20lorsque=20le=20n=C3=A9gatif=20est=20toujours=20d'actualit?= =?UTF-8?q?=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commands/sendrappelsnegatifs.py | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/kfet/management/commands/sendrappelsnegatifs.py b/kfet/management/commands/sendrappelsnegatifs.py index 58c47d4e..2351f68f 100644 --- a/kfet/management/commands/sendrappelsnegatifs.py +++ b/kfet/management/commands/sendrappelsnegatifs.py @@ -19,23 +19,31 @@ class Command(BaseCommand): def handle(self, *args, **options): now = timezone.now() + + # Le premier mail est envoyé après 24h de négatif, puis toutes les semaines first_delay = timedelta(days=1) periodic_delay = timedelta(weeks=1) - accounts_first_mail = ( - AccountNegative.objects.filter(start__lt=now - first_delay) - .filter(last_rappel__isnull=True) - .all() + + # On n'envoie des mails qu'aux comptes qui ont un négatif vraiment actif + # et dont la balance est négative + account_negatives = AccountNegative.objects.filter( + account__balance__lt=0 + ).exclude(end__lte=now) + + accounts_first_mail = account_negatives.filter( + start__lt=now - first_delay, last_rappel__isnull=True ) - accounts_periodic_mail = ( - AccountNegative.objects.filter(last_rappel__isnull=False) - .filter(last_rappel__lt=now - periodic_delay) - .all() + accounts_periodic_mail = account_negatives.filter( + last_rappel__isnull=False, last_rappel__lt=now - periodic_delay ) + for neg in accounts_first_mail: neg.send_rappel() self.stdout.write(f"Mail de rappel pour {neg.account} envoyé avec succès.") + for neg in accounts_periodic_mail: neg.send_rappel() self.stdout.write("Mail de rappel pour {neg.account} envoyé avec succès.") - if (not accounts_first_mail) and (not accounts_periodic_mail): + + if not (accounts_first_mail.exists() or accounts_periodic_mail.exists()): self.stdout.write("Aucun mail à envoyer.")