diff --git a/CHANGELOG.md b/CHANGELOG.md index 548361a1..1da8ef52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ adhérents ni des cotisations. ### K-Fêt +- Ajoute de mails de rappels pour les comptes en négatif - La recherche de comptes sur K-Psul remarche normalement - Le pointeur de la souris change de forme quand on survole un item d'autocomplétion - Modification du gel de compte: diff --git a/gestioasso/settings/cof_prod.py b/gestioasso/settings/cof_prod.py index b8b1c0ff..cbb643c6 100644 --- a/gestioasso/settings/cof_prod.py +++ b/gestioasso/settings/cof_prod.py @@ -204,6 +204,9 @@ MAIL_DATA = { "REPLYTO": "cof@ens.fr", }, "rappels": {"FROM": "Le BdA ", "REPLYTO": "Le BdA "}, + "rappel_negatif": { + "FROM": "La K-Fêt ", + }, "revente": { "FROM": "BdA-Revente ", "REPLYTO": "BdA-Revente ", diff --git a/kfet/management/commands/sendrappelsnegatifs.py b/kfet/management/commands/sendrappelsnegatifs.py new file mode 100644 index 00000000..58c47d4e --- /dev/null +++ b/kfet/management/commands/sendrappelsnegatifs.py @@ -0,0 +1,41 @@ +""" +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.") diff --git a/kfet/migrations/0080_accountnegative_last_rappel.py b/kfet/migrations/0080_accountnegative_last_rappel.py new file mode 100644 index 00000000..9dc82bea --- /dev/null +++ b/kfet/migrations/0080_accountnegative_last_rappel.py @@ -0,0 +1,20 @@ +# Generated by Django 2.2.17 on 2021-02-23 21:37 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("kfet", "0079_auto_20210627_0022"), + ] + + operations = [ + migrations.AddField( + model_name="accountnegative", + name="last_rappel", + field=models.DateTimeField( + blank=True, null=True, verbose_name="Mail de rappel envoyé" + ), + ), + ] diff --git a/kfet/models.py b/kfet/models.py index 8e9a6e42..7a0ec777 100644 --- a/kfet/models.py +++ b/kfet/models.py @@ -1,9 +1,12 @@ import re +from django.conf import settings from django.contrib.auth.models import User +from django.core.mail import send_mail from django.core.validators import RegexValidator from django.db import models, transaction from django.db.models import F +from django.template import loader from django.urls import reverse from django.utils import timezone from django.utils.translation import ugettext_lazy as _ @@ -285,10 +288,34 @@ class AccountNegative(models.Model): ) start = models.DateTimeField(blank=True, null=True, default=None) end = models.DateTimeField(blank=True, null=True, default=None) + last_rappel = models.DateTimeField("Mail de rappel envoyé", blank=True, null=True) class Meta: permissions = (("view_negs", "Voir la liste des négatifs"),) + def send_rappel(self): + """ + Envoie un mail de rappel signalant que la personne est en négatif. + """ + # On envoie le mail + send_mail( + "Compte K-Psul négatif", + loader.render_to_string( + "kfet/mails/rappel.txt", + context={ + "account": self.account, + "neg_amount": -self.account.balance, + "start_date": self.start, + }, + ), + settings.MAIL_DATA["rappel_negatif"]["FROM"], + [self.account.email], + ) + # On enregistre le fait que l'envoi a bien eu lieu + self.last_rappel = timezone.now() + self.save() + return + class CheckoutQuerySet(models.QuerySet): def is_valid(self): diff --git a/kfet/templates/kfet/mails/rappel.txt b/kfet/templates/kfet/mails/rappel.txt new file mode 100644 index 00000000..b37b2b0e --- /dev/null +++ b/kfet/templates/kfet/mails/rappel.txt @@ -0,0 +1,8 @@ +Bonjour {{ account.first_name }}, + +Nous te rappelons que tu es en négatif de {{ neg_amount }}€ depuis le {{ start_date }}. +N'oublie pas de régulariser ta situation au plus vite. + +En espérant te revoir très bientôt, +-- +L'équipe K-Fêt