2015-12-26 13:21:33 +00:00
|
|
|
import datetime
|
2015-12-20 19:23:33 +00:00
|
|
|
import os
|
2015-12-21 02:44:24 +00:00
|
|
|
import time
|
2015-12-20 19:23:33 +00:00
|
|
|
|
|
|
|
|
from django.conf import settings
|
2016-01-14 19:47:57 +00:00
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
2015-12-20 19:23:33 +00:00
|
|
|
|
2016-01-30 01:18:52 +00:00
|
|
|
from ...consumers import (
|
|
|
|
|
FileConsumer, FileConsumerError, MailConsumer, MailConsumerError)
|
2016-01-23 02:33:29 +00:00
|
|
|
|
|
|
|
|
|
2015-12-20 19:23:33 +00:00
|
|
|
class Command(BaseCommand):
|
|
|
|
|
"""
|
|
|
|
|
Loop over every file found in CONSUMPTION_DIR and:
|
2016-01-10 15:51:38 +00:00
|
|
|
1. Convert it to a greyscale png
|
|
|
|
|
2. Use tesseract on the png
|
2016-01-29 23:18:03 +00:00
|
|
|
3. Encrypt and store the document in the MEDIA_ROOT
|
2016-01-10 15:51:38 +00:00
|
|
|
4. Store the OCR'd text in the database
|
2016-01-29 23:18:03 +00:00
|
|
|
5. Delete the document and image(s)
|
2015-12-20 19:23:33 +00:00
|
|
|
"""
|
|
|
|
|
|
2015-12-26 13:21:33 +00:00
|
|
|
LOOP_TIME = 10 # Seconds
|
2016-01-30 01:18:52 +00:00
|
|
|
MAIL_DELTA = datetime.timedelta(minutes=10)
|
2015-12-26 13:21:33 +00:00
|
|
|
|
2016-01-29 23:18:03 +00:00
|
|
|
MEDIA_DOCS = os.path.join(settings.MEDIA_ROOT, "documents")
|
2015-12-20 19:23:33 +00:00
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2016-01-21 12:50:22 -05:00
|
|
|
|
2015-12-20 19:23:33 +00:00
|
|
|
self.verbosity = 0
|
2016-01-30 01:18:52 +00:00
|
|
|
|
|
|
|
|
self.file_consumer = None
|
|
|
|
|
self.mail_consumer = None
|
2016-01-21 12:50:22 -05:00
|
|
|
|
2015-12-20 19:23:33 +00:00
|
|
|
BaseCommand.__init__(self, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
|
|
|
|
|
|
self.verbosity = options["verbosity"]
|
|
|
|
|
|
2016-01-30 01:18:52 +00:00
|
|
|
try:
|
|
|
|
|
self.file_consumer = FileConsumer(verbosity=self.verbosity)
|
|
|
|
|
self.mail_consumer = MailConsumer(verbosity=self.verbosity)
|
|
|
|
|
except (FileConsumerError, MailConsumerError) as e:
|
|
|
|
|
raise CommandError(e)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
os.makedirs(self.MEDIA_DOCS)
|
|
|
|
|
except FileExistsError:
|
|
|
|
|
pass
|
2015-12-20 19:23:33 +00:00
|
|
|
|
2015-12-21 02:44:24 +00:00
|
|
|
try:
|
|
|
|
|
while True:
|
|
|
|
|
self.loop()
|
2015-12-26 13:21:33 +00:00
|
|
|
time.sleep(self.LOOP_TIME)
|
|
|
|
|
if self.verbosity > 1:
|
|
|
|
|
print(".")
|
2015-12-21 02:44:24 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
print("Exiting")
|
|
|
|
|
|
|
|
|
|
def loop(self):
|
|
|
|
|
|
2016-01-30 01:18:52 +00:00
|
|
|
self.file_consumer.consume()
|
2016-01-01 16:13:59 +00:00
|
|
|
|
2016-02-05 20:15:08 +00:00
|
|
|
delta = self.mail_consumer.last_checked + self.MAIL_DELTA
|
|
|
|
|
if delta > datetime.datetime.now():
|
2016-01-30 01:18:52 +00:00
|
|
|
self.mail_consumer.consume()
|
2016-01-14 19:47:57 +00:00
|
|
|
|
2016-01-01 16:13:59 +00:00
|
|
|
def _render(self, text, verbosity):
|
|
|
|
|
if self.verbosity >= verbosity:
|
|
|
|
|
print(text)
|