From eaf256b1b6691978c9d56224c13b5d2f83c6e875 Mon Sep 17 00:00:00 2001
From: Yohann D'ANELLO <yohann.danello@gmail.com>
Date: Tue, 1 Sep 2020 15:52:27 +0200
Subject: [PATCH] Fix mails when the user or the club has a negative balance

---
 apps/note/models/notes.py                            | 7 ++++---
 apps/note/templates/note/mails/negative_balance.html | 6 +++---
 apps/note/templates/note/mails/negative_balance.txt  | 4 ++--
 3 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/apps/note/models/notes.py b/apps/note/models/notes.py
index 53dd18ef..e5d9c13c 100644
--- a/apps/note/models/notes.py
+++ b/apps/note/models/notes.py
@@ -4,7 +4,9 @@
 import unicodedata
 
 from django.conf import settings
+from django.conf.global_settings import DEFAULT_FROM_EMAIL
 from django.core.exceptions import ValidationError
+from django.core.mail import send_mail
 from django.core.validators import RegexValidator
 from django.db import models
 from django.template.loader import render_to_string
@@ -193,7 +195,6 @@ class NoteClub(Note):
             super().save(*args, **kwargs)
             if old_note.balance >= 0:
                 # Passage en négatif
-                super().save(*args, **kwargs)
                 self.last_negative = timezone.now()
                 self._force_save = True
                 self.save(*args, **kwargs)
@@ -204,8 +205,8 @@ class NoteClub(Note):
     def send_mail_negative_balance(self):
         plain_text = render_to_string("note/mails/negative_balance.txt", dict(note=self))
         html = render_to_string("note/mails/negative_balance.html", dict(note=self))
-        self.user.email_user("[Note Kfet] Passage en négatif (club {})"
-                             .format(self.club.name), plain_text, html_message=html)
+        send_mail("[Note Kfet] Passage en négatif (club {})".format(self.club.name), plain_text, DEFAULT_FROM_EMAIL,
+                  [self.club.email], html_message=html)
 
 
 class NoteSpecial(Note):
diff --git a/apps/note/templates/note/mails/negative_balance.html b/apps/note/templates/note/mails/negative_balance.html
index ef532998..8c869a54 100644
--- a/apps/note/templates/note/mails/negative_balance.html
+++ b/apps/note/templates/note/mails/negative_balance.html
@@ -6,11 +6,11 @@
 <html lang="fr">
 <head>
     <meta charset="UTF-8">
-    <title>Passage en négatif (compte n°{{ note.user.pk }})</title>
+    <title>Passage en négatif (compte n°{{ note.pk }})</title>
 </head>
 <body>
 <p>
-    Bonjour {{ note }},
+    Bonjour {% if note.user %}{{ note.user }}{% else %}{{ note.club.name }}{% endif %},
 </p>
 
 <p>
@@ -28,7 +28,7 @@
 
 <p>
     Si tu ne comprends pas ton solde, tu peux consulter ton historique
-    sur <a href="https://{{ "NOTE_URL"|getenv }}{% url "member:user_detail" pk=note.user.pk %}">ton compte</a>.
+    sur <a href="https://{{ "NOTE_URL"|getenv }}{% if note.user %}{% url "member:user_detail" pk=note.user.pk %}{% else %}{% url "member:club_detail" pk=note.club.pk %}{% endif %}">ton compte</a>.
 </p>
 
 <p>
diff --git a/apps/note/templates/note/mails/negative_balance.txt b/apps/note/templates/note/mails/negative_balance.txt
index f257194d..0052995a 100644
--- a/apps/note/templates/note/mails/negative_balance.txt
+++ b/apps/note/templates/note/mails/negative_balance.txt
@@ -2,7 +2,7 @@
 {% load getenv %}
 {% load i18n %}
 
-Bonjour {{ note }},
+Bonjour {% if note.user %}{{ note.user }}{% else %}{{ note.club.name }}{% endif %},
 
 Ce mail t'a été envoyé parce que le solde de ta Note Kfet
 {{ note }} est négatif !
@@ -13,7 +13,7 @@ Par ailleurs, le BDE ne sert pas d'alcool aux adhérents dont le solde
 est inférieur à 0 € depuis plus de 24h.
 
 Si tu ne comprends pas ton solde, tu peux consulter ton historique
-sur ton compte {{ "NOTE_URL"|getenv }}{% url "member:user_detail" pk=note.user.pk %}
+sur ton compte {{ "NOTE_URL"|getenv }}{% if note.user %}{% url "member:user_detail" pk=note.user.pk %}{% else %}{% url "member:club_detail" pk=note.club.pk %}{% endif %}
 
 Tu peux venir recharger ta note rapidement à la Kfet, ou envoyer un mail à
 la trésorerie du BdE (tresorerie.bde@lists.crans.org) pour payer par
-- 
GitLab