A community-supported supercharged document management system: scan, index and archive all your documents
Python aiangulararchivingdjangodmsdocument-managementdocument-management-systemllmmachine-learningocroptical-character-recognitionpdf
Added the Renderable mixin because DRY
Daniel Quinn7aadab23cc85
diff --git a/src/documents/mail.py b/src/documents/mail.pyindex dd58673a5..3d961f4d5 100644--- a/src/documents/mail.py+++ b/src/documents/mail.py@@ -11,6 +11,7 @@ from dateutil import parserfrom django.conf import settingsfrom .consumer import Consumer+from .mixins import Renderablefrom .models import Sender@@ -22,7 +23,7 @@ class InvalidMessageError(Exception):pass-class Message(object):+class Message(Renderable):"""A crude, but simple email message class. We assume that there's a subjectand n attachments, and that we don't care about the message body.@@ -37,7 +38,7 @@ class Message(object):except (ValueError, AttributeError):pass # We assume that "now" is ok- def __init__(self, data):+ def __init__(self, data, verbosity=1):"""Cribbed heavily fromhttps://www.ianlewis.org/en/parsing-email-attachments-python@@ -57,7 +58,7 @@ class Message(object):if not Sender.SAFE_REGEX.match(self.subject):raise InvalidMessageError("Message subject is unsafe")- print('Fetching email: "{}"'.format(self.subject))+ self._render('Fetching email: "{}"'.format(self.subject), 1)attachments = []for part in message.walk():@@ -116,9 +117,9 @@ class Attachment(object):return self.data-class MailFetcher(object):+class MailFetcher(Renderable):- def __init__(self):+ def __init__(self, verbosity=1):self._connection = Noneself._host = settings.MAIL_CONSUMPTION["HOST"]@@ -130,6 +131,7 @@ class MailFetcher(object):self._enabled = bool(self._host)self.last_checked = datetime.datetime.now()+ self.verbosity = verbositydef pull(self):"""@@ -142,7 +144,7 @@ class MailFetcher(object):for message in self._get_messages():- print("Storing email: \"{}\"".format(message.subject))+ self._render("Storing email: \"{}\"".format(message.subject), 1)t = int(time.mktime(message.time.timetuple()))file_name = os.path.join(Consumer.CONSUME, message.file_name)@@ -189,9 +191,9 @@ class MailFetcher(object):message = Nonetry:- message = Message(data[0][1])+ message = Message(data[0][1], self.verbosity)except InvalidMessageError as e:- print(e)+ self._render(e, 0)passself._connection.store(num, "+FLAGS", "\\Deleted")diff --git a/src/documents/management/commands/document_consumer.py b/src/documents/management/commands/document_consumer.pyindex d384d1486..0d8677ed0 100644--- a/src/documents/management/commands/document_consumer.py+++ b/src/documents/management/commands/document_consumer.py@@ -7,9 +7,10 @@ from django.core.management.base import BaseCommand, CommandErrorfrom ...consumer import Consumer, ConsumerErrorfrom ...mail import MailFetcher, MailFetcherError+from ...mixins import Renderable-class Command(BaseCommand):+class Command(Renderable, BaseCommand):"""On every iteration of an infinite loop, consume what we can from theconsumption directory, and fetch any mail available.@@ -62,7 +63,3 @@ class Command(BaseCommand):delta = self.mail_fetcher.last_checked + self.MAIL_DELTAif delta > datetime.datetime.now():self.mail_fetcher.pull()-- def _render(self, text, verbosity):- if self.verbosity >= verbosity:- print(text)diff --git a/src/documents/management/commands/document_exporter.py b/src/documents/management/commands/document_exporter.pyindex 5b70f7050..c8129ad7b 100644--- a/src/documents/management/commands/document_exporter.py+++ b/src/documents/management/commands/document_exporter.py@@ -8,8 +8,10 @@ from django.core.management.base import BaseCommand, CommandErrorfrom documents.models import Documentfrom paperless.db import GnuPG+from ...mixins import Renderable-class Command(BaseCommand):++class Command(Renderable, BaseCommand):help = """Decrypt and rename all files in our collection into a given target@@ -50,7 +52,3 @@ class Command(BaseCommand):f.write(GnuPG.decrypted(document.source_file))t = int(time.mktime(document.created.timetuple()))os.utime(target, times=(t, t))-- def _render(self, text, verbosity):- if self.verbosity >= verbosity:- print(text)diff --git a/src/documents/management/commands/document_retagger.py b/src/documents/management/commands/document_retagger.pyindex 79f6d2c66..84a887de0 100644--- a/src/documents/management/commands/document_retagger.py+++ b/src/documents/management/commands/document_retagger.py@@ -2,8 +2,10 @@ from django.core.management.base import BaseCommandfrom documents.models import Document, Tag+from ...mixins import Renderable-class Command(BaseCommand):++class Command(Renderable, BaseCommand):help = """Using the current set of tagging rules, apply said rules to all@@ -28,7 +30,3 @@ class Command(BaseCommand):self._render('Tagging {} with "{}"'.format(document, tag), 1)document.tags.add(tag)-- def _render(self, text, verbosity):- if self.verbosity >= verbosity:- print(text)diff --git a/src/documents/mixins.py b/src/documents/mixins.pynew file mode 100644index 000000000..881589fa3--- /dev/null+++ b/src/documents/mixins.py@@ -0,0 +1,9 @@+class Renderable(object):+ """+ A handy mixin to make it easier/cleaner to print output based on a verbosity+ value.+ """++ def _render(self, text, verbosity):+ if self.verbosity >= verbosity:+ print(text)