Implement signals
This commit is contained in:
parent
d957e643c9
commit
085cd90a7f
5 changed files with 111 additions and 22 deletions
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue