From 9cd050e2f02ac721f007fb248634be74a4a7c990 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Thu, 27 Feb 2020 16:25:18 +0100 Subject: [PATCH] Logging is finally processed at post saved, but old instance is querried --- apps/logs/signals.py | 24 +++++++++++++++++------- apps/member/apps.py | 4 ++-- apps/member/signals.py | 4 ++-- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/apps/logs/signals.py b/apps/logs/signals.py index 45a0714a..55e0f041 100644 --- a/apps/logs/signals.py +++ b/apps/logs/signals.py @@ -5,7 +5,7 @@ import inspect from django.contrib.contenttypes.models import ContentType from django.core import serializers -from django.db.models.signals import pre_save, pre_delete +from django.db.models.signals import pre_save, post_save, post_delete from django.dispatch import receiver from .models import Changelog @@ -58,22 +58,32 @@ EXCLUDED = [ 'reversion.version', ] + @receiver(pre_save) +def pre_save_object(sender, instance, **kwargs): + qs = sender.objects.filter(pk=instance.pk).all() + if qs.exists(): + instance._previous = qs.get() + else: + instance._previous = None + + +@receiver(post_save) def save_object(sender, instance, **kwargs): # noinspection PyProtectedMember if instance._meta.label_lower in EXCLUDED: return - previous = sender.objects.filter(pk=instance.pk).all() + previous = instance._previous user, ip = get_user_and_ip(sender) - if user is not None and instance._meta.label_lower == "auth.user" and previous.exists(): + if user is not None and instance._meta.label_lower == "auth.user" and previous: # Don't save last login modifications - if instance.last_login != previous.get().last_login: + if instance.last_login != previous.last_login: return - previous_json = serializers.serialize('json', previous)[1:-1] if previous.exists() else None + previous_json = serializers.serialize('json', [previous, ])[1:-1] if previous else None instance_json = serializers.serialize('json', [instance, ])[1:-1] if previous_json == instance_json: @@ -86,11 +96,11 @@ def save_object(sender, instance, **kwargs): instance_pk=instance.pk, previous=previous_json, data=instance_json, - action=("edit" if previous.exists() else "create") + action=("edit" if previous else "create") ).save() -@receiver(pre_delete) +@receiver(post_delete) def delete_object(sender, instance, **kwargs): # noinspection PyProtectedMember if instance._meta.label_lower in EXCLUDED: diff --git a/apps/member/apps.py b/apps/member/apps.py index 8a6ebe1f..83dfbc40 100644 --- a/apps/member/apps.py +++ b/apps/member/apps.py @@ -6,7 +6,7 @@ from django.conf import settings from django.db.models.signals import post_save from django.utils.translation import gettext_lazy as _ -from .signals import save_user_note +from .signals import save_user_profile class MemberConfig(AppConfig): @@ -18,6 +18,6 @@ class MemberConfig(AppConfig): Define app internal signals to interact with other apps """ post_save.connect( - save_user_note, + save_user_profile, sender=settings.AUTH_USER_MODEL, ) diff --git a/apps/member/signals.py b/apps/member/signals.py index 5debce4b..3361f9de 100644 --- a/apps/member/signals.py +++ b/apps/member/signals.py @@ -1,7 +1,7 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later -def save_user_note(instance, created, raw, **_kwargs): +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 """ @@ -11,5 +11,5 @@ def save_user_note(instance, created, raw, **_kwargs): if created: from .models import Profile - Profile.objects.get_or_create(user=instance) + #Profile.objects.get_or_create(user=instance) instance.profile.save()