From d8cabda6789d51a45af241b9178fe17e32636aa1 Mon Sep 17 00:00:00 2001 From: Alseidon Date: Tue, 23 Feb 2021 23:26:25 +0100 Subject: [PATCH 1/3] First draft of reminder mail for negative K-Psul accounts --- gestioasso/settings/cof_prod.py | 3 ++ .../commands/sendrappelsnegatifs.py | 41 +++++++++++++++++++ .../0080_accountnegative_last_rappel.py | 20 +++++++++ kfet/models.py | 23 +++++++++++ kfet/templates/kfet/mails/rappel.txt | 8 ++++ 5 files changed, 95 insertions(+) create mode 100644 kfet/management/commands/sendrappelsnegatifs.py create mode 100644 kfet/migrations/0080_accountnegative_last_rappel.py create mode 100644 kfet/templates/kfet/mails/rappel.txt 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..7ff0be20 --- /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 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.") 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..b8b4a445 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,30 @@ 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, "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..438ac68f --- /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 {{ account.balance }}€ 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 \ No newline at end of file From a77cf59b18b6b1c412d01333f19f3f8412ef0d19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Fri, 22 Oct 2021 22:36:30 +0200 Subject: [PATCH 2/3] =?UTF-8?q?Rappels=20n=C3=A9gatifs=20K-F=C3=AAt:=20aju?= =?UTF-8?q?stements=20cosm=C3=A9tiques?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kfet/management/commands/sendrappelsnegatifs.py | 12 ++++++------ kfet/models.py | 6 +++++- kfet/templates/kfet/mails/rappel.txt | 4 ++-- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/kfet/management/commands/sendrappelsnegatifs.py b/kfet/management/commands/sendrappelsnegatifs.py index 7ff0be20..58c47d4e 100644 --- a/kfet/management/commands/sendrappelsnegatifs.py +++ b/kfet/management/commands/sendrappelsnegatifs.py @@ -31,11 +31,11 @@ class Command(BaseCommand): .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) + 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/models.py b/kfet/models.py index b8b4a445..7a0ec777 100644 --- a/kfet/models.py +++ b/kfet/models.py @@ -302,7 +302,11 @@ class AccountNegative(models.Model): "Compte K-Psul négatif", loader.render_to_string( "kfet/mails/rappel.txt", - context={"account": self.account, "start_date": self.start}, + context={ + "account": self.account, + "neg_amount": -self.account.balance, + "start_date": self.start, + }, ), settings.MAIL_DATA["rappel_negatif"]["FROM"], [self.account.email], diff --git a/kfet/templates/kfet/mails/rappel.txt b/kfet/templates/kfet/mails/rappel.txt index 438ac68f..b37b2b0e 100644 --- a/kfet/templates/kfet/mails/rappel.txt +++ b/kfet/templates/kfet/mails/rappel.txt @@ -1,8 +1,8 @@ Bonjour {{ account.first_name }}, -Nous te rappelons que tu es en négatif de {{ account.balance }}€ depuis le {{ start_date }}. +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 \ No newline at end of file +L'équipe K-Fêt From 9a143521d5e4ffd9e54ada49353b7c0a2f6694bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Fri, 22 Oct 2021 22:37:56 +0200 Subject: [PATCH 3/3] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) 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: