From 4a8d7c6a5ced7945d0d03574861d70eb3da27d7c Mon Sep 17 00:00:00 2001 From: Laurent Date: Wed, 11 Dec 2024 08:59:15 +0100 Subject: [PATCH] Fix and improvement --- sync/signals.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/sync/signals.py b/sync/signals.py index 7e0f093..c9efc53 100644 --- a/sync/signals.py +++ b/sync/signals.py @@ -13,13 +13,15 @@ User = get_user_model() @receiver([pre_save, pre_delete]) def synchronization_prepare(sender, instance, created=False, **kwargs): - # some classes are excluded in settings_app.py: SYNC_APPS + # some other classes are excluded in settings_app.py: SYNC_APPS if not isinstance(instance, BaseModel) and not isinstance(instance, User): return save_model_log_if_possible(instance, kwargs.get('signal'), created) -@receiver(pre_save) + if kwargs.get('signal') == pre_save: + detect_foreign_key_changes(sender, instance) + def detect_foreign_key_changes(sender, instance, **kwargs): if not hasattr(instance, 'pk') or not instance.pk: return @@ -47,7 +49,6 @@ def detect_foreign_key_changes(sender, instance, **kwargs): model_name = new_value.__class__.__name__ save_model_log(data_access.shared_with.all(), 'GRANT_ACCESS', model_name, new_value.id, new_value.get_store_id()) - # def save_model_log(users, model_operation, model_name, model_id, store_id): # REVOKE access for old_value and GRANT new_value print(f"Foreign key changed in {sender.__name__}: " f"{field.name} from {old_value} to {new_value}") @@ -61,7 +62,7 @@ def synchronization_notifications(sender, instance, created=False, **kwargs): """ # some classes are excluded in settings_app.py: SYNC_APPS - if not isinstance(instance, BaseModel): + if not isinstance(instance, BaseModel) and not isinstance(instance, User): return # print(f'*** signals {sender}')