paperless-ngx/src/documents/management/commands/document_correspondents.py

46 lines
1.6 KiB
Python
Raw Normal View History

from django.core.management.base import BaseCommand
from documents.models import Document, Correspondent
from ...mixins import Renderable
class Command(Renderable, BaseCommand):
help = """
Using the current set of correspondent rules, apply said rules to all
documents in the database, effectively allowing you to back-tag all
2017-01-31 22:37:48 +01:00
previously indexed documents with correspondent created (or modified)
after their initial import.
""".replace(" ", "")
def __init__(self, *args, **kwargs):
self.verbosity = 0
BaseCommand.__init__(self, *args, **kwargs)
def handle(self, *args, **options):
self.verbosity = options["verbosity"]
for document in Document.objects.all():
# No matching correspondents, so no need to continue
if document.correspondent:
continue
2017-01-31 22:37:48 +01:00
potential_correspondents = list(
Correspondent.match_all(document.content))
if not potential_correspondents:
continue
potential_count = len(potential_correspondents)
selected = potential_correspondents[0]
if potential_count > 1:
2017-01-31 22:37:48 +01:00
message = "Detected {} potential correspondents for {}, " \
"so we've opted for {}"
print(message.format(potential_count, document, selected))
2017-01-31 22:37:48 +01:00
print('Tagging {} with correspondent "{}"'.format(document,
selected))
document.correspondent = selected
document.save(update_fields=("correspondent",))