From 71902d3f5cb7905f7d2e7e859e9a8dad37c15220 Mon Sep 17 00:00:00 2001 From: Aleksandr Bogdanov Date: Wed, 5 Oct 2016 20:42:43 +0200 Subject: [PATCH 1/2] Updating Django admin event log on document_consumption_finished --- .../management/commands/document_consumer.py | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/documents/management/commands/document_consumer.py b/src/documents/management/commands/document_consumer.py index 0acdaeeb0..5f135fd22 100644 --- a/src/documents/management/commands/document_consumer.py +++ b/src/documents/management/commands/document_consumer.py @@ -4,11 +4,16 @@ import os import time from django.conf import settings +from django.contrib.contenttypes.models import ContentType from django.core.management.base import BaseCommand, CommandError +from django.contrib.admin.models import LogEntry, ADDITION +from django.contrib.auth.models import User +from django.dispatch import receiver -from ...models import Log +from ...models import Document from ...consumer import Consumer, ConsumerError from ...mail import MailFetcher, MailFetcherError +from ...signals import document_consumption_finished class Command(BaseCommand): @@ -16,7 +21,7 @@ class Command(BaseCommand): On every iteration of an infinite loop, consume what we can from the consumption directory, and fetch any mail available. """ - + CONSUME_USER_ID = 1 LOOP_TIME = 10 # Seconds MAIL_DELTA = datetime.timedelta(minutes=10) @@ -68,3 +73,17 @@ class Command(BaseCommand): delta = self.mail_fetcher.last_checked + self.MAIL_DELTA if delta < datetime.datetime.now(): self.mail_fetcher.pull() + + @receiver(document_consumption_finished) + def notify_finished(sender, document=None, logging_group=None, **kwargs): + doctype = ContentType.objects.get_for_model(Document) + user = User.objects.get(**{'id': Command.CONSUME_USER_ID}) + + LogEntry.objects.log_action( + user_id=user.pk, + content_type_id=doctype.pk, + object_id=document.pk, + object_repr=repr(document), + action_flag=ADDITION, + change_message='Document %s consumption finished' % document.title + ) From ca21929cee1cc5a1830e86181505e4906096f5a3 Mon Sep 17 00:00:00 2001 From: Daniel Quinn Date: Wed, 26 Oct 2016 09:52:09 +0000 Subject: [PATCH 2/2] Moved logging logic into the consumer --- src/documents/consumer.py | 5 +++++ .../management/commands/document_consumer.py | 21 ------------------- 2 files changed, 5 insertions(+), 21 deletions(-) diff --git a/src/documents/consumer.py b/src/documents/consumer.py index 72debcd3e..147b45edd 100644 --- a/src/documents/consumer.py +++ b/src/documents/consumer.py @@ -142,6 +142,11 @@ class Consumer(object): self._cleanup_tempdir(tempdir) self._cleanup_doc(doc) + self.log( + "info", + "Document {} consumption finished".format(document) + ) + document_consumption_finished.send( sender=self.__class__, document=document, diff --git a/src/documents/management/commands/document_consumer.py b/src/documents/management/commands/document_consumer.py index b035eb22a..a03845bd3 100644 --- a/src/documents/management/commands/document_consumer.py +++ b/src/documents/management/commands/document_consumer.py @@ -4,16 +4,10 @@ import os import time from django.conf import settings -from django.contrib.contenttypes.models import ContentType from django.core.management.base import BaseCommand, CommandError -from django.contrib.admin.models import LogEntry, ADDITION -from django.contrib.auth.models import User -from django.dispatch import receiver -from ...models import Document from ...consumer import Consumer, ConsumerError from ...mail import MailFetcher, MailFetcherError -from ...signals import document_consumption_finished class Command(BaseCommand): @@ -22,7 +16,6 @@ class Command(BaseCommand): consumption directory, and fetch any mail available. """ - CONSUME_USER_ID = 1 LOOP_TIME = 10 # Seconds MAIL_DELTA = datetime.timedelta(minutes=10) @@ -74,17 +67,3 @@ class Command(BaseCommand): delta = self.mail_fetcher.last_checked + self.MAIL_DELTA if delta < datetime.datetime.now(): self.mail_fetcher.pull() - - @receiver(document_consumption_finished) - def notify_finished(sender, document=None, logging_group=None, **kwargs): - doctype = ContentType.objects.get_for_model(Document) - user = User.objects.get(pk=Command.CONSUME_USER_ID) - - LogEntry.objects.log_action( - user_id=user.pk, - content_type_id=doctype.pk, - object_id=document.pk, - object_repr=repr(document), - action_flag=ADDITION, - change_message='Document %s consumption finished' % document.title - )