kpsul/kfet/management/commands/sendrappelsnegatifs.py
2021-10-22 22:36:30 +02:00

42 lines
1.4 KiB
Python

"""
Gestion en ligne de commande des mails de rappel K-Fet.
"""
from datetime import timedelta
from django.core.management.base import BaseCommand
from django.utils import timezone
from kfet.models import AccountNegative
class Command(BaseCommand):
help = (
"Envoie un mail de rappel aux personnes en négatif.\n"
"Envoie un mail au bout de 24h, puis un mail par semaine."
)
leave_locale_alone = True
def handle(self, *args, **options):
now = timezone.now()
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()
)
accounts_periodic_mail = (
AccountNegative.objects.filter(last_rappel__isnull=False)
.filter(last_rappel__lt=now - periodic_delay)
.all()
)
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):
self.stdout.write("Aucun mail à envoyer.")