2017-01-31 22:18:05 +01:00
|
|
|
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.
|
2017-01-31 22:18:05 +01:00
|
|
|
""".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))
|
2017-01-31 22:18:05 +01:00
|
|
|
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 {}"
|
2017-01-31 22:18:05 +01:00
|
|
|
print(message.format(potential_count, document, selected))
|
|
|
|
|
|
2017-01-31 22:37:48 +01:00
|
|
|
print('Tagging {} with correspondent "{}"'.format(document,
|
|
|
|
|
selected))
|
2017-01-31 22:18:05 +01:00
|
|
|
document.correspondent = selected
|
|
|
|
|
document.save(update_fields=("correspondent",))
|