Don't trigger signals when we add an object through a permission check

This commit is contained in:
Yohann D'ANELLO 2020-09-07 14:52:37 +02:00
parent 353416618a
commit 78586b9343
6 changed files with 25 additions and 23 deletions

View File

@ -50,7 +50,7 @@ def save_object(sender, instance, **kwargs):
in order to store each modification made in order to store each modification made
""" """
# noinspection PyProtectedMember # noinspection PyProtectedMember
if instance._meta.label_lower in EXCLUDED or hasattr(instance, "_no_log"): if instance._meta.label_lower in EXCLUDED or hasattr(instance, "_no_signal"):
return return
# noinspection PyProtectedMember # noinspection PyProtectedMember
@ -117,7 +117,7 @@ def delete_object(sender, instance, **kwargs):
Each time a model is deleted, an entry in the table `Changelog` is added in the database Each time a model is deleted, an entry in the table `Changelog` is added in the database
""" """
# noinspection PyProtectedMember # noinspection PyProtectedMember
if instance._meta.label_lower in EXCLUDED or hasattr(instance, "_no_log"): if instance._meta.label_lower in EXCLUDED or hasattr(instance, "_no_signal"):
return return
# Si un utilisateur est connecté, on récupère l'utilisateur courant ainsi que son adresse IP # Si un utilisateur est connecté, on récupère l'utilisateur courant ainsi que son adresse IP

View File

@ -6,7 +6,7 @@ def save_user_profile(instance, created, raw, **_kwargs):
""" """
Hook to create and save a profile when an user is updated if it is not registered with the signup form Hook to create and save a profile when an user is updated if it is not registered with the signup form
""" """
if not raw and created and instance.is_active: if not raw and created and instance.is_active and not hasattr(instance, "_no_signal"):
from .models import Profile from .models import Profile
Profile.objects.get_or_create(user=instance) Profile.objects.get_or_create(user=instance)
if instance.is_superuser: if instance.is_superuser:

View File

@ -6,7 +6,8 @@ def save_user_note(instance, raw, **_kwargs):
""" """
Hook to create and save a note when an user is updated Hook to create and save a note when an user is updated
""" """
if not raw and (instance.is_superuser or instance.profile.registration_valid): if not raw and (instance.is_superuser or instance.profile.registration_valid)\
and not hasattr(instance, "_no_signal"):
# Create note only when the registration is validated # Create note only when the registration is validated
from note.models import NoteUser from note.models import NoteUser
NoteUser.objects.get_or_create(user=instance) NoteUser.objects.get_or_create(user=instance)
@ -17,18 +18,17 @@ def save_club_note(instance, raw, **_kwargs):
""" """
Hook to create and save a note when a club is updated Hook to create and save a note when a club is updated
""" """
if raw: # When provisionning data, do not try to autocreate
# When provisionning data, do not try to autocreate if not raw and not hasattr(instance, "_no_signal"):
return from .models import NoteClub
NoteClub.objects.get_or_create(club=instance)
from .models import NoteClub instance.note.save()
NoteClub.objects.get_or_create(club=instance)
instance.note.save()
def delete_transaction(instance, **_kwargs): def delete_transaction(instance, **_kwargs):
""" """
Whenever we want to delete a transaction (caution with this), we ensure the transaction is invalid first. Whenever we want to delete a transaction (caution with this), we ensure the transaction is invalid first.
""" """
instance.valid = False if not hasattr(instance, "_no_signal"):
instance.save() instance.valid = False
instance.save()

View File

@ -57,8 +57,8 @@ class InstancedPermission:
# Force insertion, no data verification, no trigger # Force insertion, no data verification, no trigger
obj._force_save = True obj._force_save = True
# We don't want log anything # We don't want to trigger any signal (log, ...)
obj._no_log = True obj.no_signal = True
Model.save(obj, force_insert=True) Model.save(obj, force_insert=True)
ret = self.model.model_class().objects.filter(self.query & Q(pk=0)).exists() ret = self.model.model_class().objects.filter(self.query & Q(pk=0)).exists()
# Delete testing object # Delete testing object

View File

@ -28,7 +28,7 @@ def pre_save_object(sender, instance, **kwargs):
if instance._meta.label_lower in EXCLUDED: if instance._meta.label_lower in EXCLUDED:
return return
if hasattr(instance, "_force_save"): if hasattr(instance, "_force_save") or hasattr(instance, "_no_signal"):
return return
user = get_current_authenticated_user() user = get_current_authenticated_user()
@ -82,7 +82,8 @@ def pre_delete_object(instance, **kwargs):
if instance._meta.label_lower in EXCLUDED: if instance._meta.label_lower in EXCLUDED:
return return
if hasattr(instance, "_force_delete") or hasattr(instance, "pk") and instance.pk == 0: if hasattr(instance, "_force_delete") or hasattr(instance, "pk") and instance.pk == 0 \
or hasattr(instance, "_no_signal"):
# Don't check permissions on force-deleted objects # Don't check permissions on force-deleted objects
return return

View File

@ -9,9 +9,10 @@ def save_special_transaction(instance, created, **kwargs):
When a special transaction is created, we create its linked proxy When a special transaction is created, we create its linked proxy
""" """
if instance.is_credit(): if not hasattr(instance, "_no_signal"):
if created and RemittanceType.objects.filter(note=instance.source).exists(): if instance.is_credit():
SpecialTransactionProxy.objects.create(transaction=instance, remittance=None).save() if created and RemittanceType.objects.filter(note=instance.source).exists():
else: SpecialTransactionProxy.objects.create(transaction=instance, remittance=None).save()
if created and RemittanceType.objects.filter(note=instance.destination).exists(): else:
SpecialTransactionProxy.objects.create(transaction=instance, remittance=None).save() if created and RemittanceType.objects.filter(note=instance.destination).exists():
SpecialTransactionProxy.objects.create(transaction=instance, remittance=None).save()