signals.py 960 B

12345678910111213141516171819202122232425
  1. from django.dispatch import receiver
  2. from django.db.models.signals import post_save, post_delete
  3. from .models import User, Corporation, Adhesion
  4. @receiver(post_save, sender=User, dispatch_uid='create_user_adhesion')
  5. def create_user_adhesion(sender, instance, created, **kwargs):
  6. if created and not hasattr(instance, 'skip_adhesion_creation'):
  7. Adhesion.objects.create(user=instance)
  8. @receiver(post_save, sender=Corporation, dispatch_uid='create_corporation_adhesion')
  9. def create_corporation_adhesion(sender, instance, created, **kwargs):
  10. if created and not hasattr(instance, 'skip_adhesion_creation'):
  11. Adhesion.objects.create(corporation=instance)
  12. @receiver(post_delete, sender=Adhesion, dispatch_uid='delete_adhesion')
  13. def delete_adhesion(sender, instance, **kwargs):
  14. if instance.is_physical():
  15. instance.user.delete()
  16. if instance.is_moral():
  17. instance.corporation.delete()
  18. instance.membership.delete()