From 4b37f8286f493b1a28bd0faa0052ee3967fe543e Mon Sep 17 00:00:00 2001
From: Yohann D'ANELLO <yohann.danello@gmail.com>
Date: Sat, 1 Aug 2020 21:44:01 +0200
Subject: [PATCH] Add script to send mail to negative notes

---
 .../send_mail_to_negative_balances.py         | 25 +++++++++++++++++++
 1 file changed, 25 insertions(+)
 create mode 100644 management/commands/send_mail_to_negative_balances.py

diff --git a/management/commands/send_mail_to_negative_balances.py b/management/commands/send_mail_to_negative_balances.py
new file mode 100644
index 0000000..510de1c
--- /dev/null
+++ b/management/commands/send_mail_to_negative_balances.py
@@ -0,0 +1,25 @@
+# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
+# SPDX-License-Identifier: GPL-3.0-or-later
+
+from django.core.mail import send_mail
+from django.core.management import BaseCommand
+from django.template.loader import render_to_string
+from django.utils import timezone
+from django.utils.translation import activate
+
+from note.models import NoteUser
+
+
+class Command(BaseCommand):
+    def handle(self, *args, **options):
+        notes = NoteUser.objects.filter(
+            balance__lte=-1000,
+            user__memberships__date_end__gte=timezone.now(),
+        ).order_by('balance').distinct().all()
+        for note in notes:
+            note.send_mail_negative_balance()
+        activate('fr')
+        plain_text = render_to_string("note/mails/negative_notes_report.txt", context=dict(notes=notes))
+        html = render_to_string("note/mails/negative_notes_report.html", context=dict(notes=notes))
+        send_mail("[Note Kfet] Liste des négatifs", plain_text, "Note Kfet 2020 <notekfet2020@crans.org>",
+                  recipient_list=["respoinfo.bde@lists.crans.org", "tresorerie.bde@lists.crans.org"], html_message=html)
-- 
GitLab