From ca7ad0574661e076b2cd6060a7bc70558ec2d3ea Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Sun, 4 Oct 2020 20:26:43 +0200 Subject: [PATCH] Use a signal to prevent a user that the note balance is negative --- apps/note/apps.py | 11 ++++++++++- apps/note/models/notes.py | 28 ---------------------------- apps/note/signals.py | 12 ++++++++++++ 3 files changed, 22 insertions(+), 29 deletions(-) diff --git a/apps/note/apps.py b/apps/note/apps.py index b3dc5a0f..af1dcc12 100644 --- a/apps/note/apps.py +++ b/apps/note/apps.py @@ -3,7 +3,7 @@ from django.apps import AppConfig from django.conf import settings -from django.db.models.signals import post_save, pre_delete +from django.db.models.signals import pre_delete, pre_save, post_save from django.utils.translation import gettext_lazy as _ from . import signals @@ -17,6 +17,15 @@ class NoteConfig(AppConfig): """ Define app internal signals to interact with other apps """ + pre_save.connect( + signals.pre_save_note, + sender="note.noteuser", + ) + pre_save.connect( + signals.pre_save_note, + sender="note.noteclub", + ) + post_save.connect( signals.save_user_note, sender=settings.AUTH_USER_MODEL, diff --git a/apps/note/models/notes.py b/apps/note/models/notes.py index 49b9fd58..c649dbc9 100644 --- a/apps/note/models/notes.py +++ b/apps/note/models/notes.py @@ -159,20 +159,6 @@ class NoteUser(Note): def pretty(self): return _("%(user)s's note") % {'user': str(self.user)} - @transaction.atomic - def save(self, *args, **kwargs): - if self.pk and self.balance < 0: - old_note = NoteUser.objects.get(pk=self.pk) - super().save(*args, **kwargs) - if old_note.balance >= 0: - # Passage en négatif - self.last_negative = timezone.now() - self._force_save = True - self.save(*args, **kwargs) - self.send_mail_negative_balance() - else: - super().save(*args, **kwargs) - 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)) @@ -201,20 +187,6 @@ class NoteClub(Note): def pretty(self): return _("Note of %(club)s club") % {'club': str(self.club)} - @transaction.atomic - def save(self, *args, **kwargs): - if self.pk and self.balance < 0: - old_note = NoteClub.objects.get(pk=self.pk) - super().save(*args, **kwargs) - if old_note.balance >= 0: - # Passage en négatif - self.last_negative = timezone.now() - self._force_save = True - self.save(*args, **kwargs) - self.send_mail_negative_balance() - else: - super().save(*args, **kwargs) - 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)) diff --git a/apps/note/signals.py b/apps/note/signals.py index c1545ec2..8c02b3a5 100644 --- a/apps/note/signals.py +++ b/apps/note/signals.py @@ -1,6 +1,8 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later +from django.utils import timezone + def save_user_note(instance, raw, **_kwargs): """ @@ -25,6 +27,16 @@ def save_club_note(instance, raw, **_kwargs): instance.note.save() +def pre_save_note(instance, raw, **_kwargs): + if not raw and instance.pk and not hasattr(instance, "_no_signal") and instance.balance < 0: + from note.models import Note + old_note = Note.objects.get(pk=instance.pk) + if old_note.balance >= 0: + # Passage en négatif + instance.last_negative = timezone.now() + instance.send_mail_negative_balance() + + def delete_transaction(instance, **_kwargs): """ Whenever we want to delete a transaction (caution with this), we ensure the transaction is invalid first.