42 lines
1.4 KiB
Python
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 account in accounts_first_mail:
|
||
|
account.send_rappel()
|
||
|
self.stdout.write("Mail de rappel pour %s envoyé avec succès." % account)
|
||
|
for account in accounts_periodic_mail:
|
||
|
account.send_rappel()
|
||
|
self.stdout.write("Mail de rappel pour %s envoyé avec succès." % account)
|
||
|
if (not accounts_first_mail) and (not accounts_periodic_mail):
|
||
|
self.stdout.write("Aucun mail à envoyer.")
|