paperless-ngx/src/documents/signals/handlers.py

100 lines
2.6 KiB
Python
Raw Normal View History

import logging
import os
2016-03-28 19:47:11 +01:00
from subprocess import Popen
from django.conf import settings
from django.contrib.admin.models import ADDITION, LogEntry
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.utils import timezone
2016-03-28 19:47:11 +01:00
from documents.classifier import DocumentClassifier
from .. import index
from ..models import Document, Tag
def logger(message, group):
logging.getLogger(__name__).debug(message, extra={"group": group})
#TODO: global? really?
classifier = DocumentClassifier()
2018-08-24 13:45:15 +02:00
def index_document(sender, document=None, logging_group=None, **kwargs):
index.add_document_to_index(sender, instance=document)
def classify_document(sender, document=None, logging_group=None, **kwargs):
global classifier
try:
classifier.reload()
2018-09-25 16:09:33 +02:00
classifier.classify_document(
document,
classify_correspondent=True,
classify_tags=True,
classify_document_type=True
)
except FileNotFoundError:
2018-09-25 16:09:33 +02:00
logging.getLogger(__name__).fatal(
"Cannot classify document, classifier model file was not found."
)
def add_inbox_tags(sender, document=None, logging_group=None, **kwargs):
inbox_tags = Tag.objects.filter(is_inbox_tag=True)
document.tags.add(*inbox_tags)
2016-03-28 19:47:11 +01:00
def run_pre_consume_script(sender, filename, **kwargs):
if not settings.PRE_CONSUME_SCRIPT:
return
Popen((settings.PRE_CONSUME_SCRIPT, filename)).wait()
def run_post_consume_script(sender, document, **kwargs):
2016-03-28 19:47:11 +01:00
if not settings.POST_CONSUME_SCRIPT:
return
Popen((
settings.POST_CONSUME_SCRIPT,
str(document.id),
2016-03-28 19:47:11 +01:00
document.file_name,
document.source_path,
document.thumbnail_path,
document.download_url,
document.thumbnail_url,
str(document.correspondent),
str(",".join(document.tags.all().values_list("slug", flat=True)))
)).wait()
def cleanup_document_deletion(sender, instance, using, **kwargs):
if not isinstance(instance, Document):
return
2016-08-20 14:03:42 +01:00
for f in (instance.source_path, instance.thumbnail_path):
try:
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.get(username="consumer")
LogEntry.objects.create(
action_flag=ADDITION,
action_time=timezone.now(),
content_type=ct,
object_id=document.id,
user=user,
object_repr=document.__str__(),
)