1
0
mirror of https://gitlab.crans.org/bde/nk20 synced 2024-11-27 02:43:01 +00:00

Don't log when there is no modification, or only when a user is connected

This commit is contained in:
Yohann D'ANELLO 2020-02-27 15:53:06 +01:00
parent 125f437345
commit bcee5f8f2f

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 post_save, post_delete from django.db.models.signals import pre_save, pre_delete
from django.dispatch import receiver from django.dispatch import receiver
from .models import Changelog from .models import Changelog
@ -58,7 +58,7 @@ EXCLUDED = [
'reversion.version', 'reversion.version',
] ]
@receiver(post_save) @receiver(pre_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:
@ -68,8 +68,18 @@ def save_object(sender, instance, **kwargs):
user, ip = get_user_and_ip(sender) user, ip = get_user_and_ip(sender)
previous_json = serializers.serialize('json', previous)[1:-1] if previous.exists else None if user is not None and instance._meta.label_lower == "auth.user" and previous.exists():
# Don't save last login modifications
if instance.last_login != previous.get().last_login:
return
previous_json = serializers.serialize('json', previous)[1:-1] if previous.exists() else None
instance_json = serializers.serialize('json', [instance, ])[1:-1] instance_json = serializers.serialize('json', [instance, ])[1:-1]
if previous_json == instance_json:
# No modification
return
Changelog.objects.create(user=user, Changelog.objects.create(user=user,
ip=ip, ip=ip,
model=ContentType.objects.get_for_model(instance), model=ContentType.objects.get_for_model(instance),
@ -80,7 +90,7 @@ def save_object(sender, instance, **kwargs):
).save() ).save()
@receiver(post_delete) @receiver(pre_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: