paperless-ngx/src/documents/tasks.py

104 lines
2.9 KiB
Python
Raw Normal View History

import logging
import tqdm
from django.conf import settings
2020-12-11 14:27:54 +01:00
from django.db.models.signals import post_save
from whoosh.writing import AsyncWriter
2020-11-25 16:04:58 +01:00
from documents import index, sanity_checker
from documents.classifier import DocumentClassifier, \
IncompatibleClassifierVersionError
2020-11-16 18:26:54 +01:00
from documents.consumer import Consumer, ConsumerError
from documents.models import Document
2020-11-25 16:04:58 +01:00
from documents.sanity_checker import SanityFailedError
def index_optimize():
2020-11-28 11:49:46 +01:00
ix = index.open_index()
2020-11-30 21:38:21 +01:00
writer = AsyncWriter(ix)
writer.commit(optimize=True)
def index_reindex():
documents = Document.objects.all()
ix = index.open_index(recreate=True)
with AsyncWriter(ix) as writer:
for document in tqdm.tqdm(documents):
index.update_document(writer, document)
def train_classifier():
classifier = DocumentClassifier()
try:
# load the classifier, since we might not have to train it again.
classifier.reload()
2020-12-30 21:54:36 +01:00
except (OSError, EOFError, IncompatibleClassifierVersionError):
# This is what we're going to fix here.
2020-12-30 21:54:36 +01:00
classifier = DocumentClassifier()
try:
if classifier.train():
logging.getLogger(__name__).info(
"Saving updated classifier model to {}...".format(
settings.MODEL_FILE)
)
classifier.save_classifier()
else:
logging.getLogger(__name__).debug(
"Training data unchanged."
)
except Exception as e:
logging.getLogger(__name__).error(
"Classifier error: " + str(e)
)
2020-11-16 18:26:54 +01:00
2020-11-17 11:49:44 +01:00
def consume_file(path,
override_filename=None,
override_title=None,
override_correspondent_id=None,
override_document_type_id=None,
override_tag_ids=None):
2020-11-16 18:26:54 +01:00
document = Consumer().try_consume_file(
2020-11-17 11:49:44 +01:00
path,
override_filename=override_filename,
override_title=override_title,
override_correspondent_id=override_correspondent_id,
override_document_type_id=override_document_type_id,
override_tag_ids=override_tag_ids)
2020-11-16 18:26:54 +01:00
if document:
return "Success. New document id {} created".format(
document.pk
)
else:
raise ConsumerError("Unknown error: Returned document was null, but "
"no error message was given.")
2020-11-25 16:04:58 +01:00
def sanity_check():
messages = sanity_checker.check_sanity()
if len(messages) > 0:
raise SanityFailedError(messages)
else:
return "No issues detected."
2020-12-11 14:27:54 +01:00
def bulk_update_documents(document_ids):
documents = Document.objects.filter(id__in=document_ids)
ix = index.open_index()
for doc in documents:
post_save.send(Document, instance=doc, created=False)
with AsyncWriter(ix) as writer:
for doc in documents:
index.update_document(writer, doc)