mirror of
https://gitlab.crans.org/bde/nk20
synced 2024-11-26 18:37:12 +00:00
Logging is finally processed at post saved, but old instance is querried
This commit is contained in:
parent
bcee5f8f2f
commit
9cd050e2f0
@ -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:
|
||||
|
@ -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,
|
||||
)
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user