localization for websockets

This commit is contained in:
jonaswinkler
2021-01-28 22:06:02 +01:00
parent 00cdd2b59b
commit 5f7d817d69
5 changed files with 154 additions and 78 deletions

View File

@@ -25,17 +25,30 @@ from .signals import (
document_consumption_started
)
from django.utils.translation import gettext as _
class ConsumerError(Exception):
pass
MESSAGE_DOCUMENT_ALREADY_EXISTS = "document_already_exists"
MESSAGE_FILE_NOT_FOUND = "file_not_found"
MESSAGE_PRE_CONSUME_SCRIPT_NOT_FOUND = "pre_consume_script_not_found"
MESSAGE_PRE_CONSUME_SCRIPT_ERROR = "pre_consume_script_error"
MESSAGE_POST_CONSUME_SCRIPT_NOT_FOUND = "post_consume_script_not_found"
MESSAGE_POST_CONSUME_SCRIPT_ERROR = "post_consume_script_error"
MESSAGE_NEW_FILE = "new_file"
MESSAGE_UNSUPPORTED_TYPE = "unsupported_type"
MESSAGE_PARSING_DOCUMENT = "parsing_document"
MESSAGE_GENERATING_THUMBNAIL = "generating_thumbnail"
MESSAGE_PARSE_DATE = "parse_date"
MESSAGE_SAVE_DOCUMENT = "save_document"
MESSAGE_FINISHED = "finished"
class Consumer(LoggingMixin):
def _send_progress(self, current_progress, max_progress, status,
message, document_id=None):
message=None, document_id=None):
payload = {
'filename': os.path.basename(self.filename) if self.filename else None, # NOQA: E501
'task_id': self.task_id,
@@ -69,7 +82,7 @@ class Consumer(LoggingMixin):
def pre_check_file_exists(self):
if not os.path.isfile(self.path):
self._fail(
_("File not found"),
MESSAGE_FILE_NOT_FOUND,
f"Cannot consume {self.path}: File not found."
)
@@ -80,7 +93,7 @@ class Consumer(LoggingMixin):
if settings.CONSUMER_DELETE_DUPLICATES:
os.unlink(self.path)
self._fail(
_("Document already exists"),
MESSAGE_DOCUMENT_ALREADY_EXISTS,
f"Not consuming {self.filename}: It is a duplicate."
)
@@ -96,7 +109,7 @@ class Consumer(LoggingMixin):
if not os.path.isfile(settings.PRE_CONSUME_SCRIPT):
self._fail(
_("Pre-consume script does not exist."),
MESSAGE_PRE_CONSUME_SCRIPT_NOT_FOUND,
f"Configured pre-consume script "
f"{settings.PRE_CONSUME_SCRIPT} does not exist.")
@@ -104,7 +117,7 @@ class Consumer(LoggingMixin):
Popen((settings.PRE_CONSUME_SCRIPT, self.path)).wait()
except Exception as e:
self._fail(
_("Error while executing pre-consume script"),
MESSAGE_PRE_CONSUME_SCRIPT_ERROR,
f"Error while executing pre-consume script: {e}"
)
@@ -114,7 +127,7 @@ class Consumer(LoggingMixin):
if not os.path.isfile(settings.POST_CONSUME_SCRIPT):
self._fail(
_("Post-consume script does not exist."),
MESSAGE_POST_CONSUME_SCRIPT_NOT_FOUND,
f"Configured post-consume script "
f"{settings.POST_CONSUME_SCRIPT} does not exist."
)
@@ -134,7 +147,7 @@ class Consumer(LoggingMixin):
)).wait()
except Exception as e:
self._fail(
_("Error while executing post-consume script"),
MESSAGE_POST_CONSUME_SCRIPT_ERROR,
f"Error while executing post-consume script: {e}"
)
@@ -158,7 +171,7 @@ class Consumer(LoggingMixin):
self.override_tag_ids = override_tag_ids
self.task_id = task_id or str(uuid.uuid4())
self._send_progress(0, 100, 'STARTING', _('Received new file'))
self._send_progress(0, 100, 'STARTING', MESSAGE_NEW_FILE)
# this is for grouping logging entries for this particular file
# together.
@@ -182,8 +195,7 @@ class Consumer(LoggingMixin):
parser_class = get_parser_class_for_mime_type(mime_type)
if not parser_class:
self._fail(
_("File type %(type)s not supported") %
{'type': mime_type},
MESSAGE_UNSUPPORTED_TYPE,
f"Unsupported mime type {mime_type}"
)
else:
@@ -199,10 +211,10 @@ class Consumer(LoggingMixin):
self.run_pre_consume_script()
def progress_callback(current_progress, max_progress, message):
def progress_callback(current_progress, max_progress):
# recalculate progress to be within 20 and 80
p = int((current_progress / max_progress) * 50 + 20)
self._send_progress(p, 100, "WORKING", message)
self._send_progress(p, 100, "WORKING")
# This doesn't parse the document yet, but gives us a parser.
@@ -219,13 +231,13 @@ class Consumer(LoggingMixin):
archive_path = None
try:
self._send_progress(20, 100, 'WORKING', _('Parsing document...'))
self._send_progress(20, 100, 'WORKING', MESSAGE_PARSING_DOCUMENT)
self.log("debug", "Parsing {}...".format(self.filename))
document_parser.parse(self.path, mime_type, self.filename)
self.log("debug", f"Generating thumbnail for {self.filename}...")
self._send_progress(70, 100, 'WORKING',
_('Generating thumbnail...'))
MESSAGE_GENERATING_THUMBNAIL)
thumbnail = document_parser.get_optimised_thumbnail(
self.path, mime_type)
@@ -233,7 +245,7 @@ class Consumer(LoggingMixin):
date = document_parser.get_date()
if not date:
self._send_progress(90, 100, 'WORKING',
_('Getting date from document...'))
MESSAGE_PARSE_DATE)
date = parse_date(self.filename, text)
archive_path = document_parser.get_archive_path()
@@ -258,7 +270,7 @@ class Consumer(LoggingMixin):
"warning",
f"Cannot classify documents: {e}.")
classifier = None
self._send_progress(95, 100, 'WORKING', _('Saving document...'))
self._send_progress(95, 100, 'WORKING', MESSAGE_SAVE_DOCUMENT)
# now that everything is done, we can start to store the document
# in the system. This will be a transaction and reasonably fast.
try:
@@ -327,7 +339,7 @@ class Consumer(LoggingMixin):
"Document {} consumption finished".format(document)
)
self._send_progress(100, 100, 'SUCCESS', _('Finished.'), document.id)
self._send_progress(100, 100, 'SUCCESS', MESSAGE_FINISHED, document.id)
return document

View File

@@ -273,6 +273,11 @@ class DocumentParser(LoggingMixin):
self.date = None
self.progress_callback = progress_callback
def progress(self, current, max):
print(self.progress_callback)
if self.progress_callback:
self.progress_callback(current, max)
def extract_metadata(self, document_path, mime_type):
return []

View File

@@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2021-01-27 17:57+0100\n"
"POT-Creation-Date: 2021-01-28 22:02+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@@ -21,60 +21,6 @@ msgstr ""
msgid "Documents"
msgstr ""
#: documents/consumer.py:72
msgid "File not found"
msgstr "Datei nicht gefunden"
#: documents/consumer.py:83
msgid "Document already exists"
msgstr ""
#: documents/consumer.py:99
msgid "Pre-consume script does not exist."
msgstr ""
#: documents/consumer.py:107
msgid "Error while executing pre-consume script"
msgstr ""
#: documents/consumer.py:117
msgid "Post-consume script does not exist."
msgstr ""
#: documents/consumer.py:137
msgid "Error while executing post-consume script"
msgstr ""
#: documents/consumer.py:161
msgid "Received new file"
msgstr ""
#: documents/consumer.py:185 documents/serialisers.py:383
#, fuzzy, python-format
#| msgid "File type {type} not supported."
msgid "File type %(type)s not supported"
msgstr "Dateityp {type} wird nicht unterstützt"
#: documents/consumer.py:222
msgid "Parsing document..."
msgstr ""
#: documents/consumer.py:228
msgid "Generating thumbnail..."
msgstr ""
#: documents/consumer.py:236
msgid "Getting date from document..."
msgstr ""
#: documents/consumer.py:261
msgid "Saving document..."
msgstr ""
#: documents/consumer.py:330
msgid "Finished."
msgstr ""
#: documents/models.py:33
msgid "Any word"
msgstr ""
@@ -392,6 +338,11 @@ msgstr ""
msgid "filter rules"
msgstr ""
#: documents/serialisers.py:383
#, python-format
msgid "File type %(type)s not supported"
msgstr ""
#: documents/templates/index.html:20
msgid "Paperless-ng is loading..."
msgstr ""
@@ -674,6 +625,3 @@ msgstr ""
#: paperless_mail/models.py:205
msgid "assign this correspondent"
msgstr ""
#~ msgid "Document is a duplicate"
#~ msgstr "Dokument existiert bereits"