Use pre_delete signal insted of Model.delete() to prevent note balance issues when deleting a transaction (don't do it) in Django Admin

This commit is contained in:
Yohann D'ANELLO 2020-09-06 20:18:59 +02:00
parent cc5996121b
commit 1023c6c502
3 changed files with 14 additions and 9 deletions

View File

@ -3,7 +3,7 @@
from django.apps import AppConfig from django.apps import AppConfig
from django.conf import settings from django.conf import settings
from django.db.models.signals import post_save from django.db.models.signals import post_save, pre_delete
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from . import signals from . import signals
@ -25,3 +25,8 @@ class NoteConfig(AppConfig):
signals.save_club_note, signals.save_club_note,
sender='member.Club', sender='member.Club',
) )
pre_delete.connect(
signals.delete_transaction,
sender='note.transaction',
)

View File

@ -242,14 +242,6 @@ class Transaction(PolymorphicModel):
self.destination._force_save = True self.destination._force_save = True
self.destination.save() self.destination.save()
def delete(self, **kwargs):
"""
Whenever we want to delete a transaction (caution with this), we ensure the transaction is invalid first.
"""
self.valid = False
self.save(**kwargs)
super().delete(**kwargs)
@property @property
def total(self): def total(self):
return self.amount * self.quantity return self.amount * self.quantity

View File

@ -24,3 +24,11 @@ def save_club_note(instance, raw, **_kwargs):
from .models import NoteClub from .models import NoteClub
NoteClub.objects.get_or_create(club=instance) NoteClub.objects.get_or_create(club=instance)
instance.note.save() instance.note.save()
def delete_transaction(instance, **_kwargs):
"""
Whenever we want to delete a transaction (caution with this), we ensure the transaction is invalid first.
"""
instance.valid = False
instance.save()