Logging is finally processed at post saved, but old instance is querried

This commit is contained in:
Yohann D'ANELLO 2020-02-27 16:25:18 +01:00
parent bcee5f8f2f
commit 9cd050e2f0
3 changed files with 21 additions and 11 deletions

View File

@ -5,7 +5,7 @@ import inspect
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.core import serializers 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 django.dispatch import receiver
from .models import Changelog from .models import Changelog
@ -58,22 +58,32 @@ EXCLUDED = [
'reversion.version', 'reversion.version',
] ]
@receiver(pre_save) @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): def save_object(sender, instance, **kwargs):
# noinspection PyProtectedMember # noinspection PyProtectedMember
if instance._meta.label_lower in EXCLUDED: if instance._meta.label_lower in EXCLUDED:
return return
previous = sender.objects.filter(pk=instance.pk).all() previous = instance._previous
user, ip = get_user_and_ip(sender) 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 # Don't save last login modifications
if instance.last_login != previous.get().last_login: if instance.last_login != previous.last_login:
return 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] instance_json = serializers.serialize('json', [instance, ])[1:-1]
if previous_json == instance_json: if previous_json == instance_json:
@ -86,11 +96,11 @@ def save_object(sender, instance, **kwargs):
instance_pk=instance.pk, instance_pk=instance.pk,
previous=previous_json, previous=previous_json,
data=instance_json, data=instance_json,
action=("edit" if previous.exists() else "create") action=("edit" if previous else "create")
).save() ).save()
@receiver(pre_delete) @receiver(post_delete)
def delete_object(sender, instance, **kwargs): def delete_object(sender, instance, **kwargs):
# noinspection PyProtectedMember # noinspection PyProtectedMember
if instance._meta.label_lower in EXCLUDED: if instance._meta.label_lower in EXCLUDED:

View File

@ -6,7 +6,7 @@ from django.conf import settings
from django.db.models.signals import post_save from django.db.models.signals import post_save
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from .signals import save_user_note from .signals import save_user_profile
class MemberConfig(AppConfig): class MemberConfig(AppConfig):
@ -18,6 +18,6 @@ class MemberConfig(AppConfig):
Define app internal signals to interact with other apps Define app internal signals to interact with other apps
""" """
post_save.connect( post_save.connect(
save_user_note, save_user_profile,
sender=settings.AUTH_USER_MODEL, sender=settings.AUTH_USER_MODEL,
) )

View File

@ -1,7 +1,7 @@
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later # 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 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: if created:
from .models import Profile from .models import Profile
Profile.objects.get_or_create(user=instance) #Profile.objects.get_or_create(user=instance)
instance.profile.save() instance.profile.save()