1
0
mirror of https://gitlab.crans.org/bde/nk20 synced 2025-06-27 11:58:54 +02:00

Implement signals

This commit is contained in:
Alexandre Iooss
2019-07-19 15:00:44 +02:00
parent d957e643c9
commit 085cd90a7f
5 changed files with 111 additions and 22 deletions

View File

@ -4,8 +4,6 @@
from django.conf import settings
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.utils.translation import gettext_lazy as _
from polymorphic.models import PolymorphicModel
@ -64,6 +62,19 @@ class NoteUser(Note):
def __str__(self):
return _("%(user)s's note") % {'user': str(self.user)}
def save(self, *args, **kwargs):
"""
When saving, also create an alias
TODO: check availability of alias
TODO: remove old alias
"""
created = self.pk is None
if created:
alias = Alias.objects.create(name=str(self.user), note=self)
alias.save()
super().save(*args, **kwargs)
class NoteClub(Note):
"""
@ -83,6 +94,19 @@ class NoteClub(Note):
def __str__(self):
return _("Note for %(club)s club") % {'club': str(self.club)}
def save(self, *args, **kwargs):
"""
When saving, also create an alias
TODO: check availability of alias
TODO: remove old alias
"""
created = self.pk is None
if created:
alias = Alias.objects.create(name=str(self.club), note=self)
alias.save()
super().save(*args, **kwargs)
class NoteSpecial(Note):
"""
@ -106,6 +130,19 @@ class NoteSpecial(Note):
def __str__(self):
return self.special_type
def save(self, *args, **kwargs):
"""
When saving, also create an alias
TODO: check availability of alias
TODO: remove old alias
"""
created = self.pk is None
if created:
alias = Alias.objects.create(name=str(self.club), note=self)
alias.save()
super().save(*args, **kwargs)
class Alias(models.Model):
"""
@ -127,23 +164,3 @@ class Alias(models.Model):
def __str__(self):
return self.name
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def save_user_note(instance, created, **_kwargs):
"""
Hook to create and save a note when an user is updated
"""
if created:
NoteUser.objects.create(user=instance)
instance.note.save()
@receiver(post_save, sender='member.Club')
def save_club_note(instance, created, **_kwargs):
"""
Hook to create and save a note when a club is updated
"""
if created:
NoteClub.objects.create(club=instance)
instance.note.save()

View File

@ -79,6 +79,28 @@ class Transaction(models.Model):
verbose_name = _("transaction")
verbose_name_plural = _("transactions")
def save(self, *args, **kwargs):
"""
When saving, also transfer money between two notes
"""
created = self.pk is None
to_transfer = self.amount * self.quantity
if not created:
# Revert old transaction
old_transaction = Transaction.objects.get(pk=self.pk)
if old_transaction.valid:
self.source.balance += to_transfer
self.destination.balance -= to_transfer
if self.valid:
self.source.balance -= to_transfer
self.destination.balance += to_transfer
# Save notes
self.source.save()
self.destination.save()
super().save(*args, **kwargs)
class MembershipTransaction(Transaction):
membership = models.OneToOneField(