From 695ef446ec717670a7a8342b07a335560a3cfa6b Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Wed, 11 Mar 2020 17:20:16 +0100 Subject: [PATCH] Add logs in tox settings --- apps/logs/api/serializers.py | 2 +- apps/logs/apps.py | 6 +++++- apps/logs/middlewares.py | 1 + apps/logs/signals.py | 11 ++++------- tox.ini | 2 +- 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/apps/logs/api/serializers.py b/apps/logs/api/serializers.py index 108f2087..c76e3a5d 100644 --- a/apps/logs/api/serializers.py +++ b/apps/logs/api/serializers.py @@ -16,4 +16,4 @@ class ChangelogSerializer(serializers.ModelSerializer): model = Changelog fields = '__all__' # noinspection PyProtectedMember - read_only_fields = [f.name for f in model._meta.get_fields()] # Changelogs are read-only protected + read_only_fields = [f.name for f in model._meta.get_fields()] # Changelogs are read-only protected diff --git a/apps/logs/apps.py b/apps/logs/apps.py index f48820c7..239f86cf 100644 --- a/apps/logs/apps.py +++ b/apps/logs/apps.py @@ -2,6 +2,7 @@ # SPDX-License-Identifier: GPL-3.0-or-later from django.apps import AppConfig +from django.db.models.signals import pre_save, post_save, post_delete from django.utils.translation import gettext_lazy as _ @@ -11,4 +12,7 @@ class LogsConfig(AppConfig): def ready(self): # noinspection PyUnresolvedReferences - import logs.signals + from . import signals + pre_save.connect(signals.pre_save_object) + post_save.connect(signals.save_object) + post_delete.connect(signals.delete_object) diff --git a/apps/logs/middlewares.py b/apps/logs/middlewares.py index cc6e1f4c..4378e165 100644 --- a/apps/logs/middlewares.py +++ b/apps/logs/middlewares.py @@ -3,6 +3,7 @@ from django.conf import settings from django.contrib.auth.models import AnonymousUser + from threading import local diff --git a/apps/logs/signals.py b/apps/logs/signals.py index f3694cd3..f77f5ef7 100644 --- a/apps/logs/signals.py +++ b/apps/logs/signals.py @@ -2,13 +2,13 @@ # SPDX-License-Identifier: GPL-3.0-or-later from django.contrib.contenttypes.models import ContentType -from django.db.models.signals import pre_save, post_save, post_delete -from django.dispatch import receiver from rest_framework.renderers import JSONRenderer from rest_framework.serializers import ModelSerializer + import getpass from note.models import NoteUser, Alias + from .middlewares import get_current_authenticated_user, get_current_ip from .models import Changelog @@ -23,15 +23,14 @@ EXCLUDED = [ 'cas_server.user', 'cas_server.userattributes', 'contenttypes.contenttype', - 'logs.changelog', # Never remove this line + 'logs.changelog', # Never remove this line 'migrations.migration', - 'note.note' # We only store the subclasses + 'note.note' # We only store the subclasses 'note.transaction', 'sessions.session', ] -@receiver(pre_save) def pre_save_object(sender, instance, **kwargs): """ Avant la sauvegarde d'un modèle, on récupère l'ancienne instance actuellement en base de données @@ -44,7 +43,6 @@ def pre_save_object(sender, instance, **kwargs): instance._previous = None -@receiver(post_save) def save_object(sender, instance, **kwargs): """ Dès qu'un modèle est sauvegardé, une entrée dans la table `Changelog` est ajouté dans la base de données @@ -102,7 +100,6 @@ def save_object(sender, instance, **kwargs): ).save() -@receiver(post_delete) def delete_object(sender, instance, **kwargs): """ Dès qu'un modèle est supprimé, une entrée dans la table `Changelog` est ajouté dans la base de données diff --git a/tox.ini b/tox.ini index 7c432d55..2217b6bf 100644 --- a/tox.ini +++ b/tox.ini @@ -32,7 +32,7 @@ deps = pep8-naming pyflakes commands = - flake8 apps/activity apps/api apps/member apps/note + flake8 apps/activity apps/api apps/logs apps/member apps/note [flake8] # Ignore too many errors, should be reduced in the future