From 45e18d709406e9631c5406d5c718c5ef43de1351 Mon Sep 17 00:00:00 2001 From: CkuT Date: Sun, 11 Mar 2018 17:09:43 +0100 Subject: [PATCH 1/3] Add LogEntry after document consumption See #319 --- src/documents/apps.py | 4 +++- src/documents/signals/handlers.py | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/documents/apps.py b/src/documents/apps.py index dd065c0a5..435e1afa5 100644 --- a/src/documents/apps.py +++ b/src/documents/apps.py @@ -15,13 +15,15 @@ class DocumentsConfig(AppConfig): set_tags, run_pre_consume_script, run_post_consume_script, - cleanup_document_deletion + cleanup_document_deletion, + set_log_entry ) document_consumption_started.connect(run_pre_consume_script) document_consumption_finished.connect(set_tags) document_consumption_finished.connect(set_correspondent) + document_consumption_finished.connect(set_log_entry) document_consumption_finished.connect(run_post_consume_script) post_delete.connect(cleanup_document_deletion) diff --git a/src/documents/signals/handlers.py b/src/documents/signals/handlers.py index e6fecbf6a..d66be3ea9 100644 --- a/src/documents/signals/handlers.py +++ b/src/documents/signals/handlers.py @@ -3,6 +3,9 @@ import os from subprocess import Popen from django.conf import settings +from django.contrib.admin.models import LogEntry, ADDITION +from django.contrib.contenttypes.models import ContentType +from django.contrib.auth.models import User from ..models import Correspondent, Document, Tag @@ -93,3 +96,18 @@ def cleanup_document_deletion(sender, instance, using, **kwargs): os.unlink(f) except FileNotFoundError: pass # The file's already gone, so we're cool with it. + + +def set_log_entry(sender, document=None, logging_group=None, **kwargs): + + ct = ContentType.objects.get(model="document") + user = User.objects.first() + + LogEntry.objects.create( + action_flag=ADDITION, + action_time=document.created, + content_type=ct, + object_id=document.id, + user=user, + object_repr=document.__str__(), + ) From a1f257369dfee3f35ab20d5316f4ee0c73c3174f Mon Sep 17 00:00:00 2001 From: CkuT Date: Tue, 13 Mar 2018 19:09:48 +0100 Subject: [PATCH 2/3] Use datetime.now() instead of document.created for LogEntry action_time --- src/documents/signals/handlers.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/documents/signals/handlers.py b/src/documents/signals/handlers.py index d66be3ea9..3af27bb03 100644 --- a/src/documents/signals/handlers.py +++ b/src/documents/signals/handlers.py @@ -3,9 +3,11 @@ import os from subprocess import Popen from django.conf import settings -from django.contrib.admin.models import LogEntry, ADDITION -from django.contrib.contenttypes.models import ContentType +from django.contrib.admin.models import ADDITION, LogEntry from django.contrib.auth.models import User +from django.contrib.contenttypes.models import ContentType + +from datetime import datetime from ..models import Correspondent, Document, Tag @@ -105,7 +107,7 @@ def set_log_entry(sender, document=None, logging_group=None, **kwargs): LogEntry.objects.create( action_flag=ADDITION, - action_time=document.created, + action_time=datetime.now(), content_type=ct, object_id=document.id, user=user, From 1d162dc769ef9e38dfb994b33414b176c80c4969 Mon Sep 17 00:00:00 2001 From: CkuT Date: Tue, 13 Mar 2018 19:27:59 +0100 Subject: [PATCH 3/3] Add test case --- src/documents/tests/test_matchables.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/documents/tests/test_matchables.py b/src/documents/tests/test_matchables.py index 44eca5138..94d6ee97a 100644 --- a/src/documents/tests/test_matchables.py +++ b/src/documents/tests/test_matchables.py @@ -1,5 +1,7 @@ from random import randint +from django.contrib.admin.models import LogEntry +from django.contrib.auth.models import User from django.test import TestCase, override_settings from ..models import Correspondent, Document, Tag @@ -208,6 +210,7 @@ class TestDocumentConsumptionFinishedSignal(TestCase): def setUp(self): TestCase.setUp(self) + User.objects.create_user(username='testuser', password='12345') self.doc_contains = Document.objects.create( content="I contain the keyword.", file_type="pdf") @@ -244,3 +247,9 @@ class TestDocumentConsumptionFinishedSignal(TestCase): document_consumption_finished.send( sender=self.__class__, document=self.doc_contains) self.assertEqual(self.doc_contains.correspondent, None) + + def test_logentry_created(self): + document_consumption_finished.send( + sender=self.__class__, document=self.doc_contains) + + self.assertEqual(LogEntry.objects.count(), 1)