Merge branch 'thubrecht/rappels_negatifs' into 'master'

On n'envoie des mails de rappel que lorsque le négatif est toujours d'actualité

Closes #298

See merge request klub-dev-ens/gestioCOF!509
This commit is contained in:
Tom Hubrecht 2023-01-28 15:33:23 +01:00
commit a8d4035d33
3 changed files with 20 additions and 10 deletions

View file

@ -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__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.")

View file

@ -260,8 +260,10 @@ class Account(models.Model):
elif hasattr(self, "negative"):
if self.negative.end is None:
self.negative.end = timezone.now()
self.negative.save()
elif timezone.now() > self.negative.end + kfet_config.cancel_duration:
# Idem: on supprime le négatif après une légère période
# Nécessaire pour se souvenir du négatif après une charge annulée
self.negative.delete()
class UserHasAccount(Exception):

View file

@ -53,7 +53,7 @@
</ul>
</div>
{% if account.negative %}
{% if account.negative and account.balance_ukf < 0 %}
<div class="text">
<h4>Négatif</h4>
<ul class="list-unstyled">