A community-supported supercharged document management system: scan, index and archive all your documents
Python aiangulararchivingdjangodmsdocument-managementdocument-management-systemllmmachine-learningocroptical-character-recognitionpdf
Format Python code with black
kpjfc695896dd8b
diff --git a/src/documents/admin.py b/src/documents/admin.pyindex c8c16d791..88e2da50e 100644--- a/src/documents/admin.py+++ b/src/documents/admin.py@@ -1,39 +1,32 @@from django.contrib import admin-from .models import Correspondent, Document, DocumentType, Tag, \- SavedView, SavedViewFilterRule+from .models import (+ Correspondent,+ Document,+ DocumentType,+ Tag,+ SavedView,+ SavedViewFilterRule,+)class CorrespondentAdmin(admin.ModelAdmin):- list_display = (- "name",- "match",- "matching_algorithm"- )+ list_display = ("name", "match", "matching_algorithm")list_filter = ("matching_algorithm",)list_editable = ("match", "matching_algorithm")class TagAdmin(admin.ModelAdmin):- list_display = (- "name",- "color",- "match",- "matching_algorithm"- )+ list_display = ("name", "color", "match", "matching_algorithm")list_filter = ("color", "matching_algorithm")list_editable = ("color", "match", "matching_algorithm")class DocumentTypeAdmin(admin.ModelAdmin):- list_display = (- "name",- "match",- "matching_algorithm"- )+ list_display = ("name", "match", "matching_algorithm")list_filter = ("matching_algorithm",)list_editable = ("match", "matching_algorithm")@@ -49,18 +42,12 @@ class DocumentAdmin(admin.ModelAdmin):"filename","checksum","archive_filename",- "archive_checksum"+ "archive_checksum",)list_display_links = ("title",)- list_display = (- "id",- "title",- "mime_type",- "filename",- "archive_filename"- )+ list_display = ("id", "title", "mime_type", "filename", "archive_filename")list_filter = (("mime_type"),@@ -79,6 +66,7 @@ class DocumentAdmin(admin.ModelAdmin):def created_(self, obj):return obj.created.date().strftime("%Y-%m-%d")+created_.short_description = "Created"def delete_queryset(self, request, queryset):@@ -92,11 +80,13 @@ class DocumentAdmin(admin.ModelAdmin):def delete_model(self, request, obj):from documents import index+index.remove_document_from_index(obj)super(DocumentAdmin, self).delete_model(request, obj)def save_model(self, request, obj, form, change):from documents import index+index.add_or_update_document(obj)super(DocumentAdmin, self).save_model(request, obj, form, change)@@ -109,9 +99,7 @@ class SavedViewAdmin(admin.ModelAdmin):list_display = ("name", "user")- inlines = [- RuleInline- ]+ inlines = [RuleInline]admin.site.register(Correspondent, CorrespondentAdmin)diff --git a/src/documents/apps.py b/src/documents/apps.pyindex e21e14097..0a59fef51 100644--- a/src/documents/apps.py+++ b/src/documents/apps.py@@ -17,7 +17,7 @@ class DocumentsConfig(AppConfig):set_correspondent,set_document_type,set_tags,- add_to_index+ add_to_index,)document_consumption_finished.connect(add_inbox_tags)diff --git a/src/documents/bulk_download.py b/src/documents/bulk_download.pyindex 8c675b4b5..11770968d 100644--- a/src/documents/bulk_download.py+++ b/src/documents/bulk_download.py@@ -4,14 +4,12 @@ from documents.models import Documentclass BulkArchiveStrategy:-def __init__(self, zipf: ZipFile):self.zipf = zipf- def make_unique_filename(self,- doc: Document,- archive: bool = False,- folder: str = ""):+ def make_unique_filename(+ self, doc: Document, archive: bool = False, folder: str = ""+ ):counter = 0while True:filename = folder + doc.get_public_filename(archive, counter)@@ -25,36 +23,31 @@ class BulkArchiveStrategy:class OriginalsOnlyStrategy(BulkArchiveStrategy):-def add_document(self, doc: Document):self.zipf.write(doc.source_path, self.make_unique_filename(doc))class ArchiveOnlyStrategy(BulkArchiveStrategy):-def __init__(self, zipf):super(ArchiveOnlyStrategy, self).__init__(zipf)def add_document(self, doc: Document):if doc.has_archive_version:- self.zipf.write(doc.archive_path,- self.make_unique_filename(doc, archive=True))+ self.zipf.write(+ doc.archive_path, self.make_unique_filename(doc, archive=True)+ )else:- self.zipf.write(doc.source_path,- self.make_unique_filename(doc))+ self.zipf.write(doc.source_path, self.make_unique_filename(doc))class OriginalAndArchiveStrategy(BulkArchiveStrategy):-def add_document(self, doc: Document):if doc.has_archive_version:self.zipf.write(- doc.archive_path, self.make_unique_filename(- doc, archive=True, folder="archive/"- )+ doc.archive_path,+ self.make_unique_filename(doc, archive=True, folder="archive/"),)self.zipf.write(- doc.source_path,- self.make_unique_filename(doc, folder="originals/")+ doc.source_path, self.make_unique_filename(doc, folder="originals/"))diff --git a/src/documents/bulk_edit.py b/src/documents/bulk_edit.pyindex 7503eafc5..18ad04f2d 100644--- a/src/documents/bulk_edit.py+++ b/src/documents/bulk_edit.py@@ -10,13 +10,11 @@ def set_correspondent(doc_ids, correspondent):if correspondent:correspondent = Correspondent.objects.get(id=correspondent)- qs = Document.objects.filter(- Q(id__in=doc_ids) & ~Q(correspondent=correspondent))+ qs = Document.objects.filter(Q(id__in=doc_ids) & ~Q(correspondent=correspondent))affected_docs = [doc.id for doc in qs]qs.update(correspondent=correspondent)- async_task(- "documents.tasks.bulk_update_documents", document_ids=affected_docs)+ async_task("documents.tasks.bulk_update_documents", document_ids=affected_docs)return "OK"@@ -25,13 +23,11 @@ def set_document_type(doc_ids, document_type):if document_type:document_type = DocumentType.objects.get(id=document_type)- qs = Document.objects.filter(- Q(id__in=doc_ids) & ~Q(document_type=document_type))+ qs = Document.objects.filter(Q(id__in=doc_ids) & ~Q(document_type=document_type))affected_docs = [doc.id for doc in qs]qs.update(document_type=document_type)- async_task(- "documents.tasks.bulk_update_documents", document_ids=affected_docs)+ async_task("documents.tasks.bulk_update_documents", document_ids=affected_docs)return "OK"@@ -43,13 +39,11 @@ def add_tag(doc_ids, tag):DocumentTagRelationship = Document.tags.through- DocumentTagRelationship.objects.bulk_create([- DocumentTagRelationship(- document_id=doc, tag_id=tag) for doc in affected_docs- ])+ DocumentTagRelationship.objects.bulk_create(+ [DocumentTagRelationship(document_id=doc, tag_id=tag) for doc in affected_docs]+ )- async_task(- "documents.tasks.bulk_update_documents", document_ids=affected_docs)+ async_task("documents.tasks.bulk_update_documents", document_ids=affected_docs)return "OK"@@ -62,12 +56,10 @@ def remove_tag(doc_ids, tag):DocumentTagRelationship = Document.tags.throughDocumentTagRelationship.objects.filter(- Q(document_id__in=affected_docs) &- Q(tag_id=tag)+ Q(document_id__in=affected_docs) & Q(tag_id=tag)).delete()- async_task(- "documents.tasks.bulk_update_documents", document_ids=affected_docs)+ async_task("documents.tasks.bulk_update_documents", document_ids=affected_docs)return "OK"@@ -83,13 +75,15 @@ def modify_tags(doc_ids, add_tags, remove_tags):tag_id__in=remove_tags,).delete()- DocumentTagRelationship.objects.bulk_create([DocumentTagRelationship(- document_id=doc, tag_id=tag) for (doc, tag) in itertools.product(- affected_docs, add_tags)- ], ignore_conflicts=True)+ DocumentTagRelationship.objects.bulk_create(+ [+ DocumentTagRelationship(document_id=doc, tag_id=tag)+ for (doc, tag) in itertools.product(affected_docs, add_tags)+ ],+ ignore_conflicts=True,+ )- async_task(- "documents.tasks.bulk_update_documents", document_ids=affected_docs)+ async_task("documents.tasks.bulk_update_documents", document_ids=affected_docs)return "OK"diff --git a/src/documents/checks.py b/src/documents/checks.pyindex ba55b1397..fe3d89b12 100644--- a/src/documents/checks.py+++ b/src/documents/checks.py@@ -16,28 +16,36 @@ def changed_password_check(app_configs, **kwargs):try:encrypted_doc = Document.objects.filter(- storage_type=Document.STORAGE_TYPE_GPG).first()+ storage_type=Document.STORAGE_TYPE_GPG+ ).first()except (OperationalError, ProgrammingError, FieldError):return [] # No documents table yetif encrypted_doc:if not settings.PASSPHRASE:- return [Error(- "The database contains encrypted documents but no password "- "is set."- )]+ return [+ Error(+ "The database contains encrypted documents but no password "+ "is set."+ )+ ]if not GnuPG.decrypted(encrypted_doc.source_file):- return [Error(textwrap.dedent(- """+ return [+ Error(+ textwrap.dedent(+ """The current password doesn't match the password of theexisting documents.If you intend to change your password, you must first exportall of the old documents, start fresh with the new passwordand then re-import them."- """))]+ """+ )+ )+ ]return []@@ -50,7 +58,11 @@ def parser_check(app_configs, **kwargs):parsers.append(response[1])if len(parsers) == 0:- return [Error("No parsers found. This is a bug. The consumer won't be "- "able to consume any documents without parsers.")]+ return [+ Error(+ "No parsers found. This is a bug. The consumer won't be "+ "able to consume any documents without parsers."+ )+ ]else:return []diff --git a/src/documents/classifier.py b/src/documents/classifier.pyindex 36e906350..195c934a9 100644--- a/src/documents/classifier.py+++ b/src/documents/classifier.py@@ -39,8 +39,7 @@ def load_classifier():try:classifier.load()- except (ClassifierModelCorruptError,- IncompatibleClassifierVersionError):+ except (ClassifierModelCorruptError, IncompatibleClassifierVersionError):# there's something wrong with the model file.logger.exception(f"Unrecoverable error while loading document "@@ -49,14 +48,10 @@ def load_classifier():os.unlink(settings.MODEL_FILE)classifier = Noneexcept OSError:- logger.exception(- f"IO error while loading document classification model"- )+ logger.exception(f"IO error while loading document classification model")classifier = Noneexcept Exception:- logger.exception(- f"Unknown error while loading document classification model"- )+ logger.exception(f"Unknown error while loading document classification model")classifier = Nonereturn classifier@@ -83,7 +78,8 @@ class DocumentClassifier(object):if schema_version != self.FORMAT_VERSION:raise IncompatibleClassifierVersionError(- "Cannor load classifier, incompatible versions.")+ "Cannor load classifier, incompatible versions."+ )else:try:self.data_hash = pickle.load(f)@@ -125,30 +121,37 @@ class DocumentClassifier(object):# Step 1: Extract and preprocess training data from the database.logger.debug("Gathering data from database...")m = hashlib.sha1()- for doc in Document.objects.order_by('pk').exclude(tags__is_inbox_tag=True): # NOQA: E501+ for doc in Document.objects.order_by("pk").exclude(+ tags__is_inbox_tag=True+ ): # NOQA: E501preprocessed_content = preprocess_content(doc.content)- m.update(preprocessed_content.encode('utf-8'))+ m.update(preprocessed_content.encode("utf-8"))data.append(preprocessed_content)y = -1dt = doc.document_typeif dt and dt.matching_algorithm == MatchingModel.MATCH_AUTO:y = dt.pk- m.update(y.to_bytes(4, 'little', signed=True))+ m.update(y.to_bytes(4, "little", signed=True))labels_document_type.append(y)y = -1cor = doc.correspondentif cor and cor.matching_algorithm == MatchingModel.MATCH_AUTO:y = cor.pk- m.update(y.to_bytes(4, 'little', signed=True))+ m.update(y.to_bytes(4, "little", signed=True))labels_correspondent.append(y)- tags = sorted([tag.pk for tag in doc.tags.filter(- matching_algorithm=MatchingModel.MATCH_AUTO- )])+ tags = sorted(+ [+ tag.pk+ for tag in doc.tags.filter(+ matching_algorithm=MatchingModel.MATCH_AUTO+ )+ ]+ )for tag in tags:- m.update(tag.to_bytes(4, 'little', signed=True))+ m.update(tag.to_bytes(4, "little", signed=True))labels_tags.append(tags)if not data:@@ -174,10 +177,7 @@ class DocumentClassifier(object):logger.debug("{} documents, {} tag(s), {} correspondent(s), ""{} document type(s).".format(- len(data),- num_tags,- num_correspondents,- num_document_types+ len(data), num_tags, num_correspondents, num_document_types))@@ -188,9 +188,7 @@ class DocumentClassifier(object):# Step 2: vectorize datalogger.debug("Vectorizing data...")self.data_vectorizer = CountVectorizer(- analyzer="word",- ngram_range=(1, 2),- min_df=0.01+ analyzer="word", ngram_range=(1, 2), min_df=0.01)data_vectorized = self.data_vectorizer.fit_transform(data)@@ -201,54 +199,41 @@ class DocumentClassifier(object):if num_tags == 1:# Special case where only one tag has auto:# Fallback to binary classification.- labels_tags = [label[0] if len(label) == 1 else -1- for label in labels_tags]+ labels_tags = [+ label[0] if len(label) == 1 else -1 for label in labels_tags+ ]self.tags_binarizer = LabelBinarizer()labels_tags_vectorized = self.tags_binarizer.fit_transform(- labels_tags).ravel()+ labels_tags+ ).ravel()else:self.tags_binarizer = MultiLabelBinarizer()- labels_tags_vectorized = self.tags_binarizer.fit_transform(- labels_tags)+ labels_tags_vectorized = self.tags_binarizer.fit_transform(labels_tags)self.tags_classifier = MLPClassifier(tol=0.01)self.tags_classifier.fit(data_vectorized, labels_tags_vectorized)else:self.tags_classifier = None- logger.debug(- "There are no tags. Not training tags classifier."- )+ logger.debug("There are no tags. Not training tags classifier.")if num_correspondents > 0:- logger.debug(- "Training correspondent classifier..."- )+ logger.debug("Training correspondent classifier...")self.correspondent_classifier = MLPClassifier(tol=0.01)- self.correspondent_classifier.fit(- data_vectorized,- labels_correspondent- )+ self.correspondent_classifier.fit(data_vectorized, labels_correspondent)else:self.correspondent_classifier = Nonelogger.debug(- "There are no correspondents. Not training correspondent "- "classifier."+ "There are no correspondents. Not training correspondent " "classifier.")if num_document_types > 0:- logger.debug(- "Training document type classifier..."- )+ logger.debug("Training document type classifier...")self.document_type_classifier = MLPClassifier(tol=0.01)- self.document_type_classifier.fit(- data_vectorized,- labels_document_type- )+ self.document_type_classifier.fit(data_vectorized, labels_document_type)else:self.document_type_classifier = Nonelogger.debug(- "There are no document types. Not training document type "- "classifier."+ "There are no document types. Not training document type " "classifier.")self.data_hash = new_data_hash@@ -284,10 +269,10 @@ class DocumentClassifier(object):X = self.data_vectorizer.transform([preprocess_content(content)])y = self.tags_classifier.predict(X)tags_ids = self.tags_binarizer.inverse_transform(y)[0]- if type_of_target(y).startswith('multilabel'):+ if type_of_target(y).startswith("multilabel"):# the usual case when there are multiple tags.return list(tags_ids)- elif type_of_target(y) == 'binary' and tags_ids != -1:+ elif type_of_target(y) == "binary" and tags_ids != -1:# This is for when we have binary classification with only one# tag and the result is to assign this tag.return [tags_ids]diff --git a/src/documents/consumer.py b/src/documents/consumer.pyindex 39275cee3..0d246de26 100644--- a/src/documents/consumer.py+++ b/src/documents/consumer.py@@ -15,15 +15,11 @@ from filelock import FileLockfrom rest_framework.reverse import reversefrom .classifier import load_classifier-from .file_handling import create_source_path_directory, \- generate_unique_filename+from .file_handling import create_source_path_directory, generate_unique_filenamefrom .loggers import LoggingMixinfrom .models import Document, FileInfo, Correspondent, DocumentType, Tagfrom .parsers import ParseError, get_parser_class_for_mime_type, parse_date-from .signals import (- document_consumption_finished,- document_consumption_started-)+from .signals import document_consumption_finished, document_consumption_startedclass ConsumerError(Exception):@@ -49,23 +45,26 @@ class Consumer(LoggingMixin):logging_name = "paperless.consumer"- def _send_progress(self, current_progress, max_progress, status,- message=None, document_id=None):+ def _send_progress(+ self, current_progress, max_progress, status, message=None, document_id=None+ ):payload = {- 'filename': os.path.basename(self.filename) if self.filename else None, # NOQA: E501- 'task_id': self.task_id,- 'current_progress': current_progress,- 'max_progress': max_progress,- 'status': status,- 'message': message,- 'document_id': document_id+ "filename": os.path.basename(self.filename)+ if self.filename+ else None, # NOQA: E501+ "task_id": self.task_id,+ "current_progress": current_progress,+ "max_progress": max_progress,+ "status": status,+ "message": message,+ "document_id": document_id,}- async_to_sync(self.channel_layer.group_send)("status_updates",- {'type': 'status_update',- 'data': payload})+ async_to_sync(self.channel_layer.group_send)(+ "status_updates", {"type": "status_update", "data": payload}+ )def _fail(self, message, log_message=None, exc_info=None):- self._send_progress(100, 100, 'FAILED', message)+ self._send_progress(100, 100, "FAILED", message)self.log("error", log_message or message, exc_info=exc_info)raise ConsumerError(f"{self.filename}: {log_message or message}")@@ -84,19 +83,20 @@ class Consumer(LoggingMixin):def pre_check_file_exists(self):if not os.path.isfile(self.path):self._fail(- MESSAGE_FILE_NOT_FOUND,- f"Cannot consume {self.path}: File not found."+ MESSAGE_FILE_NOT_FOUND, f"Cannot consume {self.path}: File not found.")def pre_check_duplicate(self):with open(self.path, "rb") as f:checksum = hashlib.md5(f.read()).hexdigest()- if Document.objects.filter(Q(checksum=checksum) | Q(archive_checksum=checksum)).exists(): # NOQA: E501+ if Document.objects.filter(+ Q(checksum=checksum) | Q(archive_checksum=checksum)+ ).exists(): # NOQA: E501if settings.CONSUMER_DELETE_DUPLICATES:os.unlink(self.path)self._fail(MESSAGE_DOCUMENT_ALREADY_EXISTS,- f"Not consuming {self.filename}: It is a duplicate."+ f"Not consuming {self.filename}: It is a duplicate.",)def pre_check_directories(self):@@ -113,10 +113,10 @@ class Consumer(LoggingMixin):self._fail(MESSAGE_PRE_CONSUME_SCRIPT_NOT_FOUND,f"Configured pre-consume script "- f"{settings.PRE_CONSUME_SCRIPT} does not exist.")+ f"{settings.PRE_CONSUME_SCRIPT} does not exist.",+ )- self.log("info",- f"Executing pre-consume script {settings.PRE_CONSUME_SCRIPT}")+ self.log("info", f"Executing pre-consume script {settings.PRE_CONSUME_SCRIPT}")try:Popen((settings.PRE_CONSUME_SCRIPT, self.path)).wait()@@ -124,7 +124,7 @@ class Consumer(LoggingMixin):self._fail(MESSAGE_PRE_CONSUME_SCRIPT_ERROR,f"Error while executing pre-consume script: {e}",- exc_info=True+ exc_info=True,)def run_post_consume_script(self, document):@@ -135,42 +135,44 @@ class Consumer(LoggingMixin):self._fail(MESSAGE_POST_CONSUME_SCRIPT_NOT_FOUND,f"Configured post-consume script "- f"{settings.POST_CONSUME_SCRIPT} does not exist."+ f"{settings.POST_CONSUME_SCRIPT} does not exist.",)self.log(- "info",- f"Executing post-consume script {settings.POST_CONSUME_SCRIPT}"+ "info", f"Executing post-consume script {settings.POST_CONSUME_SCRIPT}")try:- Popen((- settings.POST_CONSUME_SCRIPT,- str(document.pk),- document.get_public_filename(),- os.path.normpath(document.source_path),- os.path.normpath(document.thumbnail_path),- reverse("document-download", kwargs={"pk": document.pk}),- reverse("document-thumb", kwargs={"pk": document.pk}),- str(document.correspondent),- str(",".join(document.tags.all().values_list(- "name", flat=True)))- )).wait()+ Popen(+ (+ settings.POST_CONSUME_SCRIPT,+ str(document.pk),+ document.get_public_filename(),+ os.path.normpath(document.source_path),+ os.path.normpath(document.thumbnail_path),+ reverse("document-download", kwargs={"pk": document.pk}),+ reverse("document-thumb", kwargs={"pk": document.pk}),+ str(document.correspondent),+ str(",".join(document.tags.all().values_list("name", flat=True))),+ )+ ).wait()except Exception as e:self._fail(MESSAGE_POST_CONSUME_SCRIPT_ERROR,f"Error while executing post-consume script: {e}",- exc_info=True+ exc_info=True,)- def try_consume_file(self,- path,- override_filename=None,- override_title=None,- override_correspondent_id=None,- override_document_type_id=None,- override_tag_ids=None,- task_id=None):+ def try_consume_file(+ self,+ path,+ override_filename=None,+ override_title=None,+ override_correspondent_id=None,+ override_document_type_id=None,+ override_tag_ids=None,+ task_id=None,+ ):"""Return the document object if it was successfully created."""@@ -183,7 +185,7 @@ class Consumer(LoggingMixin):self.override_tag_ids = override_tag_idsself.task_id = task_id or str(uuid.uuid4())- self._send_progress(0, 100, 'STARTING', MESSAGE_NEW_FILE)+ self._send_progress(0, 100, "STARTING", MESSAGE_NEW_FILE)# this is for grouping logging entries for this particular file# together.@@ -206,17 +208,12 @@ class Consumer(LoggingMixin):parser_class = get_parser_class_for_mime_type(mime_type)if not parser_class:- self._fail(- MESSAGE_UNSUPPORTED_TYPE,- f"Unsupported mime type {mime_type}"- )+ self._fail(MESSAGE_UNSUPPORTED_TYPE, f"Unsupported mime type {mime_type}")# Notify all listeners that we're going to do some work.document_consumption_started.send(- sender=self.__class__,- filename=self.path,- logging_group=self.logging_group+ sender=self.__class__, filename=self.path, logging_group=self.logging_group)self.run_pre_consume_script()@@ -243,21 +240,20 @@ class Consumer(LoggingMixin):archive_path = Nonetry:- self._send_progress(20, 100, 'WORKING', MESSAGE_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',- MESSAGE_GENERATING_THUMBNAIL)+ self._send_progress(70, 100, "WORKING", MESSAGE_GENERATING_THUMBNAIL)thumbnail = document_parser.get_optimised_thumbnail(- self.path, mime_type, self.filename)+ self.path, mime_type, self.filename+ )text = document_parser.get_text()date = document_parser.get_date()if not date:- self._send_progress(90, 100, 'WORKING',- MESSAGE_PARSE_DATE)+ self._send_progress(90, 100, "WORKING", MESSAGE_PARSE_DATE)date = parse_date(self.filename, text)archive_path = document_parser.get_archive_path()@@ -266,7 +262,7 @@ class Consumer(LoggingMixin):self._fail(str(e),f"Error while consuming document {self.filename}: {e}",- exc_info=True+ exc_info=True,)# Prepare the document classifier.@@ -277,18 +273,14 @@ class Consumer(LoggingMixin):classifier = load_classifier()- self._send_progress(95, 100, 'WORKING', MESSAGE_SAVE_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:with transaction.atomic():# store the document.- document = self._store(- text=text,- date=date,- mime_type=mime_type- )+ document = self._store(text=text, date=date, mime_type=mime_type)# If we get here, it was successful. Proceed with post-consume# hooks. If they fail, nothing will get changed.@@ -297,7 +289,7 @@ class Consumer(LoggingMixin):sender=self.__class__,document=document,logging_group=self.logging_group,- classifier=classifier+ classifier=classifier,)# After everything is in the database, copy the files into@@ -306,24 +298,25 @@ class Consumer(LoggingMixin):document.filename = generate_unique_filename(document)create_source_path_directory(document.source_path)- self._write(document.storage_type,- self.path, document.source_path)+ self._write(document.storage_type, self.path, document.source_path)- self._write(document.storage_type,- thumbnail, document.thumbnail_path)+ self._write(+ document.storage_type, thumbnail, document.thumbnail_path+ )if archive_path and os.path.isfile(archive_path):document.archive_filename = generate_unique_filename(- document,- archive_filename=True+ document, archive_filename=True)create_source_path_directory(document.archive_path)- self._write(document.storage_type,- archive_path, document.archive_path)+ self._write(+ document.storage_type, archive_path, document.archive_path+ )- with open(archive_path, 'rb') as f:+ with open(archive_path, "rb") as f:document.archive_checksum = hashlib.md5(- f.read()).hexdigest()+ f.read()+ ).hexdigest()# Don't save with the lock active. Saving will cause the file# renaming logic to aquire the lock as well.@@ -335,8 +328,8 @@ class Consumer(LoggingMixin):# https://github.com/jonaswinkler/paperless-ng/discussions/1037shadow_file = os.path.join(- os.path.dirname(self.path),- "._" + os.path.basename(self.path))+ os.path.dirname(self.path), "._" + os.path.basename(self.path)+ )if os.path.isfile(shadow_file):self.log("debug", "Deleting file {}".format(shadow_file))@@ -345,21 +338,17 @@ class Consumer(LoggingMixin):except Exception as e:self._fail(str(e),- f"The following error occured while consuming "- f"{self.filename}: {e}",- exc_info=True+ f"The following error occured while consuming " f"{self.filename}: {e}",+ exc_info=True,)finally:document_parser.cleanup()self.run_post_consume_script(document)- self.log(- "info",- "Document {} consumption finished".format(document)- )+ self.log("info", "Document {} consumption finished".format(document))- self._send_progress(100, 100, 'SUCCESS', MESSAGE_FINISHED, document.id)+ self._send_progress(100, 100, "SUCCESS", MESSAGE_FINISHED, document.id)return document@@ -373,8 +362,11 @@ class Consumer(LoggingMixin):self.log("debug", "Saving record to database")- created = file_info.created or date or timezone.make_aware(- datetime.datetime.fromtimestamp(stats.st_mtime))+ created = (+ file_info.created+ or date+ or timezone.make_aware(datetime.datetime.fromtimestamp(stats.st_mtime))+ )storage_type = Document.STORAGE_TYPE_UNENCRYPTED@@ -386,7 +378,7 @@ class Consumer(LoggingMixin):checksum=hashlib.md5(f.read()).hexdigest(),created=created,modified=created,- storage_type=storage_type+ storage_type=storage_type,)self.apply_overrides(document)@@ -398,11 +390,13 @@ class Consumer(LoggingMixin):def apply_overrides(self, document):if self.override_correspondent_id:document.correspondent = Correspondent.objects.get(- pk=self.override_correspondent_id)+ pk=self.override_correspondent_id+ )if self.override_document_type_id:document.document_type = DocumentType.objects.get(- pk=self.override_document_type_id)+ pk=self.override_document_type_id+ )if self.override_tag_ids:for tag_id in self.override_tag_ids:diff --git a/src/documents/file_handling.py b/src/documents/file_handling.pyindex d7a42db24..390643357 100644--- a/src/documents/file_handling.py+++ b/src/documents/file_handling.py@@ -12,7 +12,6 @@ logger = logging.getLogger("paperless.filehandling")class defaultdictNoStr(defaultdict):-def __str__(self):raise ValueError("Don't use {tags} directly.")@@ -63,24 +62,23 @@ def many_to_dictionary(field):mydictionary[index] = slugify(t.name)# Find delimiter- delimiter = t.name.find('_')+ delimiter = t.name.find("_")if delimiter == -1:- delimiter = t.name.find('-')+ delimiter = t.name.find("-")if delimiter == -1:continuekey = t.name[:delimiter]- value = t.name[delimiter + 1:]+ value = t.name[delimiter + 1 :]mydictionary[slugify(key)] = slugify(value)return mydictionary-def generate_unique_filename(doc,- archive_filename=False):+def generate_unique_filename(doc, archive_filename=False):"""Generates a unique filename for doc in settings.ORIGINALS_DIR.@@ -104,14 +102,17 @@ def generate_unique_filename(doc,if archive_filename and doc.filename:new_filename = os.path.splitext(doc.filename)[0] + ".pdf"- if new_filename == old_filename or not os.path.exists(os.path.join(root, new_filename)): # NOQA: E501+ if new_filename == old_filename or not os.path.exists(+ os.path.join(root, new_filename)+ ): # NOQA: E501return new_filenamecounter = 0while True:new_filename = generate_filename(- doc, counter, archive_filename=archive_filename)+ doc, counter, archive_filename=archive_filename+ )if new_filename == old_filename:# still the same as before.return new_filename@@ -127,14 +128,11 @@ def generate_filename(doc, counter=0, append_gpg=True, archive_filename=False):try:if settings.PAPERLESS_FILENAME_FORMAT is not None:- tags = defaultdictNoStr(lambda: slugify(None),- many_to_dictionary(doc.tags))+ tags = defaultdictNoStr(lambda: slugify(None), many_to_dictionary(doc.tags))tag_list = pathvalidate.sanitize_filename(- ",".join(sorted(- [tag.name for tag in doc.tags.all()]- )),- replacement_text="-"+ ",".join(sorted([tag.name for tag in doc.tags.all()])),+ replacement_text="-",)if doc.correspondent:@@ -157,13 +155,14 @@ def generate_filename(doc, counter=0, append_gpg=True, archive_filename=False):asn = "none"path = settings.PAPERLESS_FILENAME_FORMAT.format(- title=pathvalidate.sanitize_filename(- doc.title, replacement_text="-"),+ title=pathvalidate.sanitize_filename(doc.title, replacement_text="-"),correspondent=correspondent,document_type=document_type,created=datetime.date.isoformat(doc.created),created_year=doc.created.year if doc.created else "none",- created_month=f"{doc.created.month:02}" if doc.created else "none", # NOQA: E501+ created_month=f"{doc.created.month:02}"+ if doc.created+ else "none", # NOQA: E501created_day=f"{doc.created.day:02}" if doc.created else "none",added=datetime.date.isoformat(doc.added),added_year=doc.added.year if doc.added else "none",@@ -171,7 +170,7 @@ def generate_filename(doc, counter=0, append_gpg=True, archive_filename=False):added_day=f"{doc.added.day:02}" if doc.added else "none",asn=asn,tags=tags,- tag_list=tag_list+ tag_list=tag_list,).strip()path = path.strip(os.sep)@@ -179,7 +178,8 @@ def generate_filename(doc, counter=0, append_gpg=True, archive_filename=False):except (ValueError, KeyError, IndexError):logger.warning(f"Invalid PAPERLESS_FILENAME_FORMAT: "- f"{settings.PAPERLESS_FILENAME_FORMAT}, falling back to default")+ f"{settings.PAPERLESS_FILENAME_FORMAT}, falling back to default"+ )counter_str = f"_{counter:02}" if counter else ""diff --git a/src/documents/filters.py b/src/documents/filters.pyindex 152c556c3..7075eb3d0 100644--- a/src/documents/filters.py+++ b/src/documents/filters.py@@ -10,34 +10,24 @@ DATE_KWARGS = ["year", "month", "day", "date__gt", "gt", "date__lt", "lt"]class CorrespondentFilterSet(FilterSet):-class Meta:model = Correspondent- fields = {- "name": CHAR_KWARGS- }+ fields = {"name": CHAR_KWARGS}class TagFilterSet(FilterSet):-class Meta:model = Tag- fields = {- "name": CHAR_KWARGS- }+ fields = {"name": CHAR_KWARGS}class DocumentTypeFilterSet(FilterSet):-class Meta:model = DocumentType- fields = {- "name": CHAR_KWARGS- }+ fields = {"name": CHAR_KWARGS}class TagsFilter(Filter):-def __init__(self, exclude=False, in_list=False):super(TagsFilter, self).__init__()self.exclude = exclude@@ -48,7 +38,7 @@ class TagsFilter(Filter):return qstry:- tag_ids = [int(x) for x in value.split(',')]+ tag_ids = [int(x) for x in value.split(",")]except ValueError:return qs@@ -65,22 +55,19 @@ class TagsFilter(Filter):class InboxFilter(Filter):-def filter(self, qs, value):- if value == 'true':+ if value == "true":return qs.filter(tags__is_inbox_tag=True)- elif value == 'false':+ elif value == "false":return qs.exclude(tags__is_inbox_tag=True)else:return qsclass TitleContentFilter(Filter):-def filter(self, qs, value):if value:- return qs.filter(Q(title__icontains=value) |- Q(content__icontains=value))+ return qs.filter(Q(title__icontains=value) | Q(content__icontains=value))else:return qs@@ -88,10 +75,7 @@ class TitleContentFilter(Filter):class DocumentFilterSet(FilterSet):is_tagged = BooleanFilter(- label="Is tagged",- field_name="tags",- lookup_expr="isnull",- exclude=True+ label="Is tagged", field_name="tags", lookup_expr="isnull", exclude=True)tags__id__all = TagsFilter()@@ -107,38 +91,24 @@ class DocumentFilterSet(FilterSet):class Meta:model = Documentfields = {-"title": CHAR_KWARGS,"content": CHAR_KWARGS,-"archive_serial_number": INT_KWARGS,-"created": DATE_KWARGS,"added": DATE_KWARGS,"modified": DATE_KWARGS,-"correspondent": ["isnull"],"correspondent__id": ID_KWARGS,"correspondent__name": CHAR_KWARGS,-"tags__id": ID_KWARGS,"tags__name": CHAR_KWARGS,-"document_type": ["isnull"],"document_type__id": ID_KWARGS,"document_type__name": CHAR_KWARGS,-}class LogFilterSet(FilterSet):-class Meta:model = Log- fields = {-- "level": INT_KWARGS,- "created": DATE_KWARGS,- "group": ID_KWARGS-- }+ fields = {"level": INT_KWARGS, "created": DATE_KWARGS, "group": ID_KWARGS}diff --git a/src/documents/index.py b/src/documents/index.pyindex cb302da45..2c708105c 100644--- a/src/documents/index.py+++ b/src/documents/index.py@@ -21,51 +21,22 @@ logger = logging.getLogger("paperless.index")def get_schema():return Schema(- id=NUMERIC(- stored=True,- unique=True- ),- title=TEXT(- sortable=True- ),+ id=NUMERIC(stored=True, unique=True),+ title=TEXT(sortable=True),content=TEXT(),- asn=NUMERIC(- sortable=True- ),-- correspondent=TEXT(- sortable=True- ),+ asn=NUMERIC(sortable=True),+ correspondent=TEXT(sortable=True),correspondent_id=NUMERIC(),has_correspondent=BOOLEAN(),-- tag=KEYWORD(- commas=True,- scorable=True,- lowercase=True- ),- tag_id=KEYWORD(- commas=True,- scorable=True- ),+ tag=KEYWORD(commas=True, scorable=True, lowercase=True),+ tag_id=KEYWORD(commas=True, scorable=True),has_tag=BOOLEAN(),-- type=TEXT(- sortable=True- ),+ type=TEXT(sortable=True),type_id=NUMERIC(),has_type=BOOLEAN(),-- created=DATETIME(- sortable=True- ),- modified=DATETIME(- sortable=True- ),- added=DATETIME(- sortable=True- ),-+ created=DATETIME(sortable=True),+ modified=DATETIME(sortable=True),+ added=DATETIME(sortable=True),)@@ -132,7 +103,7 @@ def remove_document(writer, doc):def remove_document_by_id(writer, doc_id):- writer.delete_by_term('id', doc_id)+ writer.delete_by_term("id", doc_id)def add_or_update_document(document):@@ -146,48 +117,47 @@ def remove_document_from_index(document):class DelayedQuery:-def _get_query(self):raise NotImplementedError()def _get_query_filter(self):criterias = []for k, v in self.query_params.items():- if k == 'correspondent__id':- criterias.append(query.Term('correspondent_id', v))- elif k == 'tags__id__all':+ if k == "correspondent__id":+ criterias.append(query.Term("correspondent_id", v))+ elif k == "tags__id__all":for tag_id in v.split(","):- criterias.append(query.Term('tag_id', tag_id))- elif k == 'document_type__id':- criterias.append(query.Term('type_id', v))- elif k == 'correspondent__isnull':+ criterias.append(query.Term("tag_id", tag_id))+ elif k == "document_type__id":+ criterias.append(query.Term("type_id", v))+ elif k == "correspondent__isnull":criterias.append(query.Term("has_correspondent", v == "false"))- elif k == 'is_tagged':+ elif k == "is_tagged":criterias.append(query.Term("has_tag", v == "true"))- elif k == 'document_type__isnull':+ elif k == "document_type__isnull":criterias.append(query.Term("has_type", v == "false"))- elif k == 'created__date__lt':- criterias.append(- query.DateRange("created", start=None, end=isoparse(v)))- elif k == 'created__date__gt':+ elif k == "created__date__lt":criterias.append(- query.DateRange("created", start=isoparse(v), end=None))- elif k == 'added__date__gt':+ query.DateRange("created", start=None, end=isoparse(v))+ )+ elif k == "created__date__gt":criterias.append(- query.DateRange("added", start=isoparse(v), end=None))- elif k == 'added__date__lt':- criterias.append(- query.DateRange("added", start=None, end=isoparse(v)))+ query.DateRange("created", start=isoparse(v), end=None)+ )+ elif k == "added__date__gt":+ criterias.append(query.DateRange("added", start=isoparse(v), end=None))+ elif k == "added__date__lt":+ criterias.append(query.DateRange("added", start=None, end=isoparse(v)))if len(criterias) > 0:return query.And(criterias)else:return Nonedef _get_query_sortedby(self):- if 'ordering' not in self.query_params:+ if "ordering" not in self.query_params:return None, False- field: str = self.query_params['ordering']+ field: str = self.query_params["ordering"]sort_fields_map = {"created": "created",@@ -196,10 +166,10 @@ class DelayedQuery:"title": "title","correspondent__name": "correspondent","document_type__name": "type",- "archive_serial_number": "asn"+ "archive_serial_number": "asn",}- if field.startswith('-'):+ if field.startswith("-"):field = field[1:]reverse = Trueelse:@@ -235,24 +205,23 @@ class DelayedQuery:pagenum=math.floor(item.start / self.page_size) + 1,pagelen=self.page_size,sortedby=sortedby,- reverse=reverse+ reverse=reverse,)- page.results.fragmenter = highlight.ContextFragmenter(- surround=50)+ page.results.fragmenter = highlight.ContextFragmenter(surround=50)page.results.formatter = HtmlFormatter(tagname="span", between=" ... ")- if (not self.first_score and- len(page.results) > 0 and- sortedby is None):+ if not self.first_score and len(page.results) > 0 and sortedby is None:self.first_score = page.results[0].score- page.results.top_n = list(map(- lambda hit: (- (hit[0] / self.first_score) if self.first_score else None,- hit[1]- ),- page.results.top_n- ))+ page.results.top_n = list(+ map(+ lambda hit: (+ (hit[0] / self.first_score) if self.first_score else None,+ hit[1],+ ),+ page.results.top_n,+ )+ )self.saved_results[item.start] = page@@ -260,12 +229,12 @@ class DelayedQuery:class DelayedFullTextQuery(DelayedQuery):-def _get_query(self):- q_str = self.query_params['query']+ q_str = self.query_params["query"]qp = MultifieldParser(["content", "title", "correspondent", "tag", "type"],- self.searcher.ixreader.schema)+ self.searcher.ixreader.schema,+ )qp.add_plugin(DateParserPlugin())q = qp.parse(q_str)@@ -277,18 +246,17 @@ class DelayedFullTextQuery(DelayedQuery):class DelayedMoreLikeThisQuery(DelayedQuery):-def _get_query(self):- more_like_doc_id = int(self.query_params['more_like_id'])+ more_like_doc_id = int(self.query_params["more_like_id"])content = Document.objects.get(id=more_like_doc_id).contentdocnum = self.searcher.document_number(id=more_like_doc_id)kts = self.searcher.key_terms_from_text(- 'content', content, numterms=20,- model=classify.Bo1Model, normalize=False)+ "content", content, numterms=20, model=classify.Bo1Model, normalize=False+ )q = query.Or(- [query.Term('content', word, boost=weight)- for word, weight in kts])+ [query.Term("content", word, boost=weight) for word, weight in kts]+ )mask = {docnum}return q, mask@@ -298,6 +266,7 @@ def autocomplete(ix, term, limit=10):with ix.reader() as reader:terms = []for (score, t) in reader.most_distinctive_terms(- "content", number=limit, prefix=term.lower()):+ "content", number=limit, prefix=term.lower()+ ):terms.append(t)return termsdiff --git a/src/documents/loggers.py b/src/documents/loggers.pyindex 90f6770e0..78a2f3692 100644--- a/src/documents/loggers.py+++ b/src/documents/loggers.py@@ -17,12 +17,7 @@ class LoggingMixin:if self.logging_name:logger = logging.getLogger(self.logging_name)else:- name = ".".join([- self.__class__.__module__,- self.__class__.__name__- ])+ name = ".".join([self.__class__.__module__, self.__class__.__name__])logger = logging.getLogger(name)- getattr(logger, level)(message, extra={- "group": self.logging_group- }, **kwargs)+ getattr(logger, level)(message, extra={"group": self.logging_group}, **kwargs)diff --git a/src/documents/management/commands/decrypt_documents.py b/src/documents/management/commands/decrypt_documents.pyindex 8f5c2e123..38b49b489 100644--- a/src/documents/management/commands/decrypt_documents.py+++ b/src/documents/management/commands/decrypt_documents.py@@ -19,7 +19,7 @@ class Command(BaseCommand):parser.add_argument("--passphrase",help="If PAPERLESS_PASSPHRASE isn't set already, you need to "- "specify it here"+ "specify it here",)def handle(self, *args, **options):@@ -50,12 +50,12 @@ class Command(BaseCommand):def __gpg_to_unencrypted(passphrase):encrypted_files = Document.objects.filter(- storage_type=Document.STORAGE_TYPE_GPG)+ storage_type=Document.STORAGE_TYPE_GPG+ )for document in encrypted_files:- print("Decrypting {}".format(- document).encode('utf-8'))+ print("Decrypting {}".format(document).encode("utf-8"))old_paths = [document.source_path, document.thumbnail_path]@@ -66,10 +66,11 @@ class Command(BaseCommand):ext = os.path.splitext(document.filename)[1]- if not ext == '.gpg':+ if not ext == ".gpg":raise CommandError(f"Abort: encrypted file {document.source_path} does not "- f"end with .gpg")+ f"end with .gpg"+ )document.filename = os.path.splitext(document.filename)[0]@@ -80,7 +81,8 @@ class Command(BaseCommand):f.write(raw_thumb)Document.objects.filter(id=document.id).update(- storage_type=document.storage_type, filename=document.filename)+ storage_type=document.storage_type, filename=document.filename+ )for path in old_paths:os.unlink(path)diff --git a/src/documents/management/commands/document_archiver.py b/src/documents/management/commands/document_archiver.pyindex 96ddebe77..7b1f989f8 100644--- a/src/documents/management/commands/document_archiver.py+++ b/src/documents/management/commands/document_archiver.py@@ -16,8 +16,7 @@ from whoosh.writing import AsyncWriterfrom documents.models import Documentfrom ... import index-from ...file_handling import create_source_path_directory, \- generate_unique_filename+from ...file_handling import create_source_path_directory, generate_unique_filenamefrom ...parsers import get_parser_class_for_mime_type@@ -32,51 +31,49 @@ def handle_document(document_id):parser_class = get_parser_class_for_mime_type(mime_type)if not parser_class:- logger.error(f"No parser found for mime type {mime_type}, cannot "- f"archive document {document} (ID: {document_id})")+ logger.error(+ f"No parser found for mime type {mime_type}, cannot "+ f"archive document {document} (ID: {document_id})"+ )returnparser = parser_class(logging_group=uuid.uuid4())try:- parser.parse(- document.source_path,- mime_type,- document.get_public_filename())+ parser.parse(document.source_path, mime_type, document.get_public_filename())thumbnail = parser.get_optimised_thumbnail(- document.source_path,- mime_type,- document.get_public_filename()+ document.source_path, mime_type, document.get_public_filename())if parser.get_archive_path():with transaction.atomic():- with open(parser.get_archive_path(), 'rb') as f:+ with open(parser.get_archive_path(), "rb") as f:checksum = hashlib.md5(f.read()).hexdigest()# I'm going to save first so that in case the file move# fails, the database is rolled back.# We also don't use save() since that triggers the filehandling# logic, and we don't want that yet (file not yet in place)document.archive_filename = generate_unique_filename(- document, archive_filename=True)+ document, archive_filename=True+ )Document.objects.filter(pk=document.pk).update(archive_checksum=checksum,content=parser.get_text(),- archive_filename=document.archive_filename+ archive_filename=document.archive_filename,)with FileLock(settings.MEDIA_LOCK):create_source_path_directory(document.archive_path)- shutil.move(parser.get_archive_path(),- document.archive_path)+ shutil.move(parser.get_archive_path(), document.archive_path)shutil.move(thumbnail, document.thumbnail_path)with index.open_index_writer() as writer:index.update_document(writer, document)except Exception as e:- logger.exception(f"Error while parsing document {document} "- f"(ID: {document_id})")+ logger.exception(+ f"Error while parsing document {document} " f"(ID: {document_id})"+ )finally:parser.cleanup()@@ -88,29 +85,33 @@ class Command(BaseCommand):and document types to all documents, effectively allowing you toback-tag all previously indexed documents with metadata created (ormodified) after their initial import.- """.replace(" ", "")+ """.replace(+ " ", ""+ )def add_arguments(self, parser):parser.add_argument(- "-f", "--overwrite",+ "-f",+ "--overwrite",default=False,action="store_true",help="Recreates the archived document for documents that already "- "have an archived version."+ "have an archived version.",)parser.add_argument(- "-d", "--document",+ "-d",+ "--document",default=None,type=int,required=False,help="Specify the ID of a document, and this command will only "- "run on this specific document."+ "run on this specific document.",)parser.add_argument("--no-progress-bar",default=False,action="store_true",- help="If set, the progress bar will not be shown"+ help="If set, the progress bar will not be shown",)def handle(self, *args, **options):@@ -119,18 +120,17 @@ class Command(BaseCommand):overwrite = options["overwrite"]- if options['document']:- documents = Document.objects.filter(pk=options['document'])+ if options["document"]:+ documents = Document.objects.filter(pk=options["document"])else:documents = Document.objects.all()- document_ids = list(map(- lambda doc: doc.id,- filter(- lambda d: overwrite or not d.has_archive_version,- documents+ document_ids = list(+ map(+ lambda doc: doc.id,+ filter(lambda d: overwrite or not d.has_archive_version, documents),)- ))+ )# Note to future self: this prevents django from reusing database# conncetions between processes, which is bad and does not work@@ -141,13 +141,12 @@ class Command(BaseCommand):logging.getLogger().handlers[0].level = logging.ERRORwith multiprocessing.Pool(processes=settings.TASK_WORKERS) as pool:- list(tqdm.tqdm(- pool.imap_unordered(- handle_document,- document_ids- ),- total=len(document_ids),- disable=options['no_progress_bar']- ))+ list(+ tqdm.tqdm(+ pool.imap_unordered(handle_document, document_ids),+ total=len(document_ids),+ disable=options["no_progress_bar"],+ )+ )except KeyboardInterrupt:print("Aborting...")diff --git a/src/documents/management/commands/document_consumer.py b/src/documents/management/commands/document_consumer.pyindex eb8c57c84..c35594b8c 100644--- a/src/documents/management/commands/document_consumer.py+++ b/src/documents/management/commands/document_consumer.py@@ -23,24 +23,21 @@ logger = logging.getLogger("paperless.management.consumer")def _tags_from_path(filepath):"""Walk up the directory tree from filepath to CONSUMPTION_DIR- and get or create Tag IDs for every directory.+ and get or create Tag IDs for every directory."""tag_ids = set()- path_parts = Path(filepath).relative_to(- settings.CONSUMPTION_DIR).parent.parts+ path_parts = Path(filepath).relative_to(settings.CONSUMPTION_DIR).parent.partsfor part in path_parts:- tag_ids.add(Tag.objects.get_or_create(name__iexact=part, defaults={- "name": part- })[0].pk)+ tag_ids.add(+ Tag.objects.get_or_create(name__iexact=part, defaults={"name": part})[0].pk+ )return tag_idsdef _is_ignored(filepath: str) -> bool:- filepath_relative = PurePath(filepath).relative_to(- settings.CONSUMPTION_DIR)- return any(- filepath_relative.match(p) for p in settings.CONSUMER_IGNORE_PATTERNS)+ filepath_relative = PurePath(filepath).relative_to(settings.CONSUMPTION_DIR)+ return any(filepath_relative.match(p) for p in settings.CONSUMER_IGNORE_PATTERNS)def _consume(filepath):@@ -48,13 +45,11 @@ def _consume(filepath):returnif not os.path.isfile(filepath):- logger.debug(- f"Not consuming file {filepath}: File has moved.")+ logger.debug(f"Not consuming file {filepath}: File has moved.")returnif not is_file_ext_supported(os.path.splitext(filepath)[1]):- logger.warning(- f"Not consuming file {filepath}: Unknown file extension.")+ logger.warning(f"Not consuming file {filepath}: Unknown file extension.")returntag_ids = None@@ -66,10 +61,12 @@ def _consume(filepath):try:logger.info(f"Adding {filepath} to the task queue.")- async_task("documents.tasks.consume_file",- filepath,- override_tag_ids=tag_ids if tag_ids else None,- task_name=os.path.basename(filepath)[:100])+ async_task(+ "documents.tasks.consume_file",+ filepath,+ override_tag_ids=tag_ids if tag_ids else None,+ task_name=os.path.basename(filepath)[:100],+ )except Exception as e:# Catch all so that the consumer won't crash.# This is also what the test case is listening for to check for@@ -88,8 +85,9 @@ def _consume_wait_unmodified(file):try:new_mtime = os.stat(file).st_mtimeexcept FileNotFoundError:- logger.debug(f"File {file} moved while waiting for it to remain "- f"unmodified.")+ logger.debug(+ f"File {file} moved while waiting for it to remain " f"unmodified."+ )returnif new_mtime == mtime:_consume(file)@@ -102,16 +100,11 @@ def _consume_wait_unmodified(file):class Handler(FileSystemEventHandler):-def on_created(self, event):- Thread(- target=_consume_wait_unmodified, args=(event.src_path,)- ).start()+ Thread(target=_consume_wait_unmodified, args=(event.src_path,)).start()def on_moved(self, event):- Thread(- target=_consume_wait_unmodified, args=(event.dest_path,)- ).start()+ Thread(target=_consume_wait_unmodified, args=(event.dest_path,)).start()class Command(BaseCommand):@@ -130,26 +123,19 @@ class Command(BaseCommand):"directory",default=settings.CONSUMPTION_DIR,nargs="?",- help="The consumption directory."- )- parser.add_argument(- "--oneshot",- action="store_true",- help="Run only once."+ help="The consumption directory.",)+ parser.add_argument("--oneshot", action="store_true", help="Run only once.")def handle(self, *args, **options):directory = options["directory"]recursive = settings.CONSUMER_RECURSIVEif not directory:- raise CommandError(- "CONSUMPTION_DIR does not appear to be set."- )+ raise CommandError("CONSUMPTION_DIR does not appear to be set.")if not os.path.isdir(directory):- raise CommandError(- f"Consumption directory {directory} does not exist")+ raise CommandError(f"Consumption directory {directory} does not exist")if recursive:for dirpath, _, filenames in os.walk(directory):@@ -171,8 +157,7 @@ class Command(BaseCommand):logger.debug("Consumer exiting.")def handle_polling(self, directory, recursive):- logger.info(- f"Polling directory for changes: {directory}")+ logger.info(f"Polling directory for changes: {directory}")self.observer = PollingObserver(timeout=settings.CONSUMER_POLLING)self.observer.schedule(Handler(), directory, recursive=recursive)self.observer.start()@@ -186,8 +171,7 @@ class Command(BaseCommand):self.observer.join()def handle_inotify(self, directory, recursive):- logger.info(- f"Using inotify to watch directory for changes: {directory}")+ logger.info(f"Using inotify to watch directory for changes: {directory}")inotify = INotify()inotify_flags = flags.CLOSE_WRITE | flags.MOVED_TOdiff --git a/src/documents/management/commands/document_create_classifier.py b/src/documents/management/commands/document_create_classifier.pyindex a4ede88b5..6ad3ee9f5 100644--- a/src/documents/management/commands/document_create_classifier.py+++ b/src/documents/management/commands/document_create_classifier.py@@ -8,7 +8,9 @@ class Command(BaseCommand):help = """Trains the classifier on your data and saves the resulting models to afile. The document consumer will then automatically use this new model.- """.replace(" ", "")+ """.replace(+ " ", ""+ )def __init__(self, *args, **kwargs):BaseCommand.__init__(self, *args, **kwargs)diff --git a/src/documents/management/commands/document_exporter.py b/src/documents/management/commands/document_exporter.pyindex 217877397..c3dce40cc 100644--- a/src/documents/management/commands/document_exporter.py+++ b/src/documents/management/commands/document_exporter.py@@ -12,10 +12,19 @@ from django.core.management.base import BaseCommand, CommandErrorfrom django.db import transactionfrom filelock import FileLock-from documents.models import Document, Correspondent, Tag, DocumentType, \- SavedView, SavedViewFilterRule-from documents.settings import EXPORTER_FILE_NAME, EXPORTER_THUMBNAIL_NAME, \- EXPORTER_ARCHIVE_NAME+from documents.models import (+ Document,+ Correspondent,+ Tag,+ DocumentType,+ SavedView,+ SavedViewFilterRule,+)+from documents.settings import (+ EXPORTER_FILE_NAME,+ EXPORTER_THUMBNAIL_NAME,+ EXPORTER_ARCHIVE_NAME,+)from paperless.db import GnuPGfrom paperless_mail.models import MailAccount, MailRulefrom ...file_handling import generate_filename, delete_empty_directories@@ -27,41 +36,46 @@ class Command(BaseCommand):Decrypt and rename all files in our collection into a given targetdirectory. And include a manifest file containing document data foreasy import.- """.replace(" ", "")+ """.replace(+ " ", ""+ )def add_arguments(self, parser):parser.add_argument("target")parser.add_argument(- "-c", "--compare-checksums",+ "-c",+ "--compare-checksums",default=False,action="store_true",help="Compare file checksums when determining whether to export "- "a file or not. If not specified, file size and time "- "modified is used instead."+ "a file or not. If not specified, file size and time "+ "modified is used instead.",)parser.add_argument(- "-f", "--use-filename-format",+ "-f",+ "--use-filename-format",default=False,action="store_true",help="Use PAPERLESS_FILENAME_FORMAT for storing files in the "- "export directory, if configured."+ "export directory, if configured.",)parser.add_argument(- "-d", "--delete",+ "-d",+ "--delete",default=False,action="store_true",help="After exporting, delete files in the export directory that "- "do not belong to the current export, such as files from "- "deleted documents."+ "do not belong to the current export, such as files from "+ "deleted documents.",)parser.add_argument("--no-progress-bar",default=False,action="store_true",- help="If set, the progress bar will not be shown"+ help="If set, the progress bar will not be shown",)def __init__(self, *args, **kwargs):@@ -76,9 +90,9 @@ class Command(BaseCommand):def handle(self, *args, **options):self.target = options["target"]- self.compare_checksums = options['compare_checksums']- self.use_filename_format = options['use_filename_format']- self.delete = options['delete']+ self.compare_checksums = options["compare_checksums"]+ self.use_filename_format = options["use_filename_format"]+ self.delete = options["delete"]if not os.path.exists(self.target):raise CommandError("That path doesn't exist")@@ -87,7 +101,7 @@ class Command(BaseCommand):raise CommandError("That path doesn't appear to be writable")with FileLock(settings.MEDIA_LOCK):- self.dump(options['no_progress_bar'])+ self.dump(options["no_progress_bar"])def dump(self, progress_bar_disable=False):# 1. Take a snapshot of what files exist in the current export folder@@ -100,43 +114,48 @@ class Command(BaseCommand):# documentswith transaction.atomic():manifest = json.loads(- serializers.serialize("json", Correspondent.objects.all()))+ serializers.serialize("json", Correspondent.objects.all())+ )- manifest += json.loads(serializers.serialize(- "json", Tag.objects.all()))+ manifest += json.loads(serializers.serialize("json", Tag.objects.all()))- manifest += json.loads(serializers.serialize(- "json", DocumentType.objects.all()))+ manifest += json.loads(+ serializers.serialize("json", DocumentType.objects.all())+ )documents = Document.objects.order_by("id")document_map = {d.pk: d for d in documents}- document_manifest = json.loads(- serializers.serialize("json", documents))+ document_manifest = json.loads(serializers.serialize("json", documents))manifest += document_manifest- manifest += json.loads(serializers.serialize(- "json", MailAccount.objects.all()))+ manifest += json.loads(+ serializers.serialize("json", MailAccount.objects.all())+ )- manifest += json.loads(serializers.serialize(- "json", MailRule.objects.all()))+ manifest += json.loads(+ serializers.serialize("json", MailRule.objects.all())+ )- manifest += json.loads(serializers.serialize(- "json", SavedView.objects.all()))+ manifest += json.loads(+ serializers.serialize("json", SavedView.objects.all())+ )- manifest += json.loads(serializers.serialize(- "json", SavedViewFilterRule.objects.all()))+ manifest += json.loads(+ serializers.serialize("json", SavedViewFilterRule.objects.all())+ )- manifest += json.loads(serializers.serialize(- "json", User.objects.all()))+ manifest += json.loads(serializers.serialize("json", User.objects.all()))# 3. Export files from each documentfor index, document_dict in tqdm.tqdm(enumerate(document_manifest),total=len(document_manifest),- disable=progress_bar_disable+ disable=progress_bar_disable,):# 3.1. store files unencrypted- document_dict["fields"]["storage_type"] = Document.STORAGE_TYPE_UNENCRYPTED # NOQA: E501+ document_dict["fields"][+ "storage_type"+ ] = Document.STORAGE_TYPE_UNENCRYPTED # NOQA: E501document = document_map[document_dict["pk"]]@@ -145,11 +164,10 @@ class Command(BaseCommand):while True:if self.use_filename_format:base_name = generate_filename(- document, counter=filename_counter,- append_gpg=False)+ document, counter=filename_counter, append_gpg=False+ )else:- base_name = document.get_public_filename(- counter=filename_counter)+ base_name = document.get_public_filename(counter=filename_counter)if base_name not in self.exported_files:self.exported_files.append(base_name)@@ -193,22 +211,19 @@ class Command(BaseCommand):f.write(GnuPG.decrypted(document.archive_path))os.utime(archive_target, times=(t, t))else:- self.check_and_copy(document.source_path,- document.checksum,- original_target)+ self.check_and_copy(+ document.source_path, document.checksum, original_target+ )- self.check_and_copy(document.thumbnail_path,- None,- thumbnail_target)+ self.check_and_copy(document.thumbnail_path, None, thumbnail_target)if archive_target:- self.check_and_copy(document.archive_path,- document.archive_checksum,- archive_target)+ self.check_and_copy(+ document.archive_path, document.archive_checksum, archive_target+ )# 4. write manifest to target forlder- manifest_path = os.path.abspath(- os.path.join(self.target, "manifest.json"))+ manifest_path = os.path.abspath(os.path.join(self.target, "manifest.json"))with open(manifest_path, "w") as f:json.dump(manifest, f, indent=2)@@ -222,8 +237,9 @@ class Command(BaseCommand):for f in self.files_in_export_dir:os.remove(f)- delete_empty_directories(os.path.abspath(os.path.dirname(f)),- os.path.abspath(self.target))+ delete_empty_directories(+ os.path.abspath(os.path.dirname(f)), os.path.abspath(self.target)+ )def check_and_copy(self, source, source_checksum, target):if os.path.abspath(target) in self.files_in_export_dir:diff --git a/src/documents/management/commands/document_importer.py b/src/documents/management/commands/document_importer.pyindex 4606b91fe..9d77c1033 100644--- a/src/documents/management/commands/document_importer.py+++ b/src/documents/management/commands/document_importer.py@@ -12,8 +12,11 @@ from django.db.models.signals import post_save, m2m_changedfrom filelock import FileLockfrom documents.models import Document-from documents.settings import EXPORTER_FILE_NAME, EXPORTER_THUMBNAIL_NAME, \- EXPORTER_ARCHIVE_NAME+from documents.settings import (+ EXPORTER_FILE_NAME,+ EXPORTER_THUMBNAIL_NAME,+ EXPORTER_ARCHIVE_NAME,+)from ...file_handling import create_source_path_directoryfrom ...signals.handlers import update_filename_and_move_files@@ -32,7 +35,9 @@ class Command(BaseCommand):help = """Using a manifest.json file, load the data from there, and import thedocuments it refers to.- """.replace(" ", "")+ """.replace(+ " ", ""+ )def add_arguments(self, parser):parser.add_argument("source")@@ -40,7 +45,7 @@ class Command(BaseCommand):"--no-progress-bar",default=False,action="store_true",- help="If set, the progress bar will not be shown"+ help="If set, the progress bar will not be shown",)def __init__(self, *args, **kwargs):@@ -67,26 +72,27 @@ class Command(BaseCommand):self.manifest = json.load(f)self._check_manifest()- with disable_signal(post_save,- receiver=update_filename_and_move_files,- sender=Document):- with disable_signal(m2m_changed,- receiver=update_filename_and_move_files,- sender=Document.tags.through):+ with disable_signal(+ post_save, receiver=update_filename_and_move_files, sender=Document+ ):+ with disable_signal(+ m2m_changed,+ receiver=update_filename_and_move_files,+ sender=Document.tags.through,+ ):# Fill up the database with whatever is in the manifestcall_command("loaddata", manifest_path)- self._import_files_from_manifest(options['no_progress_bar'])+ self._import_files_from_manifest(options["no_progress_bar"])print("Updating search index...")- call_command('document_index', 'reindex')+ call_command("document_index", "reindex")@staticmethoddef _check_manifest_exists(path):if not os.path.exists(path):raise CommandError(- "That directory doesn't appear to contain a manifest.json "- "file."+ "That directory doesn't appear to contain a manifest.json " "file.")def _check_manifest(self):@@ -98,15 +104,15 @@ class Command(BaseCommand):if EXPORTER_FILE_NAME not in record:raise CommandError(- 'The manifest file contains a record which does not '- 'refer to an actual document file.'+ "The manifest file contains a record which does not "+ "refer to an actual document file.")doc_file = record[EXPORTER_FILE_NAME]if not os.path.exists(os.path.join(self.source, doc_file)):raise CommandError('The manifest file refers to "{}" which does not '- 'appear to be in the source directory.'.format(doc_file)+ "appear to be in the source directory.".format(doc_file))if EXPORTER_ARCHIVE_NAME in record:@@ -125,14 +131,11 @@ class Command(BaseCommand):print("Copy files into paperless...")- manifest_documents = list(filter(- lambda r: r["model"] == "documents.document",- self.manifest))+ manifest_documents = list(+ filter(lambda r: r["model"] == "documents.document", self.manifest)+ )- for record in tqdm.tqdm(- manifest_documents,- disable=progress_bar_disable- ):+ for record in tqdm.tqdm(manifest_documents, disable=progress_bar_disable):document = Document.objects.get(pk=record["pk"])diff --git a/src/documents/management/commands/document_index.py b/src/documents/management/commands/document_index.pyindex d76f48293..3dd4d84ff 100644--- a/src/documents/management/commands/document_index.py+++ b/src/documents/management/commands/document_index.py@@ -9,17 +9,17 @@ class Command(BaseCommand):help = "Manages the document index."def add_arguments(self, parser):- parser.add_argument("command", choices=['reindex', 'optimize'])+ parser.add_argument("command", choices=["reindex", "optimize"])parser.add_argument("--no-progress-bar",default=False,action="store_true",- help="If set, the progress bar will not be shown"+ help="If set, the progress bar will not be shown",)def handle(self, *args, **options):with transaction.atomic():- if options['command'] == 'reindex':- index_reindex(progress_bar_disable=options['no_progress_bar'])- elif options['command'] == 'optimize':+ if options["command"] == "reindex":+ index_reindex(progress_bar_disable=options["no_progress_bar"])+ elif options["command"] == "optimize":index_optimize()diff --git a/src/documents/management/commands/document_renamer.py b/src/documents/management/commands/document_renamer.pyindex 682705c2d..221fb4208 100644--- a/src/documents/management/commands/document_renamer.py+++ b/src/documents/management/commands/document_renamer.py@@ -11,14 +11,16 @@ class Command(BaseCommand):help = """This will rename all documents to match the latest filename format.- """.replace(" ", "")+ """.replace(+ " ", ""+ )def add_arguments(self, parser):parser.add_argument("--no-progress-bar",default=False,action="store_true",- help="If set, the progress bar will not be shown"+ help="If set, the progress bar will not be shown",)def handle(self, *args, **options):@@ -26,7 +28,6 @@ class Command(BaseCommand):logging.getLogger().handlers[0].level = logging.ERRORfor document in tqdm.tqdm(- Document.objects.all(),- disable=options['no_progress_bar']+ Document.objects.all(), disable=options["no_progress_bar"]):post_save.send(Document, instance=document)diff --git a/src/documents/management/commands/document_retagger.py b/src/documents/management/commands/document_retagger.pyindex 6636af20a..fcf9e3478 100644--- a/src/documents/management/commands/document_retagger.py+++ b/src/documents/management/commands/document_retagger.py@@ -18,60 +18,46 @@ class Command(BaseCommand):and document types to all documents, effectively allowing you toback-tag all previously indexed documents with metadata created (ormodified) after their initial import.- """.replace(" ", "")+ """.replace(+ " ", ""+ )def add_arguments(self, parser):- parser.add_argument(- "-c", "--correspondent",- default=False,- action="store_true"- )- parser.add_argument(- "-T", "--tags",- default=False,- action="store_true"- )- parser.add_argument(- "-t", "--document_type",- default=False,- action="store_true"- )- parser.add_argument(- "-i", "--inbox-only",- default=False,- action="store_true"- )+ parser.add_argument("-c", "--correspondent", default=False, action="store_true")+ parser.add_argument("-T", "--tags", default=False, action="store_true")+ parser.add_argument("-t", "--document_type", default=False, action="store_true")+ parser.add_argument("-i", "--inbox-only", default=False, action="store_true")parser.add_argument("--use-first",default=False,action="store_true",help="By default this command won't try to assign a correspondent "- "if more than one matches the document. Use this flag if "- "you'd rather it just pick the first one it finds."+ "if more than one matches the document. Use this flag if "+ "you'd rather it just pick the first one it finds.",)parser.add_argument(- "-f", "--overwrite",+ "-f",+ "--overwrite",default=False,action="store_true",help="If set, the document retagger will overwrite any previously"- "set correspondent, document and remove correspondents, types"- "and tags that do not match anymore due to changed rules."+ "set correspondent, document and remove correspondents, types"+ "and tags that do not match anymore due to changed rules.",)parser.add_argument("--no-progress-bar",default=False,action="store_true",- help="If set, the progress bar will not be shown"+ help="If set, the progress bar will not be shown",)parser.add_argument("--suggest",default=False,action="store_true",- help="Return the suggestion, don't change anything."+ help="Return the suggestion, don't change anything.",)parser.add_argument(- "--base-url",- help="The base URL to use to build the link to the documents."+ "--base-url", help="The base URL to use to build the link to the documents.")def handle(self, *args, **options):@@ -86,38 +72,39 @@ class Command(BaseCommand):classifier = load_classifier()- for document in tqdm.tqdm(- documents,- disable=options['no_progress_bar']- ):+ for document in tqdm.tqdm(documents, disable=options["no_progress_bar"]):- if options['correspondent']:+ if options["correspondent"]:set_correspondent(sender=None,document=document,classifier=classifier,- replace=options['overwrite'],- use_first=options['use_first'],- suggest=options['suggest'],- base_url=options['base_url'],- color=color)+ replace=options["overwrite"],+ use_first=options["use_first"],+ suggest=options["suggest"],+ base_url=options["base_url"],+ color=color,+ )- if options['document_type']:- set_document_type(sender=None,- document=document,- classifier=classifier,- replace=options['overwrite'],- use_first=options['use_first'],- suggest=options['suggest'],- base_url=options['base_url'],- color=color)+ if options["document_type"]:+ set_document_type(+ sender=None,+ document=document,+ classifier=classifier,+ replace=options["overwrite"],+ use_first=options["use_first"],+ suggest=options["suggest"],+ base_url=options["base_url"],+ color=color,+ )- if options['tags']:+ if options["tags"]:set_tags(sender=None,document=document,classifier=classifier,- replace=options['overwrite'],- suggest=options['suggest'],- base_url=options['base_url'],- color=color)+ replace=options["overwrite"],+ suggest=options["suggest"],+ base_url=options["base_url"],+ color=color,+ )diff --git a/src/documents/management/commands/document_sanity_checker.py b/src/documents/management/commands/document_sanity_checker.pyindex fbbb8d600..54691fefe 100644--- a/src/documents/management/commands/document_sanity_checker.py+++ b/src/documents/management/commands/document_sanity_checker.py@@ -6,18 +6,20 @@ class Command(BaseCommand):help = """This command checks your document archive for issues.- """.replace(" ", "")+ """.replace(+ " ", ""+ )def add_arguments(self, parser):parser.add_argument("--no-progress-bar",default=False,action="store_true",- help="If set, the progress bar will not be shown"+ help="If set, the progress bar will not be shown",)def handle(self, *args, **options):- messages = check_sanity(progress=not options['no_progress_bar'])+ messages = check_sanity(progress=not options["no_progress_bar"])messages.log_messages()diff --git a/src/documents/management/commands/document_thumbnails.py b/src/documents/management/commands/document_thumbnails.pyindex 05f059039..9e2893b5f 100644--- a/src/documents/management/commands/document_thumbnails.py+++ b/src/documents/management/commands/document_thumbnails.py@@ -22,9 +22,7 @@ def _process_document(doc_in):try:thumb = parser.get_optimised_thumbnail(- document.source_path,- document.mime_type,- document.get_public_filename()+ document.source_path, document.mime_type, document.get_public_filename())shutil.move(thumb, document.thumbnail_path)@@ -36,29 +34,32 @@ class Command(BaseCommand):help = """This will regenerate the thumbnails for all documents.- """.replace(" ", "")+ """.replace(+ " ", ""+ )def add_arguments(self, parser):parser.add_argument(- "-d", "--document",+ "-d",+ "--document",default=None,type=int,required=False,help="Specify the ID of a document, and this command will only "- "run on this specific document."+ "run on this specific document.",)parser.add_argument("--no-progress-bar",default=False,action="store_true",- help="If set, the progress bar will not be shown"+ help="If set, the progress bar will not be shown",)def handle(self, *args, **options):logging.getLogger().handlers[0].level = logging.ERROR- if options['document']:- documents = Document.objects.filter(pk=options['document'])+ if options["document"]:+ documents = Document.objects.filter(pk=options["document"])else:documents = Document.objects.all()@@ -70,8 +71,10 @@ class Command(BaseCommand):db.connections.close_all()with multiprocessing.Pool() as pool:- list(tqdm.tqdm(- pool.imap_unordered(_process_document, ids),- total=len(ids),- disable=options['no_progress_bar']- ))+ list(+ tqdm.tqdm(+ pool.imap_unordered(_process_document, ids),+ total=len(ids),+ disable=options["no_progress_bar"],+ )+ )diff --git a/src/documents/management/commands/loaddata_stdin.py b/src/documents/management/commands/loaddata_stdin.pyindex 9cce7a047..23f75769b 100644--- a/src/documents/management/commands/loaddata_stdin.py+++ b/src/documents/management/commands/loaddata_stdin.py@@ -10,11 +10,11 @@ class Command(LoadDataCommand):"""def parse_name(self, fixture_name):- self.compression_formats['stdin'] = (lambda x, y: sys.stdin, None)- if fixture_name == '-':- return '-', 'json', 'stdin'+ self.compression_formats["stdin"] = (lambda x, y: sys.stdin, None)+ if fixture_name == "-":+ return "-", "json", "stdin"def find_fixtures(self, fixture_label):- if fixture_label == '-':- return [('-', None, '-')]+ if fixture_label == "-":+ return [("-", None, "-")]return super(Command, self).find_fixtures(fixture_label)diff --git a/src/documents/management/commands/manage_superuser.py b/src/documents/management/commands/manage_superuser.pyindex ef3635e52..f8cefb0d9 100644--- a/src/documents/management/commands/manage_superuser.py+++ b/src/documents/management/commands/manage_superuser.py@@ -12,16 +12,18 @@ class Command(BaseCommand):help = """Creates a Django superuser based on env variables.- """.replace(" ", "")+ """.replace(+ " ", ""+ )def handle(self, *args, **options):- username = os.getenv('PAPERLESS_ADMIN_USER')+ username = os.getenv("PAPERLESS_ADMIN_USER")if not username:return- mail = os.getenv('PAPERLESS_ADMIN_MAIL', 'root@localhost')- password = os.getenv('PAPERLESS_ADMIN_PASSWORD')+ mail = os.getenv("PAPERLESS_ADMIN_MAIL", "root@localhost")+ password = os.getenv("PAPERLESS_ADMIN_PASSWORD")# Check if user exists already, leave as is if it doesif User.objects.filter(username=username).exists():@@ -32,11 +34,10 @@ class Command(BaseCommand):elif password:# Create superuser based on env variablesUser.objects.create_superuser(username, mail, password)- self.stdout.write(- f'Created superuser "{username}" with provided password.')+ self.stdout.write(f'Created superuser "{username}" with provided password.')else:- self.stdout.write(- f'Did not create superuser "{username}".')+ self.stdout.write(f'Did not create superuser "{username}".')self.stdout.write('Make sure you specified "PAPERLESS_ADMIN_PASSWORD" in your '- '"docker-compose.env" file.')+ '"docker-compose.env" file.'+ )diff --git a/src/documents/matching.py b/src/documents/matching.pyindex a1f3896e5..2acd5b7f6 100644--- a/src/documents/matching.py+++ b/src/documents/matching.py@@ -12,7 +12,8 @@ def log_reason(matching_model, document, reason):class_name = type(matching_model).__name__logger.debug(f"{class_name} {matching_model.name} matched on document "- f"{document} because {reason}")+ f"{document} because {reason}"+ )def match_correspondents(document, classifier):@@ -23,9 +24,9 @@ def match_correspondents(document, classifier):correspondents = Correspondent.objects.all()- return list(filter(- lambda o: matches(o, document) or o.pk == pred_id,- correspondents))+ return list(+ filter(lambda o: matches(o, document) or o.pk == pred_id, correspondents)+ )def match_document_types(document, classifier):@@ -36,9 +37,9 @@ def match_document_types(document, classifier):document_types = DocumentType.objects.all()- return list(filter(- lambda o: matches(o, document) or o.pk == pred_id,- document_types))+ return list(+ filter(lambda o: matches(o, document) or o.pk == pred_id, document_types)+ )def match_tags(document, classifier):@@ -49,9 +50,9 @@ def match_tags(document, classifier):tags = Tag.objects.all()- return list(filter(- lambda o: matches(o, document) or o.pk in predicted_tag_ids,- tags))+ return list(+ filter(lambda o: matches(o, document) or o.pk in predicted_tag_ids, tags)+ )def matches(matching_model, document):@@ -68,73 +69,73 @@ def matches(matching_model, document):if matching_model.matching_algorithm == MatchingModel.MATCH_ALL:for word in _split_match(matching_model):- search_result = re.search(- rf"\b{word}\b", document_content, **search_kwargs)+ search_result = re.search(rf"\b{word}\b", document_content, **search_kwargs)if not search_result:return Falselog_reason(- matching_model, document,- f"it contains all of these words: {matching_model.match}"+ matching_model,+ document,+ f"it contains all of these words: {matching_model.match}",)return Trueelif matching_model.matching_algorithm == MatchingModel.MATCH_ANY:for word in _split_match(matching_model):if re.search(rf"\b{word}\b", document_content, **search_kwargs):- log_reason(- matching_model, document,- f"it contains this word: {word}"- )+ log_reason(matching_model, document, f"it contains this word: {word}")return Truereturn Falseelif matching_model.matching_algorithm == MatchingModel.MATCH_LITERAL:- result = bool(re.search(- rf"\b{re.escape(matching_model.match)}\b",- document_content,- **search_kwargs- ))+ result = bool(+ re.search(+ rf"\b{re.escape(matching_model.match)}\b",+ document_content,+ **search_kwargs,+ )+ )if result:log_reason(- matching_model, document,- f"it contains this string: \"{matching_model.match}\""+ matching_model,+ document,+ f'it contains this string: "{matching_model.match}"',)return resultelif matching_model.matching_algorithm == MatchingModel.MATCH_REGEX:try:match = re.search(- re.compile(matching_model.match, **search_kwargs),- document_content+ re.compile(matching_model.match, **search_kwargs), document_content)except re.error:logger.error(- f"Error while processing regular expression "- f"{matching_model.match}"+ f"Error while processing regular expression " f"{matching_model.match}")return Falseif match:log_reason(- matching_model, document,+ matching_model,+ document,f"the string {match.group()} matches the regular expression "- f"{matching_model.match}"+ f"{matching_model.match}",)return bool(match)elif matching_model.matching_algorithm == MatchingModel.MATCH_FUZZY:from fuzzywuzzy import fuzz- match = re.sub(r'[^\w\s]', '', matching_model.match)- text = re.sub(r'[^\w\s]', '', document_content)+ match = re.sub(r"[^\w\s]", "", matching_model.match)+ text = re.sub(r"[^\w\s]", "", document_content)if matching_model.is_insensitive:match = match.lower()text = text.lower()if fuzz.partial_ratio(match, text) >= 90:# TODO: make this betterlog_reason(- matching_model, document,+ matching_model,+ document,f"parts of the document content somehow match the string "- f"{matching_model.match}"+ f"{matching_model.match}",)return Trueelse:@@ -162,8 +163,6 @@ def _split_match(matching_model):normspace = re.compile(r"\s+").subreturn [# normspace(" ", (t[0] or t[1]).strip()).replace(" ", r"\s+")- re.escape(- normspace(" ", (t[0] or t[1]).strip())- ).replace(r"\ ", r"\s+")+ re.escape(normspace(" ", (t[0] or t[1]).strip())).replace(r"\ ", r"\s+")for t in findterms(matching_model.match)]diff --git a/src/documents/migrations/0001_initial.py b/src/documents/migrations/0001_initial.pyindex 4e7801267..a388ac7f2 100644--- a/src/documents/migrations/0001_initial.py+++ b/src/documents/migrations/0001_initial.py@@ -10,19 +10,33 @@ class Migration(migrations.Migration):initial = True- dependencies = [- ]+ dependencies = []operations = [migrations.CreateModel(- name='Document',+ name="Document",fields=[- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),- ('sender', models.CharField(blank=True, db_index=True, max_length=128)),- ('title', models.CharField(blank=True, db_index=True, max_length=128)),- ('content', models.TextField(db_index=("mysql" not in settings.DATABASES["default"]["ENGINE"]))),- ('created', models.DateTimeField(auto_now_add=True)),- ('modified', models.DateTimeField(auto_now=True)),+ (+ "id",+ models.AutoField(+ auto_created=True,+ primary_key=True,+ serialize=False,+ verbose_name="ID",+ ),+ ),+ ("sender", models.CharField(blank=True, db_index=True, max_length=128)),+ ("title", models.CharField(blank=True, db_index=True, max_length=128)),+ (+ "content",+ models.TextField(+ db_index=(+ "mysql" not in settings.DATABASES["default"]["ENGINE"]+ )+ ),+ ),+ ("created", models.DateTimeField(auto_now_add=True)),+ ("modified", models.DateTimeField(auto_now=True)),],),]diff --git a/src/documents/migrations/0002_auto_20151226_1316.py b/src/documents/migrations/0002_auto_20151226_1316.pyindex 5c1a78271..05d97d2c2 100644--- a/src/documents/migrations/0002_auto_20151226_1316.py+++ b/src/documents/migrations/0002_auto_20151226_1316.py@@ -9,17 +9,19 @@ import django.utils.timezoneclass Migration(migrations.Migration):dependencies = [- ('documents', '0001_initial'),+ ("documents", "0001_initial"),]operations = [migrations.AlterModelOptions(- name='document',- options={'ordering': ('sender', 'title')},+ name="document",+ options={"ordering": ("sender", "title")},),migrations.AlterField(- model_name='document',- name='created',- field=models.DateTimeField(default=django.utils.timezone.now, editable=False),+ model_name="document",+ name="created",+ field=models.DateTimeField(+ default=django.utils.timezone.now, editable=False+ ),),]diff --git a/src/documents/migrations/0003_sender.py b/src/documents/migrations/0003_sender.pyindex 27eead032..796571cd7 100644--- a/src/documents/migrations/0003_sender.py+++ b/src/documents/migrations/0003_sender.py@@ -19,9 +19,11 @@ def move_sender_strings_to_sender_model(apps, schema_editor):# Create the sender and log the relationship with the documentfor document in document_model.objects.all():if document.sender:- DOCUMENT_SENDER_MAP[document.pk], created = sender_model.objects.get_or_create(- name=document.sender,- defaults={"slug": slugify(document.sender)}+ (+ DOCUMENT_SENDER_MAP[document.pk],+ created,+ ) = sender_model.objects.get_or_create(+ name=document.sender, defaults={"slug": slugify(document.sender)})@@ -33,27 +35,39 @@ def realign_senders(apps, schema_editor):class Migration(migrations.Migration):dependencies = [- ('documents', '0002_auto_20151226_1316'),+ ("documents", "0002_auto_20151226_1316"),]operations = [migrations.CreateModel(- name='Sender',+ name="Sender",fields=[- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),- ('name', models.CharField(max_length=128, unique=True)),- ('slug', models.SlugField()),+ (+ "id",+ models.AutoField(+ auto_created=True,+ primary_key=True,+ serialize=False,+ verbose_name="ID",+ ),+ ),+ ("name", models.CharField(max_length=128, unique=True)),+ ("slug", models.SlugField()),],),migrations.RunPython(move_sender_strings_to_sender_model),migrations.RemoveField(- model_name='document',- name='sender',+ model_name="document",+ name="sender",),migrations.AddField(- model_name='document',- name='sender',- field=models.ForeignKey(blank=True, on_delete=django.db.models.deletion.CASCADE, to='documents.Sender'),+ model_name="document",+ name="sender",+ field=models.ForeignKey(+ blank=True,+ on_delete=django.db.models.deletion.CASCADE,+ to="documents.Sender",+ ),),migrations.RunPython(realign_senders),]diff --git a/src/documents/migrations/0004_auto_20160114_1844.py b/src/documents/migrations/0004_auto_20160114_1844.pyindex b9fa616ae..5d377a4a1 100644--- a/src/documents/migrations/0004_auto_20160114_1844.py+++ b/src/documents/migrations/0004_auto_20160114_1844.py@@ -9,13 +9,19 @@ import django.db.models.deletionclass Migration(migrations.Migration):dependencies = [- ('documents', '0003_sender'),+ ("documents", "0003_sender"),]operations = [migrations.AlterField(- model_name='document',- name='sender',- field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='documents', to='documents.Sender'),+ model_name="document",+ name="sender",+ field=models.ForeignKey(+ blank=True,+ null=True,+ on_delete=django.db.models.deletion.CASCADE,+ related_name="documents",+ to="documents.Sender",+ ),),]diff --git a/src/documents/migrations/0005_auto_20160123_0313.py b/src/documents/migrations/0005_auto_20160123_0313.pyindex a2c665b8c..893bf1d1f 100644--- a/src/documents/migrations/0005_auto_20160123_0313.py+++ b/src/documents/migrations/0005_auto_20160123_0313.py@@ -8,12 +8,12 @@ from django.db import migrationsclass Migration(migrations.Migration):dependencies = [- ('documents', '0004_auto_20160114_1844'),+ ("documents", "0004_auto_20160114_1844"),]operations = [migrations.AlterModelOptions(- name='sender',- options={'ordering': ('name',)},+ name="sender",+ options={"ordering": ("name",)},),]diff --git a/src/documents/migrations/0006_auto_20160123_0430.py b/src/documents/migrations/0006_auto_20160123_0430.pyindex b8d9979db..e8530f39a 100644--- a/src/documents/migrations/0006_auto_20160123_0430.py+++ b/src/documents/migrations/0006_auto_20160123_0430.py@@ -8,30 +8,59 @@ from django.db import migrations, modelsclass Migration(migrations.Migration):dependencies = [- ('documents', '0005_auto_20160123_0313'),+ ("documents", "0005_auto_20160123_0313"),]operations = [migrations.CreateModel(- name='Tag',+ name="Tag",fields=[- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),- ('name', models.CharField(max_length=128, unique=True)),- ('slug', models.SlugField(blank=True)),- ('colour', models.PositiveIntegerField(choices=[(1, '#a6cee3'), (2, '#1f78b4'), (3, '#b2df8a'), (4, '#33a02c'), (5, '#fb9a99'), (6, '#e31a1c'), (7, '#fdbf6f'), (8, '#ff7f00'), (9, '#cab2d6'), (10, '#6a3d9a'), (11, '#ffff99'), (12, '#b15928'), (13, '#000000'), (14, '#cccccc')], default=1)),+ (+ "id",+ models.AutoField(+ auto_created=True,+ primary_key=True,+ serialize=False,+ verbose_name="ID",+ ),+ ),+ ("name", models.CharField(max_length=128, unique=True)),+ ("slug", models.SlugField(blank=True)),+ (+ "colour",+ models.PositiveIntegerField(+ choices=[+ (1, "#a6cee3"),+ (2, "#1f78b4"),+ (3, "#b2df8a"),+ (4, "#33a02c"),+ (5, "#fb9a99"),+ (6, "#e31a1c"),+ (7, "#fdbf6f"),+ (8, "#ff7f00"),+ (9, "#cab2d6"),+ (10, "#6a3d9a"),+ (11, "#ffff99"),+ (12, "#b15928"),+ (13, "#000000"),+ (14, "#cccccc"),+ ],+ default=1,+ ),+ ),],options={- 'abstract': False,+ "abstract": False,},),migrations.AlterField(- model_name='sender',- name='slug',+ model_name="sender",+ name="slug",field=models.SlugField(blank=True),),migrations.AddField(- model_name='document',- name='tags',- field=models.ManyToManyField(related_name='documents', to='documents.Tag'),+ model_name="document",+ name="tags",+ field=models.ManyToManyField(related_name="documents", to="documents.Tag"),),]diff --git a/src/documents/migrations/0007_auto_20160126_2114.py b/src/documents/migrations/0007_auto_20160126_2114.pyindex f3021fc0a..e7e273611 100644--- a/src/documents/migrations/0007_auto_20160126_2114.py+++ b/src/documents/migrations/0007_auto_20160126_2114.py@@ -8,23 +8,50 @@ from django.db import migrations, modelsclass Migration(migrations.Migration):dependencies = [- ('documents', '0006_auto_20160123_0430'),+ ("documents", "0006_auto_20160123_0430"),]operations = [migrations.AddField(- model_name='tag',- name='match',+ model_name="tag",+ name="match",field=models.CharField(blank=True, max_length=256),),migrations.AddField(- model_name='tag',- name='matching_algorithm',- field=models.PositiveIntegerField(blank=True, choices=[(1, 'Any'), (2, 'All'), (3, 'Literal'), (4, 'Regular Expression')], help_text='Which algorithm you want to use when matching text to the OCR\'d PDF. Here, "any" looks for any occurrence of any word provided in the PDF, while "all" requires that every word provided appear in the PDF, albeit not in the order provided. A "literal" match means that the text you enter must appear in the PDF exactly as you\'ve entered it, and "regular expression" uses a regex to match the PDF. If you don\'t know what a regex is, you probably don\'t want this option.', null=True),+ model_name="tag",+ name="matching_algorithm",+ field=models.PositiveIntegerField(+ blank=True,+ choices=[+ (1, "Any"),+ (2, "All"),+ (3, "Literal"),+ (4, "Regular Expression"),+ ],+ help_text='Which algorithm you want to use when matching text to the OCR\'d PDF. Here, "any" looks for any occurrence of any word provided in the PDF, while "all" requires that every word provided appear in the PDF, albeit not in the order provided. A "literal" match means that the text you enter must appear in the PDF exactly as you\'ve entered it, and "regular expression" uses a regex to match the PDF. If you don\'t know what a regex is, you probably don\'t want this option.',+ null=True,+ ),),migrations.AlterField(- model_name='tag',- name='colour',- field=models.PositiveIntegerField(choices=[(1, '#a6cee3'), (2, '#1f78b4'), (3, '#b2df8a'), (4, '#33a02c'), (5, '#fb9a99'), (6, '#e31a1c'), (7, '#fdbf6f'), (8, '#ff7f00'), (9, '#cab2d6'), (10, '#6a3d9a'), (11, '#b15928'), (12, '#000000'), (13, '#cccccc')], default=1),+ model_name="tag",+ name="colour",+ field=models.PositiveIntegerField(+ choices=[+ (1, "#a6cee3"),+ (2, "#1f78b4"),+ (3, "#b2df8a"),+ (4, "#33a02c"),+ (5, "#fb9a99"),+ (6, "#e31a1c"),+ (7, "#fdbf6f"),+ (8, "#ff7f00"),+ (9, "#cab2d6"),+ (10, "#6a3d9a"),+ (11, "#b15928"),+ (12, "#000000"),+ (13, "#cccccc"),+ ],+ default=1,+ ),),]diff --git a/src/documents/migrations/0008_document_file_type.py b/src/documents/migrations/0008_document_file_type.pyindex 1d542bea7..a6770d9f7 100644--- a/src/documents/migrations/0008_document_file_type.py+++ b/src/documents/migrations/0008_document_file_type.py@@ -8,20 +8,32 @@ from django.db import migrations, modelsclass Migration(migrations.Migration):dependencies = [- ('documents', '0007_auto_20160126_2114'),+ ("documents", "0007_auto_20160126_2114"),]operations = [migrations.AddField(- model_name='document',- name='file_type',- field=models.CharField(choices=[('pdf', 'PDF'), ('png', 'PNG'), ('jpg', 'JPG'), ('gif', 'GIF'), ('tiff', 'TIFF')], default='pdf', editable=False, max_length=4),+ model_name="document",+ name="file_type",+ field=models.CharField(+ choices=[+ ("pdf", "PDF"),+ ("png", "PNG"),+ ("jpg", "JPG"),+ ("gif", "GIF"),+ ("tiff", "TIFF"),+ ],+ default="pdf",+ editable=False,+ max_length=4,+ ),preserve_default=False,),migrations.AlterField(- model_name='document',- name='tags',- field=models.ManyToManyField(blank=True, related_name='documents', to='documents.Tag'),+ model_name="document",+ name="tags",+ field=models.ManyToManyField(+ blank=True, related_name="documents", to="documents.Tag"+ ),),]-diff --git a/src/documents/migrations/0009_auto_20160214_0040.py b/src/documents/migrations/0009_auto_20160214_0040.pyindex 31be6d633..5d833c1f2 100644--- a/src/documents/migrations/0009_auto_20160214_0040.py+++ b/src/documents/migrations/0009_auto_20160214_0040.py@@ -8,13 +8,22 @@ from django.db import migrations, modelsclass Migration(migrations.Migration):dependencies = [- ('documents', '0008_document_file_type'),+ ("documents", "0008_document_file_type"),]operations = [migrations.AlterField(- model_name='tag',- name='matching_algorithm',- field=models.PositiveIntegerField(choices=[(1, 'Any'), (2, 'All'), (3, 'Literal'), (4, 'Regular Expression')], default=1, help_text='Which algorithm you want to use when matching text to the OCR\'d PDF. Here, "any" looks for any occurrence of any word provided in the PDF, while "all" requires that every word provided appear in the PDF, albeit not in the order provided. A "literal" match means that the text you enter must appear in the PDF exactly as you\'ve entered it, and "regular expression" uses a regex to match the PDF. If you don\'t know what a regex is, you probably don\'t want this option.'),+ model_name="tag",+ name="matching_algorithm",+ field=models.PositiveIntegerField(+ choices=[+ (1, "Any"),+ (2, "All"),+ (3, "Literal"),+ (4, "Regular Expression"),+ ],+ default=1,+ help_text='Which algorithm you want to use when matching text to the OCR\'d PDF. Here, "any" looks for any occurrence of any word provided in the PDF, while "all" requires that every word provided appear in the PDF, albeit not in the order provided. A "literal" match means that the text you enter must appear in the PDF exactly as you\'ve entered it, and "regular expression" uses a regex to match the PDF. If you don\'t know what a regex is, you probably don\'t want this option.',+ ),),]diff --git a/src/documents/migrations/0010_log.py b/src/documents/migrations/0010_log.pyindex 57cf804b7..b51aebc62 100644--- a/src/documents/migrations/0010_log.py+++ b/src/documents/migrations/0010_log.py@@ -8,23 +8,48 @@ from django.db import migrations, modelsclass Migration(migrations.Migration):dependencies = [- ('documents', '0009_auto_20160214_0040'),+ ("documents", "0009_auto_20160214_0040"),]operations = [migrations.CreateModel(- name='Log',+ name="Log",fields=[- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),- ('group', models.UUIDField(blank=True)),- ('message', models.TextField()),- ('level', models.PositiveIntegerField(choices=[(10, 'Debugging'), (20, 'Informational'), (30, 'Warning'), (40, 'Error'), (50, 'Critical')], default=20)),- ('component', models.PositiveIntegerField(choices=[(1, 'Consumer'), (2, 'Mail Fetcher')])),- ('created', models.DateTimeField(auto_now_add=True)),- ('modified', models.DateTimeField(auto_now=True)),+ (+ "id",+ models.AutoField(+ auto_created=True,+ primary_key=True,+ serialize=False,+ verbose_name="ID",+ ),+ ),+ ("group", models.UUIDField(blank=True)),+ ("message", models.TextField()),+ (+ "level",+ models.PositiveIntegerField(+ choices=[+ (10, "Debugging"),+ (20, "Informational"),+ (30, "Warning"),+ (40, "Error"),+ (50, "Critical"),+ ],+ default=20,+ ),+ ),+ (+ "component",+ models.PositiveIntegerField(+ choices=[(1, "Consumer"), (2, "Mail Fetcher")]+ ),+ ),+ ("created", models.DateTimeField(auto_now_add=True)),+ ("modified", models.DateTimeField(auto_now=True)),],options={- 'ordering': ('-modified',),+ "ordering": ("-modified",),},),]diff --git a/src/documents/migrations/0011_auto_20160303_1929.py b/src/documents/migrations/0011_auto_20160303_1929.pyindex 7b77a8835..643212888 100644--- a/src/documents/migrations/0011_auto_20160303_1929.py+++ b/src/documents/migrations/0011_auto_20160303_1929.py@@ -8,21 +8,21 @@ from django.db import migrationsclass Migration(migrations.Migration):atomic = Falsedependencies = [- ('documents', '0010_log'),+ ("documents", "0010_log"),]operations = [migrations.RenameModel(- old_name='Sender',- new_name='Correspondent',+ old_name="Sender",+ new_name="Correspondent",),migrations.AlterModelOptions(- name='document',- options={'ordering': ('correspondent', 'title')},+ name="document",+ options={"ordering": ("correspondent", "title")},),migrations.RenameField(- model_name='document',- old_name='sender',- new_name='correspondent',+ model_name="document",+ old_name="sender",+ new_name="correspondent",),]diff --git a/src/documents/migrations/0012_auto_20160305_0040.py b/src/documents/migrations/0012_auto_20160305_0040.pyindex a0b4b27af..5ba3838d4 100644--- a/src/documents/migrations/0012_auto_20160305_0040.py+++ b/src/documents/migrations/0012_auto_20160305_0040.py@@ -23,37 +23,40 @@ class GnuPG(object):@classmethoddef decrypted(cls, file_handle):- return cls.gpg.decrypt_file(- file_handle, passphrase=settings.PASSPHRASE).data+ return cls.gpg.decrypt_file(file_handle, passphrase=settings.PASSPHRASE).data@classmethoddef encrypted(cls, file_handle):return cls.gpg.encrypt_file(- file_handle,- recipients=None,- passphrase=settings.PASSPHRASE,- symmetric=True+ file_handle, recipients=None, passphrase=settings.PASSPHRASE, symmetric=True).datadef move_documents_and_create_thumbnails(apps, schema_editor):- os.makedirs(os.path.join(settings.MEDIA_ROOT, "documents", "originals"), exist_ok=True)- os.makedirs(os.path.join(settings.MEDIA_ROOT, "documents", "thumbnails"), exist_ok=True)+ os.makedirs(+ os.path.join(settings.MEDIA_ROOT, "documents", "originals"), exist_ok=True+ )+ os.makedirs(+ os.path.join(settings.MEDIA_ROOT, "documents", "thumbnails"), exist_ok=True+ )documents = os.listdir(os.path.join(settings.MEDIA_ROOT, "documents"))if set(documents) == {"originals", "thumbnails"}:return- print(colourise(- "\n\n"- " This is a one-time only migration to generate thumbnails for all of your\n"- " documents so that future UIs will have something to work with. If you have\n"- " a lot of documents though, this may take a while, so a coffee break may be\n"- " in order."- "\n", opts=("bold",)- ))+ print(+ colourise(+ "\n\n"+ " This is a one-time only migration to generate thumbnails for all of your\n"+ " documents so that future UIs will have something to work with. If you have\n"+ " a lot of documents though, this may take a while, so a coffee break may be\n"+ " in order."+ "\n",+ opts=("bold",),+ )+ )try:os.makedirs(settings.SCRATCH_DIR)@@ -65,16 +68,16 @@ def move_documents_and_create_thumbnails(apps, schema_editor):if not f.endswith("gpg"):continue- print(" {} {} {}".format(- colourise("*", fg="green"),- colourise("Generating a thumbnail for", fg="white"),- colourise(f, fg="cyan")- ))+ print(+ " {} {} {}".format(+ colourise("*", fg="green"),+ colourise("Generating a thumbnail for", fg="white"),+ colourise(f, fg="cyan"),+ )+ )- thumb_temp = tempfile.mkdtemp(- prefix="paperless", dir=settings.SCRATCH_DIR)- orig_temp = tempfile.mkdtemp(- prefix="paperless", dir=settings.SCRATCH_DIR)+ thumb_temp = tempfile.mkdtemp(prefix="paperless", dir=settings.SCRATCH_DIR)+ orig_temp = tempfile.mkdtemp(prefix="paperless", dir=settings.SCRATCH_DIR)orig_source = os.path.join(settings.MEDIA_ROOT, "documents", f)orig_target = os.path.join(orig_temp, f.replace(".gpg", ""))@@ -83,20 +86,24 @@ def move_documents_and_create_thumbnails(apps, schema_editor):with open(orig_target, "wb") as unencrypted:unencrypted.write(GnuPG.decrypted(encrypted))- subprocess.Popen((- settings.CONVERT_BINARY,- "-scale", "500x5000",- "-alpha", "remove",- orig_target,- os.path.join(thumb_temp, "convert-%04d.png")- )).wait()+ subprocess.Popen(+ (+ settings.CONVERT_BINARY,+ "-scale",+ "500x5000",+ "-alpha",+ "remove",+ orig_target,+ os.path.join(thumb_temp, "convert-%04d.png"),+ )+ ).wait()thumb_source = os.path.join(thumb_temp, "convert-0000.png")thumb_target = os.path.join(settings.MEDIA_ROOT,"documents","thumbnails",- re.sub(r"(\d+)\.\w+(\.gpg)", "\\1.png\\2", f)+ re.sub(r"(\d+)\.\w+(\.gpg)", "\\1.png\\2", f),)with open(thumb_source, "rb") as unencrypted:with open(thumb_target, "wb") as encrypted:@@ -113,7 +120,7 @@ def move_documents_and_create_thumbnails(apps, schema_editor):class Migration(migrations.Migration):dependencies = [- ('documents', '0011_auto_20160303_1929'),+ ("documents", "0011_auto_20160303_1929"),]operations = [diff --git a/src/documents/migrations/0013_auto_20160325_2111.py b/src/documents/migrations/0013_auto_20160325_2111.pyindex c57ddc03e..58f39758a 100644--- a/src/documents/migrations/0013_auto_20160325_2111.py+++ b/src/documents/migrations/0013_auto_20160325_2111.py@@ -9,27 +9,36 @@ import django.utils.timezoneclass Migration(migrations.Migration):dependencies = [- ('documents', '0012_auto_20160305_0040'),+ ("documents", "0012_auto_20160305_0040"),]operations = [migrations.AddField(- model_name='correspondent',- name='match',+ model_name="correspondent",+ name="match",field=models.CharField(blank=True, max_length=256),),migrations.AddField(- model_name='correspondent',- name='matching_algorithm',- field=models.PositiveIntegerField(choices=[(1, 'Any'), (2, 'All'), (3, 'Literal'), (4, 'Regular Expression')], default=1, help_text='Which algorithm you want to use when matching text to the OCR\'d PDF. Here, "any" looks for any occurrence of any word provided in the PDF, while "all" requires that every word provided appear in the PDF, albeit not in the order provided. A "literal" match means that the text you enter must appear in the PDF exactly as you\'ve entered it, and "regular expression" uses a regex to match the PDF. If you don\'t know what a regex is, you probably don\'t want this option.'),+ model_name="correspondent",+ name="matching_algorithm",+ field=models.PositiveIntegerField(+ choices=[+ (1, "Any"),+ (2, "All"),+ (3, "Literal"),+ (4, "Regular Expression"),+ ],+ default=1,+ help_text='Which algorithm you want to use when matching text to the OCR\'d PDF. Here, "any" looks for any occurrence of any word provided in the PDF, while "all" requires that every word provided appear in the PDF, albeit not in the order provided. A "literal" match means that the text you enter must appear in the PDF exactly as you\'ve entered it, and "regular expression" uses a regex to match the PDF. If you don\'t know what a regex is, you probably don\'t want this option.',+ ),),migrations.AlterField(- model_name='document',- name='created',+ model_name="document",+ name="created",field=models.DateTimeField(default=django.utils.timezone.now),),migrations.RemoveField(- model_name='log',- name='component',+ model_name="log",+ name="component",),]diff --git a/src/documents/migrations/0014_document_checksum.py b/src/documents/migrations/0014_document_checksum.pyindex a22348ba4..1ec8380f4 100644--- a/src/documents/migrations/0014_document_checksum.py+++ b/src/documents/migrations/0014_document_checksum.py@@ -22,16 +22,12 @@ class GnuPG(object):@classmethoddef decrypted(cls, file_handle):- return cls.gpg.decrypt_file(- file_handle, passphrase=settings.PASSPHRASE).data+ return cls.gpg.decrypt_file(file_handle, passphrase=settings.PASSPHRASE).data@classmethoddef encrypted(cls, file_handle):return cls.gpg.encrypt_file(- file_handle,- recipients=None,- passphrase=settings.PASSPHRASE,- symmetric=True+ file_handle, recipients=None, passphrase=settings.PASSPHRASE, symmetric=True).data@@ -53,8 +49,7 @@ class Document(object):def __str__(self):created = self.created.strftime("%Y%m%d%H%M%S")if self.correspondent and self.title:- return "{}: {} - {}".format(- created, self.correspondent, self.title)+ return "{}: {} - {}".format(created, self.correspondent, self.title)if self.correspondent or self.title:return "{}: {}".format(created, self.correspondent or self.title)return str(created)@@ -65,7 +60,7 @@ class Document(object):settings.MEDIA_ROOT,"documents","originals",- "{:07}.{}.gpg".format(self.pk, self.file_type)+ "{:07}.{}.gpg".format(self.pk, self.file_type),)@property@@ -84,38 +79,62 @@ def set_checksums(apps, schema_editor):if not document_model.objects.all().exists():return- print(colourise(- "\n\n"- " This is a one-time only migration to generate checksums for all\n"- " of your existing documents. If you have a lot of documents\n"- " though, this may take a while, so a coffee break may be in\n"- " order."- "\n", opts=("bold",)- ))+ print(+ colourise(+ "\n\n"+ " This is a one-time only migration to generate checksums for all\n"+ " of your existing documents. If you have a lot of documents\n"+ " though, this may take a while, so a coffee break may be in\n"+ " order."+ "\n",+ opts=("bold",),+ )+ )sums = {}for d in document_model.objects.all():document = Document(d)- print(" {} {} {}".format(- colourise("*", fg="green"),- colourise("Generating a checksum for", fg="white"),- colourise(document.file_name, fg="cyan")- ))+ print(+ " {} {} {}".format(+ colourise("*", fg="green"),+ colourise("Generating a checksum for", fg="white"),+ colourise(document.file_name, fg="cyan"),+ )+ )with document.source_file as encrypted:checksum = hashlib.md5(GnuPG.decrypted(encrypted)).hexdigest()if checksum in sums:error = "\n{line}{p1}\n\n{doc1}\n{doc2}\n\n{p2}\n\n{code}\n\n{p3}{line}".format(- p1=colourise("It appears that you have two identical documents in your collection and \nPaperless no longer supports this (see issue #97). The documents in question\nare:", fg="yellow"),- p2=colourise("To fix this problem, you'll have to remove one of them from the database, a task\nmost easily done by running the following command in the same\ndirectory as manage.py:", fg="yellow"),- p3=colourise("When that's finished, re-run the migrate, and provided that there aren't any\nother duplicates, you should be good to go.", fg="yellow"),- doc1=colourise(" * {} (id: {})".format(sums[checksum][1], sums[checksum][0]), fg="red"),- doc2=colourise(" * {} (id: {})".format(document.file_name, document.pk), fg="red"),- code=colourise(" $ echo 'DELETE FROM documents_document WHERE id = {pk};' | ./manage.py dbshell".format(pk=document.pk), fg="green"),- line=colourise("\n{}\n".format("=" * 80), fg="white", opts=("bold",))+ p1=colourise(+ "It appears that you have two identical documents in your collection and \nPaperless no longer supports this (see issue #97). The documents in question\nare:",+ fg="yellow",+ ),+ p2=colourise(+ "To fix this problem, you'll have to remove one of them from the database, a task\nmost easily done by running the following command in the same\ndirectory as manage.py:",+ fg="yellow",+ ),+ p3=colourise(+ "When that's finished, re-run the migrate, and provided that there aren't any\nother duplicates, you should be good to go.",+ fg="yellow",+ ),+ doc1=colourise(+ " * {} (id: {})".format(sums[checksum][1], sums[checksum][0]),+ fg="red",+ ),+ doc2=colourise(+ " * {} (id: {})".format(document.file_name, document.pk), fg="red"+ ),+ code=colourise(+ " $ echo 'DELETE FROM documents_document WHERE id = {pk};' | ./manage.py dbshell".format(+ pk=document.pk+ ),+ fg="green",+ ),+ line=colourise("\n{}\n".format("=" * 80), fg="white", opts=("bold",)),)raise RuntimeError(error)sums[checksum] = (document.pk, document.file_name)@@ -129,33 +148,35 @@ def do_nothing(apps, schema_editor):class Migration(migrations.Migration):dependencies = [- ('documents', '0013_auto_20160325_2111'),+ ("documents", "0013_auto_20160325_2111"),]operations = [migrations.AddField(- model_name='document',- name='checksum',+ model_name="document",+ name="checksum",field=models.CharField(- default='-',+ default="-",db_index=True,editable=False,max_length=32,- help_text='The checksum of the original document (before it '- 'was encrypted). We use this to prevent duplicate '- 'document imports.',+ help_text="The checksum of the original document (before it "+ "was encrypted). We use this to prevent duplicate "+ "document imports.",),preserve_default=False,),migrations.RunPython(set_checksums, do_nothing),migrations.AlterField(- model_name='document',- name='created',- field=models.DateTimeField(db_index=True, default=django.utils.timezone.now),+ model_name="document",+ name="created",+ field=models.DateTimeField(+ db_index=True, default=django.utils.timezone.now+ ),),migrations.AlterField(- model_name='document',- name='modified',+ model_name="document",+ name="modified",field=models.DateTimeField(auto_now=True, db_index=True),),]diff --git a/src/documents/migrations/0015_add_insensitive_to_match.py b/src/documents/migrations/0015_add_insensitive_to_match.pyindex 30666dea9..351a4067b 100644--- a/src/documents/migrations/0015_add_insensitive_to_match.py+++ b/src/documents/migrations/0015_add_insensitive_to_match.py@@ -8,23 +8,28 @@ from django.db import migrations, modelsclass Migration(migrations.Migration):dependencies = [- ('documents', '0014_document_checksum'),+ ("documents", "0014_document_checksum"),]operations = [migrations.AlterField(- model_name='document',- name='checksum',- field=models.CharField(editable=False, help_text='The checksum of the original document (before it was encrypted). We use this to prevent duplicate document imports.', max_length=32, unique=True),+ model_name="document",+ name="checksum",+ field=models.CharField(+ editable=False,+ help_text="The checksum of the original document (before it was encrypted). We use this to prevent duplicate document imports.",+ max_length=32,+ unique=True,+ ),),migrations.AddField(- model_name='correspondent',- name='is_insensitive',+ model_name="correspondent",+ name="is_insensitive",field=models.BooleanField(default=True),),migrations.AddField(- model_name='tag',- name='is_insensitive',+ model_name="tag",+ name="is_insensitive",field=models.BooleanField(default=True),),]diff --git a/src/documents/migrations/0016_auto_20170325_1558.py b/src/documents/migrations/0016_auto_20170325_1558.pyindex 373a3d68f..ae95b83f6 100644--- a/src/documents/migrations/0016_auto_20170325_1558.py+++ b/src/documents/migrations/0016_auto_20170325_1558.py@@ -9,13 +9,17 @@ from django.conf import settingsclass Migration(migrations.Migration):dependencies = [- ('documents', '0015_add_insensitive_to_match'),+ ("documents", "0015_add_insensitive_to_match"),]operations = [migrations.AlterField(- model_name='document',- name='content',- field=models.TextField(blank=True, db_index=("mysql" not in settings.DATABASES["default"]["ENGINE"]), help_text='The raw, text-only data of the document. This field is primarily used for searching.'),+ model_name="document",+ name="content",+ field=models.TextField(+ blank=True,+ db_index=("mysql" not in settings.DATABASES["default"]["ENGINE"]),+ help_text="The raw, text-only data of the document. This field is primarily used for searching.",+ ),),]diff --git a/src/documents/migrations/0017_auto_20170512_0507.py b/src/documents/migrations/0017_auto_20170512_0507.pyindex 0ddc45de4..603aece5e 100644--- a/src/documents/migrations/0017_auto_20170512_0507.py+++ b/src/documents/migrations/0017_auto_20170512_0507.py@@ -8,18 +8,38 @@ from django.db import migrations, modelsclass Migration(migrations.Migration):dependencies = [- ('documents', '0016_auto_20170325_1558'),+ ("documents", "0016_auto_20170325_1558"),]operations = [migrations.AlterField(- model_name='correspondent',- name='matching_algorithm',- field=models.PositiveIntegerField(choices=[(1, 'Any'), (2, 'All'), (3, 'Literal'), (4, 'Regular Expression'), (5, 'Fuzzy Match')], default=1, help_text='Which algorithm you want to use when matching text to the OCR\'d PDF. Here, "any" looks for any occurrence of any word provided in the PDF, while "all" requires that every word provided appear in the PDF, albeit not in the order provided. A "literal" match means that the text you enter must appear in the PDF exactly as you\'ve entered it, and "regular expression" uses a regex to match the PDF. (If you don\'t know what a regex is, you probably don\'t want this option.) Finally, a "fuzzy match" looks for words or phrases that are mostly—but not exactly—the same, which can be useful for matching against documents containg imperfections that foil accurate OCR.'),+ model_name="correspondent",+ name="matching_algorithm",+ field=models.PositiveIntegerField(+ choices=[+ (1, "Any"),+ (2, "All"),+ (3, "Literal"),+ (4, "Regular Expression"),+ (5, "Fuzzy Match"),+ ],+ default=1,+ help_text='Which algorithm you want to use when matching text to the OCR\'d PDF. Here, "any" looks for any occurrence of any word provided in the PDF, while "all" requires that every word provided appear in the PDF, albeit not in the order provided. A "literal" match means that the text you enter must appear in the PDF exactly as you\'ve entered it, and "regular expression" uses a regex to match the PDF. (If you don\'t know what a regex is, you probably don\'t want this option.) Finally, a "fuzzy match" looks for words or phrases that are mostly—but not exactly—the same, which can be useful for matching against documents containg imperfections that foil accurate OCR.',+ ),),migrations.AlterField(- model_name='tag',- name='matching_algorithm',- field=models.PositiveIntegerField(choices=[(1, 'Any'), (2, 'All'), (3, 'Literal'), (4, 'Regular Expression'), (5, 'Fuzzy Match')], default=1, help_text='Which algorithm you want to use when matching text to the OCR\'d PDF. Here, "any" looks for any occurrence of any word provided in the PDF, while "all" requires that every word provided appear in the PDF, albeit not in the order provided. A "literal" match means that the text you enter must appear in the PDF exactly as you\'ve entered it, and "regular expression" uses a regex to match the PDF. (If you don\'t know what a regex is, you probably don\'t want this option.) Finally, a "fuzzy match" looks for words or phrases that are mostly—but not exactly—the same, which can be useful for matching against documents containg imperfections that foil accurate OCR.'),+ model_name="tag",+ name="matching_algorithm",+ field=models.PositiveIntegerField(+ choices=[+ (1, "Any"),+ (2, "All"),+ (3, "Literal"),+ (4, "Regular Expression"),+ (5, "Fuzzy Match"),+ ],+ default=1,+ help_text='Which algorithm you want to use when matching text to the OCR\'d PDF. Here, "any" looks for any occurrence of any word provided in the PDF, while "all" requires that every word provided appear in the PDF, albeit not in the order provided. A "literal" match means that the text you enter must appear in the PDF exactly as you\'ve entered it, and "regular expression" uses a regex to match the PDF. (If you don\'t know what a regex is, you probably don\'t want this option.) Finally, a "fuzzy match" looks for words or phrases that are mostly—but not exactly—the same, which can be useful for matching against documents containg imperfections that foil accurate OCR.',+ ),),]diff --git a/src/documents/migrations/0018_auto_20170715_1712.py b/src/documents/migrations/0018_auto_20170715_1712.pyindex 58d1e9fe8..492e016e3 100644--- a/src/documents/migrations/0018_auto_20170715_1712.py+++ b/src/documents/migrations/0018_auto_20170715_1712.py@@ -9,13 +9,19 @@ import django.db.models.deletionclass Migration(migrations.Migration):dependencies = [- ('documents', '0017_auto_20170512_0507'),+ ("documents", "0017_auto_20170512_0507"),]operations = [migrations.AlterField(- model_name='document',- name='correspondent',- field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='documents', to='documents.Correspondent'),+ model_name="document",+ name="correspondent",+ field=models.ForeignKey(+ blank=True,+ null=True,+ on_delete=django.db.models.deletion.SET_NULL,+ related_name="documents",+ to="documents.Correspondent",+ ),),]diff --git a/src/documents/migrations/0019_add_consumer_user.py b/src/documents/migrations/0019_add_consumer_user.pyindex bc52ae7f6..344297805 100644--- a/src/documents/migrations/0019_add_consumer_user.py+++ b/src/documents/migrations/0019_add_consumer_user.py@@ -16,7 +16,7 @@ def reverse_func(apps, schema_editor):class Migration(migrations.Migration):dependencies = [- ('documents', '0018_auto_20170715_1712'),+ ("documents", "0018_auto_20170715_1712"),]operations = [diff --git a/src/documents/migrations/0020_document_added.py b/src/documents/migrations/0020_document_added.pyindex bd3566481..66afa1258 100644--- a/src/documents/migrations/0020_document_added.py+++ b/src/documents/migrations/0020_document_added.py@@ -14,14 +14,16 @@ def set_added_time_to_created_time(apps, schema_editor):class Migration(migrations.Migration):dependencies = [- ('documents', '0019_add_consumer_user'),+ ("documents", "0019_add_consumer_user"),]operations = [migrations.AddField(- model_name='document',- name='added',- field=models.DateTimeField(db_index=True, default=django.utils.timezone.now, editable=False),+ model_name="document",+ name="added",+ field=models.DateTimeField(+ db_index=True, default=django.utils.timezone.now, editable=False+ ),),- migrations.RunPython(set_added_time_to_created_time)+ migrations.RunPython(set_added_time_to_created_time),]diff --git a/src/documents/migrations/0021_document_storage_type.py b/src/documents/migrations/0021_document_storage_type.pyindex cec172b93..0e7425fb6 100644--- a/src/documents/migrations/0021_document_storage_type.py+++ b/src/documents/migrations/0021_document_storage_type.py@@ -8,23 +8,36 @@ from django.db import migrations, modelsclass Migration(migrations.Migration):dependencies = [- ('documents', '0020_document_added'),+ ("documents", "0020_document_added"),]operations = [-# Add the field with the default GPG-encrypted valuemigrations.AddField(- model_name='document',- name='storage_type',- field=models.CharField(choices=[('unencrypted', 'Unencrypted'), ('gpg', 'Encrypted with GNU Privacy Guard')], default='gpg', editable=False, max_length=11),+ model_name="document",+ name="storage_type",+ field=models.CharField(+ choices=[+ ("unencrypted", "Unencrypted"),+ ("gpg", "Encrypted with GNU Privacy Guard"),+ ],+ default="gpg",+ editable=False,+ max_length=11,+ ),),-# Now that the field is added, change the default to unencryptedmigrations.AlterField(- model_name='document',- name='storage_type',- field=models.CharField(choices=[('unencrypted', 'Unencrypted'), ('gpg', 'Encrypted with GNU Privacy Guard')], default='unencrypted', editable=False, max_length=11),+ model_name="document",+ name="storage_type",+ field=models.CharField(+ choices=[+ ("unencrypted", "Unencrypted"),+ ("gpg", "Encrypted with GNU Privacy Guard"),+ ],+ default="unencrypted",+ editable=False,+ max_length=11,+ ),),-]diff --git a/src/documents/migrations/0022_auto_20181007_1420.py b/src/documents/migrations/0022_auto_20181007_1420.pyindex 2853f2093..ecebf80d8 100644--- a/src/documents/migrations/0022_auto_20181007_1420.py+++ b/src/documents/migrations/0022_auto_20181007_1420.py@@ -15,38 +15,47 @@ def re_slug_all_the_things(apps, schema_editor):for klass in (Tag, Correspondent):for instance in klass.objects.all():- klass.objects.filter(- pk=instance.pk- ).update(- slug=slugify(instance.slug)- )+ klass.objects.filter(pk=instance.pk).update(slug=slugify(instance.slug))class Migration(migrations.Migration):dependencies = [- ('documents', '0021_document_storage_type'),+ ("documents", "0021_document_storage_type"),]operations = [migrations.AlterModelOptions(- name='tag',- options={'ordering': ('name',)},+ name="tag",+ options={"ordering": ("name",)},),migrations.AlterField(- model_name='correspondent',- name='slug',+ model_name="correspondent",+ name="slug",field=models.SlugField(blank=True, editable=False),),migrations.AlterField(- model_name='document',- name='file_type',- field=models.CharField(choices=[('pdf', 'PDF'), ('png', 'PNG'), ('jpg', 'JPG'), ('gif', 'GIF'), ('tiff', 'TIFF'), ('txt', 'TXT'), ('csv', 'CSV'), ('md', 'MD')], editable=False, max_length=4),+ model_name="document",+ name="file_type",+ field=models.CharField(+ choices=[+ ("pdf", "PDF"),+ ("png", "PNG"),+ ("jpg", "JPG"),+ ("gif", "GIF"),+ ("tiff", "TIFF"),+ ("txt", "TXT"),+ ("csv", "CSV"),+ ("md", "MD"),+ ],+ editable=False,+ max_length=4,+ ),),migrations.AlterField(- model_name='tag',- name='slug',+ model_name="tag",+ name="slug",field=models.SlugField(blank=True, editable=False),),- migrations.RunPython(re_slug_all_the_things, migrations.RunPython.noop)+ migrations.RunPython(re_slug_all_the_things, migrations.RunPython.noop),]diff --git a/src/documents/migrations/0023_document_current_filename.py b/src/documents/migrations/0023_document_current_filename.pyindex be78ea863..8e9f65bb9 100644--- a/src/documents/migrations/0023_document_current_filename.py+++ b/src/documents/migrations/0023_document_current_filename.py@@ -20,18 +20,20 @@ def set_filename(apps, schema_editor):class Migration(migrations.Migration):dependencies = [- ('documents', '0022_auto_20181007_1420'),+ ("documents", "0022_auto_20181007_1420"),]operations = [migrations.AddField(- model_name='document',- name='filename',- field=models.FilePathField(default=None,- null=True,- editable=False,- help_text='Current filename in storage',- max_length=256),+ model_name="document",+ name="filename",+ field=models.FilePathField(+ default=None,+ null=True,+ editable=False,+ help_text="Current filename in storage",+ max_length=256,+ ),),- migrations.RunPython(set_filename)+ migrations.RunPython(set_filename),]diff --git a/src/documents/migrations/1000_update_paperless_all.py b/src/documents/migrations/1000_update_paperless_all.pyindex f3fbbb6c1..4301a5b21 100644--- a/src/documents/migrations/1000_update_paperless_all.py+++ b/src/documents/migrations/1000_update_paperless_all.py@@ -6,7 +6,7 @@ import django.db.models.deletiondef logs_set_default_group(apps, schema_editor):- Log = apps.get_model('documents', 'Log')+ Log = apps.get_model("documents", "Log")for log in Log.objects.all():if log.group is None:log.group = uuid.uuid4()@@ -16,70 +16,132 @@ def logs_set_default_group(apps, schema_editor):class Migration(migrations.Migration):dependencies = [- ('documents', '0023_document_current_filename'),+ ("documents", "0023_document_current_filename"),]operations = [migrations.AddField(- model_name='document',- name='archive_serial_number',- field=models.IntegerField(blank=True, db_index=True, help_text='The position of this document in your physical document archive.', null=True, unique=True),+ model_name="document",+ name="archive_serial_number",+ field=models.IntegerField(+ blank=True,+ db_index=True,+ help_text="The position of this document in your physical document archive.",+ null=True,+ unique=True,+ ),),migrations.AddField(- model_name='tag',- name='is_inbox_tag',- field=models.BooleanField(default=False, help_text='Marks this tag as an inbox tag: All newly consumed documents will be tagged with inbox tags.'),+ model_name="tag",+ name="is_inbox_tag",+ field=models.BooleanField(+ default=False,+ help_text="Marks this tag as an inbox tag: All newly consumed documents will be tagged with inbox tags.",+ ),),migrations.CreateModel(- name='DocumentType',+ name="DocumentType",fields=[- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),- ('name', models.CharField(max_length=128, unique=True)),- ('slug', models.SlugField(blank=True, editable=False)),- ('match', models.CharField(blank=True, max_length=256)),- ('matching_algorithm', models.PositiveIntegerField(choices=[(1, 'Any'), (2, 'All'), (3, 'Literal'), (4, 'Regular Expression'), (5, 'Fuzzy Match'), (6, 'Automatic Classification')], default=1, help_text='Which algorithm you want to use when matching text to the OCR\'d PDF. Here, "any" looks for any occurrence of any word provided in the PDF, while "all" requires that every word provided appear in the PDF, albeit not in the order provided. A "literal" match means that the text you enter must appear in the PDF exactly as you\'ve entered it, and "regular expression" uses a regex to match the PDF. (If you don\'t know what a regex is, you probably don\'t want this option.) Finally, a "fuzzy match" looks for words or phrases that are mostly—but not exactly—the same, which can be useful for matching against documents containg imperfections that foil accurate OCR.')),- ('is_insensitive', models.BooleanField(default=True)),+ (+ "id",+ models.AutoField(+ auto_created=True,+ primary_key=True,+ serialize=False,+ verbose_name="ID",+ ),+ ),+ ("name", models.CharField(max_length=128, unique=True)),+ ("slug", models.SlugField(blank=True, editable=False)),+ ("match", models.CharField(blank=True, max_length=256)),+ (+ "matching_algorithm",+ models.PositiveIntegerField(+ choices=[+ (1, "Any"),+ (2, "All"),+ (3, "Literal"),+ (4, "Regular Expression"),+ (5, "Fuzzy Match"),+ (6, "Automatic Classification"),+ ],+ default=1,+ help_text='Which algorithm you want to use when matching text to the OCR\'d PDF. Here, "any" looks for any occurrence of any word provided in the PDF, while "all" requires that every word provided appear in the PDF, albeit not in the order provided. A "literal" match means that the text you enter must appear in the PDF exactly as you\'ve entered it, and "regular expression" uses a regex to match the PDF. (If you don\'t know what a regex is, you probably don\'t want this option.) Finally, a "fuzzy match" looks for words or phrases that are mostly—but not exactly—the same, which can be useful for matching against documents containg imperfections that foil accurate OCR.',+ ),+ ),+ ("is_insensitive", models.BooleanField(default=True)),],options={- 'abstract': False,- 'ordering': ('name',),+ "abstract": False,+ "ordering": ("name",),},),migrations.AddField(- model_name='document',- name='document_type',- field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='documents', to='documents.documenttype'),+ model_name="document",+ name="document_type",+ field=models.ForeignKey(+ blank=True,+ null=True,+ on_delete=django.db.models.deletion.SET_NULL,+ related_name="documents",+ to="documents.documenttype",+ ),),migrations.AlterField(- model_name='correspondent',- name='matching_algorithm',- field=models.PositiveIntegerField(choices=[(1, 'Any'), (2, 'All'), (3, 'Literal'), (4, 'Regular Expression'), (5, 'Fuzzy Match'), (6, 'Automatic Classification')], default=1, help_text='Which algorithm you want to use when matching text to the OCR\'d PDF. Here, "any" looks for any occurrence of any word provided in the PDF, while "all" requires that every word provided appear in the PDF, albeit not in the order provided. A "literal" match means that the text you enter must appear in the PDF exactly as you\'ve entered it, and "regular expression" uses a regex to match the PDF. (If you don\'t know what a regex is, you probably don\'t want this option.) Finally, a "fuzzy match" looks for words or phrases that are mostly—but not exactly—the same, which can be useful for matching against documents containg imperfections that foil accurate OCR.'),+ model_name="correspondent",+ name="matching_algorithm",+ field=models.PositiveIntegerField(+ choices=[+ (1, "Any"),+ (2, "All"),+ (3, "Literal"),+ (4, "Regular Expression"),+ (5, "Fuzzy Match"),+ (6, "Automatic Classification"),+ ],+ default=1,+ help_text='Which algorithm you want to use when matching text to the OCR\'d PDF. Here, "any" looks for any occurrence of any word provided in the PDF, while "all" requires that every word provided appear in the PDF, albeit not in the order provided. A "literal" match means that the text you enter must appear in the PDF exactly as you\'ve entered it, and "regular expression" uses a regex to match the PDF. (If you don\'t know what a regex is, you probably don\'t want this option.) Finally, a "fuzzy match" looks for words or phrases that are mostly—but not exactly—the same, which can be useful for matching against documents containg imperfections that foil accurate OCR.',+ ),),migrations.AlterField(- model_name='tag',- name='matching_algorithm',- field=models.PositiveIntegerField(choices=[(1, 'Any'), (2, 'All'), (3, 'Literal'), (4, 'Regular Expression'), (5, 'Fuzzy Match'), (6, 'Automatic Classification')], default=1, help_text='Which algorithm you want to use when matching text to the OCR\'d PDF. Here, "any" looks for any occurrence of any word provided in the PDF, while "all" requires that every word provided appear in the PDF, albeit not in the order provided. A "literal" match means that the text you enter must appear in the PDF exactly as you\'ve entered it, and "regular expression" uses a regex to match the PDF. (If you don\'t know what a regex is, you probably don\'t want this option.) Finally, a "fuzzy match" looks for words or phrases that are mostly—but not exactly—the same, which can be useful for matching against documents containg imperfections that foil accurate OCR.'),+ model_name="tag",+ name="matching_algorithm",+ field=models.PositiveIntegerField(+ choices=[+ (1, "Any"),+ (2, "All"),+ (3, "Literal"),+ (4, "Regular Expression"),+ (5, "Fuzzy Match"),+ (6, "Automatic Classification"),+ ],+ default=1,+ help_text='Which algorithm you want to use when matching text to the OCR\'d PDF. Here, "any" looks for any occurrence of any word provided in the PDF, while "all" requires that every word provided appear in the PDF, albeit not in the order provided. A "literal" match means that the text you enter must appear in the PDF exactly as you\'ve entered it, and "regular expression" uses a regex to match the PDF. (If you don\'t know what a regex is, you probably don\'t want this option.) Finally, a "fuzzy match" looks for words or phrases that are mostly—but not exactly—the same, which can be useful for matching against documents containg imperfections that foil accurate OCR.',+ ),),migrations.AlterField(- model_name='document',- name='content',- field=models.TextField(blank=True, help_text='The raw, text-only data of the document. This field is primarily used for searching.'),+ model_name="document",+ name="content",+ field=models.TextField(+ blank=True,+ help_text="The raw, text-only data of the document. This field is primarily used for searching.",+ ),),migrations.AlterModelOptions(- name='log',- options={'ordering': ('-created',)},+ name="log",+ options={"ordering": ("-created",)},),migrations.RemoveField(- model_name='log',- name='modified',+ model_name="log",+ name="modified",),migrations.AlterField(- model_name='log',- name='group',+ model_name="log",+ name="group",field=models.UUIDField(blank=True, null=True),),migrations.RunPython(code=django.db.migrations.operations.special.RunPython.noop,- reverse_code=logs_set_default_group+ reverse_code=logs_set_default_group,),]diff --git a/src/documents/migrations/1001_auto_20201109_1636.py b/src/documents/migrations/1001_auto_20201109_1636.pyindex 90cb53d4b..0558ee640 100644--- a/src/documents/migrations/1001_auto_20201109_1636.py+++ b/src/documents/migrations/1001_auto_20201109_1636.py@@ -7,22 +7,28 @@ from django_q.tasks import scheduledef add_schedules(apps, schema_editor):- schedule('documents.tasks.train_classifier', name="Train the classifier", schedule_type=Schedule.HOURLY)- schedule('documents.tasks.index_optimize', name="Optimize the index", schedule_type=Schedule.DAILY)+ schedule(+ "documents.tasks.train_classifier",+ name="Train the classifier",+ schedule_type=Schedule.HOURLY,+ )+ schedule(+ "documents.tasks.index_optimize",+ name="Optimize the index",+ schedule_type=Schedule.DAILY,+ )def remove_schedules(apps, schema_editor):- Schedule.objects.filter(func='documents.tasks.train_classifier').delete()- Schedule.objects.filter(func='documents.tasks.index_optimize').delete()+ Schedule.objects.filter(func="documents.tasks.train_classifier").delete()+ Schedule.objects.filter(func="documents.tasks.index_optimize").delete()class Migration(migrations.Migration):dependencies = [- ('documents', '1000_update_paperless_all'),- ('django_q', '0013_task_attempt_count'),+ ("documents", "1000_update_paperless_all"),+ ("django_q", "0013_task_attempt_count"),]- operations = [- RunPython(add_schedules, remove_schedules)- ]+ operations = [RunPython(add_schedules, remove_schedules)]diff --git a/src/documents/migrations/1002_auto_20201111_1105.py b/src/documents/migrations/1002_auto_20201111_1105.pyindex 7f6bae50b..f502fba3f 100644--- a/src/documents/migrations/1002_auto_20201111_1105.py+++ b/src/documents/migrations/1002_auto_20201111_1105.py@@ -6,13 +6,19 @@ from django.db import migrations, modelsclass Migration(migrations.Migration):dependencies = [- ('documents', '1001_auto_20201109_1636'),+ ("documents", "1001_auto_20201109_1636"),]operations = [migrations.AlterField(- model_name='document',- name='filename',- field=models.FilePathField(default=None, editable=False, help_text='Current filename in storage', max_length=1024, null=True),+ model_name="document",+ name="filename",+ field=models.FilePathField(+ default=None,+ editable=False,+ help_text="Current filename in storage",+ max_length=1024,+ null=True,+ ),),]diff --git a/src/documents/migrations/1003_mime_types.py b/src/documents/migrations/1003_mime_types.pyindex c196f29f4..4eee1e0a2 100644--- a/src/documents/migrations/1003_mime_types.py+++ b/src/documents/migrations/1003_mime_types.py@@ -20,10 +20,7 @@ def source_path(self):if self.storage_type == STORAGE_TYPE_GPG:fname += ".gpg"- return os.path.join(- settings.ORIGINALS_DIR,- fname- )+ return os.path.join(settings.ORIGINALS_DIR, fname)def add_mime_types(apps, schema_editor):@@ -49,43 +46,51 @@ def add_file_extensions(apps, schema_editor):documents = Document.objects.all()for d in documents:- d.file_type = os.path.splitext(d.filename)[1].strip('.')+ d.file_type = os.path.splitext(d.filename)[1].strip(".")d.save()class Migration(migrations.Migration):dependencies = [- ('documents', '1002_auto_20201111_1105'),+ ("documents", "1002_auto_20201111_1105"),]operations = [migrations.AddField(- model_name='document',- name='mime_type',+ model_name="document",+ name="mime_type",field=models.CharField(default="-", editable=False, max_length=256),preserve_default=False,),migrations.RunPython(add_mime_types, migrations.RunPython.noop),-# This operation is here so that we can revert the entire migration:# By allowing this field to be blank and null, we can revert the# remove operation further down and the database won't complain about# NOT NULL violations.migrations.AlterField(- model_name='document',- name='file_type',+ model_name="document",+ name="file_type",field=models.CharField(- choices=[('pdf', 'PDF'), ('png', 'PNG'), ('jpg', 'JPG'), ('gif', 'GIF'), ('tiff', 'TIFF'), ('txt', 'TXT'), ('csv', 'CSV'), ('md', 'MD')],+ choices=[+ ("pdf", "PDF"),+ ("png", "PNG"),+ ("jpg", "JPG"),+ ("gif", "GIF"),+ ("tiff", "TIFF"),+ ("txt", "TXT"),+ ("csv", "CSV"),+ ("md", "MD"),+ ],editable=False,max_length=4,null=True,- blank=True+ blank=True,),),migrations.RunPython(migrations.RunPython.noop, add_file_extensions),migrations.RemoveField(- model_name='document',- name='file_type',+ model_name="document",+ name="file_type",),]diff --git a/src/documents/migrations/1004_sanity_check_schedule.py b/src/documents/migrations/1004_sanity_check_schedule.pyindex b6346d479..61d617dde 100644--- a/src/documents/migrations/1004_sanity_check_schedule.py+++ b/src/documents/migrations/1004_sanity_check_schedule.py@@ -7,20 +7,22 @@ from django_q.tasks import scheduledef add_schedules(apps, schema_editor):- schedule('documents.tasks.sanity_check', name="Perform sanity check", schedule_type=Schedule.WEEKLY)+ schedule(+ "documents.tasks.sanity_check",+ name="Perform sanity check",+ schedule_type=Schedule.WEEKLY,+ )def remove_schedules(apps, schema_editor):- Schedule.objects.filter(func='documents.tasks.sanity_check').delete()+ Schedule.objects.filter(func="documents.tasks.sanity_check").delete()class Migration(migrations.Migration):dependencies = [- ('documents', '1003_mime_types'),- ('django_q', '0013_task_attempt_count'),+ ("documents", "1003_mime_types"),+ ("django_q", "0013_task_attempt_count"),]- operations = [- RunPython(add_schedules, remove_schedules)- ]+ operations = [RunPython(add_schedules, remove_schedules)]diff --git a/src/documents/migrations/1005_checksums.py b/src/documents/migrations/1005_checksums.pyindex 401de2e1d..b1bfc6eac 100644--- a/src/documents/migrations/1005_checksums.py+++ b/src/documents/migrations/1005_checksums.py@@ -6,18 +6,29 @@ from django.db import migrations, modelsclass Migration(migrations.Migration):dependencies = [- ('documents', '1004_sanity_check_schedule'),+ ("documents", "1004_sanity_check_schedule"),]operations = [migrations.AddField(- model_name='document',- name='archive_checksum',- field=models.CharField(blank=True, editable=False, help_text='The checksum of the archived document.', max_length=32, null=True),+ model_name="document",+ name="archive_checksum",+ field=models.CharField(+ blank=True,+ editable=False,+ help_text="The checksum of the archived document.",+ max_length=32,+ null=True,+ ),),migrations.AlterField(- model_name='document',- name='checksum',- field=models.CharField(editable=False, help_text='The checksum of the original document.', max_length=32, unique=True),+ model_name="document",+ name="checksum",+ field=models.CharField(+ editable=False,+ help_text="The checksum of the original document.",+ max_length=32,+ unique=True,+ ),),]diff --git a/src/documents/migrations/1006_auto_20201208_2209.py b/src/documents/migrations/1006_auto_20201208_2209.pyindex 49f8c8dfe..8a7e7a99d 100644--- a/src/documents/migrations/1006_auto_20201208_2209.py+++ b/src/documents/migrations/1006_auto_20201208_2209.py@@ -6,20 +6,20 @@ from django.db import migrationsclass Migration(migrations.Migration):dependencies = [- ('documents', '1005_checksums'),+ ("documents", "1005_checksums"),]operations = [migrations.RemoveField(- model_name='correspondent',- name='slug',+ model_name="correspondent",+ name="slug",),migrations.RemoveField(- model_name='documenttype',- name='slug',+ model_name="documenttype",+ name="slug",),migrations.RemoveField(- model_name='tag',- name='slug',+ model_name="tag",+ name="slug",),]diff --git a/src/documents/migrations/1007_savedview_savedviewfilterrule.py b/src/documents/migrations/1007_savedview_savedviewfilterrule.pyindex 664def5f1..401ab5adb 100644--- a/src/documents/migrations/1007_savedview_savedviewfilterrule.py+++ b/src/documents/migrations/1007_savedview_savedviewfilterrule.py@@ -9,29 +9,82 @@ class Migration(migrations.Migration):dependencies = [migrations.swappable_dependency(settings.AUTH_USER_MODEL),- ('documents', '1006_auto_20201208_2209'),+ ("documents", "1006_auto_20201208_2209"),]operations = [migrations.CreateModel(- name='SavedView',+ name="SavedView",fields=[- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),- ('name', models.CharField(max_length=128)),- ('show_on_dashboard', models.BooleanField()),- ('show_in_sidebar', models.BooleanField()),- ('sort_field', models.CharField(max_length=128)),- ('sort_reverse', models.BooleanField(default=False)),- ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),+ (+ "id",+ models.AutoField(+ auto_created=True,+ primary_key=True,+ serialize=False,+ verbose_name="ID",+ ),+ ),+ ("name", models.CharField(max_length=128)),+ ("show_on_dashboard", models.BooleanField()),+ ("show_in_sidebar", models.BooleanField()),+ ("sort_field", models.CharField(max_length=128)),+ ("sort_reverse", models.BooleanField(default=False)),+ (+ "user",+ models.ForeignKey(+ on_delete=django.db.models.deletion.CASCADE,+ to=settings.AUTH_USER_MODEL,+ ),+ ),],),migrations.CreateModel(- name='SavedViewFilterRule',+ name="SavedViewFilterRule",fields=[- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),- ('rule_type', models.PositiveIntegerField(choices=[(0, 'Title contains'), (1, 'Content contains'), (2, 'ASN is'), (3, 'Correspondent is'), (4, 'Document type is'), (5, 'Is in inbox'), (6, 'Has tag'), (7, 'Has any tag'), (8, 'Created before'), (9, 'Created after'), (10, 'Created year is'), (11, 'Created month is'), (12, 'Created day is'), (13, 'Added before'), (14, 'Added after'), (15, 'Modified before'), (16, 'Modified after'), (17, 'Does not have tag')])),- ('value', models.CharField(max_length=128)),- ('saved_view', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='filter_rules', to='documents.savedview')),+ (+ "id",+ models.AutoField(+ auto_created=True,+ primary_key=True,+ serialize=False,+ verbose_name="ID",+ ),+ ),+ (+ "rule_type",+ models.PositiveIntegerField(+ choices=[+ (0, "Title contains"),+ (1, "Content contains"),+ (2, "ASN is"),+ (3, "Correspondent is"),+ (4, "Document type is"),+ (5, "Is in inbox"),+ (6, "Has tag"),+ (7, "Has any tag"),+ (8, "Created before"),+ (9, "Created after"),+ (10, "Created year is"),+ (11, "Created month is"),+ (12, "Created day is"),+ (13, "Added before"),+ (14, "Added after"),+ (15, "Modified before"),+ (16, "Modified after"),+ (17, "Does not have tag"),+ ]+ ),+ ),+ ("value", models.CharField(max_length=128)),+ (+ "saved_view",+ models.ForeignKey(+ on_delete=django.db.models.deletion.CASCADE,+ related_name="filter_rules",+ to="documents.savedview",+ ),+ ),],),]diff --git a/src/documents/migrations/1008_auto_20201216_1736.py b/src/documents/migrations/1008_auto_20201216_1736.pyindex d94f4767f..2f6c5c59d 100644--- a/src/documents/migrations/1008_auto_20201216_1736.py+++ b/src/documents/migrations/1008_auto_20201216_1736.py@@ -7,28 +7,28 @@ import django.db.models.functions.textclass Migration(migrations.Migration):dependencies = [- ('documents', '1007_savedview_savedviewfilterrule'),+ ("documents", "1007_savedview_savedviewfilterrule"),]operations = [migrations.AlterModelOptions(- name='correspondent',- options={'ordering': (django.db.models.functions.text.Lower('name'),)},+ name="correspondent",+ options={"ordering": (django.db.models.functions.text.Lower("name"),)},),migrations.AlterModelOptions(- name='document',- options={'ordering': ('-created',)},+ name="document",+ options={"ordering": ("-created",)},),migrations.AlterModelOptions(- name='documenttype',- options={'ordering': (django.db.models.functions.text.Lower('name'),)},+ name="documenttype",+ options={"ordering": (django.db.models.functions.text.Lower("name"),)},),migrations.AlterModelOptions(- name='savedview',- options={'ordering': (django.db.models.functions.text.Lower('name'),)},+ name="savedview",+ options={"ordering": (django.db.models.functions.text.Lower("name"),)},),migrations.AlterModelOptions(- name='tag',- options={'ordering': (django.db.models.functions.text.Lower('name'),)},+ name="tag",+ options={"ordering": (django.db.models.functions.text.Lower("name"),)},),]diff --git a/src/documents/migrations/1009_auto_20201216_2005.py b/src/documents/migrations/1009_auto_20201216_2005.pyindex 5e8302bb0..9584789bc 100644--- a/src/documents/migrations/1009_auto_20201216_2005.py+++ b/src/documents/migrations/1009_auto_20201216_2005.py@@ -6,24 +6,24 @@ from django.db import migrationsclass Migration(migrations.Migration):dependencies = [- ('documents', '1008_auto_20201216_1736'),+ ("documents", "1008_auto_20201216_1736"),]operations = [migrations.AlterModelOptions(- name='correspondent',- options={'ordering': ('name',)},+ name="correspondent",+ options={"ordering": ("name",)},),migrations.AlterModelOptions(- name='documenttype',- options={'ordering': ('name',)},+ name="documenttype",+ options={"ordering": ("name",)},),migrations.AlterModelOptions(- name='savedview',- options={'ordering': ('name',)},+ name="savedview",+ options={"ordering": ("name",)},),migrations.AlterModelOptions(- name='tag',- options={'ordering': ('name',)},+ name="tag",+ options={"ordering": ("name",)},),]diff --git a/src/documents/migrations/1010_auto_20210101_2159.py b/src/documents/migrations/1010_auto_20210101_2159.pyindex 4c30125be..1d05d8f47 100644--- a/src/documents/migrations/1010_auto_20210101_2159.py+++ b/src/documents/migrations/1010_auto_20210101_2159.py@@ -6,13 +6,13 @@ from django.db import migrations, modelsclass Migration(migrations.Migration):dependencies = [- ('documents', '1009_auto_20201216_2005'),+ ("documents", "1009_auto_20201216_2005"),]operations = [migrations.AlterField(- model_name='savedviewfilterrule',- name='value',+ model_name="savedviewfilterrule",+ name="value",field=models.CharField(blank=True, max_length=128, null=True),),]diff --git a/src/documents/migrations/1011_auto_20210101_2340.py b/src/documents/migrations/1011_auto_20210101_2340.pyindex 9405a5210..d16051c21 100644--- a/src/documents/migrations/1011_auto_20210101_2340.py+++ b/src/documents/migrations/1011_auto_20210101_2340.py@@ -10,241 +10,433 @@ class Migration(migrations.Migration):dependencies = [migrations.swappable_dependency(settings.AUTH_USER_MODEL),- ('documents', '1010_auto_20210101_2159'),+ ("documents", "1010_auto_20210101_2159"),]operations = [migrations.AlterModelOptions(- name='correspondent',- options={'ordering': ('name',), 'verbose_name': 'correspondent', 'verbose_name_plural': 'correspondents'},+ name="correspondent",+ options={+ "ordering": ("name",),+ "verbose_name": "correspondent",+ "verbose_name_plural": "correspondents",+ },),migrations.AlterModelOptions(- name='document',- options={'ordering': ('-created',), 'verbose_name': 'document', 'verbose_name_plural': 'documents'},+ name="document",+ options={+ "ordering": ("-created",),+ "verbose_name": "document",+ "verbose_name_plural": "documents",+ },),migrations.AlterModelOptions(- name='documenttype',- options={'verbose_name': 'document type', 'verbose_name_plural': 'document types'},+ name="documenttype",+ options={+ "verbose_name": "document type",+ "verbose_name_plural": "document types",+ },),migrations.AlterModelOptions(- name='log',- options={'ordering': ('-created',), 'verbose_name': 'log', 'verbose_name_plural': 'logs'},+ name="log",+ options={+ "ordering": ("-created",),+ "verbose_name": "log",+ "verbose_name_plural": "logs",+ },),migrations.AlterModelOptions(- name='savedview',- options={'ordering': ('name',), 'verbose_name': 'saved view', 'verbose_name_plural': 'saved views'},+ name="savedview",+ options={+ "ordering": ("name",),+ "verbose_name": "saved view",+ "verbose_name_plural": "saved views",+ },),migrations.AlterModelOptions(- name='savedviewfilterrule',- options={'verbose_name': 'filter rule', 'verbose_name_plural': 'filter rules'},+ name="savedviewfilterrule",+ options={+ "verbose_name": "filter rule",+ "verbose_name_plural": "filter rules",+ },),migrations.AlterModelOptions(- name='tag',- options={'verbose_name': 'tag', 'verbose_name_plural': 'tags'},+ name="tag",+ options={"verbose_name": "tag", "verbose_name_plural": "tags"},+ ),+ migrations.AlterField(+ model_name="correspondent",+ name="is_insensitive",+ field=models.BooleanField(default=True, verbose_name="is insensitive"),+ ),+ migrations.AlterField(+ model_name="correspondent",+ name="match",+ field=models.CharField(blank=True, max_length=256, verbose_name="match"),+ ),+ migrations.AlterField(+ model_name="correspondent",+ name="matching_algorithm",+ field=models.PositiveIntegerField(+ choices=[+ (1, "Any word"),+ (2, "All words"),+ (3, "Exact match"),+ (4, "Regular expression"),+ (5, "Fuzzy word"),+ (6, "Automatic"),+ ],+ default=1,+ verbose_name="matching algorithm",+ ),+ ),+ migrations.AlterField(+ model_name="correspondent",+ name="name",+ field=models.CharField(max_length=128, unique=True, verbose_name="name"),+ ),+ migrations.AlterField(+ model_name="document",+ name="added",+ field=models.DateTimeField(+ db_index=True,+ default=django.utils.timezone.now,+ editable=False,+ verbose_name="added",+ ),+ ),+ migrations.AlterField(+ model_name="document",+ name="archive_checksum",+ field=models.CharField(+ blank=True,+ editable=False,+ help_text="The checksum of the archived document.",+ max_length=32,+ null=True,+ verbose_name="archive checksum",+ ),+ ),+ migrations.AlterField(+ model_name="document",+ name="archive_serial_number",+ field=models.IntegerField(+ blank=True,+ db_index=True,+ help_text="The position of this document in your physical document archive.",+ null=True,+ unique=True,+ verbose_name="archive serial number",+ ),+ ),+ migrations.AlterField(+ model_name="document",+ name="checksum",+ field=models.CharField(+ editable=False,+ help_text="The checksum of the original document.",+ max_length=32,+ unique=True,+ verbose_name="checksum",+ ),+ ),+ migrations.AlterField(+ model_name="document",+ name="content",+ field=models.TextField(+ blank=True,+ help_text="The raw, text-only data of the document. This field is primarily used for searching.",+ verbose_name="content",+ ),+ ),+ migrations.AlterField(+ model_name="document",+ name="correspondent",+ field=models.ForeignKey(+ blank=True,+ null=True,+ on_delete=django.db.models.deletion.SET_NULL,+ related_name="documents",+ to="documents.correspondent",+ verbose_name="correspondent",+ ),+ ),+ migrations.AlterField(+ model_name="document",+ name="created",+ field=models.DateTimeField(+ db_index=True, default=django.utils.timezone.now, verbose_name="created"+ ),+ ),+ migrations.AlterField(+ model_name="document",+ name="document_type",+ field=models.ForeignKey(+ blank=True,+ null=True,+ on_delete=django.db.models.deletion.SET_NULL,+ related_name="documents",+ to="documents.documenttype",+ verbose_name="document type",+ ),+ ),+ migrations.AlterField(+ model_name="document",+ name="filename",+ field=models.FilePathField(+ default=None,+ editable=False,+ help_text="Current filename in storage",+ max_length=1024,+ null=True,+ verbose_name="filename",+ ),+ ),+ migrations.AlterField(+ model_name="document",+ name="mime_type",+ field=models.CharField(+ editable=False, max_length=256, verbose_name="mime type"+ ),+ ),+ migrations.AlterField(+ model_name="document",+ name="modified",+ field=models.DateTimeField(+ auto_now=True, db_index=True, verbose_name="modified"+ ),+ ),+ migrations.AlterField(+ model_name="document",+ name="storage_type",+ field=models.CharField(+ choices=[+ ("unencrypted", "Unencrypted"),+ ("gpg", "Encrypted with GNU Privacy Guard"),+ ],+ default="unencrypted",+ editable=False,+ max_length=11,+ verbose_name="storage type",+ ),+ ),+ migrations.AlterField(+ model_name="document",+ name="tags",+ field=models.ManyToManyField(+ blank=True,+ related_name="documents",+ to="documents.Tag",+ verbose_name="tags",+ ),+ ),+ migrations.AlterField(+ model_name="document",+ name="title",+ field=models.CharField(+ blank=True, db_index=True, max_length=128, verbose_name="title"+ ),),migrations.AlterField(- model_name='correspondent',- name='is_insensitive',- field=models.BooleanField(default=True, verbose_name='is insensitive'),- ),- migrations.AlterField(- model_name='correspondent',- name='match',- field=models.CharField(blank=True, max_length=256, verbose_name='match'),- ),- migrations.AlterField(- model_name='correspondent',- name='matching_algorithm',- field=models.PositiveIntegerField(choices=[(1, 'Any word'), (2, 'All words'), (3, 'Exact match'), (4, 'Regular expression'), (5, 'Fuzzy word'), (6, 'Automatic')], default=1, verbose_name='matching algorithm'),- ),- migrations.AlterField(- model_name='correspondent',- name='name',- field=models.CharField(max_length=128, unique=True, verbose_name='name'),- ),- migrations.AlterField(- model_name='document',- name='added',- field=models.DateTimeField(db_index=True, default=django.utils.timezone.now, editable=False, verbose_name='added'),- ),- migrations.AlterField(- model_name='document',- name='archive_checksum',- field=models.CharField(blank=True, editable=False, help_text='The checksum of the archived document.', max_length=32, null=True, verbose_name='archive checksum'),- ),- migrations.AlterField(- model_name='document',- name='archive_serial_number',- field=models.IntegerField(blank=True, db_index=True, help_text='The position of this document in your physical document archive.', null=True, unique=True, verbose_name='archive serial number'),- ),- migrations.AlterField(- model_name='document',- name='checksum',- field=models.CharField(editable=False, help_text='The checksum of the original document.', max_length=32, unique=True, verbose_name='checksum'),- ),- migrations.AlterField(- model_name='document',- name='content',- field=models.TextField(blank=True, help_text='The raw, text-only data of the document. This field is primarily used for searching.', verbose_name='content'),- ),- migrations.AlterField(- model_name='document',- name='correspondent',- field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='documents', to='documents.correspondent', verbose_name='correspondent'),- ),- migrations.AlterField(- model_name='document',- name='created',- field=models.DateTimeField(db_index=True, default=django.utils.timezone.now, verbose_name='created'),- ),- migrations.AlterField(- model_name='document',- name='document_type',- field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='documents', to='documents.documenttype', verbose_name='document type'),- ),- migrations.AlterField(- model_name='document',- name='filename',- field=models.FilePathField(default=None, editable=False, help_text='Current filename in storage', max_length=1024, null=True, verbose_name='filename'),- ),- migrations.AlterField(- model_name='document',- name='mime_type',- field=models.CharField(editable=False, max_length=256, verbose_name='mime type'),- ),- migrations.AlterField(- model_name='document',- name='modified',- field=models.DateTimeField(auto_now=True, db_index=True, verbose_name='modified'),- ),- migrations.AlterField(- model_name='document',- name='storage_type',- field=models.CharField(choices=[('unencrypted', 'Unencrypted'), ('gpg', 'Encrypted with GNU Privacy Guard')], default='unencrypted', editable=False, max_length=11, verbose_name='storage type'),- ),- migrations.AlterField(- model_name='document',- name='tags',- field=models.ManyToManyField(blank=True, related_name='documents', to='documents.Tag', verbose_name='tags'),- ),- migrations.AlterField(- model_name='document',- name='title',- field=models.CharField(blank=True, db_index=True, max_length=128, verbose_name='title'),- ),- migrations.AlterField(- model_name='documenttype',- name='is_insensitive',- field=models.BooleanField(default=True, verbose_name='is insensitive'),- ),- migrations.AlterField(- model_name='documenttype',- name='match',- field=models.CharField(blank=True, max_length=256, verbose_name='match'),- ),- migrations.AlterField(- model_name='documenttype',- name='matching_algorithm',- field=models.PositiveIntegerField(choices=[(1, 'Any word'), (2, 'All words'), (3, 'Exact match'), (4, 'Regular expression'), (5, 'Fuzzy word'), (6, 'Automatic')], default=1, verbose_name='matching algorithm'),- ),- migrations.AlterField(- model_name='documenttype',- name='name',- field=models.CharField(max_length=128, unique=True, verbose_name='name'),- ),- migrations.AlterField(- model_name='log',- name='created',- field=models.DateTimeField(auto_now_add=True, verbose_name='created'),- ),- migrations.AlterField(- model_name='log',- name='group',- field=models.UUIDField(blank=True, null=True, verbose_name='group'),- ),- migrations.AlterField(- model_name='log',- name='level',- field=models.PositiveIntegerField(choices=[(10, 'debug'), (20, 'information'), (30, 'warning'), (40, 'error'), (50, 'critical')], default=20, verbose_name='level'),- ),- migrations.AlterField(- model_name='log',- name='message',- field=models.TextField(verbose_name='message'),- ),- migrations.AlterField(- model_name='savedview',- name='name',- field=models.CharField(max_length=128, verbose_name='name'),- ),- migrations.AlterField(- model_name='savedview',- name='show_in_sidebar',- field=models.BooleanField(verbose_name='show in sidebar'),- ),- migrations.AlterField(- model_name='savedview',- name='show_on_dashboard',- field=models.BooleanField(verbose_name='show on dashboard'),- ),- migrations.AlterField(- model_name='savedview',- name='sort_field',- field=models.CharField(max_length=128, verbose_name='sort field'),- ),- migrations.AlterField(- model_name='savedview',- name='sort_reverse',- field=models.BooleanField(default=False, verbose_name='sort reverse'),- ),- migrations.AlterField(- model_name='savedview',- name='user',- field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, verbose_name='user'),- ),- migrations.AlterField(- model_name='savedviewfilterrule',- name='rule_type',- field=models.PositiveIntegerField(choices=[(0, 'title contains'), (1, 'content contains'), (2, 'ASN is'), (3, 'correspondent is'), (4, 'document type is'), (5, 'is in inbox'), (6, 'has tag'), (7, 'has any tag'), (8, 'created before'), (9, 'created after'), (10, 'created year is'), (11, 'created month is'), (12, 'created day is'), (13, 'added before'), (14, 'added after'), (15, 'modified before'), (16, 'modified after'), (17, 'does not have tag')], verbose_name='rule type'),- ),- migrations.AlterField(- model_name='savedviewfilterrule',- name='saved_view',- field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='filter_rules', to='documents.savedview', verbose_name='saved view'),- ),- migrations.AlterField(- model_name='savedviewfilterrule',- name='value',- field=models.CharField(blank=True, max_length=128, null=True, verbose_name='value'),- ),- migrations.AlterField(- model_name='tag',- name='colour',- field=models.PositiveIntegerField(choices=[(1, '#a6cee3'), (2, '#1f78b4'), (3, '#b2df8a'), (4, '#33a02c'), (5, '#fb9a99'), (6, '#e31a1c'), (7, '#fdbf6f'), (8, '#ff7f00'), (9, '#cab2d6'), (10, '#6a3d9a'), (11, '#b15928'), (12, '#000000'), (13, '#cccccc')], default=1, verbose_name='color'),- ),- migrations.AlterField(- model_name='tag',- name='is_inbox_tag',- field=models.BooleanField(default=False, help_text='Marks this tag as an inbox tag: All newly consumed documents will be tagged with inbox tags.', verbose_name='is inbox tag'),- ),- migrations.AlterField(- model_name='tag',- name='is_insensitive',- field=models.BooleanField(default=True, verbose_name='is insensitive'),- ),- migrations.AlterField(- model_name='tag',- name='match',- field=models.CharField(blank=True, max_length=256, verbose_name='match'),- ),- migrations.AlterField(- model_name='tag',- name='matching_algorithm',- field=models.PositiveIntegerField(choices=[(1, 'Any word'), (2, 'All words'), (3, 'Exact match'), (4, 'Regular expression'), (5, 'Fuzzy word'), (6, 'Automatic')], default=1, verbose_name='matching algorithm'),- ),- migrations.AlterField(- model_name='tag',- name='name',- field=models.CharField(max_length=128, unique=True, verbose_name='name'),+ model_name="documenttype",+ name="is_insensitive",+ field=models.BooleanField(default=True, verbose_name="is insensitive"),+ ),+ migrations.AlterField(+ model_name="documenttype",+ name="match",+ field=models.CharField(blank=True, max_length=256, verbose_name="match"),+ ),+ migrations.AlterField(+ model_name="documenttype",+ name="matching_algorithm",+ field=models.PositiveIntegerField(+ choices=[+ (1, "Any word"),+ (2, "All words"),+ (3, "Exact match"),+ (4, "Regular expression"),+ (5, "Fuzzy word"),+ (6, "Automatic"),+ ],+ default=1,+ verbose_name="matching algorithm",+ ),+ ),+ migrations.AlterField(+ model_name="documenttype",+ name="name",+ field=models.CharField(max_length=128, unique=True, verbose_name="name"),+ ),+ migrations.AlterField(+ model_name="log",+ name="created",+ field=models.DateTimeField(auto_now_add=True, verbose_name="created"),+ ),+ migrations.AlterField(+ model_name="log",+ name="group",+ field=models.UUIDField(blank=True, null=True, verbose_name="group"),+ ),+ migrations.AlterField(+ model_name="log",+ name="level",+ field=models.PositiveIntegerField(+ choices=[+ (10, "debug"),+ (20, "information"),+ (30, "warning"),+ (40, "error"),+ (50, "critical"),+ ],+ default=20,+ verbose_name="level",+ ),+ ),+ migrations.AlterField(+ model_name="log",+ name="message",+ field=models.TextField(verbose_name="message"),+ ),+ migrations.AlterField(+ model_name="savedview",+ name="name",+ field=models.CharField(max_length=128, verbose_name="name"),+ ),+ migrations.AlterField(+ model_name="savedview",+ name="show_in_sidebar",+ field=models.BooleanField(verbose_name="show in sidebar"),+ ),+ migrations.AlterField(+ model_name="savedview",+ name="show_on_dashboard",+ field=models.BooleanField(verbose_name="show on dashboard"),+ ),+ migrations.AlterField(+ model_name="savedview",+ name="sort_field",+ field=models.CharField(max_length=128, verbose_name="sort field"),+ ),+ migrations.AlterField(+ model_name="savedview",+ name="sort_reverse",+ field=models.BooleanField(default=False, verbose_name="sort reverse"),+ ),+ migrations.AlterField(+ model_name="savedview",+ name="user",+ field=models.ForeignKey(+ on_delete=django.db.models.deletion.CASCADE,+ to=settings.AUTH_USER_MODEL,+ verbose_name="user",+ ),+ ),+ migrations.AlterField(+ model_name="savedviewfilterrule",+ name="rule_type",+ field=models.PositiveIntegerField(+ choices=[+ (0, "title contains"),+ (1, "content contains"),+ (2, "ASN is"),+ (3, "correspondent is"),+ (4, "document type is"),+ (5, "is in inbox"),+ (6, "has tag"),+ (7, "has any tag"),+ (8, "created before"),+ (9, "created after"),+ (10, "created year is"),+ (11, "created month is"),+ (12, "created day is"),+ (13, "added before"),+ (14, "added after"),+ (15, "modified before"),+ (16, "modified after"),+ (17, "does not have tag"),+ ],+ verbose_name="rule type",+ ),+ ),+ migrations.AlterField(+ model_name="savedviewfilterrule",+ name="saved_view",+ field=models.ForeignKey(+ on_delete=django.db.models.deletion.CASCADE,+ related_name="filter_rules",+ to="documents.savedview",+ verbose_name="saved view",+ ),+ ),+ migrations.AlterField(+ model_name="savedviewfilterrule",+ name="value",+ field=models.CharField(+ blank=True, max_length=128, null=True, verbose_name="value"+ ),+ ),+ migrations.AlterField(+ model_name="tag",+ name="colour",+ field=models.PositiveIntegerField(+ choices=[+ (1, "#a6cee3"),+ (2, "#1f78b4"),+ (3, "#b2df8a"),+ (4, "#33a02c"),+ (5, "#fb9a99"),+ (6, "#e31a1c"),+ (7, "#fdbf6f"),+ (8, "#ff7f00"),+ (9, "#cab2d6"),+ (10, "#6a3d9a"),+ (11, "#b15928"),+ (12, "#000000"),+ (13, "#cccccc"),+ ],+ default=1,+ verbose_name="color",+ ),+ ),+ migrations.AlterField(+ model_name="tag",+ name="is_inbox_tag",+ field=models.BooleanField(+ default=False,+ help_text="Marks this tag as an inbox tag: All newly consumed documents will be tagged with inbox tags.",+ verbose_name="is inbox tag",+ ),+ ),+ migrations.AlterField(+ model_name="tag",+ name="is_insensitive",+ field=models.BooleanField(default=True, verbose_name="is insensitive"),+ ),+ migrations.AlterField(+ model_name="tag",+ name="match",+ field=models.CharField(blank=True, max_length=256, verbose_name="match"),+ ),+ migrations.AlterField(+ model_name="tag",+ name="matching_algorithm",+ field=models.PositiveIntegerField(+ choices=[+ (1, "Any word"),+ (2, "All words"),+ (3, "Exact match"),+ (4, "Regular expression"),+ (5, "Fuzzy word"),+ (6, "Automatic"),+ ],+ default=1,+ verbose_name="matching algorithm",+ ),+ ),+ migrations.AlterField(+ model_name="tag",+ name="name",+ field=models.CharField(max_length=128, unique=True, verbose_name="name"),),]diff --git a/src/documents/migrations/1012_fix_archive_files.py b/src/documents/migrations/1012_fix_archive_files.pyindex 5f5064396..ba91cf796 100644--- a/src/documents/migrations/1012_fix_archive_files.py+++ b/src/documents/migrations/1012_fix_archive_files.py@@ -20,6 +20,7 @@ logger = logging.getLogger("paperless.migrations")# This is code copied straight paperless before the change.###############################################################################+def archive_name_from_filename(filename):return os.path.splitext(filename)[0] + ".pdf"@@ -30,10 +31,7 @@ def archive_path_old(doc):else:fname = "{:07}.pdf".format(doc.pk)- return os.path.join(- settings.ARCHIVE_DIR,- fname- )+ return os.path.join(settings.ARCHIVE_DIR, fname)STORAGE_TYPE_GPG = "gpg"@@ -41,10 +39,7 @@ STORAGE_TYPE_GPG = "gpg"def archive_path_new(doc):if doc.archive_filename is not None:- return os.path.join(- settings.ARCHIVE_DIR,- str(doc.archive_filename)- )+ return os.path.join(settings.ARCHIVE_DIR, str(doc.archive_filename))else:return None@@ -57,10 +52,7 @@ def source_path(doc):if doc.storage_type == STORAGE_TYPE_GPG:fname += ".gpg" # pragma: no cover- return os.path.join(- settings.ORIGINALS_DIR,- fname- )+ return os.path.join(settings.ORIGINALS_DIR, fname)def generate_unique_filename(doc, archive_filename=False):@@ -75,7 +67,8 @@ def generate_unique_filename(doc, archive_filename=False):while True:new_filename = generate_filename(- doc, counter, archive_filename=archive_filename)+ doc, counter, archive_filename=archive_filename+ )if new_filename == old_filename:# still the same as before.return new_filename@@ -91,14 +84,11 @@ def generate_filename(doc, counter=0, append_gpg=True, archive_filename=False):try:if settings.PAPERLESS_FILENAME_FORMAT is not None:- tags = defaultdictNoStr(lambda: slugify(None),- many_to_dictionary(doc.tags))+ tags = defaultdictNoStr(lambda: slugify(None), many_to_dictionary(doc.tags))tag_list = pathvalidate.sanitize_filename(- ",".join(sorted(- [tag.name for tag in doc.tags.all()]- )),- replacement_text="-"+ ",".join(sorted([tag.name for tag in doc.tags.all()])),+ replacement_text="-",)if doc.correspondent:@@ -116,20 +106,21 @@ def generate_filename(doc, counter=0, append_gpg=True, archive_filename=False):document_type = "none"path = settings.PAPERLESS_FILENAME_FORMAT.format(- title=pathvalidate.sanitize_filename(- doc.title, replacement_text="-"),+ title=pathvalidate.sanitize_filename(doc.title, replacement_text="-"),correspondent=correspondent,document_type=document_type,created=datetime.date.isoformat(doc.created),created_year=doc.created.year if doc.created else "none",- created_month=f"{doc.created.month:02}" if doc.created else "none", # NOQA: E501+ created_month=f"{doc.created.month:02}"+ if doc.created+ else "none", # NOQA: E501created_day=f"{doc.created.day:02}" if doc.created else "none",added=datetime.date.isoformat(doc.added),added_year=doc.added.year if doc.added else "none",added_month=f"{doc.added.month:02}" if doc.added else "none",added_day=f"{doc.added.day:02}" if doc.added else "none",tags=tags,- tag_list=tag_list+ tag_list=tag_list,).strip()path = path.strip(os.sep)@@ -137,7 +128,8 @@ def generate_filename(doc, counter=0, append_gpg=True, archive_filename=False):except (ValueError, KeyError, IndexError):logger.warning(f"Invalid PAPERLESS_FILENAME_FORMAT: "- f"{settings.PAPERLESS_FILENAME_FORMAT}, falling back to default")+ f"{settings.PAPERLESS_FILENAME_FORMAT}, falling back to default"+ )counter_str = f"_{counter:02}" if counter else ""@@ -166,29 +158,29 @@ def parse_wrapper(parser, path, mime_type, file_name):def create_archive_version(doc, retry_count=3):- from documents.parsers import get_parser_class_for_mime_type, \- DocumentParser, \- ParseError-- logger.info(- f"Regenerating archive document for document ID:{doc.id}"+ from documents.parsers import (+ get_parser_class_for_mime_type,+ DocumentParser,+ ParseError,)++ logger.info(f"Regenerating archive document for document ID:{doc.id}")parser_class = get_parser_class_for_mime_type(doc.mime_type)for try_num in range(retry_count):parser: DocumentParser = parser_class(None, None)try:- parse_wrapper(parser, source_path(doc), doc.mime_type,- os.path.basename(doc.filename))+ parse_wrapper(+ parser, source_path(doc), doc.mime_type, os.path.basename(doc.filename)+ )doc.content = parser.get_text()- if parser.get_archive_path() and os.path.isfile(- parser.get_archive_path()):+ if parser.get_archive_path() and os.path.isfile(parser.get_archive_path()):doc.archive_filename = generate_unique_filename(- doc, archive_filename=True)+ doc, archive_filename=True+ )with open(parser.get_archive_path(), "rb") as f:doc.archive_checksum = hashlib.md5(f.read()).hexdigest()- os.makedirs(os.path.dirname(archive_path_new(doc)),- exist_ok=True)+ os.makedirs(os.path.dirname(archive_path_new(doc)), exist_ok=True)shutil.copy2(parser.get_archive_path(), archive_path_new(doc))else:doc.archive_checksum = None@@ -241,8 +233,8 @@ def move_old_to_new_locations(apps, schema_editor):old_path = archive_path_old(doc)if doc.id not in affected_document_ids and not os.path.isfile(old_path):raise ValueError(- f"Archived document ID:{doc.id} does not exist at: "- f"{old_path}")+ f"Archived document ID:{doc.id} does not exist at: " f"{old_path}"+ )# check that we can regenerate affected archive versionsfor doc_id in affected_document_ids:@@ -253,7 +245,8 @@ def move_old_to_new_locations(apps, schema_editor):if not parser_class:raise ValueError(f"Document ID:{doc.id} has an invalid archived document, "- f"but no parsers are available. Cannot migrate.")+ f"but no parsers are available. Cannot migrate."+ )for doc in Document.objects.filter(archive_checksum__isnull=False):@@ -261,9 +254,7 @@ def move_old_to_new_locations(apps, schema_editor):old_path = archive_path_old(doc)# remove affected archive versionsif os.path.isfile(old_path):- logger.debug(- f"Removing {old_path}"- )+ logger.debug(f"Removing {old_path}")os.unlink(old_path)else:# Set archive path for unaffected files@@ -290,7 +281,8 @@ def move_new_to_old_locations(apps, schema_editor):raise ValueError(f"Cannot migrate: Archive file name {old_archive_path} of "f"document {doc.filename} would clash with another archive "- f"filename.")+ f"filename."+ )old_archive_paths.add(old_archive_path)if new_archive_path != old_archive_path and os.path.isfile(old_archive_path):raise ValueError(@@ -309,22 +301,35 @@ def move_new_to_old_locations(apps, schema_editor):class Migration(migrations.Migration):dependencies = [- ('documents', '1011_auto_20210101_2340'),+ ("documents", "1011_auto_20210101_2340"),]operations = [migrations.AddField(- model_name='document',- name='archive_filename',- field=models.FilePathField(default=None, editable=False, help_text='Current archive filename in storage', max_length=1024, null=True, unique=True, verbose_name='archive filename'),+ model_name="document",+ name="archive_filename",+ field=models.FilePathField(+ default=None,+ editable=False,+ help_text="Current archive filename in storage",+ max_length=1024,+ null=True,+ unique=True,+ verbose_name="archive filename",+ ),),migrations.AlterField(- model_name='document',- name='filename',- field=models.FilePathField(default=None, editable=False, help_text='Current filename in storage', max_length=1024, null=True, unique=True, verbose_name='filename'),- ),- migrations.RunPython(- move_old_to_new_locations,- move_new_to_old_locations+ model_name="document",+ name="filename",+ field=models.FilePathField(+ default=None,+ editable=False,+ help_text="Current filename in storage",+ max_length=1024,+ null=True,+ unique=True,+ verbose_name="filename",+ ),),+ migrations.RunPython(move_old_to_new_locations, move_new_to_old_locations),]diff --git a/src/documents/migrations/1013_migrate_tag_colour.py b/src/documents/migrations/1013_migrate_tag_colour.pyindex 323ff2bfb..4714f97c5 100644--- a/src/documents/migrations/1013_migrate_tag_colour.py+++ b/src/documents/migrations/1013_migrate_tag_colour.py@@ -20,7 +20,7 @@ COLOURS_OLD = {def forward(apps, schema_editor):- Tag = apps.get_model('documents', 'Tag')+ Tag = apps.get_model("documents", "Tag")for tag in Tag.objects.all():colour_old_id = tag.colour_old@@ -30,7 +30,7 @@ def forward(apps, schema_editor):def reverse(apps, schema_editor):- Tag = apps.get_model('documents', 'Tag')+ Tag = apps.get_model("documents", "Tag")def _get_colour_id(rdb):for idx, rdbx in COLOURS_OLD.items():@@ -48,23 +48,25 @@ def reverse(apps, schema_editor):class Migration(migrations.Migration):dependencies = [- ('documents', '1012_fix_archive_files'),+ ("documents", "1012_fix_archive_files"),]operations = [migrations.RenameField(- model_name='tag',- old_name='colour',- new_name='colour_old',+ model_name="tag",+ old_name="colour",+ new_name="colour_old",),migrations.AddField(- model_name='tag',- name='color',- field=models.CharField(default='#a6cee3', max_length=7, verbose_name='color'),+ model_name="tag",+ name="color",+ field=models.CharField(+ default="#a6cee3", max_length=7, verbose_name="color"+ ),),migrations.RunPython(forward, reverse),migrations.RemoveField(- model_name='tag',- name='colour_old',- )+ model_name="tag",+ name="colour_old",+ ),]diff --git a/src/documents/migrations/1014_auto_20210228_1614.py b/src/documents/migrations/1014_auto_20210228_1614.pyindex cb716fa82..c3f16b841 100644--- a/src/documents/migrations/1014_auto_20210228_1614.py+++ b/src/documents/migrations/1014_auto_20210228_1614.py@@ -6,13 +6,37 @@ from django.db import migrations, modelsclass Migration(migrations.Migration):dependencies = [- ('documents', '1013_migrate_tag_colour'),+ ("documents", "1013_migrate_tag_colour"),]operations = [migrations.AlterField(- model_name='savedviewfilterrule',- name='rule_type',- field=models.PositiveIntegerField(choices=[(0, 'title contains'), (1, 'content contains'), (2, 'ASN is'), (3, 'correspondent is'), (4, 'document type is'), (5, 'is in inbox'), (6, 'has tag'), (7, 'has any tag'), (8, 'created before'), (9, 'created after'), (10, 'created year is'), (11, 'created month is'), (12, 'created day is'), (13, 'added before'), (14, 'added after'), (15, 'modified before'), (16, 'modified after'), (17, 'does not have tag'), (18, 'does not have ASN'), (19, 'title or content contains')], verbose_name='rule type'),+ model_name="savedviewfilterrule",+ name="rule_type",+ field=models.PositiveIntegerField(+ choices=[+ (0, "title contains"),+ (1, "content contains"),+ (2, "ASN is"),+ (3, "correspondent is"),+ (4, "document type is"),+ (5, "is in inbox"),+ (6, "has tag"),+ (7, "has any tag"),+ (8, "created before"),+ (9, "created after"),+ (10, "created year is"),+ (11, "created month is"),+ (12, "created day is"),+ (13, "added before"),+ (14, "added after"),+ (15, "modified before"),+ (16, "modified after"),+ (17, "does not have tag"),+ (18, "does not have ASN"),+ (19, "title or content contains"),+ ],+ verbose_name="rule type",+ ),),]diff --git a/src/documents/migrations/1015_remove_null_characters.py b/src/documents/migrations/1015_remove_null_characters.pyindex 2f7ee99b6..accc41162 100644--- a/src/documents/migrations/1015_remove_null_characters.py+++ b/src/documents/migrations/1015_remove_null_characters.py@@ -8,20 +8,20 @@ logger = logging.getLogger("paperless.migrations")def remove_null_characters(apps, schema_editor):- Document = apps.get_model('documents', 'Document')+ Document = apps.get_model("documents", "Document")for doc in Document.objects.all():content: str = doc.content- if '\0' in content:+ if "\0" in content:logger.info(f"Removing null characters from document {doc}...")- doc.content = content.replace('\0', ' ')+ doc.content = content.replace("\0", " ")doc.save()class Migration(migrations.Migration):dependencies = [- ('documents', '1014_auto_20210228_1614'),+ ("documents", "1014_auto_20210228_1614"),]operations = [diff --git a/src/documents/migrations/1016_auto_20210317_1351.py b/src/documents/migrations/1016_auto_20210317_1351.pyindex 733c1bb33..53994f916 100644--- a/src/documents/migrations/1016_auto_20210317_1351.py+++ b/src/documents/migrations/1016_auto_20210317_1351.py@@ -6,18 +6,46 @@ from django.db import migrations, modelsclass Migration(migrations.Migration):dependencies = [- ('documents', '1015_remove_null_characters'),+ ("documents", "1015_remove_null_characters"),]operations = [migrations.AlterField(- model_name='savedview',- name='sort_field',- field=models.CharField(blank=True, max_length=128, null=True, verbose_name='sort field'),+ model_name="savedview",+ name="sort_field",+ field=models.CharField(+ blank=True, max_length=128, null=True, verbose_name="sort field"+ ),),migrations.AlterField(- model_name='savedviewfilterrule',- name='rule_type',- field=models.PositiveIntegerField(choices=[(0, 'title contains'), (1, 'content contains'), (2, 'ASN is'), (3, 'correspondent is'), (4, 'document type is'), (5, 'is in inbox'), (6, 'has tag'), (7, 'has any tag'), (8, 'created before'), (9, 'created after'), (10, 'created year is'), (11, 'created month is'), (12, 'created day is'), (13, 'added before'), (14, 'added after'), (15, 'modified before'), (16, 'modified after'), (17, 'does not have tag'), (18, 'does not have ASN'), (19, 'title or content contains'), (20, 'fulltext query'), (21, 'more like this')], verbose_name='rule type'),+ model_name="savedviewfilterrule",+ name="rule_type",+ field=models.PositiveIntegerField(+ choices=[+ (0, "title contains"),+ (1, "content contains"),+ (2, "ASN is"),+ (3, "correspondent is"),+ (4, "document type is"),+ (5, "is in inbox"),+ (6, "has tag"),+ (7, "has any tag"),+ (8, "created before"),+ (9, "created after"),+ (10, "created year is"),+ (11, "created month is"),+ (12, "created day is"),+ (13, "added before"),+ (14, "added after"),+ (15, "modified before"),+ (16, "modified after"),+ (17, "does not have tag"),+ (18, "does not have ASN"),+ (19, "title or content contains"),+ (20, "fulltext query"),+ (21, "more like this"),+ ],+ verbose_name="rule type",+ ),),]diff --git a/src/documents/models.py b/src/documents/models.pyindex 1e9760334..02a6b56dc 100644--- a/src/documents/models.py+++ b/src/documents/models.py@@ -37,23 +37,15 @@ class MatchingModel(models.Model):(MATCH_AUTO, _("Automatic")),)- name = models.CharField(- _("name"),- max_length=128, unique=True)+ name = models.CharField(_("name"), max_length=128, unique=True)- match = models.CharField(- _("match"),- max_length=256, blank=True)+ match = models.CharField(_("match"), max_length=256, blank=True)matching_algorithm = models.PositiveIntegerField(- _("matching algorithm"),- choices=MATCHING_ALGORITHMS,- default=MATCH_ANY+ _("matching algorithm"), choices=MATCHING_ALGORITHMS, default=MATCH_ANY)- is_insensitive = models.BooleanField(- _("is insensitive"),- default=True)+ is_insensitive = models.BooleanField(_("is insensitive"), default=True)class Meta:abstract = True@@ -64,7 +56,6 @@ class MatchingModel(models.Model):class Correspondent(MatchingModel):-class Meta:ordering = ("name",)verbose_name = _("correspondent")@@ -73,17 +64,15 @@ class Correspondent(MatchingModel):class Tag(MatchingModel):- color = models.CharField(- _("color"),- max_length=7,- default="#a6cee3"- )+ color = models.CharField(_("color"), max_length=7, default="#a6cee3")is_inbox_tag = models.BooleanField(_("is inbox tag"),default=False,- help_text=_("Marks this tag as an inbox tag: All newly consumed "- "documents will be tagged with inbox tags.")+ help_text=_(+ "Marks this tag as an inbox tag: All newly consumed "+ "documents will be tagged with inbox tags."+ ),)class Meta:@@ -92,7 +81,6 @@ class Tag(MatchingModel):class DocumentType(MatchingModel):-class Meta:verbose_name = _("document type")verbose_name_plural = _("document types")@@ -104,7 +92,7 @@ class Document(models.Model):STORAGE_TYPE_GPG = "gpg"STORAGE_TYPES = ((STORAGE_TYPE_UNENCRYPTED, _("Unencrypted")),- (STORAGE_TYPE_GPG, _("Encrypted with GNU Privacy Guard"))+ (STORAGE_TYPE_GPG, _("Encrypted with GNU Privacy Guard")),)correspondent = models.ForeignKey(@@ -113,12 +101,10 @@ class Document(models.Model):null=True,related_name="documents",on_delete=models.SET_NULL,- verbose_name=_("correspondent")+ verbose_name=_("correspondent"),)- title = models.CharField(- _("title"),- max_length=128, blank=True, db_index=True)+ title = models.CharField(_("title"), max_length=128, blank=True, db_index=True)document_type = models.ForeignKey(DocumentType,@@ -126,25 +112,22 @@ class Document(models.Model):null=True,related_name="documents",on_delete=models.SET_NULL,- verbose_name=_("document type")+ verbose_name=_("document type"),)content = models.TextField(_("content"),blank=True,- help_text=_("The raw, text-only data of the document. This field is "- "primarily used for searching.")+ help_text=_(+ "The raw, text-only data of the document. This field is "+ "primarily used for searching."+ ),)- mime_type = models.CharField(- _("mime type"),- max_length=256,- editable=False- )+ mime_type = models.CharField(_("mime type"), max_length=256, editable=False)tags = models.ManyToManyField(- Tag, related_name="documents", blank=True,- verbose_name=_("tags")+ Tag, related_name="documents", blank=True, verbose_name=_("tags"))checksum = models.CharField(@@ -152,7 +135,7 @@ class Document(models.Model):max_length=32,editable=False,unique=True,- help_text=_("The checksum of the original document.")+ help_text=_("The checksum of the original document."),)archive_checksum = models.CharField(@@ -161,28 +144,26 @@ class Document(models.Model):editable=False,blank=True,null=True,- help_text=_("The checksum of the archived document.")+ help_text=_("The checksum of the archived document."),)- created = models.DateTimeField(- _("created"),- default=timezone.now, db_index=True)+ created = models.DateTimeField(_("created"), default=timezone.now, db_index=True)modified = models.DateTimeField(- _("modified"),- auto_now=True, editable=False, db_index=True)+ _("modified"), auto_now=True, editable=False, db_index=True+ )storage_type = models.CharField(_("storage type"),max_length=11,choices=STORAGE_TYPES,default=STORAGE_TYPE_UNENCRYPTED,- editable=False+ editable=False,)added = models.DateTimeField(- _("added"),- default=timezone.now, editable=False, db_index=True)+ _("added"), default=timezone.now, editable=False, db_index=True+ )filename = models.FilePathField(_("filename"),@@ -191,7 +172,7 @@ class Document(models.Model):default=None,unique=True,null=True,- help_text=_("Current filename in storage")+ help_text=_("Current filename in storage"),)archive_filename = models.FilePathField(@@ -201,7 +182,7 @@ class Document(models.Model):default=None,unique=True,null=True,- help_text=_("Current archive filename in storage")+ help_text=_("Current archive filename in storage"),)archive_serial_number = models.IntegerField(@@ -210,8 +191,9 @@ class Document(models.Model):null=True,unique=True,db_index=True,- help_text=_("The position of this document in your physical document "- "archive.")+ help_text=_(+ "The position of this document in your physical document " "archive."+ ),)class Meta:@@ -238,10 +220,7 @@ class Document(models.Model):if self.storage_type == self.STORAGE_TYPE_GPG:fname += ".gpg" # pragma: no cover- return os.path.join(- settings.ORIGINALS_DIR,- fname- )+ return os.path.join(settings.ORIGINALS_DIR, fname)@propertydef source_file(self):@@ -254,10 +233,7 @@ class Document(models.Model):@propertydef archive_path(self):if self.has_archive_version:- return os.path.join(- settings.ARCHIVE_DIR,- str(self.archive_filename)- )+ return os.path.join(settings.ARCHIVE_DIR, str(self.archive_filename))else:return None@@ -291,10 +267,7 @@ class Document(models.Model):if self.storage_type == self.STORAGE_TYPE_GPG:file_name += ".gpg"- return os.path.join(- settings.THUMBNAIL_DIR,- file_name- )+ return os.path.join(settings.THUMBNAIL_DIR, file_name)@propertydef thumbnail_file(self):@@ -311,15 +284,13 @@ class Log(models.Model):(logging.CRITICAL, _("critical")),)- group = models.UUIDField(- _("group"),- blank=True, null=True)+ group = models.UUIDField(_("group"), blank=True, null=True)message = models.TextField(_("message"))level = models.PositiveIntegerField(- _("level"),- choices=LEVELS, default=logging.INFO)+ _("level"), choices=LEVELS, default=logging.INFO+ )created = models.DateTimeField(_("created"), auto_now_add=True)@@ -333,18 +304,14 @@ class Log(models.Model):class SavedView(models.Model):-class Meta:ordering = ("name",)verbose_name = _("saved view")verbose_name_plural = _("saved views")- user = models.ForeignKey(User, on_delete=models.CASCADE,- verbose_name=_("user"))- name = models.CharField(- _("name"),- max_length=128)+ user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name=_("user"))+ name = models.CharField(_("name"), max_length=128)show_on_dashboard = models.BooleanField(_("show on dashboard"),@@ -354,14 +321,9 @@ class SavedView(models.Model):)sort_field = models.CharField(- _("sort field"),- max_length=128,- null=True,- blank=True+ _("sort field"), max_length=128, null=True, blank=True)- sort_reverse = models.BooleanField(- _("sort reverse"),- default=False)+ sort_reverse = models.BooleanField(_("sort reverse"), default=False)class SavedViewFilterRule(models.Model):@@ -388,25 +350,19 @@ class SavedViewFilterRule(models.Model):(19, _("title or content contains")),(20, _("fulltext query")),(21, _("more like this")),- (22, _("has tags in"))+ (22, _("has tags in")),]saved_view = models.ForeignKey(SavedView,on_delete=models.CASCADE,related_name="filter_rules",- verbose_name=_("saved view")+ verbose_name=_("saved view"),)- rule_type = models.PositiveIntegerField(- _("rule type"),- choices=RULE_TYPES)+ rule_type = models.PositiveIntegerField(_("rule type"), choices=RULE_TYPES)- value = models.CharField(- _("value"),- max_length=128,- blank=True,- null=True)+ value = models.CharField(_("value"), max_length=128, blank=True, null=True)class Meta:verbose_name = _("filter rule")@@ -416,20 +372,23 @@ class SavedViewFilterRule(models.Model):# TODO: why is this in the models file?class FileInfo:- REGEXES = OrderedDict([- ("created-title", re.compile(- r"^(?P<created>\d\d\d\d\d\d\d\d(\d\d\d\d\d\d)?Z) - "- r"(?P<title>.*)$",- flags=re.IGNORECASE- )),- ("title", re.compile(- r"(?P<title>.*)$",- flags=re.IGNORECASE- ))- ])-- def __init__(self, created=None, correspondent=None, title=None, tags=(),- extension=None):+ REGEXES = OrderedDict(+ [+ (+ "created-title",+ re.compile(+ r"^(?P<created>\d\d\d\d\d\d\d\d(\d\d\d\d\d\d)?Z) - "+ r"(?P<title>.*)$",+ flags=re.IGNORECASE,+ ),+ ),+ ("title", re.compile(r"(?P<title>.*)$", flags=re.IGNORECASE)),+ ]+ )++ def __init__(+ self, created=None, correspondent=None, title=None, tags=(), extension=None+ ):self.created = createdself.title = title@@ -451,9 +410,7 @@ class FileInfo:@classmethoddef _mangle_property(cls, properties, name):if name in properties:- properties[name] = getattr(cls, "_get_{}".format(name))(- properties[name]- )+ properties[name] = getattr(cls, "_get_{}".format(name))(properties[name])@classmethoddef from_filename(cls, filename):diff --git a/src/documents/parsers.py b/src/documents/parsers.pyindex 8cb8f5399..f179337a4 100644--- a/src/documents/parsers.py+++ b/src/documents/parsers.py@@ -27,11 +27,11 @@ from documents.signals import document_consumer_declaration# TODO: isnt there a date parsing library for this?DATE_REGEX = re.compile(- r'(\b|(?!=([_-])))([0-9]{1,2})[\.\/-]([0-9]{1,2})[\.\/-]([0-9]{4}|[0-9]{2})(\b|(?=([_-])))|' # NOQA: E501- r'(\b|(?!=([_-])))([0-9]{4}|[0-9]{2})[\.\/-]([0-9]{1,2})[\.\/-]([0-9]{1,2})(\b|(?=([_-])))|' # NOQA: E501- r'(\b|(?!=([_-])))([0-9]{1,2}[\. ]+[^ ]{3,9} ([0-9]{4}|[0-9]{2}))(\b|(?=([_-])))|' # NOQA: E501- r'(\b|(?!=([_-])))([^\W\d_]{3,9} [0-9]{1,2}, ([0-9]{4}))(\b|(?=([_-])))|'- r'(\b|(?!=([_-])))([^\W\d_]{3,9} [0-9]{4})(\b|(?=([_-])))'+ r"(\b|(?!=([_-])))([0-9]{1,2})[\.\/-]([0-9]{1,2})[\.\/-]([0-9]{4}|[0-9]{2})(\b|(?=([_-])))|" # NOQA: E501+ r"(\b|(?!=([_-])))([0-9]{4}|[0-9]{2})[\.\/-]([0-9]{1,2})[\.\/-]([0-9]{1,2})(\b|(?=([_-])))|" # NOQA: E501+ r"(\b|(?!=([_-])))([0-9]{1,2}[\. ]+[^ ]{3,9} ([0-9]{4}|[0-9]{2}))(\b|(?=([_-])))|" # NOQA: E501+ r"(\b|(?!=([_-])))([^\W\d_]{3,9} [0-9]{1,2}, ([0-9]{4}))(\b|(?=([_-])))|"+ r"(\b|(?!=([_-])))([^\W\d_]{3,9} [0-9]{4})(\b|(?=([_-])))")@@ -93,8 +93,7 @@ def get_parser_class_for_mime_type(mime_type):return None# Return the parser with the highest weight.- return sorted(- options, key=lambda _: _["weight"], reverse=True)[0]["parser"]+ return sorted(options, key=lambda _: _["weight"], reverse=True)[0]["parser"]def get_parser_class(path):@@ -107,18 +106,20 @@ def get_parser_class(path):return get_parser_class_for_mime_type(mime_type)-def run_convert(input_file,- output_file,- density=None,- scale=None,- alpha=None,- strip=False,- trim=False,- type=None,- depth=None,- auto_orient=False,- extra=None,- logging_group=None):+def run_convert(+ input_file,+ output_file,+ density=None,+ scale=None,+ alpha=None,+ strip=False,+ trim=False,+ type=None,+ depth=None,+ auto_orient=False,+ extra=None,+ logging_group=None,+):environment = os.environ.copy()if settings.CONVERT_MEMORY_LIMIT:@@ -127,17 +128,17 @@ def run_convert(input_file,environment["MAGICK_TMPDIR"] = settings.CONVERT_TMPDIRargs = [settings.CONVERT_BINARY]- args += ['-density', str(density)] if density else []- args += ['-scale', str(scale)] if scale else []- args += ['-alpha', str(alpha)] if alpha else []- args += ['-strip'] if strip else []- args += ['-trim'] if trim else []- args += ['-type', str(type)] if type else []- args += ['-depth', str(depth)] if depth else []- args += ['-auto-orient'] if auto_orient else []+ args += ["-density", str(density)] if density else []+ args += ["-scale", str(scale)] if scale else []+ args += ["-alpha", str(alpha)] if alpha else []+ args += ["-strip"] if strip else []+ args += ["-trim"] if trim else []+ args += ["-type", str(type)] if type else []+ args += ["-depth", str(depth)] if depth else []+ args += ["-auto-orient"] if auto_orient else []args += [input_file, output_file]- logger.debug("Execute: " + " ".join(args), extra={'group': logging_group})+ logger.debug("Execute: " + " ".join(args), extra={"group": logging_group})if not subprocess.Popen(args, env=environment).wait() == 0:raise ParseError("Convert failed at {}".format(args))@@ -155,27 +156,25 @@ def make_thumbnail_from_pdf_gs_fallback(in_path, temp_dir, logging_group=None):logger.warning("Thumbnail generation with ImageMagick failed, falling back ""to ghostscript. Check your /etc/ImageMagick-x/policy.xml!",- extra={'group': logging_group}+ extra={"group": logging_group},)gs_out_path = os.path.join(temp_dir, "gs_out.png")- cmd = [settings.GS_BINARY,- "-q",- "-sDEVICE=pngalpha",- "-o", gs_out_path,- in_path]+ cmd = [settings.GS_BINARY, "-q", "-sDEVICE=pngalpha", "-o", gs_out_path, in_path]try:if not subprocess.Popen(cmd).wait() == 0:raise ParseError("Thumbnail (gs) failed at {}".format(cmd))# then run convert on the output from gs- run_convert(density=300,- scale="500x5000>",- alpha="remove",- strip=True,- trim=False,- auto_orient=True,- input_file=gs_out_path,- output_file=out_path,- logging_group=logging_group)+ run_convert(+ density=300,+ scale="500x5000>",+ alpha="remove",+ strip=True,+ trim=False,+ auto_orient=True,+ input_file=gs_out_path,+ output_file=out_path,+ logging_group=logging_group,+ )return out_path@@ -191,18 +190,19 @@ def make_thumbnail_from_pdf(in_path, temp_dir, logging_group=None):# Run convert to get a decent thumbnailtry:- run_convert(density=300,- scale="500x5000>",- alpha="remove",- strip=True,- trim=False,- auto_orient=True,- input_file="{}[0]".format(in_path),- output_file=out_path,- logging_group=logging_group)+ run_convert(+ density=300,+ scale="500x5000>",+ alpha="remove",+ strip=True,+ trim=False,+ auto_orient=True,+ input_file="{}[0]".format(in_path),+ output_file=out_path,+ logging_group=logging_group,+ )except ParseError:- out_path = make_thumbnail_from_pdf_gs_fallback(- in_path, temp_dir, logging_group)+ out_path = make_thumbnail_from_pdf_gs_fallback(in_path, temp_dir, logging_group)return out_path@@ -223,15 +223,17 @@ def parse_date(filename, text):settings={"DATE_ORDER": date_order,"PREFER_DAY_OF_MONTH": "first",- "RETURN_AS_TIMEZONE_AWARE":- True- }+ "RETURN_AS_TIMEZONE_AWARE": True,+ },)def __filter(date):- if date and date.year > 1900 and \- date <= timezone.now() and \- date.date() not in settings.IGNORE_DATES:+ if (+ date+ and date.year > 1900+ and date <= timezone.now()+ and date.date() not in settings.IGNORE_DATES+ ):return datereturn None@@ -285,8 +287,7 @@ class DocumentParser(LoggingMixin):super().__init__()self.logging_group = logging_groupos.makedirs(settings.SCRATCH_DIR, exist_ok=True)- self.tempdir = tempfile.mkdtemp(- prefix="paperless-", dir=settings.SCRATCH_DIR)+ self.tempdir = tempfile.mkdtemp(prefix="paperless-", dir=settings.SCRATCH_DIR)self.archive_path = Noneself.text = None@@ -312,18 +313,21 @@ class DocumentParser(LoggingMixin):"""raise NotImplementedError()- def get_optimised_thumbnail(self,- document_path,- mime_type,- file_name=None):+ def get_optimised_thumbnail(self, document_path, mime_type, file_name=None):thumbnail = self.get_thumbnail(document_path, mime_type, file_name)if settings.OPTIMIZE_THUMBNAILS:out_path = os.path.join(self.tempdir, "thumb_optipng.png")- args = (settings.OPTIPNG_BINARY,- "-silent", "-o5", thumbnail, "-out", out_path)+ args = (+ settings.OPTIPNG_BINARY,+ "-silent",+ "-o5",+ thumbnail,+ "-out",+ out_path,+ )- self.log('debug', f"Execute: {' '.join(args)}")+ self.log("debug", f"Execute: {' '.join(args)}")if not subprocess.Popen(args).wait() == 0:raise ParseError("Optipng failed at {}".format(args))diff --git a/src/documents/sanity_checker.py b/src/documents/sanity_checker.pyindex 26467d3cf..5dee84258 100644--- a/src/documents/sanity_checker.py+++ b/src/documents/sanity_checker.py@@ -9,7 +9,6 @@ from documents.models import Documentclass SanityCheckMessages:-def __init__(self):self._messages = []@@ -29,7 +28,7 @@ class SanityCheckMessages:logger.info("Sanity checker detected no issues.")else:for msg in self._messages:- logger.log(msg['level'], msg['message'])+ logger.log(msg["level"], msg["message"])def __len__(self):return len(self._messages)@@ -38,10 +37,10 @@ class SanityCheckMessages:return self._messages[item]def has_error(self):- return any([msg['level'] == logging.ERROR for msg in self._messages])+ return any([msg["level"] == logging.ERROR for msg in self._messages])def has_warning(self):- return any([msg['level'] == logging.WARNING for msg in self._messages])+ return any([msg["level"] == logging.WARNING for msg in self._messages])class SanityCheckFailedException(Exception):@@ -71,9 +70,7 @@ def check_sanity(progress=False):with doc.thumbnail_file as f:f.read()except OSError as e:- messages.error(- f"Cannot read thumbnail file of document {doc.pk}: {e}"- )+ messages.error(f"Cannot read thumbnail file of document {doc.pk}: {e}")# Check sanity of the original file# TODO: extract method@@ -86,8 +83,7 @@ def check_sanity(progress=False):with doc.source_file as f:checksum = hashlib.md5(f.read()).hexdigest()except OSError as e:- messages.error(- f"Cannot read original file of document {doc.pk}: {e}")+ messages.error(f"Cannot read original file of document {doc.pk}: {e}")else:if not checksum == doc.checksum:messages.error(@@ -108,9 +104,7 @@ def check_sanity(progress=False):)elif doc.has_archive_version:if not os.path.isfile(doc.archive_path):- messages.error(- f"Archived version of document {doc.pk} does not exist."- )+ messages.error(f"Archived version of document {doc.pk} does not exist.")else:if os.path.normpath(doc.archive_path) in present_files:present_files.remove(os.path.normpath(doc.archive_path))diff --git a/src/documents/serialisers.py b/src/documents/serialisers.pyindex 3641c73a5..0206eb2ae 100644--- a/src/documents/serialisers.py+++ b/src/documents/serialisers.py@@ -7,8 +7,15 @@ from rest_framework import serializersfrom rest_framework.fields import SerializerMethodFieldfrom . import bulk_edit-from .models import Correspondent, Tag, Document, DocumentType, \- SavedView, SavedViewFilterRule, MatchingModel+from .models import (+ Correspondent,+ Tag,+ Document,+ DocumentType,+ SavedView,+ SavedViewFilterRule,+ MatchingModel,+)from .parsers import is_mime_type_supportedfrom django.utils.translation import gettext as _@@ -23,7 +30,7 @@ class DynamicFieldsModelSerializer(serializers.ModelSerializer):def __init__(self, *args, **kwargs):# Don't pass the 'fields' arg up to the superclass- fields = kwargs.pop('fields', None)+ fields = kwargs.pop("fields", None)# Instantiate the superclass normallysuper(DynamicFieldsModelSerializer, self).__init__(*args, **kwargs)@@ -42,16 +49,19 @@ class MatchingModelSerializer(serializers.ModelSerializer):def get_slug(self, obj):return slugify(obj.name)+slug = SerializerMethodField()def validate_match(self, match):- if 'matching_algorithm' in self.initial_data and self.initial_data['matching_algorithm'] == MatchingModel.MATCH_REGEX: # NOQA: E501+ if (+ "matching_algorithm" in self.initial_data+ and self.initial_data["matching_algorithm"] == MatchingModel.MATCH_REGEX+ ): # NOQA: E501try:re.compile(match)except Exception as e:raise serializers.ValidationError(- _("Invalid regular expression: %(error)s") %- {'error': str(e)}+ _("Invalid regular expression: %(error)s") % {"error": str(e)})return match@@ -70,12 +80,11 @@ class CorrespondentSerializer(MatchingModelSerializer):"matching_algorithm","is_insensitive","document_count",- "last_correspondence"+ "last_correspondence",)class DocumentTypeSerializer(MatchingModelSerializer):-class Meta:model = DocumentTypefields = (@@ -85,7 +94,7 @@ class DocumentTypeSerializer(MatchingModelSerializer):"match","matching_algorithm","is_insensitive",- "document_count"+ "document_count",)@@ -104,7 +113,7 @@ class ColorField(serializers.Field):(10, "#6a3d9a"),(11, "#b15928"),(12, "#000000"),- (13, "#cccccc")+ (13, "#cccccc"),)def to_internal_value(self, data):@@ -122,7 +131,7 @@ class ColorField(serializers.Field):class TagSerializerVersion1(MatchingModelSerializer):- colour = ColorField(source='color', default="#a6cee3")+ colour = ColorField(source="color", default="#a6cee3")class Meta:model = Tag@@ -135,20 +144,19 @@ class TagSerializerVersion1(MatchingModelSerializer):"matching_algorithm","is_insensitive","is_inbox_tag",- "document_count"+ "document_count",)class TagSerializer(MatchingModelSerializer):-def get_text_color(self, obj):try:- h = obj.color.lstrip('#')- rgb = tuple(int(h[i:i + 2], 16)/256 for i in (0, 2, 4))+ h = obj.color.lstrip("#")+ rgb = tuple(int(h[i : i + 2], 16) / 256 for i in (0, 2, 4))luminance = math.sqrt(- 0.299 * math.pow(rgb[0], 2) +- 0.587 * math.pow(rgb[1], 2) +- 0.114 * math.pow(rgb[2], 2)+ 0.299 * math.pow(rgb[0], 2)+ + 0.587 * math.pow(rgb[1], 2)+ + 0.114 * math.pow(rgb[2], 2))return "#ffffff" if luminance < 0.53 else "#000000"except ValueError:@@ -168,7 +176,7 @@ class TagSerializer(MatchingModelSerializer):"matching_algorithm","is_insensitive","is_inbox_tag",- "document_count"+ "document_count",)def validate_color(self, color):@@ -231,7 +239,6 @@ class DocumentSerializer(DynamicFieldsModelSerializer):class SavedViewFilterRuleSerializer(serializers.ModelSerializer):-class Meta:model = SavedViewFilterRulefields = ["rule_type", "value"]@@ -244,28 +251,33 @@ class SavedViewSerializer(serializers.ModelSerializer):class Meta:model = SavedViewdepth = 1- fields = ["id", "name", "show_on_dashboard", "show_in_sidebar",- "sort_field", "sort_reverse", "filter_rules"]+ fields = [+ "id",+ "name",+ "show_on_dashboard",+ "show_in_sidebar",+ "sort_field",+ "sort_reverse",+ "filter_rules",+ ]def update(self, instance, validated_data):- if 'filter_rules' in validated_data:- rules_data = validated_data.pop('filter_rules')+ if "filter_rules" in validated_data:+ rules_data = validated_data.pop("filter_rules")else:rules_data = Nonesuper(SavedViewSerializer, self).update(instance, validated_data)if rules_data is not None:SavedViewFilterRule.objects.filter(saved_view=instance).delete()for rule_data in rules_data:- SavedViewFilterRule.objects.create(- saved_view=instance, **rule_data)+ SavedViewFilterRule.objects.create(saved_view=instance, **rule_data)return instancedef create(self, validated_data):- rules_data = validated_data.pop('filter_rules')+ rules_data = validated_data.pop("filter_rules")saved_view = SavedView.objects.create(**validated_data)for rule_data in rules_data:- SavedViewFilterRule.objects.create(- saved_view=saved_view, **rule_data)+ SavedViewFilterRule.objects.create(saved_view=saved_view, **rule_data)return saved_view@@ -275,20 +287,19 @@ class DocumentListSerializer(serializers.Serializer):required=True,label="Documents",write_only=True,- child=serializers.IntegerField()+ child=serializers.IntegerField(),)def _validate_document_id_list(self, documents, name="documents"):if not type(documents) == list:raise serializers.ValidationError(f"{name} must be a list")if not all([type(i) == int for i in documents]):- raise serializers.ValidationError(- f"{name} must be a list of integers")+ raise serializers.ValidationError(f"{name} must be a list of integers")count = Document.objects.filter(id__in=documents).count()if not count == len(documents):raise serializers.ValidationError(- f"Some documents in {name} don't exist or were "- f"specified twice.")+ f"Some documents in {name} don't exist or were " f"specified twice."+ )def validate_documents(self, documents):self._validate_document_id_list(documents)@@ -304,7 +315,7 @@ class BulkEditSerializer(DocumentListSerializer):"add_tag","remove_tag","modify_tags",- "delete"+ "delete",],label="Method",write_only=True,@@ -316,12 +327,12 @@ class BulkEditSerializer(DocumentListSerializer):if not type(tags) == list:raise serializers.ValidationError(f"{name} must be a list")if not all([type(i) == int for i in tags]):- raise serializers.ValidationError(- f"{name} must be a list of integers")+ raise serializers.ValidationError(f"{name} must be a list of integers")count = Tag.objects.filter(id__in=tags).count()if not count == len(tags):raise serializers.ValidationError(- f"Some tags in {name} don't exist or were specified twice.")+ f"Some tags in {name} don't exist or were specified twice."+ )def validate_method(self, method):if method == "set_correspondent":@@ -340,8 +351,8 @@ class BulkEditSerializer(DocumentListSerializer):raise serializers.ValidationError("Unsupported method.")def _validate_parameters_tags(self, parameters):- if 'tag' in parameters:- tag_id = parameters['tag']+ if "tag" in parameters:+ tag_id = parameters["tag"]try:Tag.objects.get(id=tag_id)except Tag.DoesNotExist:@@ -350,48 +361,45 @@ class BulkEditSerializer(DocumentListSerializer):raise serializers.ValidationError("tag not specified")def _validate_parameters_document_type(self, parameters):- if 'document_type' in parameters:- document_type_id = parameters['document_type']+ if "document_type" in parameters:+ document_type_id = parameters["document_type"]if document_type_id is None:# None is okreturntry:DocumentType.objects.get(id=document_type_id)except DocumentType.DoesNotExist:- raise serializers.ValidationError(- "Document type does not exist")+ raise serializers.ValidationError("Document type does not exist")else:raise serializers.ValidationError("document_type not specified")def _validate_parameters_correspondent(self, parameters):- if 'correspondent' in parameters:- correspondent_id = parameters['correspondent']+ if "correspondent" in parameters:+ correspondent_id = parameters["correspondent"]if correspondent_id is None:returntry:Correspondent.objects.get(id=correspondent_id)except Correspondent.DoesNotExist:- raise serializers.ValidationError(- "Correspondent does not exist")+ raise serializers.ValidationError("Correspondent does not exist")else:raise serializers.ValidationError("correspondent not specified")def _validate_parameters_modify_tags(self, parameters):if "add_tags" in parameters:- self._validate_tag_id_list(parameters['add_tags'], "add_tags")+ self._validate_tag_id_list(parameters["add_tags"], "add_tags")else:raise serializers.ValidationError("add_tags not specified")if "remove_tags" in parameters:- self._validate_tag_id_list(parameters['remove_tags'],- "remove_tags")+ self._validate_tag_id_list(parameters["remove_tags"], "remove_tags")else:raise serializers.ValidationError("remove_tags not specified")def validate(self, attrs):- method = attrs['method']- parameters = attrs['parameters']+ method = attrs["method"]+ parameters = attrs["parameters"]if method == bulk_edit.set_correspondent:self._validate_parameters_correspondent(parameters)@@ -448,8 +456,7 @@ class PostDocumentSerializer(serializers.Serializer):if not is_mime_type_supported(mime_type):raise serializers.ValidationError(- _("File type %(type)s not supported") %- {'type': mime_type}+ _("File type %(type)s not supported") % {"type": mime_type})return document.name, document_data@@ -476,13 +483,11 @@ class PostDocumentSerializer(serializers.Serializer):class BulkDownloadSerializer(DocumentListSerializer):content = serializers.ChoiceField(- choices=["archive", "originals", "both"],- default="archive"+ choices=["archive", "originals", "both"], default="archive")compression = serializers.ChoiceField(- choices=["none", "deflated", "bzip2", "lzma"],- default="none"+ choices=["none", "deflated", "bzip2", "lzma"], default="none")def validate_compression(self, compression):@@ -492,5 +497,5 @@ class BulkDownloadSerializer(DocumentListSerializer):"none": zipfile.ZIP_STORED,"deflated": zipfile.ZIP_DEFLATED,"bzip2": zipfile.ZIP_BZIP2,- "lzma": zipfile.ZIP_LZMA+ "lzma": zipfile.ZIP_LZMA,}[compression]diff --git a/src/documents/signals/handlers.py b/src/documents/signals/handlers.pyindex 39e94d025..d0d28a2bc 100644--- a/src/documents/signals/handlers.py+++ b/src/documents/signals/handlers.py@@ -13,9 +13,11 @@ from django.utils import termcolors, timezonefrom filelock import FileLockfrom .. import matching-from ..file_handling import delete_empty_directories, \- create_source_path_directory, \- generate_unique_filename+from ..file_handling import (+ delete_empty_directories,+ create_source_path_directory,+ generate_unique_filename,+)from ..models import Document, Tag, MatchingModel@@ -27,21 +29,22 @@ def add_inbox_tags(sender, document=None, logging_group=None, **kwargs):document.tags.add(*inbox_tags)-def set_correspondent(sender,- document=None,- logging_group=None,- classifier=None,- replace=False,- use_first=True,- suggest=False,- base_url=None,- color=False,- **kwargs):+def set_correspondent(+ sender,+ document=None,+ logging_group=None,+ classifier=None,+ replace=False,+ use_first=True,+ suggest=False,+ base_url=None,+ color=False,+ **kwargs,+):if document.correspondent and not replace:return- potential_correspondents = matching.match_correspondents(document,- classifier)+ potential_correspondents = matching.match_correspondents(document, classifier)potential_count = len(potential_correspondents)if potential_correspondents:@@ -53,13 +56,13 @@ def set_correspondent(sender,logger.debug(f"Detected {potential_count} potential correspondents, "f"so we've opted for {selected}",- extra={'group': logging_group}+ extra={"group": logging_group},)else:logger.debug(f"Detected {potential_count} potential correspondents, "f"not assigning any correspondent",- extra={'group': logging_group}+ extra={"group": logging_group},)return@@ -67,7 +70,7 @@ def set_correspondent(sender,if suggest:if base_url:print(- termcolors.colorize(str(document), fg='green')+ termcolors.colorize(str(document), fg="green")if colorelse str(document))@@ -75,37 +78,39 @@ def set_correspondent(sender,else:print((- termcolors.colorize(str(document), fg='green')+ termcolors.colorize(str(document), fg="green")if colorelse str(document)- ) + f" [{document.pk}]"+ )+ + f" [{document.pk}]")print(f"Suggest correspondent {selected}")else:logger.info(f"Assigning correspondent {selected} to {document}",- extra={'group': logging_group}+ extra={"group": logging_group},)document.correspondent = selecteddocument.save(update_fields=("correspondent",))-def set_document_type(sender,- document=None,- logging_group=None,- classifier=None,- replace=False,- use_first=True,- suggest=False,- base_url=None,- color=False,- **kwargs):+def set_document_type(+ sender,+ document=None,+ logging_group=None,+ classifier=None,+ replace=False,+ use_first=True,+ suggest=False,+ base_url=None,+ color=False,+ **kwargs,+):if document.document_type and not replace:return- potential_document_type = matching.match_document_types(document,- classifier)+ potential_document_type = matching.match_document_types(document, classifier)potential_count = len(potential_document_type)if potential_document_type:@@ -118,13 +123,13 @@ def set_document_type(sender,logger.info(f"Detected {potential_count} potential document types, "f"so we've opted for {selected}",- extra={'group': logging_group}+ extra={"group": logging_group},)else:logger.info(f"Detected {potential_count} potential document types, "f"not assigning any document type",- extra={'group': logging_group}+ extra={"group": logging_group},)return@@ -132,7 +137,7 @@ def set_document_type(sender,if suggest:if base_url:print(- termcolors.colorize(str(document), fg='green')+ termcolors.colorize(str(document), fg="green")if colorelse str(document))@@ -140,35 +145,39 @@ def set_document_type(sender,else:print((- termcolors.colorize(str(document), fg='green')+ termcolors.colorize(str(document), fg="green")if colorelse str(document)- ) + f" [{document.pk}]"+ )+ + f" [{document.pk}]")print(f"Suggest document type {selected}")else:logger.info(f"Assigning document type {selected} to {document}",- extra={'group': logging_group}+ extra={"group": logging_group},)document.document_type = selecteddocument.save(update_fields=("document_type",))-def set_tags(sender,- document=None,- logging_group=None,- classifier=None,- replace=False,- suggest=False,- base_url=None,- color=False,- **kwargs):+def set_tags(+ sender,+ document=None,+ logging_group=None,+ classifier=None,+ replace=False,+ suggest=False,+ base_url=None,+ color=False,+ **kwargs,+):if replace:Document.tags.through.objects.filter(document=document).exclude(- Q(tag__is_inbox_tag=True)).exclude(+ Q(tag__is_inbox_tag=True)+ ).exclude(Q(tag__match="") & ~Q(tag__matching_algorithm=Tag.MATCH_AUTO)).delete()@@ -181,14 +190,13 @@ def set_tags(sender,if suggest:extra_tags = current_tags - set(matched_tags)extra_tags = [- t for t in extra_tags- if t.matching_algorithm == MatchingModel.MATCH_AUTO+ t for t in extra_tags if t.matching_algorithm == MatchingModel.MATCH_AUTO]if not relevant_tags and not extra_tags:returnif base_url:print(- termcolors.colorize(str(document), fg='green')+ termcolors.colorize(str(document), fg="green")if colorelse str(document))@@ -196,15 +204,14 @@ def set_tags(sender,else:print((- termcolors.colorize(str(document), fg='green')+ termcolors.colorize(str(document), fg="green")if colorelse str(document)- ) + f" [{document.pk}]"+ )+ + f" [{document.pk}]")if relevant_tags:- print(- "Suggest tags: " + ", ".join([t.name for t in relevant_tags])- )+ print("Suggest tags: " + ", ".join([t.name for t in relevant_tags]))if extra_tags:print("Extra tags: " + ", ".join([t.name for t in extra_tags]))else:@@ -213,10 +220,8 @@ def set_tags(sender,message = 'Tagging "{}" with "{}"'logger.info(- message.format(- document, ", ".join([t.name for t in relevant_tags])- ),- extra={'group': logging_group}+ message.format(document, ", ".join([t.name for t in relevant_tags])),+ extra={"group": logging_group},)document.tags.add(*relevant_tags)@@ -235,9 +240,7 @@ def cleanup_document_deletion(sender, instance, using, **kwargs):while True:new_file_path = os.path.join(settings.TRASH_DIR,- old_filebase +- (f"_{counter:02}" if counter else "") +- old_fileext+ old_filebase + (f"_{counter:02}" if counter else "") + old_fileext,)if os.path.exists(new_file_path):@@ -245,8 +248,7 @@ def cleanup_document_deletion(sender, instance, using, **kwargs):else:break- logger.debug(- f"Moving {instance.source_path} to trash at {new_file_path}")+ logger.debug(f"Moving {instance.source_path} to trash at {new_file_path}")try:os.rename(instance.source_path, new_file_path)except OSError as e:@@ -256,14 +258,15 @@ def cleanup_document_deletion(sender, instance, using, **kwargs):)return- for filename in (instance.source_path,- instance.archive_path,- instance.thumbnail_path):+ for filename in (+ instance.source_path,+ instance.archive_path,+ instance.thumbnail_path,+ ):if filename and os.path.isfile(filename):try:os.unlink(filename)- logger.debug(- f"Deleted file {filename}.")+ logger.debug(f"Deleted file {filename}.")except OSError as e:logger.warning(f"While deleting document {str(instance)}, the file "@@ -271,14 +274,12 @@ def cleanup_document_deletion(sender, instance, using, **kwargs):)delete_empty_directories(- os.path.dirname(instance.source_path),- root=settings.ORIGINALS_DIR+ os.path.dirname(instance.source_path), root=settings.ORIGINALS_DIR)if instance.has_archive_version:delete_empty_directories(- os.path.dirname(instance.archive_path),- root=settings.ARCHIVE_DIR+ os.path.dirname(instance.archive_path), root=settings.ARCHIVE_DIR)@@ -289,15 +290,15 @@ class CannotMoveFilesException(Exception):def validate_move(instance, old_path, new_path):if not os.path.isfile(old_path):# Can't do anything if the old file does not exist anymore.- logger.fatal(- f"Document {str(instance)}: File {old_path} has gone.")+ logger.fatal(f"Document {str(instance)}: File {old_path} has gone.")raise CannotMoveFilesException()if os.path.isfile(new_path):# Can't do anything if the new file already exists. Skip updating file.logger.warning(f"Document {str(instance)}: Cannot rename file "- f"since target path {new_path} already exists.")+ f"since target path {new_path} already exists."+ )raise CannotMoveFilesException()@@ -333,7 +334,9 @@ def update_filename_and_move_files(sender, instance, **kwargs):instance, archive_filename=True)- move_archive = old_archive_filename != instance.archive_filename # NOQA: E501+ move_archive = (+ old_archive_filename != instance.archive_filename+ ) # NOQA: E501else:move_archive = False@@ -347,8 +350,7 @@ def update_filename_and_move_files(sender, instance, **kwargs):os.rename(old_source_path, instance.source_path)if move_archive:- validate_move(- instance, old_archive_path, instance.archive_path)+ validate_move(instance, old_archive_path, instance.archive_path)create_source_path_directory(instance.archive_path)os.rename(old_archive_path, instance.archive_path)@@ -390,12 +392,16 @@ def update_filename_and_move_files(sender, instance, **kwargs):# finally, remove any empty sub folders. This will do nothing if# something has failed above.if not os.path.isfile(old_source_path):- delete_empty_directories(os.path.dirname(old_source_path),- root=settings.ORIGINALS_DIR)+ delete_empty_directories(+ os.path.dirname(old_source_path), root=settings.ORIGINALS_DIR+ )- if instance.has_archive_version and not os.path.isfile(old_archive_path): # NOQA: E501- delete_empty_directories(os.path.dirname(old_archive_path),- root=settings.ARCHIVE_DIR)+ if instance.has_archive_version and not os.path.isfile(+ old_archive_path+ ): # NOQA: E501+ delete_empty_directories(+ os.path.dirname(old_archive_path), root=settings.ARCHIVE_DIR+ )def set_log_entry(sender, document=None, logging_group=None, **kwargs):diff --git a/src/documents/tasks.py b/src/documents/tasks.pyindex f24be562f..569ebf0a7 100644--- a/src/documents/tasks.py+++ b/src/documents/tasks.py@@ -31,12 +31,11 @@ def index_reindex(progress_bar_disable=False):def train_classifier():- if (not Tag.objects.filter(- matching_algorithm=Tag.MATCH_AUTO).exists() and- not DocumentType.objects.filter(- matching_algorithm=Tag.MATCH_AUTO).exists() and- not Correspondent.objects.filter(- matching_algorithm=Tag.MATCH_AUTO).exists()):+ if (+ not Tag.objects.filter(matching_algorithm=Tag.MATCH_AUTO).exists()+ and not DocumentType.objects.filter(matching_algorithm=Tag.MATCH_AUTO).exists()+ and not Correspondent.objects.filter(matching_algorithm=Tag.MATCH_AUTO).exists()+ ):return@@ -48,28 +47,25 @@ def train_classifier():try:if classifier.train():logger.info(- "Saving updated classifier model to {}...".format(- settings.MODEL_FILE)+ "Saving updated classifier model to {}...".format(settings.MODEL_FILE))classifier.save()else:- logger.debug(- "Training data unchanged."- )+ logger.debug("Training data unchanged.")except Exception as e:- logger.warning(- "Classifier error: " + str(e)- )+ logger.warning("Classifier error: " + str(e))-def consume_file(path,- override_filename=None,- override_title=None,- override_correspondent_id=None,- override_document_type_id=None,- override_tag_ids=None,- task_id=None):+def consume_file(+ path,+ override_filename=None,+ override_title=None,+ override_correspondent_id=None,+ override_document_type_id=None,+ override_tag_ids=None,+ task_id=None,+):document = Consumer().try_consume_file(path,@@ -78,16 +74,16 @@ def consume_file(path,override_correspondent_id=override_correspondent_id,override_document_type_id=override_document_type_id,override_tag_ids=override_tag_ids,- task_id=task_id+ task_id=task_id,)if document:- return "Success. New document id {} created".format(- document.pk- )+ return "Success. New document id {} created".format(document.pk)else:- raise ConsumerError("Unknown error: Returned document was null, but "- "no error message was given.")+ raise ConsumerError(+ "Unknown error: Returned document was null, but "+ "no error message was given."+ )def sanity_check():@@ -96,8 +92,7 @@ def sanity_check():messages.log_messages()if messages.has_error():- raise SanityCheckFailedException(- "Sanity check failed with errors. See log.")+ raise SanityCheckFailedException("Sanity check failed with errors. See log.")elif messages.has_warning():return "Sanity check exited with warnings. See log."elif len(messages) > 0:diff --git a/src/documents/tests/factories.py b/src/documents/tests/factories.pyindex 243821751..c2907d932 100644--- a/src/documents/tests/factories.py+++ b/src/documents/tests/factories.py@@ -5,7 +5,6 @@ from ..models import Document, Correspondentclass CorrespondentFactory(DjangoModelFactory):-class Meta:model = Correspondent@@ -13,6 +12,5 @@ class CorrespondentFactory(DjangoModelFactory):class DocumentFactory(DjangoModelFactory):-class Meta:model = Documentdiff --git a/src/documents/tests/test_admin.py b/src/documents/tests/test_admin.pyindex fc1d7ffaf..3e292dcfc 100644--- a/src/documents/tests/test_admin.py+++ b/src/documents/tests/test_admin.py@@ -11,7 +11,6 @@ from documents.tests.utils import DirectoriesMixinclass TestDocumentAdmin(DirectoriesMixin, TestCase):-def get_document_from_index(self, doc):ix = index.open_index()with ix.searcher() as searcher:@@ -27,7 +26,7 @@ class TestDocumentAdmin(DirectoriesMixin, TestCase):doc.title = "new title"self.doc_admin.save_model(None, doc, None, None)self.assertEqual(Document.objects.get(id=doc.id).title, "new title")- self.assertEqual(self.get_document_from_index(doc)['id'], doc.id)+ self.assertEqual(self.get_document_from_index(doc)["id"], doc.id)def test_delete_model(self):doc = Document.objects.create(title="test")@@ -42,7 +41,9 @@ class TestDocumentAdmin(DirectoriesMixin, TestCase):def test_delete_queryset(self):docs = []for i in range(42):- doc = Document.objects.create(title="Many documents with the same title", checksum=f"{i:02}")+ doc = Document.objects.create(+ title="Many documents with the same title", checksum=f"{i:02}"+ )docs.append(doc)index.add_or_update_document(doc)@@ -59,5 +60,7 @@ class TestDocumentAdmin(DirectoriesMixin, TestCase):self.assertIsNone(self.get_document_from_index(doc))def test_created(self):- doc = Document.objects.create(title="test", created=timezone.datetime(2020, 4, 12))+ doc = Document.objects.create(+ title="test", created=timezone.datetime(2020, 4, 12)+ )self.assertEqual(self.doc_admin.created_(doc), "2020-04-12")diff --git a/src/documents/tests/test_api.py b/src/documents/tests/test_api.pyindex 2f8dc18da..8778e313d 100644--- a/src/documents/tests/test_api.py+++ b/src/documents/tests/test_api.py@@ -15,12 +15,18 @@ from rest_framework.test import APITestCasefrom whoosh.writing import AsyncWriterfrom documents import index, bulk_edit-from documents.models import Document, Correspondent, DocumentType, Tag, SavedView, MatchingModel+from documents.models import (+ Document,+ Correspondent,+ DocumentType,+ Tag,+ SavedView,+ MatchingModel,+)from documents.tests.utils import DirectoriesMixinclass TestDocumentApi(DirectoriesMixin, APITestCase):-def setUp(self):super(TestDocumentApi, self).setUp()@@ -31,33 +37,42 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):response = self.client.get("/api/documents/").data- self.assertEqual(response['count'], 0)+ self.assertEqual(response["count"], 0)c = Correspondent.objects.create(name="c", pk=41)dt = DocumentType.objects.create(name="dt", pk=63)tag = Tag.objects.create(name="t", pk=85)- doc = Document.objects.create(title="WOW", content="the content", correspondent=c, document_type=dt, checksum="123", mime_type="application/pdf")+ doc = Document.objects.create(+ title="WOW",+ content="the content",+ correspondent=c,+ document_type=dt,+ checksum="123",+ mime_type="application/pdf",+ )doc.tags.add(tag)- response = self.client.get("/api/documents/", format='json')+ response = self.client.get("/api/documents/", format="json")self.assertEqual(response.status_code, 200)- self.assertEqual(response.data['count'], 1)+ self.assertEqual(response.data["count"], 1)- returned_doc = response.data['results'][0]- self.assertEqual(returned_doc['id'], doc.id)- self.assertEqual(returned_doc['title'], doc.title)- self.assertEqual(returned_doc['correspondent'], c.id)- self.assertEqual(returned_doc['document_type'], dt.id)- self.assertListEqual(returned_doc['tags'], [tag.id])+ returned_doc = response.data["results"][0]+ self.assertEqual(returned_doc["id"], doc.id)+ self.assertEqual(returned_doc["title"], doc.title)+ self.assertEqual(returned_doc["correspondent"], c.id)+ self.assertEqual(returned_doc["document_type"], dt.id)+ self.assertListEqual(returned_doc["tags"], [tag.id])c2 = Correspondent.objects.create(name="c2")- returned_doc['correspondent'] = c2.pk- returned_doc['title'] = "the new title"+ returned_doc["correspondent"] = c2.pk+ returned_doc["title"] = "the new title"- response = self.client.put('/api/documents/{}/'.format(doc.pk), returned_doc, format='json')+ response = self.client.put(+ "/api/documents/{}/".format(doc.pk), returned_doc, format="json"+ )self.assertEqual(response.status_code, 200)@@ -74,50 +89,59 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):c = Correspondent.objects.create(name="c", pk=41)dt = DocumentType.objects.create(name="dt", pk=63)tag = Tag.objects.create(name="t", pk=85)- doc = Document.objects.create(title="WOW", content="the content", correspondent=c, document_type=dt, checksum="123", mime_type="application/pdf")-- response = self.client.get("/api/documents/", format='json')- self.assertEqual(response.status_code, 200)- results_full = response.data['results']+ doc = Document.objects.create(+ title="WOW",+ content="the content",+ correspondent=c,+ document_type=dt,+ checksum="123",+ mime_type="application/pdf",+ )++ response = self.client.get("/api/documents/", format="json")+ self.assertEqual(response.status_code, 200)+ results_full = response.data["results"]self.assertTrue("content" in results_full[0])self.assertTrue("id" in results_full[0])- response = self.client.get("/api/documents/?fields=id", format='json')+ response = self.client.get("/api/documents/?fields=id", format="json")self.assertEqual(response.status_code, 200)- results = response.data['results']+ results = response.data["results"]self.assertFalse("content" in results[0])self.assertTrue("id" in results[0])self.assertEqual(len(results[0]), 1)- response = self.client.get("/api/documents/?fields=content", format='json')+ response = self.client.get("/api/documents/?fields=content", format="json")self.assertEqual(response.status_code, 200)- results = response.data['results']+ results = response.data["results"]self.assertTrue("content" in results[0])self.assertFalse("id" in results[0])self.assertEqual(len(results[0]), 1)- response = self.client.get("/api/documents/?fields=id,content", format='json')+ response = self.client.get("/api/documents/?fields=id,content", format="json")self.assertEqual(response.status_code, 200)- results = response.data['results']+ results = response.data["results"]self.assertTrue("content" in results[0])self.assertTrue("id" in results[0])self.assertEqual(len(results[0]), 2)- response = self.client.get("/api/documents/?fields=id,conteasdnt", format='json')+ response = self.client.get(+ "/api/documents/?fields=id,conteasdnt", format="json"+ )self.assertEqual(response.status_code, 200)- results = response.data['results']+ results = response.data["results"]self.assertFalse("content" in results[0])self.assertTrue("id" in results[0])self.assertEqual(len(results[0]), 1)- response = self.client.get("/api/documents/?fields=", format='json')+ response = self.client.get("/api/documents/?fields=", format="json")self.assertEqual(response.status_code, 200)- results = response.data['results']+ results = response.data["results"]self.assertEqual(results_full, results)- response = self.client.get("/api/documents/?fields=dgfhs", format='json')+ response = self.client.get("/api/documents/?fields=dgfhs", format="json")self.assertEqual(response.status_code, 200)- results = response.data['results']+ results = response.data["results"]self.assertEqual(len(results[0]), 0)def test_document_actions(self):@@ -130,22 +154,28 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):with open(filename, "wb") as f:f.write(content)- doc = Document.objects.create(title="none", filename=os.path.basename(filename), mime_type="application/pdf")+ doc = Document.objects.create(+ title="none",+ filename=os.path.basename(filename),+ mime_type="application/pdf",+ )- with open(os.path.join(self.dirs.thumbnail_dir, "{:07d}.png".format(doc.pk)), "wb") as f:+ with open(+ os.path.join(self.dirs.thumbnail_dir, "{:07d}.png".format(doc.pk)), "wb"+ ) as f:f.write(content_thumbnail)- response = self.client.get('/api/documents/{}/download/'.format(doc.pk))+ response = self.client.get("/api/documents/{}/download/".format(doc.pk))self.assertEqual(response.status_code, 200)self.assertEqual(response.content, content)- response = self.client.get('/api/documents/{}/preview/'.format(doc.pk))+ response = self.client.get("/api/documents/{}/preview/".format(doc.pk))self.assertEqual(response.status_code, 200)self.assertEqual(response.content, content)- response = self.client.get('/api/documents/{}/thumb/'.format(doc.pk))+ response = self.client.get("/api/documents/{}/thumb/".format(doc.pk))self.assertEqual(response.status_code, 200)self.assertEqual(response.content, content_thumbnail)@@ -156,9 +186,12 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):content = b"This is a test"content_archive = b"This is the same test but archived"- doc = Document.objects.create(title="none", filename="my_document.pdf",- archive_filename="archived.pdf",- mime_type="application/pdf")+ doc = Document.objects.create(+ title="none",+ filename="my_document.pdf",+ archive_filename="archived.pdf",+ mime_type="application/pdf",+ )with open(doc.source_path, "wb") as f:f.write(content)@@ -166,44 +199,56 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):with open(doc.archive_path, "wb") as f:f.write(content_archive)- response = self.client.get('/api/documents/{}/download/'.format(doc.pk))+ response = self.client.get("/api/documents/{}/download/".format(doc.pk))self.assertEqual(response.status_code, 200)self.assertEqual(response.content, content_archive)- response = self.client.get('/api/documents/{}/download/?original=true'.format(doc.pk))+ response = self.client.get(+ "/api/documents/{}/download/?original=true".format(doc.pk)+ )self.assertEqual(response.status_code, 200)self.assertEqual(response.content, content)- response = self.client.get('/api/documents/{}/preview/'.format(doc.pk))+ response = self.client.get("/api/documents/{}/preview/".format(doc.pk))self.assertEqual(response.status_code, 200)self.assertEqual(response.content, content_archive)- response = self.client.get('/api/documents/{}/preview/?original=true'.format(doc.pk))+ response = self.client.get(+ "/api/documents/{}/preview/?original=true".format(doc.pk)+ )self.assertEqual(response.status_code, 200)self.assertEqual(response.content, content)def test_document_actions_not_existing_file(self):- doc = Document.objects.create(title="none", filename=os.path.basename("asd"), mime_type="application/pdf")+ doc = Document.objects.create(+ title="none", filename=os.path.basename("asd"), mime_type="application/pdf"+ )- response = self.client.get('/api/documents/{}/download/'.format(doc.pk))+ response = self.client.get("/api/documents/{}/download/".format(doc.pk))self.assertEqual(response.status_code, 404)- response = self.client.get('/api/documents/{}/preview/'.format(doc.pk))+ response = self.client.get("/api/documents/{}/preview/".format(doc.pk))self.assertEqual(response.status_code, 404)- response = self.client.get('/api/documents/{}/thumb/'.format(doc.pk))+ response = self.client.get("/api/documents/{}/thumb/".format(doc.pk))self.assertEqual(response.status_code, 404)def test_document_filters(self):- doc1 = Document.objects.create(title="none1", checksum="A", mime_type="application/pdf")- doc2 = Document.objects.create(title="none2", checksum="B", mime_type="application/pdf")- doc3 = Document.objects.create(title="none3", checksum="C", mime_type="application/pdf")+ doc1 = Document.objects.create(+ title="none1", checksum="A", mime_type="application/pdf"+ )+ doc2 = Document.objects.create(+ title="none2", checksum="B", mime_type="application/pdf"+ )+ doc3 = Document.objects.create(+ title="none3", checksum="C", mime_type="application/pdf"+ )tag_inbox = Tag.objects.create(name="t1", is_inbox_tag=True)tag_2 = Tag.objects.create(name="t2")@@ -216,89 +261,144 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):response = self.client.get("/api/documents/?is_in_inbox=true")self.assertEqual(response.status_code, 200)- results = response.data['results']+ results = response.data["results"]self.assertEqual(len(results), 1)- self.assertEqual(results[0]['id'], doc1.id)+ self.assertEqual(results[0]["id"], doc1.id)response = self.client.get("/api/documents/?is_in_inbox=false")self.assertEqual(response.status_code, 200)- results = response.data['results']+ results = response.data["results"]self.assertEqual(len(results), 2)- self.assertCountEqual([results[0]['id'], results[1]['id']], [doc2.id, doc3.id])+ self.assertCountEqual([results[0]["id"], results[1]["id"]], [doc2.id, doc3.id])- response = self.client.get("/api/documents/?tags__id__in={},{}".format(tag_inbox.id, tag_3.id))+ response = self.client.get(+ "/api/documents/?tags__id__in={},{}".format(tag_inbox.id, tag_3.id)+ )self.assertEqual(response.status_code, 200)- results = response.data['results']+ results = response.data["results"]self.assertEqual(len(results), 2)- self.assertCountEqual([results[0]['id'], results[1]['id']], [doc1.id, doc3.id])+ self.assertCountEqual([results[0]["id"], results[1]["id"]], [doc1.id, doc3.id])- response = self.client.get("/api/documents/?tags__id__in={},{}".format(tag_2.id, tag_3.id))+ response = self.client.get(+ "/api/documents/?tags__id__in={},{}".format(tag_2.id, tag_3.id)+ )self.assertEqual(response.status_code, 200)- results = response.data['results']+ results = response.data["results"]self.assertEqual(len(results), 2)- self.assertCountEqual([results[0]['id'], results[1]['id']], [doc2.id, doc3.id])+ self.assertCountEqual([results[0]["id"], results[1]["id"]], [doc2.id, doc3.id])- response = self.client.get("/api/documents/?tags__id__all={},{}".format(tag_2.id, tag_3.id))+ response = self.client.get(+ "/api/documents/?tags__id__all={},{}".format(tag_2.id, tag_3.id)+ )self.assertEqual(response.status_code, 200)- results = response.data['results']+ results = response.data["results"]self.assertEqual(len(results), 1)- self.assertEqual(results[0]['id'], doc3.id)+ self.assertEqual(results[0]["id"], doc3.id)- response = self.client.get("/api/documents/?tags__id__all={},{}".format(tag_inbox.id, tag_3.id))+ response = self.client.get(+ "/api/documents/?tags__id__all={},{}".format(tag_inbox.id, tag_3.id)+ )self.assertEqual(response.status_code, 200)- results = response.data['results']+ results = response.data["results"]self.assertEqual(len(results), 0)- response = self.client.get("/api/documents/?tags__id__all={}a{}".format(tag_inbox.id, tag_3.id))+ response = self.client.get(+ "/api/documents/?tags__id__all={}a{}".format(tag_inbox.id, tag_3.id)+ )self.assertEqual(response.status_code, 200)- results = response.data['results']+ results = response.data["results"]self.assertEqual(len(results), 3)response = self.client.get("/api/documents/?tags__id__none={}".format(tag_3.id))self.assertEqual(response.status_code, 200)- results = response.data['results']+ results = response.data["results"]self.assertEqual(len(results), 2)- self.assertCountEqual([results[0]['id'], results[1]['id']], [doc1.id, doc2.id])+ self.assertCountEqual([results[0]["id"], results[1]["id"]], [doc1.id, doc2.id])- response = self.client.get("/api/documents/?tags__id__none={},{}".format(tag_3.id, tag_2.id))+ response = self.client.get(+ "/api/documents/?tags__id__none={},{}".format(tag_3.id, tag_2.id)+ )self.assertEqual(response.status_code, 200)- results = response.data['results']+ results = response.data["results"]self.assertEqual(len(results), 1)- self.assertEqual(results[0]['id'], doc1.id)+ self.assertEqual(results[0]["id"], doc1.id)- response = self.client.get("/api/documents/?tags__id__none={},{}".format(tag_2.id, tag_inbox.id))+ response = self.client.get(+ "/api/documents/?tags__id__none={},{}".format(tag_2.id, tag_inbox.id)+ )self.assertEqual(response.status_code, 200)- results = response.data['results']+ results = response.data["results"]self.assertEqual(len(results), 0)def test_documents_title_content_filter(self):- doc1 = Document.objects.create(title="title A", content="content A", checksum="A", mime_type="application/pdf")- doc2 = Document.objects.create(title="title B", content="content A", checksum="B", mime_type="application/pdf")- doc3 = Document.objects.create(title="title A", content="content B", checksum="C", mime_type="application/pdf")- doc4 = Document.objects.create(title="title B", content="content B", checksum="D", mime_type="application/pdf")+ doc1 = Document.objects.create(+ title="title A",+ content="content A",+ checksum="A",+ mime_type="application/pdf",+ )+ doc2 = Document.objects.create(+ title="title B",+ content="content A",+ checksum="B",+ mime_type="application/pdf",+ )+ doc3 = Document.objects.create(+ title="title A",+ content="content B",+ checksum="C",+ mime_type="application/pdf",+ )+ doc4 = Document.objects.create(+ title="title B",+ content="content B",+ checksum="D",+ mime_type="application/pdf",+ )response = self.client.get("/api/documents/?title_content=A")self.assertEqual(response.status_code, 200)- results = response.data['results']+ results = response.data["results"]self.assertEqual(len(results), 3)- self.assertCountEqual([results[0]['id'], results[1]['id'], results[2]['id']], [doc1.id, doc2.id, doc3.id])+ self.assertCountEqual(+ [results[0]["id"], results[1]["id"], results[2]["id"]],+ [doc1.id, doc2.id, doc3.id],+ )response = self.client.get("/api/documents/?title_content=B")self.assertEqual(response.status_code, 200)- results = response.data['results']+ results = response.data["results"]self.assertEqual(len(results), 3)- self.assertCountEqual([results[0]['id'], results[1]['id'], results[2]['id']], [doc2.id, doc3.id, doc4.id])+ self.assertCountEqual(+ [results[0]["id"], results[1]["id"], results[2]["id"]],+ [doc2.id, doc3.id, doc4.id],+ )response = self.client.get("/api/documents/?title_content=X")self.assertEqual(response.status_code, 200)- results = response.data['results']+ results = response.data["results"]self.assertEqual(len(results), 0)def test_search(self):- d1=Document.objects.create(title="invoice", content="the thing i bought at a shop and paid with bank account", checksum="A", pk=1)- d2=Document.objects.create(title="bank statement 1", content="things i paid for in august", pk=2, checksum="B")- d3=Document.objects.create(title="bank statement 3", content="things i paid for in september", pk=3, checksum="C")+ d1 = Document.objects.create(+ title="invoice",+ content="the thing i bought at a shop and paid with bank account",+ checksum="A",+ pk=1,+ )+ d2 = Document.objects.create(+ title="bank statement 1",+ content="things i paid for in august",+ pk=2,+ checksum="B",+ )+ d3 = Document.objects.create(+ title="bank statement 3",+ content="things i paid for in september",+ pk=3,+ checksum="C",+ )with AsyncWriter(index.open_index()) as writer:# Note to future self: there is a reason we dont use a model signal handler to update the index: some operations edit many documents at once# (retagger, renamer) and we don't want to open a writer for each of these, but rather perform the entire operation with one writer.@@ -307,57 +407,69 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):index.update_document(writer, d2)index.update_document(writer, d3)response = self.client.get("/api/documents/?query=bank")- results = response.data['results']- self.assertEqual(response.data['count'], 3)+ results = response.data["results"]+ self.assertEqual(response.data["count"], 3)self.assertEqual(len(results), 3)response = self.client.get("/api/documents/?query=september")- results = response.data['results']- self.assertEqual(response.data['count'], 1)+ results = response.data["results"]+ self.assertEqual(response.data["count"], 1)self.assertEqual(len(results), 1)response = self.client.get("/api/documents/?query=statement")- results = response.data['results']- self.assertEqual(response.data['count'], 2)+ results = response.data["results"]+ self.assertEqual(response.data["count"], 2)self.assertEqual(len(results), 2)response = self.client.get("/api/documents/?query=sfegdfg")- results = response.data['results']- self.assertEqual(response.data['count'], 0)+ results = response.data["results"]+ self.assertEqual(response.data["count"], 0)self.assertEqual(len(results), 0)def test_search_multi_page(self):with AsyncWriter(index.open_index()) as writer:for i in range(55):- doc = Document.objects.create(checksum=str(i), pk=i+1, title=f"Document {i+1}", content="content")+ doc = Document.objects.create(+ checksum=str(i),+ pk=i + 1,+ title=f"Document {i+1}",+ content="content",+ )index.update_document(writer, doc)# This is here so that we test that no document gets returned twice (might happen if the paging is not working)seen_ids = []for i in range(1, 6):- response = self.client.get(f"/api/documents/?query=content&page={i}&page_size=10")- results = response.data['results']- self.assertEqual(response.data['count'], 55)+ response = self.client.get(+ f"/api/documents/?query=content&page={i}&page_size=10"+ )+ results = response.data["results"]+ self.assertEqual(response.data["count"], 55)self.assertEqual(len(results), 10)for result in results:- self.assertNotIn(result['id'], seen_ids)- seen_ids.append(result['id'])+ self.assertNotIn(result["id"], seen_ids)+ seen_ids.append(result["id"])response = self.client.get(f"/api/documents/?query=content&page=6&page_size=10")- results = response.data['results']- self.assertEqual(response.data['count'], 55)+ results = response.data["results"]+ self.assertEqual(response.data["count"], 55)self.assertEqual(len(results), 5)for result in results:- self.assertNotIn(result['id'], seen_ids)- seen_ids.append(result['id'])+ self.assertNotIn(result["id"], seen_ids)+ seen_ids.append(result["id"])def test_search_invalid_page(self):with AsyncWriter(index.open_index()) as writer:for i in range(15):- doc = Document.objects.create(checksum=str(i), pk=i+1, title=f"Document {i+1}", content="content")+ doc = Document.objects.create(+ checksum=str(i),+ pk=i + 1,+ title=f"Document {i+1}",+ content="content",+ )index.update_document(writer, doc)response = self.client.get(f"/api/documents/?query=content&page=0&page_size=10")@@ -391,23 +503,43 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):def test_search_spelling_correction(self):with AsyncWriter(index.open_index()) as writer:for i in range(55):- doc = Document.objects.create(checksum=str(i), pk=i+1, title=f"Document {i+1}", content=f"Things document {i+1}")+ doc = Document.objects.create(+ checksum=str(i),+ pk=i + 1,+ title=f"Document {i+1}",+ content=f"Things document {i+1}",+ )index.update_document(writer, doc)response = self.client.get("/api/search/?query=thing")- correction = response.data['corrected_query']+ correction = response.data["corrected_query"]self.assertEqual(correction, "things")response = self.client.get("/api/search/?query=things")- correction = response.data['corrected_query']+ correction = response.data["corrected_query"]self.assertEqual(correction, None)def test_search_more_like(self):- d1=Document.objects.create(title="invoice", content="the thing i bought at a shop and paid with bank account", checksum="A", pk=1)- d2=Document.objects.create(title="bank statement 1", content="things i paid for in august", pk=2, checksum="B")- d3=Document.objects.create(title="bank statement 3", content="things i paid for in september", pk=3, checksum="C")+ d1 = Document.objects.create(+ title="invoice",+ content="the thing i bought at a shop and paid with bank account",+ checksum="A",+ pk=1,+ )+ d2 = Document.objects.create(+ title="bank statement 1",+ content="things i paid for in august",+ pk=2,+ checksum="B",+ )+ d3 = Document.objects.create(+ title="bank statement 3",+ content="things i paid for in september",+ pk=3,+ checksum="C",+ )with AsyncWriter(index.open_index()) as writer:index.update_document(writer, d1)index.update_document(writer, d2)@@ -417,11 +549,11 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):self.assertEqual(response.status_code, 200)- results = response.data['results']+ results = response.data["results"]self.assertEqual(len(results), 2)- self.assertEqual(results[0]['id'], d3.id)- self.assertEqual(results[1]['id'], d1.id)+ self.assertEqual(results[0]["id"], d3.id)+ self.assertEqual(results[1]["id"], d1.id)def test_search_filtering(self):t = Tag.objects.create(name="tag")@@ -434,9 +566,13 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):d3 = Document.objects.create(checksum="3", content="test")d3.tags.add(t)d3.tags.add(t2)- d4 = Document.objects.create(checksum="4", created=datetime.datetime(2020, 7, 13), content="test")+ d4 = Document.objects.create(+ checksum="4", created=datetime.datetime(2020, 7, 13), content="test"+ )d4.tags.add(t2)- d5 = Document.objects.create(checksum="5", added=datetime.datetime(2020, 7, 13), content="test")+ d5 = Document.objects.create(+ checksum="5", added=datetime.datetime(2020, 7, 13), content="test"+ )d6 = Document.objects.create(checksum="6", content="test2")with AsyncWriter(index.open_index()) as writer:@@ -446,38 +582,108 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):def search_query(q):r = self.client.get("/api/documents/?query=test" + q)self.assertEqual(r.status_code, 200)- return [hit['id'] for hit in r.data['results']]+ return [hit["id"] for hit in r.data["results"]]self.assertCountEqual(search_query(""), [d1.id, d2.id, d3.id, d4.id, d5.id])self.assertCountEqual(search_query("&is_tagged=true"), [d3.id, d4.id])self.assertCountEqual(search_query("&is_tagged=false"), [d1.id, d2.id, d5.id])self.assertCountEqual(search_query("&correspondent__id=" + str(c.id)), [d1.id])self.assertCountEqual(search_query("&document_type__id=" + str(dt.id)), [d2.id])- self.assertCountEqual(search_query("&correspondent__isnull"), [d2.id, d3.id, d4.id, d5.id])- self.assertCountEqual(search_query("&document_type__isnull"), [d1.id, d3.id, d4.id, d5.id])- self.assertCountEqual(search_query("&tags__id__all=" + str(t.id) + "," + str(t2.id)), [d3.id])+ self.assertCountEqual(+ search_query("&correspondent__isnull"), [d2.id, d3.id, d4.id, d5.id]+ )+ self.assertCountEqual(+ search_query("&document_type__isnull"), [d1.id, d3.id, d4.id, d5.id]+ )+ self.assertCountEqual(+ search_query("&tags__id__all=" + str(t.id) + "," + str(t2.id)), [d3.id]+ )self.assertCountEqual(search_query("&tags__id__all=" + str(t.id)), [d3.id])- self.assertCountEqual(search_query("&tags__id__all=" + str(t2.id)), [d3.id, d4.id])-- self.assertIn(d4.id, search_query("&created__date__lt=" + datetime.datetime(2020, 9, 2).strftime("%Y-%m-%d")))- self.assertNotIn(d4.id, search_query("&created__date__gt=" + datetime.datetime(2020, 9, 2).strftime("%Y-%m-%d")))-- self.assertNotIn(d4.id, search_query("&created__date__lt=" + datetime.datetime(2020, 1, 2).strftime("%Y-%m-%d")))- self.assertIn(d4.id, search_query("&created__date__gt=" + datetime.datetime(2020, 1, 2).strftime("%Y-%m-%d")))-- self.assertIn(d5.id, search_query("&added__date__lt=" + datetime.datetime(2020, 9, 2).strftime("%Y-%m-%d")))- self.assertNotIn(d5.id, search_query("&added__date__gt=" + datetime.datetime(2020, 9, 2).strftime("%Y-%m-%d")))-- self.assertNotIn(d5.id, search_query("&added__date__lt=" + datetime.datetime(2020, 1, 2).strftime("%Y-%m-%d")))- self.assertIn(d5.id, search_query("&added__date__gt=" + datetime.datetime(2020, 1, 2).strftime("%Y-%m-%d")))+ self.assertCountEqual(+ search_query("&tags__id__all=" + str(t2.id)), [d3.id, d4.id]+ )++ self.assertIn(+ d4.id,+ search_query(+ "&created__date__lt="+ + datetime.datetime(2020, 9, 2).strftime("%Y-%m-%d")+ ),+ )+ self.assertNotIn(+ d4.id,+ search_query(+ "&created__date__gt="+ + datetime.datetime(2020, 9, 2).strftime("%Y-%m-%d")+ ),+ )++ self.assertNotIn(+ d4.id,+ search_query(+ "&created__date__lt="+ + datetime.datetime(2020, 1, 2).strftime("%Y-%m-%d")+ ),+ )+ self.assertIn(+ d4.id,+ search_query(+ "&created__date__gt="+ + datetime.datetime(2020, 1, 2).strftime("%Y-%m-%d")+ ),+ )++ self.assertIn(+ d5.id,+ search_query(+ "&added__date__lt=" + datetime.datetime(2020, 9, 2).strftime("%Y-%m-%d")+ ),+ )+ self.assertNotIn(+ d5.id,+ search_query(+ "&added__date__gt=" + datetime.datetime(2020, 9, 2).strftime("%Y-%m-%d")+ ),+ )++ self.assertNotIn(+ d5.id,+ search_query(+ "&added__date__lt=" + datetime.datetime(2020, 1, 2).strftime("%Y-%m-%d")+ ),+ )+ self.assertIn(+ d5.id,+ search_query(+ "&added__date__gt=" + datetime.datetime(2020, 1, 2).strftime("%Y-%m-%d")+ ),+ )def test_search_sorting(self):c1 = Correspondent.objects.create(name="corres Ax")c2 = Correspondent.objects.create(name="corres Cx")c3 = Correspondent.objects.create(name="corres Bx")- d1 = Document.objects.create(checksum="1", correspondent=c1, content="test", archive_serial_number=2, title="3")- d2 = Document.objects.create(checksum="2", correspondent=c2, content="test", archive_serial_number=3, title="2")- d3 = Document.objects.create(checksum="3", correspondent=c3, content="test", archive_serial_number=1, title="1")+ d1 = Document.objects.create(+ checksum="1",+ correspondent=c1,+ content="test",+ archive_serial_number=2,+ title="3",+ )+ d2 = Document.objects.create(+ checksum="2",+ correspondent=c2,+ content="test",+ archive_serial_number=3,+ title="2",+ )+ d3 = Document.objects.create(+ checksum="3",+ correspondent=c3,+ content="test",+ archive_serial_number=1,+ title="1",+ )with AsyncWriter(index.open_index()) as writer:for doc in Document.objects.all():@@ -486,15 +692,22 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):def search_query(q):r = self.client.get("/api/documents/?query=test" + q)self.assertEqual(r.status_code, 200)- return [hit['id'] for hit in r.data['results']]-- self.assertListEqual(search_query("&ordering=archive_serial_number"), [d3.id, d1.id, d2.id])- self.assertListEqual(search_query("&ordering=-archive_serial_number"), [d2.id, d1.id, d3.id])+ return [hit["id"] for hit in r.data["results"]]++ self.assertListEqual(+ search_query("&ordering=archive_serial_number"), [d3.id, d1.id, d2.id]+ )+ self.assertListEqual(+ search_query("&ordering=-archive_serial_number"), [d2.id, d1.id, d3.id]+ )self.assertListEqual(search_query("&ordering=title"), [d3.id, d2.id, d1.id])self.assertListEqual(search_query("&ordering=-title"), [d1.id, d2.id, d3.id])- self.assertListEqual(search_query("&ordering=correspondent__name"), [d1.id, d3.id, d2.id])- self.assertListEqual(search_query("&ordering=-correspondent__name"), [d2.id, d3.id, d1.id])-+ self.assertListEqual(+ search_query("&ordering=correspondent__name"), [d1.id, d3.id, d2.id]+ )+ self.assertListEqual(+ search_query("&ordering=-correspondent__name"), [d2.id, d3.id, d1.id]+ )def test_statistics(self):@@ -508,95 +721,125 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):response = self.client.get("/api/statistics/")self.assertEqual(response.status_code, 200)- self.assertEqual(response.data['documents_total'], 3)- self.assertEqual(response.data['documents_inbox'], 1)+ self.assertEqual(response.data["documents_total"], 3)+ self.assertEqual(response.data["documents_inbox"], 1)def test_statistics_no_inbox_tag(self):Document.objects.create(title="none1", checksum="A")response = self.client.get("/api/statistics/")self.assertEqual(response.status_code, 200)- self.assertEqual(response.data['documents_inbox'], None)+ self.assertEqual(response.data["documents_inbox"], None)@mock.patch("documents.views.async_task")def test_upload(self, m):- with open(os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), "rb") as f:- response = self.client.post("/api/documents/post_document/", {"document": f})+ with open(+ os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), "rb"+ ) as f:+ response = self.client.post(+ "/api/documents/post_document/", {"document": f}+ )self.assertEqual(response.status_code, 200)m.assert_called_once()args, kwargs = m.call_args- self.assertEqual(kwargs['override_filename'], "simple.pdf")- self.assertIsNone(kwargs['override_title'])- self.assertIsNone(kwargs['override_correspondent_id'])- self.assertIsNone(kwargs['override_document_type_id'])- self.assertIsNone(kwargs['override_tag_ids'])+ self.assertEqual(kwargs["override_filename"], "simple.pdf")+ self.assertIsNone(kwargs["override_title"])+ self.assertIsNone(kwargs["override_correspondent_id"])+ self.assertIsNone(kwargs["override_document_type_id"])+ self.assertIsNone(kwargs["override_tag_ids"])@mock.patch("documents.views.async_task")def test_upload_empty_metadata(self, m):- with open(os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), "rb") as f:- response = self.client.post("/api/documents/post_document/", {"document": f, "title": "", "correspondent": "", "document_type": ""})+ with open(+ os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), "rb"+ ) as f:+ response = self.client.post(+ "/api/documents/post_document/",+ {"document": f, "title": "", "correspondent": "", "document_type": ""},+ )self.assertEqual(response.status_code, 200)m.assert_called_once()args, kwargs = m.call_args- self.assertEqual(kwargs['override_filename'], "simple.pdf")- self.assertIsNone(kwargs['override_title'])- self.assertIsNone(kwargs['override_correspondent_id'])- self.assertIsNone(kwargs['override_document_type_id'])- self.assertIsNone(kwargs['override_tag_ids'])+ self.assertEqual(kwargs["override_filename"], "simple.pdf")+ self.assertIsNone(kwargs["override_title"])+ self.assertIsNone(kwargs["override_correspondent_id"])+ self.assertIsNone(kwargs["override_document_type_id"])+ self.assertIsNone(kwargs["override_tag_ids"])@mock.patch("documents.views.async_task")def test_upload_invalid_form(self, m):- with open(os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), "rb") as f:- response = self.client.post("/api/documents/post_document/", {"documenst": f})+ with open(+ os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), "rb"+ ) as f:+ response = self.client.post(+ "/api/documents/post_document/", {"documenst": f}+ )self.assertEqual(response.status_code, 400)m.assert_not_called()@mock.patch("documents.views.async_task")def test_upload_invalid_file(self, m):- with open(os.path.join(os.path.dirname(__file__), "samples", "simple.zip"), "rb") as f:- response = self.client.post("/api/documents/post_document/", {"document": f})+ with open(+ os.path.join(os.path.dirname(__file__), "samples", "simple.zip"), "rb"+ ) as f:+ response = self.client.post(+ "/api/documents/post_document/", {"document": f}+ )self.assertEqual(response.status_code, 400)m.assert_not_called()@mock.patch("documents.views.async_task")def test_upload_with_title(self, async_task):- with open(os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), "rb") as f:- response = self.client.post("/api/documents/post_document/", {"document": f, "title": "my custom title"})+ with open(+ os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), "rb"+ ) as f:+ response = self.client.post(+ "/api/documents/post_document/",+ {"document": f, "title": "my custom title"},+ )self.assertEqual(response.status_code, 200)async_task.assert_called_once()args, kwargs = async_task.call_args- self.assertEqual(kwargs['override_title'], "my custom title")+ self.assertEqual(kwargs["override_title"], "my custom title")@mock.patch("documents.views.async_task")def test_upload_with_correspondent(self, async_task):c = Correspondent.objects.create(name="test-corres")- with open(os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), "rb") as f:- response = self.client.post("/api/documents/post_document/", {"document": f, "correspondent": c.id})+ with open(+ os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), "rb"+ ) as f:+ response = self.client.post(+ "/api/documents/post_document/", {"document": f, "correspondent": c.id}+ )self.assertEqual(response.status_code, 200)async_task.assert_called_once()args, kwargs = async_task.call_args- self.assertEqual(kwargs['override_correspondent_id'], c.id)+ self.assertEqual(kwargs["override_correspondent_id"], c.id)@mock.patch("documents.views.async_task")def test_upload_with_invalid_correspondent(self, async_task):- with open(os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), "rb") as f:- response = self.client.post("/api/documents/post_document/", {"document": f, "correspondent": 3456})+ with open(+ os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), "rb"+ ) as f:+ response = self.client.post(+ "/api/documents/post_document/", {"document": f, "correspondent": 3456}+ )self.assertEqual(response.status_code, 400)async_task.assert_not_called()@@ -604,20 +847,28 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):@mock.patch("documents.views.async_task")def test_upload_with_document_type(self, async_task):dt = DocumentType.objects.create(name="invoice")- with open(os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), "rb") as f:- response = self.client.post("/api/documents/post_document/", {"document": f, "document_type": dt.id})+ with open(+ os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), "rb"+ ) as f:+ response = self.client.post(+ "/api/documents/post_document/", {"document": f, "document_type": dt.id}+ )self.assertEqual(response.status_code, 200)async_task.assert_called_once()args, kwargs = async_task.call_args- self.assertEqual(kwargs['override_document_type_id'], dt.id)+ self.assertEqual(kwargs["override_document_type_id"], dt.id)@mock.patch("documents.views.async_task")def test_upload_with_invalid_document_type(self, async_task):- with open(os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), "rb") as f:- response = self.client.post("/api/documents/post_document/", {"document": f, "document_type": 34578})+ with open(+ os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), "rb"+ ) as f:+ response = self.client.post(+ "/api/documents/post_document/", {"document": f, "document_type": 34578}+ )self.assertEqual(response.status_code, 400)async_task.assert_not_called()@@ -626,34 +877,51 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):def test_upload_with_tags(self, async_task):t1 = Tag.objects.create(name="tag1")t2 = Tag.objects.create(name="tag2")- with open(os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), "rb") as f:+ with open(+ os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), "rb"+ ) as f:response = self.client.post(- "/api/documents/post_document/",- {"document": f, "tags": [t2.id, t1.id]})+ "/api/documents/post_document/", {"document": f, "tags": [t2.id, t1.id]}+ )self.assertEqual(response.status_code, 200)async_task.assert_called_once()args, kwargs = async_task.call_args- self.assertCountEqual(kwargs['override_tag_ids'], [t1.id, t2.id])+ self.assertCountEqual(kwargs["override_tag_ids"], [t1.id, t2.id])@mock.patch("documents.views.async_task")def test_upload_with_invalid_tags(self, async_task):t1 = Tag.objects.create(name="tag1")t2 = Tag.objects.create(name="tag2")- with open(os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), "rb") as f:+ with open(+ os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), "rb"+ ) as f:response = self.client.post("/api/documents/post_document/",- {"document": f, "tags": [t2.id, t1.id, 734563]})+ {"document": f, "tags": [t2.id, t1.id, 734563]},+ )self.assertEqual(response.status_code, 400)async_task.assert_not_called()def test_get_metadata(self):- doc = Document.objects.create(title="test", filename="file.pdf", mime_type="image/png", archive_checksum="A", archive_filename="archive.pdf")-- source_file = os.path.join(os.path.dirname(__file__), "samples", "documents", "thumbnails", "0000001.png")+ doc = Document.objects.create(+ title="test",+ filename="file.pdf",+ mime_type="image/png",+ archive_checksum="A",+ archive_filename="archive.pdf",+ )++ source_file = os.path.join(+ os.path.dirname(__file__),+ "samples",+ "documents",+ "thumbnails",+ "0000001.png",+ )archive_file = os.path.join(os.path.dirname(__file__), "samples", "simple.pdf")shutil.copy(source_file, doc.source_path)@@ -664,49 +932,60 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):meta = response.data- self.assertEqual(meta['original_mime_type'], "image/png")- self.assertTrue(meta['has_archive_version'])- self.assertEqual(len(meta['original_metadata']), 0)- self.assertGreater(len(meta['archive_metadata']), 0)- self.assertEqual(meta['media_filename'], "file.pdf")- self.assertEqual(meta['archive_media_filename'], "archive.pdf")- self.assertEqual(meta['original_size'], os.stat(source_file).st_size)- self.assertEqual(meta['archive_size'], os.stat(archive_file).st_size)+ self.assertEqual(meta["original_mime_type"], "image/png")+ self.assertTrue(meta["has_archive_version"])+ self.assertEqual(len(meta["original_metadata"]), 0)+ self.assertGreater(len(meta["archive_metadata"]), 0)+ self.assertEqual(meta["media_filename"], "file.pdf")+ self.assertEqual(meta["archive_media_filename"], "archive.pdf")+ self.assertEqual(meta["original_size"], os.stat(source_file).st_size)+ self.assertEqual(meta["archive_size"], os.stat(archive_file).st_size)def test_get_metadata_invalid_doc(self):response = self.client.get(f"/api/documents/34576/metadata/")self.assertEqual(response.status_code, 404)def test_get_metadata_no_archive(self):- doc = Document.objects.create(title="test", filename="file.pdf", mime_type="application/pdf")+ doc = Document.objects.create(+ title="test", filename="file.pdf", mime_type="application/pdf"+ )- shutil.copy(os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), doc.source_path)+ shutil.copy(+ os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),+ doc.source_path,+ )response = self.client.get(f"/api/documents/{doc.pk}/metadata/")self.assertEqual(response.status_code, 200)meta = response.data- self.assertEqual(meta['original_mime_type'], "application/pdf")- self.assertFalse(meta['has_archive_version'])- self.assertGreater(len(meta['original_metadata']), 0)- self.assertIsNone(meta['archive_metadata'])- self.assertIsNone(meta['archive_media_filename'])+ self.assertEqual(meta["original_mime_type"], "application/pdf")+ self.assertFalse(meta["has_archive_version"])+ self.assertGreater(len(meta["original_metadata"]), 0)+ self.assertIsNone(meta["archive_metadata"])+ self.assertIsNone(meta["archive_media_filename"])def test_get_metadata_missing_files(self):- doc = Document.objects.create(title="test", filename="file.pdf", mime_type="application/pdf", archive_filename="file.pdf", archive_checksum="B", checksum="A")+ doc = Document.objects.create(+ title="test",+ filename="file.pdf",+ mime_type="application/pdf",+ archive_filename="file.pdf",+ archive_checksum="B",+ checksum="A",+ )response = self.client.get(f"/api/documents/{doc.pk}/metadata/")self.assertEqual(response.status_code, 200)meta = response.data- self.assertTrue(meta['has_archive_version'])- self.assertIsNone(meta['original_metadata'])- self.assertIsNone(meta['original_size'])- self.assertIsNone(meta['archive_metadata'])- self.assertIsNone(meta['archive_size'])-+ self.assertTrue(meta["has_archive_version"])+ self.assertIsNone(meta["original_metadata"])+ self.assertIsNone(meta["original_size"])+ self.assertIsNone(meta["archive_metadata"])+ self.assertIsNone(meta["archive_size"])def test_get_empty_suggestions(self):doc = Document.objects.create(title="test", mime_type="application/pdf")@@ -714,7 +993,9 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):response = self.client.get(f"/api/documents/{doc.pk}/suggestions/")self.assertEqual(response.status_code, 200)- self.assertEqual(response.data, {'correspondents': [], 'tags': [], 'document_types': []})+ self.assertEqual(+ response.data, {"correspondents": [], "tags": [], "document_types": []}+ )def test_get_suggestions_invalid_doc(self):response = self.client.get(f"/api/documents/34676/suggestions/")@@ -723,26 +1004,51 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):@mock.patch("documents.views.match_correspondents")@mock.patch("documents.views.match_tags")@mock.patch("documents.views.match_document_types")- def test_get_suggestions(self, match_document_types, match_tags, match_correspondents):- doc = Document.objects.create(title="test", mime_type="application/pdf", content="this is an invoice!")+ def test_get_suggestions(+ self, match_document_types, match_tags, match_correspondents+ ):+ doc = Document.objects.create(+ title="test", mime_type="application/pdf", content="this is an invoice!"+ )match_tags.return_value = [Tag(id=56), Tag(id=123)]match_document_types.return_value = [DocumentType(id=23)]match_correspondents.return_value = [Correspondent(id=88), Correspondent(id=2)]response = self.client.get(f"/api/documents/{doc.pk}/suggestions/")- self.assertEqual(response.data, {'correspondents': [88,2], 'tags': [56,123], 'document_types': [23]})+ self.assertEqual(+ response.data,+ {"correspondents": [88, 2], "tags": [56, 123], "document_types": [23]},+ )def test_saved_views(self):u1 = User.objects.create_user("user1")u2 = User.objects.create_user("user2")- v1 = SavedView.objects.create(user=u1, name="test1", sort_field="", show_on_dashboard=False, show_in_sidebar=False)- v2 = SavedView.objects.create(user=u2, name="test2", sort_field="", show_on_dashboard=False, show_in_sidebar=False)- v3 = SavedView.objects.create(user=u2, name="test3", sort_field="", show_on_dashboard=False, show_in_sidebar=False)+ v1 = SavedView.objects.create(+ user=u1,+ name="test1",+ sort_field="",+ show_on_dashboard=False,+ show_in_sidebar=False,+ )+ v2 = SavedView.objects.create(+ user=u2,+ name="test2",+ sort_field="",+ show_on_dashboard=False,+ show_in_sidebar=False,+ )+ v3 = SavedView.objects.create(+ user=u2,+ name="test3",+ sort_field="",+ show_on_dashboard=False,+ show_in_sidebar=False,+ )response = self.client.get("/api/saved_views/")self.assertEqual(response.status_code, 200)- self.assertEqual(response.data['count'], 0)+ self.assertEqual(response.data["count"], 0)self.assertEqual(self.client.get(f"/api/saved_views/{v1.id}/").status_code, 404)@@ -750,7 +1056,7 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):response = self.client.get("/api/saved_views/")self.assertEqual(response.status_code, 200)- self.assertEqual(response.data['count'], 1)+ self.assertEqual(response.data["count"], 1)self.assertEqual(self.client.get(f"/api/saved_views/{v1.id}/").status_code, 200)@@ -758,7 +1064,7 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):response = self.client.get("/api/saved_views/")self.assertEqual(response.status_code, 200)- self.assertEqual(response.data['count'], 2)+ self.assertEqual(response.data["count"], 2)self.assertEqual(self.client.get(f"/api/saved_views/{v1.id}/").status_code, 404)@@ -771,15 +1077,10 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):"show_on_dashboard": True,"show_in_sidebar": True,"sort_field": "created2",- "filter_rules": [- {- "rule_type": 4,- "value": "test"- }- ]+ "filter_rules": [{"rule_type": 4, "value": "test"}],}- response = self.client.post("/api/saved_views/", view, format='json')+ response = self.client.post("/api/saved_views/", view, format="json")self.assertEqual(response.status_code, 201)v1 = SavedView.objects.get(name="test")@@ -787,30 +1088,27 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):self.assertEqual(v1.filter_rules.count(), 1)self.assertEqual(v1.user, self.user)- response = self.client.patch(f"/api/saved_views/{v1.id}/", {- "show_in_sidebar": False- }, format='json')+ response = self.client.patch(+ f"/api/saved_views/{v1.id}/", {"show_in_sidebar": False}, format="json"+ )v1 = SavedView.objects.get(id=v1.id)self.assertEqual(response.status_code, 200)self.assertFalse(v1.show_in_sidebar)self.assertEqual(v1.filter_rules.count(), 1)- view['filter_rules'] = [{- "rule_type": 12,- "value": "secret"- }]+ view["filter_rules"] = [{"rule_type": 12, "value": "secret"}]- response = self.client.put(f"/api/saved_views/{v1.id}/", view, format='json')+ response = self.client.put(f"/api/saved_views/{v1.id}/", view, format="json")self.assertEqual(response.status_code, 200)v1 = SavedView.objects.get(id=v1.id)self.assertEqual(v1.filter_rules.count(), 1)self.assertEqual(v1.filter_rules.first().value, "secret")- view['filter_rules'] = []+ view["filter_rules"] = []- response = self.client.put(f"/api/saved_views/{v1.id}/", view, format='json')+ response = self.client.put(f"/api/saved_views/{v1.id}/", view, format="json")self.assertEqual(response.status_code, 200)v1 = SavedView.objects.get(id=v1.id)@@ -839,113 +1137,167 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):self.assertListEqual(response.data, ["test", "test2"])def test_invalid_regex_other_algorithm(self):- for endpoint in ['correspondents', 'tags', 'document_types']:- response = self.client.post(f"/api/{endpoint}/", {- "name": "test",- "matching_algorithm": MatchingModel.MATCH_ANY,- "match": "["- }, format='json')+ for endpoint in ["correspondents", "tags", "document_types"]:+ response = self.client.post(+ f"/api/{endpoint}/",+ {+ "name": "test",+ "matching_algorithm": MatchingModel.MATCH_ANY,+ "match": "[",+ },+ format="json",+ )self.assertEqual(response.status_code, 201, endpoint)def test_invalid_regex(self):- for endpoint in ['correspondents', 'tags', 'document_types']:- response = self.client.post(f"/api/{endpoint}/", {- "name": "test",- "matching_algorithm": MatchingModel.MATCH_REGEX,- "match": "["- }, format='json')+ for endpoint in ["correspondents", "tags", "document_types"]:+ response = self.client.post(+ f"/api/{endpoint}/",+ {+ "name": "test",+ "matching_algorithm": MatchingModel.MATCH_REGEX,+ "match": "[",+ },+ format="json",+ )self.assertEqual(response.status_code, 400, endpoint)def test_valid_regex(self):- for endpoint in ['correspondents', 'tags', 'document_types']:- response = self.client.post(f"/api/{endpoint}/", {- "name": "test",- "matching_algorithm": MatchingModel.MATCH_REGEX,- "match": "[0-9]"- }, format='json')+ for endpoint in ["correspondents", "tags", "document_types"]:+ response = self.client.post(+ f"/api/{endpoint}/",+ {+ "name": "test",+ "matching_algorithm": MatchingModel.MATCH_REGEX,+ "match": "[0-9]",+ },+ format="json",+ )self.assertEqual(response.status_code, 201, endpoint)def test_regex_no_algorithm(self):- for endpoint in ['correspondents', 'tags', 'document_types']:- response = self.client.post(f"/api/{endpoint}/", {- "name": "test",- "match": "[0-9]"- }, format='json')+ for endpoint in ["correspondents", "tags", "document_types"]:+ response = self.client.post(+ f"/api/{endpoint}/", {"name": "test", "match": "[0-9]"}, format="json"+ )self.assertEqual(response.status_code, 201, endpoint)def test_tag_color_default(self):- response = self.client.post("/api/tags/", {- "name": "tag"- }, format="json")+ response = self.client.post("/api/tags/", {"name": "tag"}, format="json")self.assertEqual(response.status_code, 201)- self.assertEqual(Tag.objects.get(id=response.data['id']).color, "#a6cee3")- self.assertEqual(self.client.get(f"/api/tags/{response.data['id']}/", format="json").data['colour'], 1)+ self.assertEqual(Tag.objects.get(id=response.data["id"]).color, "#a6cee3")+ self.assertEqual(+ self.client.get(f"/api/tags/{response.data['id']}/", format="json").data[+ "colour"+ ],+ 1,+ )def test_tag_color(self):- response = self.client.post("/api/tags/", {- "name": "tag",- "colour": 3- }, format="json")+ response = self.client.post(+ "/api/tags/", {"name": "tag", "colour": 3}, format="json"+ )self.assertEqual(response.status_code, 201)- self.assertEqual(Tag.objects.get(id=response.data['id']).color, "#b2df8a")- self.assertEqual(self.client.get(f"/api/tags/{response.data['id']}/", format="json").data['colour'], 3)+ self.assertEqual(Tag.objects.get(id=response.data["id"]).color, "#b2df8a")+ self.assertEqual(+ self.client.get(f"/api/tags/{response.data['id']}/", format="json").data[+ "colour"+ ],+ 3,+ )def test_tag_color_invalid(self):- response = self.client.post("/api/tags/", {- "name": "tag",- "colour": 34- }, format="json")+ response = self.client.post(+ "/api/tags/", {"name": "tag", "colour": 34}, format="json"+ )self.assertEqual(response.status_code, 400)def test_tag_color_custom(self):tag = Tag.objects.create(name="test", color="#abcdef")- self.assertEqual(self.client.get(f"/api/tags/{tag.id}/", format="json").data['colour'], 1)+ self.assertEqual(+ self.client.get(f"/api/tags/{tag.id}/", format="json").data["colour"], 1+ )class TestDocumentApiV2(DirectoriesMixin, APITestCase):-def setUp(self):super(TestDocumentApiV2, self).setUp()self.user = User.objects.create_superuser(username="temp_admin")self.client.force_login(user=self.user)- self.client.defaults['HTTP_ACCEPT'] = 'application/json; version=2'+ self.client.defaults["HTTP_ACCEPT"] = "application/json; version=2"def test_tag_validate_color(self):- self.assertEqual(self.client.post("/api/tags/", {"name": "test", "color": "#12fFaA"}, format="json").status_code, 201)-- self.assertEqual(self.client.post("/api/tags/", {"name": "test1", "color": "abcdef"}, format="json").status_code, 400)- self.assertEqual(self.client.post("/api/tags/", {"name": "test2", "color": "#abcdfg"}, format="json").status_code, 400)- self.assertEqual(self.client.post("/api/tags/", {"name": "test3", "color": "#asd"}, format="json").status_code, 400)- self.assertEqual(self.client.post("/api/tags/", {"name": "test4", "color": "#12121212"}, format="json").status_code, 400)+ self.assertEqual(+ self.client.post(+ "/api/tags/", {"name": "test", "color": "#12fFaA"}, format="json"+ ).status_code,+ 201,+ )++ self.assertEqual(+ self.client.post(+ "/api/tags/", {"name": "test1", "color": "abcdef"}, format="json"+ ).status_code,+ 400,+ )+ self.assertEqual(+ self.client.post(+ "/api/tags/", {"name": "test2", "color": "#abcdfg"}, format="json"+ ).status_code,+ 400,+ )+ self.assertEqual(+ self.client.post(+ "/api/tags/", {"name": "test3", "color": "#asd"}, format="json"+ ).status_code,+ 400,+ )+ self.assertEqual(+ self.client.post(+ "/api/tags/", {"name": "test4", "color": "#12121212"}, format="json"+ ).status_code,+ 400,+ )def test_tag_text_color(self):t = Tag.objects.create(name="tag1", color="#000000")- self.assertEqual(self.client.get(f"/api/tags/{t.id}/", format="json").data['text_color'], "#ffffff")+ self.assertEqual(+ self.client.get(f"/api/tags/{t.id}/", format="json").data["text_color"],+ "#ffffff",+ )t.color = "#ffffff"t.save()- self.assertEqual(self.client.get(f"/api/tags/{t.id}/", format="json").data['text_color'], "#000000")+ self.assertEqual(+ self.client.get(f"/api/tags/{t.id}/", format="json").data["text_color"],+ "#000000",+ )t.color = "asdf"t.save()- self.assertEqual(self.client.get(f"/api/tags/{t.id}/", format="json").data['text_color'], "#000000")+ self.assertEqual(+ self.client.get(f"/api/tags/{t.id}/", format="json").data["text_color"],+ "#000000",+ )t.color = "123"t.save()- self.assertEqual(self.client.get(f"/api/tags/{t.id}/", format="json").data['text_color'], "#000000")+ self.assertEqual(+ self.client.get(f"/api/tags/{t.id}/", format="json").data["text_color"],+ "#000000",+ )class TestBulkEdit(DirectoriesMixin, APITestCase):-def setUp(self):super(TestBulkEdit, self).setUp()user = User.objects.create_superuser(username="temp_admin")self.client.force_login(user=user)- patcher = mock.patch('documents.bulk_edit.async_task')+ patcher = mock.patch("documents.bulk_edit.async_task")self.async_task = patcher.start()self.addCleanup(patcher.stop)self.c1 = Correspondent.objects.create(name="c1")@@ -955,8 +1307,12 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):self.t1 = Tag.objects.create(name="t1")self.t2 = Tag.objects.create(name="t2")self.doc1 = Document.objects.create(checksum="A", title="A")- self.doc2 = Document.objects.create(checksum="B", title="B", correspondent=self.c1, document_type=self.dt1)- self.doc3 = Document.objects.create(checksum="C", title="C", correspondent=self.c2, document_type=self.dt2)+ self.doc2 = Document.objects.create(+ checksum="B", title="B", correspondent=self.c1, document_type=self.dt1+ )+ self.doc3 = Document.objects.create(+ checksum="C", title="C", correspondent=self.c2, document_type=self.dt2+ )self.doc4 = Document.objects.create(checksum="D", title="D")self.doc5 = Document.objects.create(checksum="E", title="E")self.doc2.tags.add(self.t1)@@ -965,11 +1321,13 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):def test_set_correspondent(self):self.assertEqual(Document.objects.filter(correspondent=self.c2).count(), 1)- bulk_edit.set_correspondent([self.doc1.id, self.doc2.id, self.doc3.id], self.c2.id)+ bulk_edit.set_correspondent(+ [self.doc1.id, self.doc2.id, self.doc3.id], self.c2.id+ )self.assertEqual(Document.objects.filter(correspondent=self.c2).count(), 3)self.async_task.assert_called_once()args, kwargs = self.async_task.call_args- self.assertCountEqual(kwargs['document_ids'], [self.doc1.id, self.doc2.id])+ self.assertCountEqual(kwargs["document_ids"], [self.doc1.id, self.doc2.id])def test_unset_correspondent(self):self.assertEqual(Document.objects.filter(correspondent=self.c2).count(), 1)@@ -977,15 +1335,17 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):self.assertEqual(Document.objects.filter(correspondent=self.c2).count(), 0)self.async_task.assert_called_once()args, kwargs = self.async_task.call_args- self.assertCountEqual(kwargs['document_ids'], [self.doc2.id, self.doc3.id])+ self.assertCountEqual(kwargs["document_ids"], [self.doc2.id, self.doc3.id])def test_set_document_type(self):self.assertEqual(Document.objects.filter(document_type=self.dt2).count(), 1)- bulk_edit.set_document_type([self.doc1.id, self.doc2.id, self.doc3.id], self.dt2.id)+ bulk_edit.set_document_type(+ [self.doc1.id, self.doc2.id, self.doc3.id], self.dt2.id+ )self.assertEqual(Document.objects.filter(document_type=self.dt2).count(), 3)self.async_task.assert_called_once()args, kwargs = self.async_task.call_args- self.assertCountEqual(kwargs['document_ids'], [self.doc1.id, self.doc2.id])+ self.assertCountEqual(kwargs["document_ids"], [self.doc1.id, self.doc2.id])def test_unset_document_type(self):self.assertEqual(Document.objects.filter(document_type=self.dt2).count(), 1)@@ -993,15 +1353,17 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):self.assertEqual(Document.objects.filter(document_type=self.dt2).count(), 0)self.async_task.assert_called_once()args, kwargs = self.async_task.call_args- self.assertCountEqual(kwargs['document_ids'], [self.doc2.id, self.doc3.id])+ self.assertCountEqual(kwargs["document_ids"], [self.doc2.id, self.doc3.id])def test_add_tag(self):self.assertEqual(Document.objects.filter(tags__id=self.t1.id).count(), 2)- bulk_edit.add_tag([self.doc1.id, self.doc2.id, self.doc3.id, self.doc4.id], self.t1.id)+ bulk_edit.add_tag(+ [self.doc1.id, self.doc2.id, self.doc3.id, self.doc4.id], self.t1.id+ )self.assertEqual(Document.objects.filter(tags__id=self.t1.id).count(), 4)self.async_task.assert_called_once()args, kwargs = self.async_task.call_args- self.assertCountEqual(kwargs['document_ids'], [self.doc1.id, self.doc3.id])+ self.assertCountEqual(kwargs["document_ids"], [self.doc1.id, self.doc3.id])def test_remove_tag(self):self.assertEqual(Document.objects.filter(tags__id=self.t1.id).count(), 2)@@ -1009,13 +1371,17 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):self.assertEqual(Document.objects.filter(tags__id=self.t1.id).count(), 1)self.async_task.assert_called_once()args, kwargs = self.async_task.call_args- self.assertCountEqual(kwargs['document_ids'], [self.doc4.id])+ self.assertCountEqual(kwargs["document_ids"], [self.doc4.id])def test_modify_tags(self):tag_unrelated = Tag.objects.create(name="unrelated")self.doc2.tags.add(tag_unrelated)self.doc3.tags.add(tag_unrelated)- bulk_edit.modify_tags([self.doc2.id, self.doc3.id], add_tags=[self.t2.id], remove_tags=[self.t1.id])+ bulk_edit.modify_tags(+ [self.doc2.id, self.doc3.id],+ add_tags=[self.t2.id],+ remove_tags=[self.t1.id],+ )self.assertCountEqual(list(self.doc2.tags.all()), [self.t2, tag_unrelated])self.assertCountEqual(list(self.doc3.tags.all()), [self.t2, tag_unrelated])@@ -1023,121 +1389,171 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):self.async_task.assert_called_once()args, kwargs = self.async_task.call_args# TODO: doc3 should not be affected, but the query for that is rather complicated- self.assertCountEqual(kwargs['document_ids'], [self.doc2.id, self.doc3.id])+ self.assertCountEqual(kwargs["document_ids"], [self.doc2.id, self.doc3.id])def test_delete(self):self.assertEqual(Document.objects.count(), 5)bulk_edit.delete([self.doc1.id, self.doc2.id])self.assertEqual(Document.objects.count(), 3)- self.assertCountEqual([doc.id for doc in Document.objects.all()], [self.doc3.id, self.doc4.id, self.doc5.id])+ self.assertCountEqual(+ [doc.id for doc in Document.objects.all()],+ [self.doc3.id, self.doc4.id, self.doc5.id],+ )@mock.patch("documents.serialisers.bulk_edit.set_correspondent")def test_api_set_correspondent(self, m):m.return_value = "OK"- response = self.client.post("/api/documents/bulk_edit/", json.dumps({- "documents": [self.doc1.id],- "method": "set_correspondent",- "parameters": {"correspondent": self.c1.id}- }), content_type='application/json')+ response = self.client.post(+ "/api/documents/bulk_edit/",+ json.dumps(+ {+ "documents": [self.doc1.id],+ "method": "set_correspondent",+ "parameters": {"correspondent": self.c1.id},+ }+ ),+ content_type="application/json",+ )self.assertEqual(response.status_code, 200)m.assert_called_once()args, kwargs = m.call_argsself.assertEqual(args[0], [self.doc1.id])- self.assertEqual(kwargs['correspondent'], self.c1.id)+ self.assertEqual(kwargs["correspondent"], self.c1.id)@mock.patch("documents.serialisers.bulk_edit.set_correspondent")def test_api_unset_correspondent(self, m):m.return_value = "OK"- response = self.client.post("/api/documents/bulk_edit/", json.dumps({- "documents": [self.doc1.id],- "method": "set_correspondent",- "parameters": {"correspondent": None}- }), content_type='application/json')+ response = self.client.post(+ "/api/documents/bulk_edit/",+ json.dumps(+ {+ "documents": [self.doc1.id],+ "method": "set_correspondent",+ "parameters": {"correspondent": None},+ }+ ),+ content_type="application/json",+ )self.assertEqual(response.status_code, 200)m.assert_called_once()args, kwargs = m.call_argsself.assertEqual(args[0], [self.doc1.id])- self.assertIsNone(kwargs['correspondent'])+ self.assertIsNone(kwargs["correspondent"])@mock.patch("documents.serialisers.bulk_edit.set_document_type")def test_api_set_type(self, m):m.return_value = "OK"- response = self.client.post("/api/documents/bulk_edit/", json.dumps({- "documents": [self.doc1.id],- "method": "set_document_type",- "parameters": {"document_type": self.dt1.id}- }), content_type='application/json')+ response = self.client.post(+ "/api/documents/bulk_edit/",+ json.dumps(+ {+ "documents": [self.doc1.id],+ "method": "set_document_type",+ "parameters": {"document_type": self.dt1.id},+ }+ ),+ content_type="application/json",+ )self.assertEqual(response.status_code, 200)m.assert_called_once()args, kwargs = m.call_argsself.assertEqual(args[0], [self.doc1.id])- self.assertEqual(kwargs['document_type'], self.dt1.id)+ self.assertEqual(kwargs["document_type"], self.dt1.id)@mock.patch("documents.serialisers.bulk_edit.set_document_type")def test_api_unset_type(self, m):m.return_value = "OK"- response = self.client.post("/api/documents/bulk_edit/", json.dumps({- "documents": [self.doc1.id],- "method": "set_document_type",- "parameters": {"document_type": None}- }), content_type='application/json')+ response = self.client.post(+ "/api/documents/bulk_edit/",+ json.dumps(+ {+ "documents": [self.doc1.id],+ "method": "set_document_type",+ "parameters": {"document_type": None},+ }+ ),+ content_type="application/json",+ )self.assertEqual(response.status_code, 200)m.assert_called_once()args, kwargs = m.call_argsself.assertEqual(args[0], [self.doc1.id])- self.assertIsNone(kwargs['document_type'])+ self.assertIsNone(kwargs["document_type"])@mock.patch("documents.serialisers.bulk_edit.add_tag")def test_api_add_tag(self, m):m.return_value = "OK"- response = self.client.post("/api/documents/bulk_edit/", json.dumps({- "documents": [self.doc1.id],- "method": "add_tag",- "parameters": {"tag": self.t1.id}- }), content_type='application/json')+ response = self.client.post(+ "/api/documents/bulk_edit/",+ json.dumps(+ {+ "documents": [self.doc1.id],+ "method": "add_tag",+ "parameters": {"tag": self.t1.id},+ }+ ),+ content_type="application/json",+ )self.assertEqual(response.status_code, 200)m.assert_called_once()args, kwargs = m.call_argsself.assertEqual(args[0], [self.doc1.id])- self.assertEqual(kwargs['tag'], self.t1.id)+ self.assertEqual(kwargs["tag"], self.t1.id)@mock.patch("documents.serialisers.bulk_edit.remove_tag")def test_api_remove_tag(self, m):m.return_value = "OK"- response = self.client.post("/api/documents/bulk_edit/", json.dumps({- "documents": [self.doc1.id],- "method": "remove_tag",- "parameters": {"tag": self.t1.id}- }), content_type='application/json')+ response = self.client.post(+ "/api/documents/bulk_edit/",+ json.dumps(+ {+ "documents": [self.doc1.id],+ "method": "remove_tag",+ "parameters": {"tag": self.t1.id},+ }+ ),+ content_type="application/json",+ )self.assertEqual(response.status_code, 200)m.assert_called_once()args, kwargs = m.call_argsself.assertEqual(args[0], [self.doc1.id])- self.assertEqual(kwargs['tag'], self.t1.id)+ self.assertEqual(kwargs["tag"], self.t1.id)@mock.patch("documents.serialisers.bulk_edit.modify_tags")def test_api_modify_tags(self, m):m.return_value = "OK"- response = self.client.post("/api/documents/bulk_edit/", json.dumps({- "documents": [self.doc1.id, self.doc3.id],- "method": "modify_tags",- "parameters": {"add_tags": [self.t1.id], "remove_tags": [self.t2.id]}- }), content_type='application/json')+ response = self.client.post(+ "/api/documents/bulk_edit/",+ json.dumps(+ {+ "documents": [self.doc1.id, self.doc3.id],+ "method": "modify_tags",+ "parameters": {+ "add_tags": [self.t1.id],+ "remove_tags": [self.t2.id],+ },+ }+ ),+ content_type="application/json",+ )self.assertEqual(response.status_code, 200)m.assert_called_once()args, kwargs = m.call_argsself.assertListEqual(args[0], [self.doc1.id, self.doc3.id])- self.assertEqual(kwargs['add_tags'], [self.t1.id])- self.assertEqual(kwargs['remove_tags'], [self.t2.id])+ self.assertEqual(kwargs["add_tags"], [self.t1.id])+ self.assertEqual(kwargs["remove_tags"], [self.t2.id])@mock.patch("documents.serialisers.bulk_edit.delete")def test_api_delete(self, m):m.return_value = "OK"- response = self.client.post("/api/documents/bulk_edit/", json.dumps({- "documents": [self.doc1.id],- "method": "delete",- "parameters": {}- }), content_type='application/json')+ response = self.client.post(+ "/api/documents/bulk_edit/",+ json.dumps(+ {"documents": [self.doc1.id], "method": "delete", "parameters": {}}+ ),+ content_type="application/json",+ )self.assertEqual(response.status_code, 200)m.assert_called_once()args, kwargs = m.call_args@@ -1146,152 +1562,243 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):def test_api_invalid_doc(self):self.assertEqual(Document.objects.count(), 5)- response = self.client.post("/api/documents/bulk_edit/", json.dumps({- "documents": [-235],- "method": "delete",- "parameters": {}- }), content_type='application/json')+ response = self.client.post(+ "/api/documents/bulk_edit/",+ json.dumps({"documents": [-235], "method": "delete", "parameters": {}}),+ content_type="application/json",+ )self.assertEqual(response.status_code, 400)self.assertEqual(Document.objects.count(), 5)def test_api_invalid_method(self):self.assertEqual(Document.objects.count(), 5)- response = self.client.post("/api/documents/bulk_edit/", json.dumps({- "documents": [self.doc2.id],- "method": "exterminate",- "parameters": {}- }), content_type='application/json')+ response = self.client.post(+ "/api/documents/bulk_edit/",+ json.dumps(+ {"documents": [self.doc2.id], "method": "exterminate", "parameters": {}}+ ),+ content_type="application/json",+ )self.assertEqual(response.status_code, 400)self.assertEqual(Document.objects.count(), 5)def test_api_invalid_correspondent(self):self.assertEqual(self.doc2.correspondent, self.c1)- response = self.client.post("/api/documents/bulk_edit/", json.dumps({- "documents": [self.doc2.id],- "method": "set_correspondent",- "parameters": {'correspondent': 345657}- }), content_type='application/json')+ response = self.client.post(+ "/api/documents/bulk_edit/",+ json.dumps(+ {+ "documents": [self.doc2.id],+ "method": "set_correspondent",+ "parameters": {"correspondent": 345657},+ }+ ),+ content_type="application/json",+ )self.assertEqual(response.status_code, 400)doc2 = Document.objects.get(id=self.doc2.id)self.assertEqual(doc2.correspondent, self.c1)def test_api_no_correspondent(self):- response = self.client.post("/api/documents/bulk_edit/", json.dumps({- "documents": [self.doc2.id],- "method": "set_correspondent",- "parameters": {}- }), content_type='application/json')+ response = self.client.post(+ "/api/documents/bulk_edit/",+ json.dumps(+ {+ "documents": [self.doc2.id],+ "method": "set_correspondent",+ "parameters": {},+ }+ ),+ content_type="application/json",+ )self.assertEqual(response.status_code, 400)def test_api_invalid_document_type(self):self.assertEqual(self.doc2.document_type, self.dt1)- response = self.client.post("/api/documents/bulk_edit/", json.dumps({- "documents": [self.doc2.id],- "method": "set_document_type",- "parameters": {'document_type': 345657}- }), content_type='application/json')+ response = self.client.post(+ "/api/documents/bulk_edit/",+ json.dumps(+ {+ "documents": [self.doc2.id],+ "method": "set_document_type",+ "parameters": {"document_type": 345657},+ }+ ),+ content_type="application/json",+ )self.assertEqual(response.status_code, 400)doc2 = Document.objects.get(id=self.doc2.id)self.assertEqual(doc2.document_type, self.dt1)def test_api_no_document_type(self):- response = self.client.post("/api/documents/bulk_edit/", json.dumps({- "documents": [self.doc2.id],- "method": "set_document_type",- "parameters": {}- }), content_type='application/json')+ response = self.client.post(+ "/api/documents/bulk_edit/",+ json.dumps(+ {+ "documents": [self.doc2.id],+ "method": "set_document_type",+ "parameters": {},+ }+ ),+ content_type="application/json",+ )self.assertEqual(response.status_code, 400)def test_api_add_invalid_tag(self):self.assertEqual(list(self.doc2.tags.all()), [self.t1])- response = self.client.post("/api/documents/bulk_edit/", json.dumps({- "documents": [self.doc2.id],- "method": "add_tag",- "parameters": {'tag': 345657}- }), content_type='application/json')+ response = self.client.post(+ "/api/documents/bulk_edit/",+ json.dumps(+ {+ "documents": [self.doc2.id],+ "method": "add_tag",+ "parameters": {"tag": 345657},+ }+ ),+ content_type="application/json",+ )self.assertEqual(response.status_code, 400)self.assertEqual(list(self.doc2.tags.all()), [self.t1])def test_api_add_tag_no_tag(self):- response = self.client.post("/api/documents/bulk_edit/", json.dumps({- "documents": [self.doc2.id],- "method": "add_tag",- "parameters": {}- }), content_type='application/json')+ response = self.client.post(+ "/api/documents/bulk_edit/",+ json.dumps(+ {"documents": [self.doc2.id], "method": "add_tag", "parameters": {}}+ ),+ content_type="application/json",+ )self.assertEqual(response.status_code, 400)def test_api_delete_invalid_tag(self):self.assertEqual(list(self.doc2.tags.all()), [self.t1])- response = self.client.post("/api/documents/bulk_edit/", json.dumps({- "documents": [self.doc2.id],- "method": "remove_tag",- "parameters": {'tag': 345657}- }), content_type='application/json')+ response = self.client.post(+ "/api/documents/bulk_edit/",+ json.dumps(+ {+ "documents": [self.doc2.id],+ "method": "remove_tag",+ "parameters": {"tag": 345657},+ }+ ),+ content_type="application/json",+ )self.assertEqual(response.status_code, 400)self.assertEqual(list(self.doc2.tags.all()), [self.t1])def test_api_delete_tag_no_tag(self):- response = self.client.post("/api/documents/bulk_edit/", json.dumps({- "documents": [self.doc2.id],- "method": "remove_tag",- "parameters": {}- }), content_type='application/json')+ response = self.client.post(+ "/api/documents/bulk_edit/",+ json.dumps(+ {"documents": [self.doc2.id], "method": "remove_tag", "parameters": {}}+ ),+ content_type="application/json",+ )self.assertEqual(response.status_code, 400)def test_api_modify_invalid_tags(self):self.assertEqual(list(self.doc2.tags.all()), [self.t1])- response = self.client.post("/api/documents/bulk_edit/", json.dumps({- "documents": [self.doc2.id],- "method": "modify_tags",- "parameters": {'add_tags': [self.t2.id, 1657], "remove_tags": [1123123]}- }), content_type='application/json')+ response = self.client.post(+ "/api/documents/bulk_edit/",+ json.dumps(+ {+ "documents": [self.doc2.id],+ "method": "modify_tags",+ "parameters": {+ "add_tags": [self.t2.id, 1657],+ "remove_tags": [1123123],+ },+ }+ ),+ content_type="application/json",+ )self.assertEqual(response.status_code, 400)def test_api_modify_tags_no_tags(self):- response = self.client.post("/api/documents/bulk_edit/", json.dumps({- "documents": [self.doc2.id],- "method": "modify_tags",- "parameters": {"remove_tags": [1123123]}- }), content_type='application/json')+ response = self.client.post(+ "/api/documents/bulk_edit/",+ json.dumps(+ {+ "documents": [self.doc2.id],+ "method": "modify_tags",+ "parameters": {"remove_tags": [1123123]},+ }+ ),+ content_type="application/json",+ )self.assertEqual(response.status_code, 400)- response = self.client.post("/api/documents/bulk_edit/", json.dumps({- "documents": [self.doc2.id],- "method": "modify_tags",- "parameters": {'add_tags': [self.t2.id, 1657]}- }), content_type='application/json')+ response = self.client.post(+ "/api/documents/bulk_edit/",+ json.dumps(+ {+ "documents": [self.doc2.id],+ "method": "modify_tags",+ "parameters": {"add_tags": [self.t2.id, 1657]},+ }+ ),+ content_type="application/json",+ )self.assertEqual(response.status_code, 400)def test_api_selection_data_empty(self):- response = self.client.post("/api/documents/selection_data/", json.dumps({- "documents": []- }), content_type='application/json')- self.assertEqual(response.status_code, 200)- for field, Entity in [('selected_correspondents', Correspondent), ('selected_tags', Tag), ('selected_document_types', DocumentType)]:+ response = self.client.post(+ "/api/documents/selection_data/",+ json.dumps({"documents": []}),+ content_type="application/json",+ )+ self.assertEqual(response.status_code, 200)+ for field, Entity in [+ ("selected_correspondents", Correspondent),+ ("selected_tags", Tag),+ ("selected_document_types", DocumentType),+ ]:self.assertEqual(len(response.data[field]), Entity.objects.count())for correspondent in response.data[field]:- self.assertEqual(correspondent['document_count'], 0)+ self.assertEqual(correspondent["document_count"], 0)self.assertCountEqual(- map(lambda c: c['id'], response.data[field]),- map(lambda c: c['id'], Entity.objects.values('id')))+ map(lambda c: c["id"], response.data[field]),+ map(lambda c: c["id"], Entity.objects.values("id")),+ )def test_api_selection_data(self):- response = self.client.post("/api/documents/selection_data/", json.dumps({- "documents": [self.doc1.id, self.doc2.id, self.doc4.id, self.doc5.id]- }), content_type='application/json')- self.assertEqual(response.status_code, 200)-- self.assertCountEqual(response.data['selected_correspondents'], [{"id": self.c1.id, "document_count": 1}, {"id": self.c2.id, "document_count": 0}])- self.assertCountEqual(response.data['selected_tags'], [{"id": self.t1.id, "document_count": 2}, {"id": self.t2.id, "document_count": 1}])- self.assertCountEqual(response.data['selected_document_types'], [{"id": self.c1.id, "document_count": 1}, {"id": self.c2.id, "document_count": 0}])+ response = self.client.post(+ "/api/documents/selection_data/",+ json.dumps(+ {"documents": [self.doc1.id, self.doc2.id, self.doc4.id, self.doc5.id]}+ ),+ content_type="application/json",+ )+ self.assertEqual(response.status_code, 200)++ self.assertCountEqual(+ response.data["selected_correspondents"],+ [+ {"id": self.c1.id, "document_count": 1},+ {"id": self.c2.id, "document_count": 0},+ ],+ )+ self.assertCountEqual(+ response.data["selected_tags"],+ [+ {"id": self.t1.id, "document_count": 2},+ {"id": self.t2.id, "document_count": 1},+ ],+ )+ self.assertCountEqual(+ response.data["selected_document_types"],+ [+ {"id": self.c1.id, "document_count": 1},+ {"id": self.c2.id, "document_count": 0},+ ],+ )class TestBulkDownload(DirectoriesMixin, APITestCase):-def setUp(self):super(TestBulkDownload, self).setUp()@@ -1299,23 +1806,58 @@ class TestBulkDownload(DirectoriesMixin, APITestCase):self.client.force_login(user=user)self.doc1 = Document.objects.create(title="unrelated", checksum="A")- self.doc2 = Document.objects.create(title="document A", filename="docA.pdf", mime_type="application/pdf", checksum="B", created=datetime.datetime(2021, 1, 1))- self.doc2b = Document.objects.create(title="document A", filename="docA2.pdf", mime_type="application/pdf", checksum="D", created=datetime.datetime(2021, 1, 1))- self.doc3 = Document.objects.create(title="document B", filename="docB.jpg", mime_type="image/jpeg", checksum="C", created=datetime.datetime(2020, 3, 21), archive_filename="docB.pdf", archive_checksum="D")-- shutil.copy(os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), self.doc2.source_path)- shutil.copy(os.path.join(os.path.dirname(__file__), "samples", "simple.png"), self.doc2b.source_path)- shutil.copy(os.path.join(os.path.dirname(__file__), "samples", "simple.jpg"), self.doc3.source_path)- shutil.copy(os.path.join(os.path.dirname(__file__), "samples", "test_with_bom.pdf"), self.doc3.archive_path)+ self.doc2 = Document.objects.create(+ title="document A",+ filename="docA.pdf",+ mime_type="application/pdf",+ checksum="B",+ created=datetime.datetime(2021, 1, 1),+ )+ self.doc2b = Document.objects.create(+ title="document A",+ filename="docA2.pdf",+ mime_type="application/pdf",+ checksum="D",+ created=datetime.datetime(2021, 1, 1),+ )+ self.doc3 = Document.objects.create(+ title="document B",+ filename="docB.jpg",+ mime_type="image/jpeg",+ checksum="C",+ created=datetime.datetime(2020, 3, 21),+ archive_filename="docB.pdf",+ archive_checksum="D",+ )++ shutil.copy(+ os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),+ self.doc2.source_path,+ )+ shutil.copy(+ os.path.join(os.path.dirname(__file__), "samples", "simple.png"),+ self.doc2b.source_path,+ )+ shutil.copy(+ os.path.join(os.path.dirname(__file__), "samples", "simple.jpg"),+ self.doc3.source_path,+ )+ shutil.copy(+ os.path.join(os.path.dirname(__file__), "samples", "test_with_bom.pdf"),+ self.doc3.archive_path,+ )def test_download_originals(self):- response = self.client.post("/api/documents/bulk_download/", json.dumps({- "documents": [self.doc2.id, self.doc3.id],- "content": "originals"- }), content_type='application/json')+ response = self.client.post(+ "/api/documents/bulk_download/",+ json.dumps(+ {"documents": [self.doc2.id, self.doc3.id], "content": "originals"}+ ),+ content_type="application/json",+ )self.assertEqual(response.status_code, 200)- self.assertEqual(response['Content-Type'], 'application/zip')+ self.assertEqual(response["Content-Type"], "application/zip")with zipfile.ZipFile(io.BytesIO(response.content)) as zipf:self.assertEqual(len(zipf.filelist), 2)@@ -1329,12 +1871,14 @@ class TestBulkDownload(DirectoriesMixin, APITestCase):self.assertEqual(f.read(), zipf.read("2020-03-21 document B.jpg"))def test_download_default(self):- response = self.client.post("/api/documents/bulk_download/", json.dumps({- "documents": [self.doc2.id, self.doc3.id]- }), content_type='application/json')+ response = self.client.post(+ "/api/documents/bulk_download/",+ json.dumps({"documents": [self.doc2.id, self.doc3.id]}),+ content_type="application/json",+ )self.assertEqual(response.status_code, 200)- self.assertEqual(response['Content-Type'], 'application/zip')+ self.assertEqual(response["Content-Type"], "application/zip")with zipfile.ZipFile(io.BytesIO(response.content)) as zipf:self.assertEqual(len(zipf.filelist), 2)@@ -1348,13 +1892,14 @@ class TestBulkDownload(DirectoriesMixin, APITestCase):self.assertEqual(f.read(), zipf.read("2020-03-21 document B.pdf"))def test_download_both(self):- response = self.client.post("/api/documents/bulk_download/", json.dumps({- "documents": [self.doc2.id, self.doc3.id],- "content": "both"- }), content_type='application/json')+ response = self.client.post(+ "/api/documents/bulk_download/",+ json.dumps({"documents": [self.doc2.id, self.doc3.id], "content": "both"}),+ content_type="application/json",+ )self.assertEqual(response.status_code, 200)- self.assertEqual(response['Content-Type'], 'application/zip')+ self.assertEqual(response["Content-Type"], "application/zip")with zipfile.ZipFile(io.BytesIO(response.content)) as zipf:self.assertEqual(len(zipf.filelist), 3)@@ -1363,21 +1908,29 @@ class TestBulkDownload(DirectoriesMixin, APITestCase):self.assertIn("originals/2020-03-21 document B.jpg", zipf.namelist())with self.doc2.source_file as f:- self.assertEqual(f.read(), zipf.read("originals/2021-01-01 document A.pdf"))+ self.assertEqual(+ f.read(), zipf.read("originals/2021-01-01 document A.pdf")+ )with self.doc3.archive_file as f:- self.assertEqual(f.read(), zipf.read("archive/2020-03-21 document B.pdf"))+ self.assertEqual(+ f.read(), zipf.read("archive/2020-03-21 document B.pdf")+ )with self.doc3.source_file as f:- self.assertEqual(f.read(), zipf.read("originals/2020-03-21 document B.jpg"))+ self.assertEqual(+ f.read(), zipf.read("originals/2020-03-21 document B.jpg")+ )def test_filename_clashes(self):- response = self.client.post("/api/documents/bulk_download/", json.dumps({- "documents": [self.doc2.id, self.doc2b.id]- }), content_type='application/json')+ response = self.client.post(+ "/api/documents/bulk_download/",+ json.dumps({"documents": [self.doc2.id, self.doc2b.id]}),+ content_type="application/json",+ )self.assertEqual(response.status_code, 200)- self.assertEqual(response['Content-Type'], 'application/zip')+ self.assertEqual(response["Content-Type"], "application/zip")with zipfile.ZipFile(io.BytesIO(response.content)) as zipf:self.assertEqual(len(zipf.filelist), 2)@@ -1392,13 +1945,16 @@ class TestBulkDownload(DirectoriesMixin, APITestCase):self.assertEqual(f.read(), zipf.read("2021-01-01 document A_01.pdf"))def test_compression(self):- response = self.client.post("/api/documents/bulk_download/", json.dumps({- "documents": [self.doc2.id, self.doc2b.id],- "compression": "lzma"- }), content_type='application/json')+ response = self.client.post(+ "/api/documents/bulk_download/",+ json.dumps(+ {"documents": [self.doc2.id, self.doc2b.id], "compression": "lzma"}+ ),+ content_type="application/json",+ )-class TestApiAuth(APITestCase):+class TestApiAuth(APITestCase):def test_auth_required(self):d = Document.objects.create(title="Test")@@ -1406,9 +1962,15 @@ class TestApiAuth(APITestCase):self.assertEqual(self.client.get("/api/documents/").status_code, 401)self.assertEqual(self.client.get(f"/api/documents/{d.id}/").status_code, 401)- self.assertEqual(self.client.get(f"/api/documents/{d.id}/download/").status_code, 401)- self.assertEqual(self.client.get(f"/api/documents/{d.id}/preview/").status_code, 401)- self.assertEqual(self.client.get(f"/api/documents/{d.id}/thumb/").status_code, 401)+ self.assertEqual(+ self.client.get(f"/api/documents/{d.id}/download/").status_code, 401+ )+ self.assertEqual(+ self.client.get(f"/api/documents/{d.id}/preview/").status_code, 401+ )+ self.assertEqual(+ self.client.get(f"/api/documents/{d.id}/thumb/").status_code, 401+ )self.assertEqual(self.client.get("/api/tags/").status_code, 401)self.assertEqual(self.client.get("/api/correspondents/").status_code, 401)@@ -1419,8 +1981,12 @@ class TestApiAuth(APITestCase):self.assertEqual(self.client.get("/api/search/autocomplete/").status_code, 401)self.assertEqual(self.client.get("/api/documents/bulk_edit/").status_code, 401)- self.assertEqual(self.client.get("/api/documents/bulk_download/").status_code, 401)- self.assertEqual(self.client.get("/api/documents/selection_data/").status_code, 401)+ self.assertEqual(+ self.client.get("/api/documents/bulk_download/").status_code, 401+ )+ self.assertEqual(+ self.client.get("/api/documents/selection_data/").status_code, 401+ )def test_api_version_no_auth(self):diff --git a/src/documents/tests/test_checks.py b/src/documents/tests/test_checks.pyindex ee4fbe8d1..7a1a81ec1 100644--- a/src/documents/tests/test_checks.py+++ b/src/documents/tests/test_checks.py@@ -11,7 +11,6 @@ from ..models import Documentclass ChecksTestCase(TestCase):-def test_changed_password_check_empty_db(self):self.assertEqual(changed_password_check(None), [])@@ -23,8 +22,15 @@ class ChecksTestCase(TestCase):self.assertEqual(parser_check(None), [])- with mock.patch('documents.checks.document_consumer_declaration.send') as m:+ with mock.patch("documents.checks.document_consumer_declaration.send") as m:m.return_value = []- self.assertEqual(parser_check(None), [Error("No parsers found. This is a bug. The consumer won't be "- "able to consume any documents without parsers.")])+ self.assertEqual(+ parser_check(None),+ [+ Error(+ "No parsers found. This is a bug. The consumer won't be "+ "able to consume any documents without parsers."+ )+ ],+ )diff --git a/src/documents/tests/test_classifier.py b/src/documents/tests/test_classifier.pyindex fcc08f842..dad8231a7 100644--- a/src/documents/tests/test_classifier.py+++ b/src/documents/tests/test_classifier.py@@ -7,30 +7,60 @@ import pytestfrom django.conf import settingsfrom django.test import TestCase, override_settings-from documents.classifier import DocumentClassifier, IncompatibleClassifierVersionError, load_classifier+from documents.classifier import (+ DocumentClassifier,+ IncompatibleClassifierVersionError,+ load_classifier,+)from documents.models import Correspondent, Document, Tag, DocumentTypefrom documents.tests.utils import DirectoriesMixinclass TestClassifier(DirectoriesMixin, TestCase):-def setUp(self):super(TestClassifier, self).setUp()self.classifier = DocumentClassifier()def generate_test_data(self):- self.c1 = Correspondent.objects.create(name="c1", matching_algorithm=Correspondent.MATCH_AUTO)+ self.c1 = Correspondent.objects.create(+ name="c1", matching_algorithm=Correspondent.MATCH_AUTO+ )self.c2 = Correspondent.objects.create(name="c2")- self.c3 = Correspondent.objects.create(name="c3", matching_algorithm=Correspondent.MATCH_AUTO)- self.t1 = Tag.objects.create(name="t1", matching_algorithm=Tag.MATCH_AUTO, pk=12)- self.t2 = Tag.objects.create(name="t2", matching_algorithm=Tag.MATCH_ANY, pk=34, is_inbox_tag=True)- self.t3 = Tag.objects.create(name="t3", matching_algorithm=Tag.MATCH_AUTO, pk=45)- self.dt = DocumentType.objects.create(name="dt", matching_algorithm=DocumentType.MATCH_AUTO)- self.dt2 = DocumentType.objects.create(name="dt2", matching_algorithm=DocumentType.MATCH_AUTO)-- self.doc1 = Document.objects.create(title="doc1", content="this is a document from c1", correspondent=self.c1, checksum="A", document_type=self.dt)- self.doc2 = Document.objects.create(title="doc1", content="this is another document, but from c2", correspondent=self.c2, checksum="B")- self.doc_inbox = Document.objects.create(title="doc235", content="aa", checksum="C")+ self.c3 = Correspondent.objects.create(+ name="c3", matching_algorithm=Correspondent.MATCH_AUTO+ )+ self.t1 = Tag.objects.create(+ name="t1", matching_algorithm=Tag.MATCH_AUTO, pk=12+ )+ self.t2 = Tag.objects.create(+ name="t2", matching_algorithm=Tag.MATCH_ANY, pk=34, is_inbox_tag=True+ )+ self.t3 = Tag.objects.create(+ name="t3", matching_algorithm=Tag.MATCH_AUTO, pk=45+ )+ self.dt = DocumentType.objects.create(+ name="dt", matching_algorithm=DocumentType.MATCH_AUTO+ )+ self.dt2 = DocumentType.objects.create(+ name="dt2", matching_algorithm=DocumentType.MATCH_AUTO+ )++ self.doc1 = Document.objects.create(+ title="doc1",+ content="this is a document from c1",+ correspondent=self.c1,+ checksum="A",+ document_type=self.dt,+ )+ self.doc2 = Document.objects.create(+ title="doc1",+ content="this is another document, but from c2",+ correspondent=self.c2,+ checksum="B",+ )+ self.doc_inbox = Document.objects.create(+ title="doc235", content="aa", checksum="C"+ )self.doc1.tags.add(self.t1)self.doc2.tags.add(self.t1)@@ -59,17 +89,29 @@ class TestClassifier(DirectoriesMixin, TestCase):def testTrain(self):self.generate_test_data()self.classifier.train()- self.assertListEqual(list(self.classifier.correspondent_classifier.classes_), [-1, self.c1.pk])- self.assertListEqual(list(self.classifier.tags_binarizer.classes_), [self.t1.pk, self.t3.pk])+ self.assertListEqual(+ list(self.classifier.correspondent_classifier.classes_), [-1, self.c1.pk]+ )+ self.assertListEqual(+ list(self.classifier.tags_binarizer.classes_), [self.t1.pk, self.t3.pk]+ )def testPredict(self):self.generate_test_data()self.classifier.train()- self.assertEqual(self.classifier.predict_correspondent(self.doc1.content), self.c1.pk)+ self.assertEqual(+ self.classifier.predict_correspondent(self.doc1.content), self.c1.pk+ )self.assertEqual(self.classifier.predict_correspondent(self.doc2.content), None)- self.assertListEqual(self.classifier.predict_tags(self.doc1.content), [self.t1.pk])- self.assertListEqual(self.classifier.predict_tags(self.doc2.content), [self.t1.pk, self.t3.pk])- self.assertEqual(self.classifier.predict_document_type(self.doc1.content), self.dt.pk)+ self.assertListEqual(+ self.classifier.predict_tags(self.doc1.content), [self.t1.pk]+ )+ self.assertListEqual(+ self.classifier.predict_tags(self.doc2.content), [self.t1.pk, self.t3.pk]+ )+ self.assertEqual(+ self.classifier.predict_document_type(self.doc1.content), self.dt.pk+ )self.assertEqual(self.classifier.predict_document_type(self.doc2.content), None)def testDatasetHashing(self):@@ -90,7 +132,9 @@ class TestClassifier(DirectoriesMixin, TestCase):classifier2 = DocumentClassifier()current_ver = DocumentClassifier.FORMAT_VERSION- with mock.patch("documents.classifier.DocumentClassifier.FORMAT_VERSION", current_ver+1):+ with mock.patch(+ "documents.classifier.DocumentClassifier.FORMAT_VERSION", current_ver + 1+ ):# assure that we won't load old classifiers.self.assertRaises(IncompatibleClassifierVersionError, classifier2.load)@@ -112,7 +156,9 @@ class TestClassifier(DirectoriesMixin, TestCase):new_classifier.load()self.assertFalse(new_classifier.train())- @override_settings(MODEL_FILE=os.path.join(os.path.dirname(__file__), "data", "model.pickle"))+ @override_settings(+ MODEL_FILE=os.path.join(os.path.dirname(__file__), "data", "model.pickle")+ )def test_load_and_classify(self):self.generate_test_data()@@ -122,38 +168,67 @@ class TestClassifier(DirectoriesMixin, TestCase):self.assertCountEqual(new_classifier.predict_tags(self.doc2.content), [45, 12])def test_one_correspondent_predict(self):- c1 = Correspondent.objects.create(name="c1", matching_algorithm=Correspondent.MATCH_AUTO)- doc1 = Document.objects.create(title="doc1", content="this is a document from c1", correspondent=c1, checksum="A")+ c1 = Correspondent.objects.create(+ name="c1", matching_algorithm=Correspondent.MATCH_AUTO+ )+ doc1 = Document.objects.create(+ title="doc1",+ content="this is a document from c1",+ correspondent=c1,+ checksum="A",+ )self.classifier.train()self.assertEqual(self.classifier.predict_correspondent(doc1.content), c1.pk)def test_one_correspondent_predict_manydocs(self):- c1 = Correspondent.objects.create(name="c1", matching_algorithm=Correspondent.MATCH_AUTO)- doc1 = Document.objects.create(title="doc1", content="this is a document from c1", correspondent=c1, checksum="A")- doc2 = Document.objects.create(title="doc2", content="this is a document from noone", checksum="B")+ c1 = Correspondent.objects.create(+ name="c1", matching_algorithm=Correspondent.MATCH_AUTO+ )+ doc1 = Document.objects.create(+ title="doc1",+ content="this is a document from c1",+ correspondent=c1,+ checksum="A",+ )+ doc2 = Document.objects.create(+ title="doc2", content="this is a document from noone", checksum="B"+ )self.classifier.train()self.assertEqual(self.classifier.predict_correspondent(doc1.content), c1.pk)self.assertIsNone(self.classifier.predict_correspondent(doc2.content))def test_one_type_predict(self):- dt = DocumentType.objects.create(name="dt", matching_algorithm=DocumentType.MATCH_AUTO)+ dt = DocumentType.objects.create(+ name="dt", matching_algorithm=DocumentType.MATCH_AUTO+ )- doc1 = Document.objects.create(title="doc1", content="this is a document from c1",- checksum="A", document_type=dt)+ doc1 = Document.objects.create(+ title="doc1",+ content="this is a document from c1",+ checksum="A",+ document_type=dt,+ )self.classifier.train()self.assertEqual(self.classifier.predict_document_type(doc1.content), dt.pk)def test_one_type_predict_manydocs(self):- dt = DocumentType.objects.create(name="dt", matching_algorithm=DocumentType.MATCH_AUTO)+ dt = DocumentType.objects.create(+ name="dt", matching_algorithm=DocumentType.MATCH_AUTO+ )- doc1 = Document.objects.create(title="doc1", content="this is a document from c1",- checksum="A", document_type=dt)+ doc1 = Document.objects.create(+ title="doc1",+ content="this is a document from c1",+ checksum="A",+ document_type=dt,+ )- doc2 = Document.objects.create(title="doc1", content="this is a document from c2",- checksum="B")+ doc2 = Document.objects.create(+ title="doc1", content="this is a document from c2", checksum="B"+ )self.classifier.train()self.assertEqual(self.classifier.predict_document_type(doc1.content), dt.pk)@@ -162,7 +237,9 @@ class TestClassifier(DirectoriesMixin, TestCase):def test_one_tag_predict(self):t1 = Tag.objects.create(name="t1", matching_algorithm=Tag.MATCH_AUTO, pk=12)- doc1 = Document.objects.create(title="doc1", content="this is a document from c1", checksum="A")+ doc1 = Document.objects.create(+ title="doc1", content="this is a document from c1", checksum="A"+ )doc1.tags.add(t1)self.classifier.train()@@ -171,7 +248,9 @@ class TestClassifier(DirectoriesMixin, TestCase):def test_one_tag_predict_unassigned(self):t1 = Tag.objects.create(name="t1", matching_algorithm=Tag.MATCH_AUTO, pk=12)- doc1 = Document.objects.create(title="doc1", content="this is a document from c1", checksum="A")+ doc1 = Document.objects.create(+ title="doc1", content="this is a document from c1", checksum="A"+ )self.classifier.train()self.assertListEqual(self.classifier.predict_tags(doc1.content), [])@@ -180,7 +259,9 @@ class TestClassifier(DirectoriesMixin, TestCase):t1 = Tag.objects.create(name="t1", matching_algorithm=Tag.MATCH_AUTO, pk=12)t2 = Tag.objects.create(name="t2", matching_algorithm=Tag.MATCH_AUTO, pk=121)- doc4 = Document.objects.create(title="doc1", content="this is a document from c4", checksum="D")+ doc4 = Document.objects.create(+ title="doc1", content="this is a document from c4", checksum="D"+ )doc4.tags.add(t1)doc4.tags.add(t2)@@ -191,10 +272,18 @@ class TestClassifier(DirectoriesMixin, TestCase):t1 = Tag.objects.create(name="t1", matching_algorithm=Tag.MATCH_AUTO, pk=12)t2 = Tag.objects.create(name="t2", matching_algorithm=Tag.MATCH_AUTO, pk=121)- doc1 = Document.objects.create(title="doc1", content="this is a document from c1", checksum="A")- doc2 = Document.objects.create(title="doc1", content="this is a document from c2", checksum="B")- doc3 = Document.objects.create(title="doc1", content="this is a document from c3", checksum="C")- doc4 = Document.objects.create(title="doc1", content="this is a document from c4", checksum="D")+ doc1 = Document.objects.create(+ title="doc1", content="this is a document from c1", checksum="A"+ )+ doc2 = Document.objects.create(+ title="doc1", content="this is a document from c2", checksum="B"+ )+ doc3 = Document.objects.create(+ title="doc1", content="this is a document from c3", checksum="C"+ )+ doc4 = Document.objects.create(+ title="doc1", content="this is a document from c4", checksum="D"+ )doc1.tags.add(t1)doc2.tags.add(t2)@@ -210,8 +299,12 @@ class TestClassifier(DirectoriesMixin, TestCase):def test_one_tag_predict_multi(self):t1 = Tag.objects.create(name="t1", matching_algorithm=Tag.MATCH_AUTO, pk=12)- doc1 = Document.objects.create(title="doc1", content="this is a document from c1", checksum="A")- doc2 = Document.objects.create(title="doc2", content="this is a document from c2", checksum="B")+ doc1 = Document.objects.create(+ title="doc1", content="this is a document from c1", checksum="A"+ )+ doc2 = Document.objects.create(+ title="doc2", content="this is a document from c2", checksum="B"+ )doc1.tags.add(t1)doc2.tags.add(t1)@@ -222,8 +315,12 @@ class TestClassifier(DirectoriesMixin, TestCase):def test_one_tag_predict_multi_2(self):t1 = Tag.objects.create(name="t1", matching_algorithm=Tag.MATCH_AUTO, pk=12)- doc1 = Document.objects.create(title="doc1", content="this is a document from c1", checksum="A")- doc2 = Document.objects.create(title="doc2", content="this is a document from c2", checksum="B")+ doc1 = Document.objects.create(+ title="doc1", content="this is a document from c1", checksum="A"+ )+ doc2 = Document.objects.create(+ title="doc2", content="this is a document from c2", checksum="B"+ )doc1.tags.add(t1)self.classifier.train()@@ -240,9 +337,15 @@ class TestClassifier(DirectoriesMixin, TestCase):self.assertIsNotNone(load_classifier())load.assert_called_once()- @override_settings(CACHES={'default': {'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'}})- @override_settings(MODEL_FILE=os.path.join(os.path.dirname(__file__), "data", "model.pickle"))- @pytest.mark.skip(reason="Disabled caching due to high memory usage - need to investigate.")+ @override_settings(+ CACHES={"default": {"BACKEND": "django.core.cache.backends.locmem.LocMemCache"}}+ )+ @override_settings(+ MODEL_FILE=os.path.join(os.path.dirname(__file__), "data", "model.pickle")+ )+ @pytest.mark.skip(+ reason="Disabled caching due to high memory usage - need to investigate."+ )def test_load_classifier_cached(self):classifier = load_classifier()self.assertIsNotNone(classifier)diff --git a/src/documents/tests/test_consumer.py b/src/documents/tests/test_consumer.pyindex 514c3ca19..6c79c7713 100644--- a/src/documents/tests/test_consumer.py+++ b/src/documents/tests/test_consumer.py@@ -31,21 +31,14 @@ class TestAttributes(TestCase):self.assertEqual(tuple([t.name for t in file_info.tags]), tags, filename)-def test_guess_attributes_from_name_when_title_starts_with_dash(self):self._test_guess_attributes_from_name(- '- weird but should not break.pdf',- None,- '- weird but should not break',- ()+ "- weird but should not break.pdf", None, "- weird but should not break", ())def test_guess_attributes_from_name_when_title_ends_with_dash(self):self._test_guess_attributes_from_name(- 'weird but should not break -.pdf',- None,- 'weird but should not break -',- ()+ "weird but should not break -.pdf", None, "weird but should not break -", ())@@ -55,19 +48,13 @@ class TestFieldPermutations(TestCase):"20150102030405Z","20150102Z",)- valid_correspondents = [- "timmy",- "Dr. McWheelie",- "Dash Gor-don",- "ο Θερμαστής",- ""- ]+ valid_correspondents = ["timmy", "Dr. McWheelie", "Dash Gor-don", "ο Θερμαστής", ""]valid_titles = ["title", "Title w Spaces", "Title a-dash", "Τίτλος", ""]valid_tags = ["tag", "tig,tag", "tag1,tag2,tag-3"]- def _test_guessed_attributes(self, filename, created=None,- correspondent=None, title=None,- tags=None):+ def _test_guessed_attributes(+ self, filename, created=None, correspondent=None, title=None, tags=None+ ):info = FileInfo.from_filename(filename)@@ -92,13 +79,10 @@ class TestFieldPermutations(TestCase):if tags is None:self.assertEqual(info.tags, (), filename)else:- self.assertEqual(- [t.name for t in info.tags], tags.split(','),- filename- )+ self.assertEqual([t.name for t in info.tags], tags.split(","), filename)def test_just_title(self):- template = '{title}.pdf'+ template = "{title}.pdf"for title in self.valid_titles:spec = dict(title=title)filename = template.format(**spec)@@ -109,12 +93,8 @@ class TestFieldPermutations(TestCase):for created in self.valid_dates:for title in self.valid_titles:- spec = {- "created": created,- "title": title- }- self._test_guessed_attributes(- template.format(**spec), **spec)+ spec = {"created": created, "title": title}+ self._test_guessed_attributes(template.format(**spec), **spec)def test_invalid_date_format(self):info = FileInfo.from_filename("06112017Z - title.pdf")@@ -127,7 +107,7 @@ class TestFieldPermutations(TestCase):all_patt = re.compile("^.*$")none_patt = re.compile("$a")exact_patt = re.compile("^([a-z0-9,]+)_(\\d{8})_(\\d{6})_([0-9]+)\\.")- repl1 = " - \\4 - \\1." # (empty) corrspondent, title and tags+ repl1 = " - \\4 - \\1." # (empty) corrspondent, title and tagsrepl2 = "\\2Z - " + repl1 # creation date + repl1# No transformations configured (= default)@@ -137,36 +117,37 @@ class TestFieldPermutations(TestCase):self.assertIsNone(info.created)# Pattern doesn't match (filename unaltered)- with self.settings(- FILENAME_PARSE_TRANSFORMS=[(none_patt, "none.gif")]):+ with self.settings(FILENAME_PARSE_TRANSFORMS=[(none_patt, "none.gif")]):info = FileInfo.from_filename(filename)self.assertEqual(info.title, "tag1,tag2_20190908_180610_0001")# Simple transformation (match all)- with self.settings(- FILENAME_PARSE_TRANSFORMS=[(all_patt, "all.gif")]):+ with self.settings(FILENAME_PARSE_TRANSFORMS=[(all_patt, "all.gif")]):info = FileInfo.from_filename(filename)self.assertEqual(info.title, "all")# Multiple transformations configured (first pattern matches)with self.settings(- FILENAME_PARSE_TRANSFORMS=[- (all_patt, "all.gif"),- (all_patt, "anotherall.gif")]):+ FILENAME_PARSE_TRANSFORMS=[+ (all_patt, "all.gif"),+ (all_patt, "anotherall.gif"),+ ]+ ):info = FileInfo.from_filename(filename)self.assertEqual(info.title, "all")# Multiple transformations configured (second pattern matches)with self.settings(- FILENAME_PARSE_TRANSFORMS=[- (none_patt, "none.gif"),- (all_patt, "anotherall.gif")]):+ FILENAME_PARSE_TRANSFORMS=[+ (none_patt, "none.gif"),+ (all_patt, "anotherall.gif"),+ ]+ ):info = FileInfo.from_filename(filename)self.assertEqual(info.title, "anotherall")class DummyParser(DocumentParser):-def get_thumbnail(self, document_path, mime_type, file_name=None):# not important during testsraise NotImplementedError()@@ -184,7 +165,6 @@ class DummyParser(DocumentParser):class CopyParser(DocumentParser):-def get_thumbnail(self, document_path, mime_type, file_name=None):return self.fake_thumb@@ -202,7 +182,6 @@ class CopyParser(DocumentParser):class FaultyParser(DocumentParser):-def get_thumbnail(self, document_path, mime_type, file_name=None):# not important during testsraise NotImplementedError()@@ -233,8 +212,15 @@ def fake_magic_from_file(file, mime=False):@mock.patch("documents.consumer.magic.from_file", fake_magic_from_file)class TestConsumer(DirectoriesMixin, TestCase):-- def _assert_first_last_send_progress(self, first_status="STARTING", last_status="SUCCESS", first_progress=0, first_progress_max=100, last_progress=100, last_progress_max=100):+ def _assert_first_last_send_progress(+ self,+ first_status="STARTING",+ last_status="SUCCESS",+ first_progress=0,+ first_progress_max=100,+ last_progress=100,+ last_progress_max=100,+ ):self._send_progress.assert_called()@@ -243,13 +229,17 @@ class TestConsumer(DirectoriesMixin, TestCase):self.assertEqual(args[1], first_progress_max)self.assertEqual(args[2], first_status)- args, kwargs = self._send_progress.call_args_list[len(self._send_progress.call_args_list) - 1]+ args, kwargs = self._send_progress.call_args_list[+ len(self._send_progress.call_args_list) - 1+ ]self.assertEqual(args[0], last_progress)self.assertEqual(args[1], last_progress_max)self.assertEqual(args[2], last_status)def make_dummy_parser(self, logging_group, progress_callback=None):- return DummyParser(logging_group, self.dirs.scratch_dir, self.get_test_archive_file())+ return DummyParser(+ logging_group, self.dirs.scratch_dir, self.get_test_archive_file()+ )def make_faulty_parser(self, logging_group, progress_callback=None):return FaultyParser(logging_group, self.dirs.scratch_dir)@@ -259,11 +249,16 @@ class TestConsumer(DirectoriesMixin, TestCase):patcher = mock.patch("documents.parsers.document_consumer_declaration.send")m = patcher.start()- m.return_value = [(None, {- "parser": self.make_dummy_parser,- "mime_types": {"application/pdf": ".pdf"},- "weight": 0- })]+ m.return_value = [+ (+ None,+ {+ "parser": self.make_dummy_parser,+ "mime_types": {"application/pdf": ".pdf"},+ "weight": 0,+ },+ )+ ]self.addCleanup(patcher.stop)# this prevents websocket message reports during testing.@@ -274,13 +269,21 @@ class TestConsumer(DirectoriesMixin, TestCase):self.consumer = Consumer()def get_test_file(self):- src = os.path.join(os.path.dirname(__file__), "samples", "documents", "originals", "0000001.pdf")+ src = os.path.join(+ os.path.dirname(__file__),+ "samples",+ "documents",+ "originals",+ "0000001.pdf",+ )dst = os.path.join(self.dirs.scratch_dir, "sample.pdf")shutil.copy(src, dst)return dstdef get_test_archive_file(self):- src = os.path.join(os.path.dirname(__file__), "samples", "documents", "archive", "0000001.pdf")+ src = os.path.join(+ os.path.dirname(__file__), "samples", "documents", "archive", "0000001.pdf"+ )dst = os.path.join(self.dirs.scratch_dir, "sample_archive.pdf")shutil.copy(src, dst)return dst@@ -292,23 +295,19 @@ class TestConsumer(DirectoriesMixin, TestCase):document = self.consumer.try_consume_file(filename)self.assertEqual(document.content, "The Text")- self.assertEqual(document.title, os.path.splitext(os.path.basename(filename))[0])+ self.assertEqual(+ document.title, os.path.splitext(os.path.basename(filename))[0]+ )self.assertIsNone(document.correspondent)self.assertIsNone(document.document_type)self.assertEqual(document.filename, "0000001.pdf")self.assertEqual(document.archive_filename, "0000001.pdf")- self.assertTrue(os.path.isfile(- document.source_path- ))+ self.assertTrue(os.path.isfile(document.source_path))- self.assertTrue(os.path.isfile(- document.thumbnail_path- ))+ self.assertTrue(os.path.isfile(document.thumbnail_path))- self.assertTrue(os.path.isfile(- document.archive_path- ))+ self.assertTrue(os.path.isfile(document.archive_path))self.assertEqual(document.checksum, "42995833e01aea9b3edee44bbfdd7ce1")self.assertEqual(document.archive_checksum, "62acb0bcbfbcaa62ca6ad3668e4e404b")@@ -330,40 +329,45 @@ class TestConsumer(DirectoriesMixin, TestCase):document = self.consumer.try_consume_file(filename)- self.assertTrue(os.path.isfile(- document.source_path- ))+ self.assertTrue(os.path.isfile(document.source_path))self.assertFalse(os.path.isfile(shadow_file))self.assertFalse(os.path.isfile(filename))-def testOverrideFilename(self):filename = self.get_test_file()override_filename = "Statement for November.pdf"- document = self.consumer.try_consume_file(filename, override_filename=override_filename)+ document = self.consumer.try_consume_file(+ filename, override_filename=override_filename+ )self.assertEqual(document.title, "Statement for November")self._assert_first_last_send_progress()def testOverrideTitle(self):- document = self.consumer.try_consume_file(self.get_test_file(), override_title="Override Title")+ document = self.consumer.try_consume_file(+ self.get_test_file(), override_title="Override Title"+ )self.assertEqual(document.title, "Override Title")self._assert_first_last_send_progress()def testOverrideCorrespondent(self):c = Correspondent.objects.create(name="test")- document = self.consumer.try_consume_file(self.get_test_file(), override_correspondent_id=c.pk)+ document = self.consumer.try_consume_file(+ self.get_test_file(), override_correspondent_id=c.pk+ )self.assertEqual(document.correspondent.id, c.id)self._assert_first_last_send_progress()def testOverrideDocumentType(self):dt = DocumentType.objects.create(name="test")- document = self.consumer.try_consume_file(self.get_test_file(), override_document_type_id=dt.pk)+ document = self.consumer.try_consume_file(+ self.get_test_file(), override_document_type_id=dt.pk+ )self.assertEqual(document.document_type.id, dt.id)self._assert_first_last_send_progress()@@ -371,7 +375,9 @@ class TestConsumer(DirectoriesMixin, TestCase):t1 = Tag.objects.create(name="t1")t2 = Tag.objects.create(name="t2")t3 = Tag.objects.create(name="t3")- document = self.consumer.try_consume_file(self.get_test_file(), override_tag_ids=[t1.id, t3.id])+ document = self.consumer.try_consume_file(+ self.get_test_file(), override_tag_ids=[t1.id, t3.id]+ )self.assertIn(t1, document.tags.all())self.assertNotIn(t2, document.tags.all())@@ -384,7 +390,7 @@ class TestConsumer(DirectoriesMixin, TestCase):ConsumerError,"File not found",self.consumer.try_consume_file,- "non-existing-file"+ "non-existing-file",)self._assert_first_last_send_progress(last_status="FAILED")@@ -396,7 +402,7 @@ class TestConsumer(DirectoriesMixin, TestCase):ConsumerError,"It is a duplicate",self.consumer.try_consume_file,- self.get_test_file()+ self.get_test_file(),)self._assert_first_last_send_progress(last_status="FAILED")@@ -408,7 +414,7 @@ class TestConsumer(DirectoriesMixin, TestCase):ConsumerError,"It is a duplicate",self.consumer.try_consume_file,- self.get_test_archive_file()+ self.get_test_archive_file(),)self._assert_first_last_send_progress(last_status="FAILED")@@ -425,25 +431,29 @@ class TestConsumer(DirectoriesMixin, TestCase):ConsumerError,"sample.pdf: Unsupported mime type application/pdf",self.consumer.try_consume_file,- self.get_test_file()+ self.get_test_file(),)self._assert_first_last_send_progress(last_status="FAILED")-@mock.patch("documents.parsers.document_consumer_declaration.send")def testFaultyParser(self, m):- m.return_value = [(None, {- "parser": self.make_faulty_parser,- "mime_types": {"application/pdf": ".pdf"},- "weight": 0- })]+ m.return_value = [+ (+ None,+ {+ "parser": self.make_faulty_parser,+ "mime_types": {"application/pdf": ".pdf"},+ "weight": 0,+ },+ )+ ]self.assertRaisesMessage(ConsumerError,"sample.pdf: Error while consuming document sample.pdf: Does not compute.",self.consumer.try_consume_file,- self.get_test_file()+ self.get_test_file(),)self._assert_first_last_send_progress(last_status="FAILED")@@ -457,7 +467,7 @@ class TestConsumer(DirectoriesMixin, TestCase):ConsumerError,"sample.pdf: The following error occured while consuming sample.pdf: NO.",self.consumer.try_consume_file,- filename+ filename,)self._assert_first_last_send_progress(last_status="FAILED")@@ -491,7 +501,7 @@ class TestConsumer(DirectoriesMixin, TestCase):filenames.insert(0, f)return f- m.side_effect = lambda f, archive_filename = False: get_filename()+ m.side_effect = lambda f, archive_filename=False: get_filename()filename = self.get_test_file()@@ -565,17 +575,37 @@ class TestConsumer(DirectoriesMixin, TestCase):@override_settings(PAPERLESS_FILENAME_FORMAT="{title}")@mock.patch("documents.parsers.document_consumer_declaration.send")def test_similar_filenames(self, m):- shutil.copy(os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), os.path.join(settings.CONSUMPTION_DIR, "simple.pdf"))- shutil.copy(os.path.join(os.path.dirname(__file__), "samples", "simple.png"), os.path.join(settings.CONSUMPTION_DIR, "simple.png"))- shutil.copy(os.path.join(os.path.dirname(__file__), "samples", "simple-noalpha.png"), os.path.join(settings.CONSUMPTION_DIR, "simple.png.pdf"))- m.return_value = [(None, {- "parser": CopyParser,- "mime_types": {"application/pdf": ".pdf", "image/png": ".png"},- "weight": 0- })]- doc1 = self.consumer.try_consume_file(os.path.join(settings.CONSUMPTION_DIR, "simple.png"))- doc2 = self.consumer.try_consume_file(os.path.join(settings.CONSUMPTION_DIR, "simple.pdf"))- doc3 = self.consumer.try_consume_file(os.path.join(settings.CONSUMPTION_DIR, "simple.png.pdf"))+ shutil.copy(+ os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),+ os.path.join(settings.CONSUMPTION_DIR, "simple.pdf"),+ )+ shutil.copy(+ os.path.join(os.path.dirname(__file__), "samples", "simple.png"),+ os.path.join(settings.CONSUMPTION_DIR, "simple.png"),+ )+ shutil.copy(+ os.path.join(os.path.dirname(__file__), "samples", "simple-noalpha.png"),+ os.path.join(settings.CONSUMPTION_DIR, "simple.png.pdf"),+ )+ m.return_value = [+ (+ None,+ {+ "parser": CopyParser,+ "mime_types": {"application/pdf": ".pdf", "image/png": ".png"},+ "weight": 0,+ },+ )+ ]+ doc1 = self.consumer.try_consume_file(+ os.path.join(settings.CONSUMPTION_DIR, "simple.png")+ )+ doc2 = self.consumer.try_consume_file(+ os.path.join(settings.CONSUMPTION_DIR, "simple.pdf")+ )+ doc3 = self.consumer.try_consume_file(+ os.path.join(settings.CONSUMPTION_DIR, "simple.png.pdf")+ )self.assertEqual(doc1.filename, "simple.png")self.assertEqual(doc1.archive_filename, "simple.pdf")@@ -588,7 +618,6 @@ class TestConsumer(DirectoriesMixin, TestCase):class PreConsumeTestCase(TestCase):-@mock.patch("documents.consumer.Popen")@override_settings(PRE_CONSUME_SCRIPT=None)def test_no_pre_consume_script(self, m):@@ -625,7 +654,6 @@ class PreConsumeTestCase(TestCase):class PostConsumeTestCase(TestCase):-@mock.patch("documents.consumer.Popen")@override_settings(POST_CONSUME_SCRIPT=None)def test_no_post_consume_script(self, m):@@ -662,7 +690,9 @@ class PostConsumeTestCase(TestCase):with tempfile.NamedTemporaryFile() as script:with override_settings(POST_CONSUME_SCRIPT=script.name):c = Correspondent.objects.create(name="my_bank")- doc = Document.objects.create(title="Test", mime_type="application/pdf", correspondent=c)+ doc = Document.objects.create(+ title="Test", mime_type="application/pdf", correspondent=c+ )tag1 = Tag.objects.create(name="a")tag2 = Tag.objects.create(name="b")doc.tags.add(tag1)diff --git a/src/documents/tests/test_date_parsing.py b/src/documents/tests/test_date_parsing.pyindex 50543ee75..d5dbaf60b 100644--- a/src/documents/tests/test_date_parsing.py+++ b/src/documents/tests/test_date_parsing.py@@ -12,7 +12,9 @@ from documents.parsers import parse_dateclass TestDate(TestCase):- SAMPLE_FILES = os.path.join(os.path.dirname(__file__), "../../paperless_tesseract/tests/samples")+ SAMPLE_FILES = os.path.join(+ os.path.dirname(__file__), "../../paperless_tesseract/tests/samples"+ )SCRATCH = "/tmp/paperless-tests-{}".format(str(uuid4())[:8])def setUp(self):@@ -38,24 +40,15 @@ class TestDate(TestCase):date = parse_date("", text)self.assertEqual(date,- datetime.datetime(- 2018, 2, 13, 0, 0,- tzinfo=tz.gettz(settings.TIME_ZONE)- )+ datetime.datetime(2018, 2, 13, 0, 0, tzinfo=tz.gettz(settings.TIME_ZONE)),)def test_date_format_5(self):- text = (- "lorem ipsum 130218, 2018, 20180213 and lorem 13.02.2018 lorem "- "ipsum"- )+ text = "lorem ipsum 130218, 2018, 20180213 and lorem 13.02.2018 lorem " "ipsum"date = parse_date("", text)self.assertEqual(date,- datetime.datetime(- 2018, 2, 13, 0, 0,- tzinfo=tz.gettz(settings.TIME_ZONE)- )+ datetime.datetime(2018, 2, 13, 0, 0, tzinfo=tz.gettz(settings.TIME_ZONE)),)def test_date_format_6(self):@@ -73,18 +66,11 @@ class TestDate(TestCase):self.assertEqual(parse_date("", text), None)def test_date_format_7(self):- text = (- "lorem ipsum\n"- "März 2019\n"- "lorem ipsum"- )+ text = "lorem ipsum\n" "März 2019\n" "lorem ipsum"date = parse_date("", text)self.assertEqual(date,- datetime.datetime(- 2019, 3, 1, 0, 0,- tzinfo=tz.gettz(settings.TIME_ZONE)- )+ datetime.datetime(2019, 3, 1, 0, 0, tzinfo=tz.gettz(settings.TIME_ZONE)),)def test_date_format_8(self):@@ -102,26 +88,15 @@ class TestDate(TestCase):)self.assertEqual(parse_date("", text),- datetime.datetime(- 2020, 3, 1, 0, 0,- tzinfo=tz.gettz(settings.TIME_ZONE)- )+ datetime.datetime(2020, 3, 1, 0, 0, tzinfo=tz.gettz(settings.TIME_ZONE)),)@override_settings(SCRATCH_DIR=SCRATCH)def test_date_format_9(self):- text = (- "lorem ipsum\n"- "27. Nullmonth 2020\n"- "März 2020\n"- "lorem ipsum"- )+ text = "lorem ipsum\n" "27. Nullmonth 2020\n" "März 2020\n" "lorem ipsum"self.assertEqual(parse_date("", text),- datetime.datetime(- 2020, 3, 1, 0, 0,- tzinfo=tz.gettz(settings.TIME_ZONE)- )+ datetime.datetime(2020, 3, 1, 0, 0, tzinfo=tz.gettz(settings.TIME_ZONE)),)def test_crazy_date_past(self, *args):@@ -135,19 +110,17 @@ class TestDate(TestCase):@override_settings(FILENAME_DATE_ORDER="YMD")def test_filename_date_parse_invalid(self, *args):- self.assertIsNone(parse_date("/tmp/20 408000l 2475 - test.pdf", "No date in here"))+ self.assertIsNone(+ parse_date("/tmp/20 408000l 2475 - test.pdf", "No date in here")+ )- @override_settings(IGNORE_DATES=(datetime.date(2019, 11, 3), datetime.date(2020, 1, 17)))+ @override_settings(+ IGNORE_DATES=(datetime.date(2019, 11, 3), datetime.date(2020, 1, 17))+ )def test_ignored_dates(self, *args):- text = (- "lorem ipsum 110319, 20200117 and lorem 13.02.2018 lorem "- "ipsum"- )+ text = "lorem ipsum 110319, 20200117 and lorem 13.02.2018 lorem " "ipsum"date = parse_date("", text)self.assertEqual(date,- datetime.datetime(- 2018, 2, 13, 0, 0,- tzinfo=tz.gettz(settings.TIME_ZONE)- )+ datetime.datetime(2018, 2, 13, 0, 0, tzinfo=tz.gettz(settings.TIME_ZONE)),)diff --git a/src/documents/tests/test_document_model.py b/src/documents/tests/test_document_model.pyindex 87261ecd9..3a7a88b87 100644--- a/src/documents/tests/test_document_model.py+++ b/src/documents/tests/test_document_model.py@@ -10,7 +10,6 @@ from ..models import Document, Correspondentclass TestDocument(TestCase):-def setUp(self) -> None:self.originals_dir = tempfile.mkdtemp()self.thumb_dir = tempfile.mkdtemp()@@ -30,7 +29,7 @@ class TestDocument(TestCase):title="Title",content="content",checksum="checksum",- mime_type="application/pdf"+ mime_type="application/pdf",)file_path = document.source_path@@ -47,20 +46,36 @@ class TestDocument(TestCase):def test_file_name(self):- doc = Document(mime_type="application/pdf", title="test", created=timezone.datetime(2020, 12, 25))+ doc = Document(+ mime_type="application/pdf",+ title="test",+ created=timezone.datetime(2020, 12, 25),+ )self.assertEqual(doc.get_public_filename(), "2020-12-25 test.pdf")def test_file_name_jpg(self):- doc = Document(mime_type="image/jpeg", title="test", created=timezone.datetime(2020, 12, 25))+ doc = Document(+ mime_type="image/jpeg",+ title="test",+ created=timezone.datetime(2020, 12, 25),+ )self.assertEqual(doc.get_public_filename(), "2020-12-25 test.jpg")def test_file_name_unknown(self):- doc = Document(mime_type="application/zip", title="test", created=timezone.datetime(2020, 12, 25))+ doc = Document(+ mime_type="application/zip",+ title="test",+ created=timezone.datetime(2020, 12, 25),+ )self.assertEqual(doc.get_public_filename(), "2020-12-25 test.zip")def test_file_name_invalid_type(self):- doc = Document(mime_type="image/jpegasd", title="test", created=timezone.datetime(2020, 12, 25))+ doc = Document(+ mime_type="image/jpegasd",+ title="test",+ created=timezone.datetime(2020, 12, 25),+ )self.assertEqual(doc.get_public_filename(), "2020-12-25 test")diff --git a/src/documents/tests/test_file_handling.py b/src/documents/tests/test_file_handling.pyindex 8be4b568b..6ffa4481d 100644--- a/src/documents/tests/test_file_handling.py+++ b/src/documents/tests/test_file_handling.py@@ -13,13 +13,16 @@ from django.test import TestCase, override_settingsfrom django.utils import timezonefrom .utils import DirectoriesMixin-from ..file_handling import generate_filename, create_source_path_directory, delete_empty_directories, \- generate_unique_filename+from ..file_handling import (+ generate_filename,+ create_source_path_directory,+ delete_empty_directories,+ generate_unique_filename,+)from ..models import Document, Correspondent, Tag, DocumentTypeclass TestFileHandling(DirectoriesMixin, TestCase):-@override_settings(PAPERLESS_FILENAME_FORMAT="")def test_generate_source_filename(self):document = Document()@@ -30,8 +33,9 @@ class TestFileHandling(DirectoriesMixin, TestCase):self.assertEqual(generate_filename(document), "{:07d}.pdf".format(document.pk))document.storage_type = Document.STORAGE_TYPE_GPG- self.assertEqual(generate_filename(document),- "{:07d}.pdf.gpg".format(document.pk))+ self.assertEqual(+ generate_filename(document), "{:07d}.pdf.gpg".format(document.pk)+ )@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{correspondent}")def test_file_renaming(self):@@ -41,7 +45,10 @@ class TestFileHandling(DirectoriesMixin, TestCase):document.save()# Test default source_path- self.assertEqual(document.source_path, settings.ORIGINALS_DIR + "/{:07d}.pdf".format(document.pk))+ self.assertEqual(+ document.source_path,+ settings.ORIGINALS_DIR + "/{:07d}.pdf".format(document.pk),+ )document.filename = generate_filename(document)@@ -51,8 +58,7 @@ class TestFileHandling(DirectoriesMixin, TestCase):# Enable encryption and check againdocument.storage_type = Document.STORAGE_TYPE_GPGdocument.filename = generate_filename(document)- self.assertEqual(document.filename,- "none/none.pdf.gpg")+ self.assertEqual(document.filename, "none/none.pdf.gpg")document.save()@@ -68,7 +74,9 @@ class TestFileHandling(DirectoriesMixin, TestCase):# Check proper handling of filesself.assertEqual(os.path.isdir(settings.ORIGINALS_DIR + "/test"), True)self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR + "/none"), False)- self.assertEqual(os.path.isfile(settings.ORIGINALS_DIR + "/test/test.pdf.gpg"), True)+ self.assertEqual(+ os.path.isfile(settings.ORIGINALS_DIR + "/test/test.pdf.gpg"), True+ )@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{correspondent}")def test_file_renaming_missing_permissions(self):@@ -79,13 +87,14 @@ class TestFileHandling(DirectoriesMixin, TestCase):# Ensure that filename is properly generateddocument.filename = generate_filename(document)- self.assertEqual(document.filename,- "none/none.pdf")+ self.assertEqual(document.filename, "none/none.pdf")create_source_path_directory(document.source_path)Path(document.source_path).touch()# Test source_path- self.assertEqual(document.source_path, settings.ORIGINALS_DIR + "/none/none.pdf")+ self.assertEqual(+ document.source_path, settings.ORIGINALS_DIR + "/none/none.pdf"+ )# Make the folder read- and execute-only (no writing and no renaming)os.chmod(settings.ORIGINALS_DIR + "/none", 0o555)@@ -95,7 +104,9 @@ class TestFileHandling(DirectoriesMixin, TestCase):document.save()# Check proper handling of files- self.assertEqual(os.path.isfile(settings.ORIGINALS_DIR + "/none/none.pdf"), True)+ self.assertEqual(+ os.path.isfile(settings.ORIGINALS_DIR + "/none/none.pdf"), True+ )self.assertEqual(document.filename, "none/none.pdf")os.chmod(settings.ORIGINALS_DIR + "/none", 0o777)@@ -103,7 +114,11 @@ class TestFileHandling(DirectoriesMixin, TestCase):@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{correspondent}")def test_file_renaming_database_error(self):- document1 = Document.objects.create(mime_type="application/pdf", storage_type=Document.STORAGE_TYPE_UNENCRYPTED, checksum="AAAAA")+ document1 = Document.objects.create(+ mime_type="application/pdf",+ storage_type=Document.STORAGE_TYPE_UNENCRYPTED,+ checksum="AAAAA",+ )document = Document()document.mime_type = "application/pdf"@@ -113,8 +128,7 @@ class TestFileHandling(DirectoriesMixin, TestCase):# Ensure that filename is properly generateddocument.filename = generate_filename(document)- self.assertEqual(document.filename,- "none/none.pdf")+ self.assertEqual(document.filename, "none/none.pdf")create_source_path_directory(document.source_path)Path(document.source_path).touch()@@ -122,8 +136,7 @@ class TestFileHandling(DirectoriesMixin, TestCase):self.assertTrue(os.path.isfile(document.source_path))# Set a correspondent and save the document- document.correspondent = Correspondent.objects.get_or_create(- name="test")[0]+ document.correspondent = Correspondent.objects.get_or_create(name="test")[0]with mock.patch("documents.signals.handlers.Document.objects.filter") as m:m.side_effect = DatabaseError()@@ -131,7 +144,9 @@ class TestFileHandling(DirectoriesMixin, TestCase):# Check proper handling of filesself.assertTrue(os.path.isfile(document.source_path))- self.assertEqual(os.path.isfile(settings.ORIGINALS_DIR + "/none/none.pdf"), True)+ self.assertEqual(+ os.path.isfile(settings.ORIGINALS_DIR + "/none/none.pdf"), True+ )self.assertEqual(document.filename, "none/none.pdf")@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{correspondent}")@@ -143,8 +158,7 @@ class TestFileHandling(DirectoriesMixin, TestCase):# Ensure that filename is properly generateddocument.filename = generate_filename(document)- self.assertEqual(document.filename,- "none/none.pdf")+ self.assertEqual(document.filename, "none/none.pdf")create_source_path_directory(document.source_path)Path(document.source_path).touch()@@ -152,10 +166,15 @@ class TestFileHandling(DirectoriesMixin, TestCase):# Ensure file deletion after deletepk = document.pkdocument.delete()- self.assertEqual(os.path.isfile(settings.ORIGINALS_DIR + "/none/none.pdf"), False)+ self.assertEqual(+ os.path.isfile(settings.ORIGINALS_DIR + "/none/none.pdf"), False+ )self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR + "/none"), False)- @override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{correspondent}", TRASH_DIR=tempfile.mkdtemp())+ @override_settings(+ PAPERLESS_FILENAME_FORMAT="{correspondent}/{correspondent}",+ TRASH_DIR=tempfile.mkdtemp(),+ )def test_document_delete_trash(self):document = Document()document.mime_type = "application/pdf"@@ -164,8 +183,7 @@ class TestFileHandling(DirectoriesMixin, TestCase):# Ensure that filename is properly generateddocument.filename = generate_filename(document)- self.assertEqual(document.filename,- "none/none.pdf")+ self.assertEqual(document.filename, "none/none.pdf")create_source_path_directory(document.source_path)Path(document.source_path).touch()@@ -173,7 +191,9 @@ class TestFileHandling(DirectoriesMixin, TestCase):# Ensure file was moved to trash after deleteself.assertEqual(os.path.isfile(settings.TRASH_DIR + "/none/none.pdf"), False)document.delete()- self.assertEqual(os.path.isfile(settings.ORIGINALS_DIR + "/none/none.pdf"), False)+ self.assertEqual(+ os.path.isfile(settings.ORIGINALS_DIR + "/none/none.pdf"), False+ )self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR + "/none"), False)self.assertEqual(os.path.isfile(settings.TRASH_DIR + "/none.pdf"), True)self.assertEqual(os.path.isfile(settings.TRASH_DIR + "/none_01.pdf"), False)@@ -207,8 +227,7 @@ class TestFileHandling(DirectoriesMixin, TestCase):# Ensure that filename is properly generateddocument.filename = generate_filename(document)- self.assertEqual(document.filename,- "none/none.pdf")+ self.assertEqual(document.filename, "none/none.pdf")create_source_path_directory(document.source_path)@@ -238,8 +257,18 @@ class TestFileHandling(DirectoriesMixin, TestCase):@override_settings(PAPERLESS_FILENAME_FORMAT="{asn} - {title}")def test_asn(self):- d1 = Document.objects.create(title="the_doc", mime_type="application/pdf", archive_serial_number=652, checksum="A")- d2 = Document.objects.create(title="the_doc", mime_type="application/pdf", archive_serial_number=None, checksum="B")+ d1 = Document.objects.create(+ title="the_doc",+ mime_type="application/pdf",+ archive_serial_number=652,+ checksum="A",+ )+ d2 = Document.objects.create(+ title="the_doc",+ mime_type="application/pdf",+ archive_serial_number=None,+ checksum="B",+ )self.assertEqual(generate_filename(d1), "652 - the_doc.pdf")self.assertEqual(generate_filename(d2), "none - the_doc.pdf")@@ -256,8 +285,7 @@ class TestFileHandling(DirectoriesMixin, TestCase):document.save()# Ensure that filename is properly generated- self.assertEqual(generate_filename(document),- "demo.pdf")+ self.assertEqual(generate_filename(document), "demo.pdf")@override_settings(PAPERLESS_FILENAME_FORMAT="{tags[type]}")def test_tags_with_dash(self):@@ -272,8 +300,7 @@ class TestFileHandling(DirectoriesMixin, TestCase):document.save()# Ensure that filename is properly generated- self.assertEqual(generate_filename(document),- "demo.pdf")+ self.assertEqual(generate_filename(document), "demo.pdf")@override_settings(PAPERLESS_FILENAME_FORMAT="{tags[type]}")def test_tags_malformed(self):@@ -288,8 +315,7 @@ class TestFileHandling(DirectoriesMixin, TestCase):document.save()# Ensure that filename is properly generated- self.assertEqual(generate_filename(document),- "none.pdf")+ self.assertEqual(generate_filename(document), "none.pdf")@override_settings(PAPERLESS_FILENAME_FORMAT="{tags[0]}")def test_tags_all(self):@@ -303,8 +329,7 @@ class TestFileHandling(DirectoriesMixin, TestCase):document.save()# Ensure that filename is properly generated- self.assertEqual(generate_filename(document),- "demo.pdf")+ self.assertEqual(generate_filename(document), "demo.pdf")@override_settings(PAPERLESS_FILENAME_FORMAT="{tags[1]}")def test_tags_out_of_bounds(self):@@ -318,8 +343,7 @@ class TestFileHandling(DirectoriesMixin, TestCase):document.save()# Ensure that filename is properly generated- self.assertEqual(generate_filename(document),- "none.pdf")+ self.assertEqual(generate_filename(document), "none.pdf")@override_settings(PAPERLESS_FILENAME_FORMAT="{tags}")def test_tags_without_args(self):@@ -338,7 +362,9 @@ class TestFileHandling(DirectoriesMixin, TestCase):self.assertEqual(generate_filename(doc), "doc1 tag1,tag2.pdf")- doc = Document.objects.create(title="doc2", checksum="B", mime_type="application/pdf")+ doc = Document.objects.create(+ title="doc2", checksum="B", mime_type="application/pdf"+ )self.assertEqual(generate_filename(doc), "doc2.pdf")@@ -348,12 +374,19 @@ class TestFileHandling(DirectoriesMixin, TestCase):doc.filename = generate_filename(doc)doc.save()- self.assertEqual(doc.source_path, os.path.join(settings.ORIGINALS_DIR, "etc", "something", "doc1.pdf"))+ self.assertEqual(+ doc.source_path,+ os.path.join(settings.ORIGINALS_DIR, "etc", "something", "doc1.pdf"),+ )- @override_settings(PAPERLESS_FILENAME_FORMAT="{created_year}-{created_month}-{created_day}")+ @override_settings(+ PAPERLESS_FILENAME_FORMAT="{created_year}-{created_month}-{created_day}"+ )def test_created_year_month_day(self):d1 = timezone.make_aware(datetime.datetime(2020, 3, 6, 1, 1, 1))- doc1 = Document.objects.create(title="doc1", mime_type="application/pdf", created=d1)+ doc1 = Document.objects.create(+ title="doc1", mime_type="application/pdf", created=d1+ )self.assertEqual(generate_filename(doc1), "2020-03-06.pdf")@@ -361,10 +394,14 @@ class TestFileHandling(DirectoriesMixin, TestCase):self.assertEqual(generate_filename(doc1), "2020-11-16.pdf")- @override_settings(PAPERLESS_FILENAME_FORMAT="{added_year}-{added_month}-{added_day}")+ @override_settings(+ PAPERLESS_FILENAME_FORMAT="{added_year}-{added_month}-{added_day}"+ )def test_added_year_month_day(self):d1 = timezone.make_aware(datetime.datetime(232, 1, 9, 1, 1, 1))- doc1 = Document.objects.create(title="doc1", mime_type="application/pdf", added=d1)+ doc1 = Document.objects.create(+ title="doc1", mime_type="application/pdf", added=d1+ )self.assertEqual(generate_filename(doc1), "232-01-09.pdf")@@ -372,7 +409,9 @@ class TestFileHandling(DirectoriesMixin, TestCase):self.assertEqual(generate_filename(doc1), "2020-11-16.pdf")- @override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{correspondent}/{correspondent}")+ @override_settings(+ PAPERLESS_FILENAME_FORMAT="{correspondent}/{correspondent}/{correspondent}"+ )def test_nested_directory_cleanup(self):document = Document()document.mime_type = "application/pdf"@@ -391,7 +430,9 @@ class TestFileHandling(DirectoriesMixin, TestCase):pk = document.pkdocument.delete()- self.assertEqual(os.path.isfile(settings.ORIGINALS_DIR + "/none/none/none.pdf"), False)+ self.assertEqual(+ os.path.isfile(settings.ORIGINALS_DIR + "/none/none/none.pdf"), False+ )self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR + "/none/none"), False)self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR + "/none"), False)self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR), True)@@ -414,12 +455,12 @@ class TestFileHandling(DirectoriesMixin, TestCase):Path(os.path.join(tmp, "notempty", "file")).touch()os.makedirs(os.path.join(tmp, "notempty", "empty"))- delete_empty_directories(os.path.join(tmp, "notempty", "empty"), root=settings.ORIGINALS_DIR)+ delete_empty_directories(+ os.path.join(tmp, "notempty", "empty"), root=settings.ORIGINALS_DIR+ )self.assertEqual(os.path.isdir(os.path.join(tmp, "notempty")), True)- self.assertEqual(os.path.isfile(- os.path.join(tmp, "notempty", "file")), True)- self.assertEqual(os.path.isdir(- os.path.join(tmp, "notempty", "empty")), False)+ self.assertEqual(os.path.isfile(os.path.join(tmp, "notempty", "file")), True)+ self.assertEqual(os.path.isdir(os.path.join(tmp, "notempty", "empty")), False)@override_settings(PAPERLESS_FILENAME_FORMAT="{created/[title]")def test_invalid_format(self):@@ -441,8 +482,12 @@ class TestFileHandling(DirectoriesMixin, TestCase):@override_settings(PAPERLESS_FILENAME_FORMAT="{title}")def test_duplicates(self):- document = Document.objects.create(mime_type="application/pdf", title="qwe", checksum="A", pk=1)- document2 = Document.objects.create(mime_type="application/pdf", title="qwe", checksum="B", pk=2)+ document = Document.objects.create(+ mime_type="application/pdf", title="qwe", checksum="A", pk=1+ )+ document2 = Document.objects.create(+ mime_type="application/pdf", title="qwe", checksum="B", pk=2+ )Path(document.source_path).touch()Path(document2.source_path).touch()document.filename = "0000001.pdf"@@ -480,11 +525,17 @@ class TestFileHandling(DirectoriesMixin, TestCase):self.assertTrue(os.path.isfile(document.source_path))self.assertEqual(document2.filename, "qwe.pdf")-@override_settings(PAPERLESS_FILENAME_FORMAT="{title}")@mock.patch("documents.signals.handlers.Document.objects.filter")def test_no_update_without_change(self, m):- doc = Document.objects.create(title="document", filename="document.pdf", archive_filename="document.pdf", checksum="A", archive_checksum="B", mime_type="application/pdf")+ doc = Document.objects.create(+ title="document",+ filename="document.pdf",+ archive_filename="document.pdf",+ checksum="A",+ archive_checksum="B",+ mime_type="application/pdf",+ )Path(doc.source_path).touch()Path(doc.archive_path).touch()@@ -493,16 +544,20 @@ class TestFileHandling(DirectoriesMixin, TestCase):m.assert_not_called()-class TestFileHandlingWithArchive(DirectoriesMixin, TestCase):-@override_settings(PAPERLESS_FILENAME_FORMAT=None)def test_create_no_format(self):original = os.path.join(settings.ORIGINALS_DIR, "0000001.pdf")archive = os.path.join(settings.ARCHIVE_DIR, "0000001.pdf")Path(original).touch()Path(archive).touch()- doc = Document.objects.create(mime_type="application/pdf", filename="0000001.pdf", checksum="A", archive_filename="0000001.pdf", archive_checksum="B")+ doc = Document.objects.create(+ mime_type="application/pdf",+ filename="0000001.pdf",+ checksum="A",+ archive_filename="0000001.pdf",+ archive_checksum="B",+ )self.assertTrue(os.path.isfile(original))self.assertTrue(os.path.isfile(archive))@@ -515,21 +570,39 @@ class TestFileHandlingWithArchive(DirectoriesMixin, TestCase):archive = os.path.join(settings.ARCHIVE_DIR, "0000001.pdf")Path(original).touch()Path(archive).touch()- doc = Document.objects.create(mime_type="application/pdf", title="my_doc", filename="0000001.pdf", checksum="A", archive_checksum="B", archive_filename="0000001.pdf")+ doc = Document.objects.create(+ mime_type="application/pdf",+ title="my_doc",+ filename="0000001.pdf",+ checksum="A",+ archive_checksum="B",+ archive_filename="0000001.pdf",+ )self.assertFalse(os.path.isfile(original))self.assertFalse(os.path.isfile(archive))self.assertTrue(os.path.isfile(doc.source_path))self.assertTrue(os.path.isfile(doc.archive_path))- self.assertEqual(doc.source_path, os.path.join(settings.ORIGINALS_DIR, "none", "my_doc.pdf"))- self.assertEqual(doc.archive_path, os.path.join(settings.ARCHIVE_DIR, "none", "my_doc.pdf"))+ self.assertEqual(+ doc.source_path, os.path.join(settings.ORIGINALS_DIR, "none", "my_doc.pdf")+ )+ self.assertEqual(+ doc.archive_path, os.path.join(settings.ARCHIVE_DIR, "none", "my_doc.pdf")+ )@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{title}")def test_move_archive_gone(self):original = os.path.join(settings.ORIGINALS_DIR, "0000001.pdf")archive = os.path.join(settings.ARCHIVE_DIR, "0000001.pdf")Path(original).touch()- doc = Document.objects.create(mime_type="application/pdf", title="my_doc", filename="0000001.pdf", checksum="A", archive_checksum="B", archive_filename="0000001.pdf")+ doc = Document.objects.create(+ mime_type="application/pdf",+ title="my_doc",+ filename="0000001.pdf",+ checksum="A",+ archive_checksum="B",+ archive_filename="0000001.pdf",+ )self.assertTrue(os.path.isfile(original))self.assertFalse(os.path.isfile(archive))@@ -545,7 +618,14 @@ class TestFileHandlingWithArchive(DirectoriesMixin, TestCase):Path(archive).touch()os.makedirs(os.path.join(settings.ARCHIVE_DIR, "none"))Path(existing_archive_file).touch()- doc = Document.objects.create(mime_type="application/pdf", title="my_doc", filename="0000001.pdf", checksum="A", archive_checksum="B", archive_filename="0000001.pdf")+ doc = Document.objects.create(+ mime_type="application/pdf",+ title="my_doc",+ filename="0000001.pdf",+ checksum="A",+ archive_checksum="B",+ archive_filename="0000001.pdf",+ )self.assertFalse(os.path.isfile(original))self.assertFalse(os.path.isfile(archive))@@ -561,8 +641,14 @@ class TestFileHandlingWithArchive(DirectoriesMixin, TestCase):Path(original).touch()Path(archive).touch()- doc = Document.objects.create(mime_type="application/pdf", title="document", filename="document_01.pdf", checksum="A",- archive_checksum="B", archive_filename="document.pdf")+ doc = Document.objects.create(+ mime_type="application/pdf",+ title="document",+ filename="document_01.pdf",+ checksum="A",+ archive_checksum="B",+ archive_filename="document.pdf",+ )self.assertEqual(doc.filename, "document.pdf")self.assertEqual(doc.archive_filename, "document.pdf")@@ -577,8 +663,14 @@ class TestFileHandlingWithArchive(DirectoriesMixin, TestCase):Path(original).touch()Path(archive).touch()- doc = Document.objects.create(mime_type="application/pdf", title="document", filename="document.pdf", checksum="A",- archive_checksum="B", archive_filename="document_01.pdf")+ doc = Document.objects.create(+ mime_type="application/pdf",+ title="document",+ filename="document.pdf",+ checksum="A",+ archive_checksum="B",+ archive_filename="document_01.pdf",+ )self.assertEqual(doc.filename, "document.pdf")self.assertEqual(doc.archive_filename, "document.pdf")@@ -589,7 +681,6 @@ class TestFileHandlingWithArchive(DirectoriesMixin, TestCase):@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{title}")@mock.patch("documents.signals.handlers.os.rename")def test_move_archive_error(self, m):-def fake_rename(src, dst):if "archive" in src:raise OSError()@@ -603,7 +694,14 @@ class TestFileHandlingWithArchive(DirectoriesMixin, TestCase):archive = os.path.join(settings.ARCHIVE_DIR, "0000001.pdf")Path(original).touch()Path(archive).touch()- doc = Document.objects.create(mime_type="application/pdf", title="my_doc", filename="0000001.pdf", checksum="A", archive_checksum="B", archive_filename="0000001.pdf")+ doc = Document.objects.create(+ mime_type="application/pdf",+ title="my_doc",+ filename="0000001.pdf",+ checksum="A",+ archive_checksum="B",+ archive_filename="0000001.pdf",+ )m.assert_called()self.assertTrue(os.path.isfile(original))@@ -615,9 +713,16 @@ class TestFileHandlingWithArchive(DirectoriesMixin, TestCase):def test_move_file_gone(self):original = os.path.join(settings.ORIGINALS_DIR, "0000001.pdf")archive = os.path.join(settings.ARCHIVE_DIR, "0000001.pdf")- #Path(original).touch()+ # Path(original).touch()Path(archive).touch()- doc = Document.objects.create(mime_type="application/pdf", title="my_doc", filename="0000001.pdf", archive_filename="0000001.pdf", checksum="A", archive_checksum="B")+ doc = Document.objects.create(+ mime_type="application/pdf",+ title="my_doc",+ filename="0000001.pdf",+ archive_filename="0000001.pdf",+ checksum="A",+ archive_checksum="B",+ )self.assertFalse(os.path.isfile(original))self.assertTrue(os.path.isfile(archive))@@ -627,7 +732,6 @@ class TestFileHandlingWithArchive(DirectoriesMixin, TestCase):@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{title}")@mock.patch("documents.signals.handlers.os.rename")def test_move_file_error(self, m):-def fake_rename(src, dst):if "original" in src:raise OSError()@@ -641,7 +745,14 @@ class TestFileHandlingWithArchive(DirectoriesMixin, TestCase):archive = os.path.join(settings.ARCHIVE_DIR, "0000001.pdf")Path(original).touch()Path(archive).touch()- doc = Document.objects.create(mime_type="application/pdf", title="my_doc", filename="0000001.pdf", archive_filename="0000001.pdf", checksum="A", archive_checksum="B")+ doc = Document.objects.create(+ mime_type="application/pdf",+ title="my_doc",+ filename="0000001.pdf",+ archive_filename="0000001.pdf",+ checksum="A",+ archive_checksum="B",+ )m.assert_called()self.assertTrue(os.path.isfile(original))@@ -655,7 +766,14 @@ class TestFileHandlingWithArchive(DirectoriesMixin, TestCase):archive = os.path.join(settings.ARCHIVE_DIR, "0000001.pdf")Path(original).touch()Path(archive).touch()- doc = Document.objects.create(mime_type="application/pdf", title="my_doc", filename="0000001.pdf", checksum="A", archive_checksum="B", archive_filename="0000001.pdf")+ doc = Document.objects.create(+ mime_type="application/pdf",+ title="my_doc",+ filename="0000001.pdf",+ checksum="A",+ archive_checksum="B",+ archive_filename="0000001.pdf",+ )self.assertTrue(os.path.isfile(original))self.assertTrue(os.path.isfile(archive))@@ -678,8 +796,20 @@ class TestFileHandlingWithArchive(DirectoriesMixin, TestCase):Path(original2).touch()Path(archive).touch()- doc1 = Document.objects.create(mime_type="image/png", title="document", filename="document.png", checksum="A", archive_checksum="B", archive_filename="0000001.pdf")- doc2 = Document.objects.create(mime_type="application/pdf", title="0000001", filename="0000001.pdf", checksum="C")+ doc1 = Document.objects.create(+ mime_type="image/png",+ title="document",+ filename="document.png",+ checksum="A",+ archive_checksum="B",+ archive_filename="0000001.pdf",+ )+ doc2 = Document.objects.create(+ mime_type="application/pdf",+ title="0000001",+ filename="0000001.pdf",+ checksum="C",+ )self.assertTrue(os.path.isfile(doc1.source_path))self.assertTrue(os.path.isfile(doc1.archive_path))@@ -698,7 +828,14 @@ class TestFileHandlingWithArchive(DirectoriesMixin, TestCase):archive = os.path.join(settings.ARCHIVE_DIR, "0000001.pdf")Path(original).touch()Path(archive).touch()- doc = Document(mime_type="application/pdf", title="my_doc", filename="0000001.pdf", checksum="A", archive_filename="0000001.pdf", archive_checksum="B")+ doc = Document(+ mime_type="application/pdf",+ title="my_doc",+ filename="0000001.pdf",+ checksum="A",+ archive_filename="0000001.pdf",+ archive_checksum="B",+ )with mock.patch("documents.signals.handlers.Document.objects.filter") as m:m.side_effect = DatabaseError()doc.save()@@ -710,28 +847,38 @@ class TestFileHandlingWithArchive(DirectoriesMixin, TestCase):class TestFilenameGeneration(TestCase):-- @override_settings(- PAPERLESS_FILENAME_FORMAT="{title}"- )+ @override_settings(PAPERLESS_FILENAME_FORMAT="{title}")def test_invalid_characters(self):- doc = Document.objects.create(title="This. is the title.", mime_type="application/pdf", pk=1, checksum="1")+ doc = Document.objects.create(+ title="This. is the title.", mime_type="application/pdf", pk=1, checksum="1"+ )self.assertEqual(generate_filename(doc), "This. is the title.pdf")- doc = Document.objects.create(title="my\\invalid/../title:yay", mime_type="application/pdf", pk=2, checksum="2")+ doc = Document.objects.create(+ title="my\\invalid/../title:yay",+ mime_type="application/pdf",+ pk=2,+ checksum="2",+ )self.assertEqual(generate_filename(doc), "my-invalid-..-title-yay.pdf")- @override_settings(- PAPERLESS_FILENAME_FORMAT="{created}"- )+ @override_settings(PAPERLESS_FILENAME_FORMAT="{created}")def test_date(self):- doc = Document.objects.create(title="does not matter", created=timezone.make_aware(datetime.datetime(2020,5,21, 7,36,51, 153)), mime_type="application/pdf", pk=2, checksum="2")+ doc = Document.objects.create(+ title="does not matter",+ created=timezone.make_aware(datetime.datetime(2020, 5, 21, 7, 36, 51, 153)),+ mime_type="application/pdf",+ pk=2,+ checksum="2",+ )self.assertEqual(generate_filename(doc), "2020-05-21.pdf")def run():- doc = Document.objects.create(checksum=str(uuid.uuid4()), title=str(uuid.uuid4()), content="wow")+ doc = Document.objects.create(+ checksum=str(uuid.uuid4()), title=str(uuid.uuid4()), content="wow"+ )doc.filename = generate_unique_filename(doc)Path(doc.thumbnail_path).touch()with open(doc.source_path, "w") as f:diff --git a/src/documents/tests/test_importer.py b/src/documents/tests/test_importer.pyindex 01600df33..73215173a 100644--- a/src/documents/tests/test_importer.py+++ b/src/documents/tests/test_importer.py@@ -6,14 +6,14 @@ from ..management.commands.document_importer import Commandclass TestImporter(TestCase):-def __init__(self, *args, **kwargs):TestCase.__init__(self, *args, **kwargs)def test_check_manifest_exists(self):cmd = Command()self.assertRaises(- CommandError, cmd._check_manifest_exists, "/tmp/manifest.json")+ CommandError, cmd._check_manifest_exists, "/tmp/manifest.json"+ )def test_check_manifest(self):@@ -23,15 +23,14 @@ class TestImporter(TestCase):cmd.manifest = [{"model": "documents.document"}]with self.assertRaises(CommandError) as cm:cmd._check_manifest()- self.assertTrue(- 'The manifest file contains a record' in str(cm.exception))+ self.assertTrue("The manifest file contains a record" in str(cm.exception))- cmd.manifest = [{- "model": "documents.document",- EXPORTER_FILE_NAME: "noexist.pdf"- }]+ cmd.manifest = [+ {"model": "documents.document", EXPORTER_FILE_NAME: "noexist.pdf"}+ ]# self.assertRaises(CommandError, cmd._check_manifest)with self.assertRaises(CommandError) as cm:cmd._check_manifest()self.assertTrue(- 'The manifest file refers to "noexist.pdf"' in str(cm.exception))+ 'The manifest file refers to "noexist.pdf"' in str(cm.exception)+ )diff --git a/src/documents/tests/test_index.py b/src/documents/tests/test_index.pyindex 14304ab28..31ad2aebf 100644--- a/src/documents/tests/test_index.py+++ b/src/documents/tests/test_index.py@@ -6,10 +6,11 @@ from documents.tests.utils import DirectoriesMixinclass TestAutoComplete(DirectoriesMixin, TestCase):-def test_auto_complete(self):- doc1 = Document.objects.create(title="doc1", checksum="A", content="test test2 test3")+ doc1 = Document.objects.create(+ title="doc1", checksum="A", content="test test2 test3"+ )doc2 = Document.objects.create(title="doc2", checksum="B", content="test test2")doc3 = Document.objects.create(title="doc3", checksum="C", content="test2")@@ -19,7 +20,11 @@ class TestAutoComplete(DirectoriesMixin, TestCase):ix = index.open_index()- self.assertListEqual(index.autocomplete(ix, "tes"), [b"test3", b"test", b"test2"])- self.assertListEqual(index.autocomplete(ix, "tes", limit=3), [b"test3", b"test", b"test2"])+ self.assertListEqual(+ index.autocomplete(ix, "tes"), [b"test3", b"test", b"test2"]+ )+ self.assertListEqual(+ index.autocomplete(ix, "tes", limit=3), [b"test3", b"test", b"test2"]+ )self.assertListEqual(index.autocomplete(ix, "tes", limit=1), [b"test3"])self.assertListEqual(index.autocomplete(ix, "tes", limit=0), [])diff --git a/src/documents/tests/test_management.py b/src/documents/tests/test_management.pyindex f7beb8907..f3c3a3fae 100644--- a/src/documents/tests/test_management.py+++ b/src/documents/tests/test_management.py@@ -22,21 +22,29 @@ sample_file = os.path.join(os.path.dirname(__file__), "samples", "simple.pdf")@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{title}")class TestArchiver(DirectoriesMixin, TestCase):-def make_models(self):- return Document.objects.create(checksum="A", title="A", content="first document", mime_type="application/pdf")+ return Document.objects.create(+ checksum="A",+ title="A",+ content="first document",+ mime_type="application/pdf",+ )def test_archiver(self):doc = self.make_models()- shutil.copy(sample_file, os.path.join(self.dirs.originals_dir, f"{doc.id:07}.pdf"))+ shutil.copy(+ sample_file, os.path.join(self.dirs.originals_dir, f"{doc.id:07}.pdf")+ )- call_command('document_archiver')+ call_command("document_archiver")def test_handle_document(self):doc = self.make_models()- shutil.copy(sample_file, os.path.join(self.dirs.originals_dir, f"{doc.id:07}.pdf"))+ shutil.copy(+ sample_file, os.path.join(self.dirs.originals_dir, f"{doc.id:07}.pdf")+ )handle_document(doc.pk)@@ -66,10 +74,24 @@ class TestArchiver(DirectoriesMixin, TestCase):@override_settings(PAPERLESS_FILENAME_FORMAT="{title}")def test_naming_priorities(self):- doc1 = Document.objects.create(checksum="A", title="document", content="first document", mime_type="application/pdf", filename="document.pdf")- doc2 = Document.objects.create(checksum="B", title="document", content="second document", mime_type="application/pdf", filename="document_01.pdf")+ doc1 = Document.objects.create(+ checksum="A",+ title="document",+ content="first document",+ mime_type="application/pdf",+ filename="document.pdf",+ )+ doc2 = Document.objects.create(+ checksum="B",+ title="document",+ content="second document",+ mime_type="application/pdf",+ filename="document_01.pdf",+ )shutil.copy(sample_file, os.path.join(self.dirs.originals_dir, f"document.pdf"))- shutil.copy(sample_file, os.path.join(self.dirs.originals_dir, f"document_01.pdf"))+ shutil.copy(+ sample_file, os.path.join(self.dirs.originals_dir, f"document_01.pdf")+ )handle_document(doc2.pk)handle_document(doc1.pk)@@ -82,12 +104,11 @@ class TestArchiver(DirectoriesMixin, TestCase):class TestDecryptDocuments(TestCase):-@override_settings(ORIGINALS_DIR=os.path.join(os.path.dirname(__file__), "samples", "originals"),THUMBNAIL_DIR=os.path.join(os.path.dirname(__file__), "samples", "thumb"),PASSPHRASE="test",- PAPERLESS_FILENAME_FORMAT=None+ PAPERLESS_FILENAME_FORMAT=None,)@mock.patch("documents.management.commands.decrypt_documents.input")def test_decrypt(self, m):@@ -99,17 +120,39 @@ class TestDecryptDocuments(TestCase):os.makedirs(thumb_dir, exist_ok=True)override_settings(- ORIGINALS_DIR=originals_dir,- THUMBNAIL_DIR=thumb_dir,- PASSPHRASE="test"+ ORIGINALS_DIR=originals_dir, THUMBNAIL_DIR=thumb_dir, PASSPHRASE="test").enable()- doc = Document.objects.create(checksum="82186aaa94f0b98697d704b90fd1c072", title="wow", filename="0000004.pdf.gpg", mime_type="application/pdf", storage_type=Document.STORAGE_TYPE_GPG)-- shutil.copy(os.path.join(os.path.dirname(__file__), "samples", "documents", "originals", "0000004.pdf.gpg"), os.path.join(originals_dir, "0000004.pdf.gpg"))- shutil.copy(os.path.join(os.path.dirname(__file__), "samples", "documents", "thumbnails", f"0000004.png.gpg"), os.path.join(thumb_dir, f"{doc.id:07}.png.gpg"))-- call_command('decrypt_documents')+ doc = Document.objects.create(+ checksum="82186aaa94f0b98697d704b90fd1c072",+ title="wow",+ filename="0000004.pdf.gpg",+ mime_type="application/pdf",+ storage_type=Document.STORAGE_TYPE_GPG,+ )++ shutil.copy(+ os.path.join(+ os.path.dirname(__file__),+ "samples",+ "documents",+ "originals",+ "0000004.pdf.gpg",+ ),+ os.path.join(originals_dir, "0000004.pdf.gpg"),+ )+ shutil.copy(+ os.path.join(+ os.path.dirname(__file__),+ "samples",+ "documents",+ "thumbnails",+ f"0000004.png.gpg",+ ),+ os.path.join(thumb_dir, f"{doc.id:07}.png.gpg"),+ )++ call_command("decrypt_documents")doc.refresh_from_db()@@ -126,7 +169,6 @@ class TestDecryptDocuments(TestCase):class TestMakeIndex(TestCase):-@mock.patch("documents.management.commands.document_index.index_reindex")def test_reindex(self, m):call_command("document_index", "reindex")@@ -139,7 +181,6 @@ class TestMakeIndex(TestCase):class TestRenamer(DirectoriesMixin, TestCase):-@override_settings(PAPERLESS_FILENAME_FORMAT="")def test_rename(self):doc = Document.objects.create(title="test", mime_type="image/jpeg")@@ -164,8 +205,9 @@ class TestRenamer(DirectoriesMixin, TestCase):class TestCreateClassifier(TestCase):-- @mock.patch("documents.management.commands.document_create_classifier.train_classifier")+ @mock.patch(+ "documents.management.commands.document_create_classifier.train_classifier"+ )def test_create_classifier(self, m):call_command("document_create_classifier")@@ -173,7 +215,6 @@ class TestCreateClassifier(TestCase):class TestSanityChecker(DirectoriesMixin, TestCase):-def test_no_issues(self):with self.assertLogs() as capture:call_command("document_sanity_checker")@@ -182,7 +223,9 @@ class TestSanityChecker(DirectoriesMixin, TestCase):self.assertIn("Sanity checker detected no issues.", capture.output[0])def test_errors(self):- doc = Document.objects.create(title="test", content="test", filename="test.pdf", checksum="abc")+ doc = Document.objects.create(+ title="test", content="test", filename="test.pdf", checksum="abc"+ )Path(doc.source_path).touch()Path(doc.thumbnail_path).touch()diff --git a/src/documents/tests/test_management_consumer.py b/src/documents/tests/test_management_consumer.pyindex 92a5070dc..31ab69339 100644--- a/src/documents/tests/test_management_consumer.py+++ b/src/documents/tests/test_management_consumer.py@@ -16,7 +16,6 @@ from documents.tests.utils import DirectoriesMixinclass ConsumerThread(Thread):-def __init__(self):super().__init__()self.cmd = document_consumer.Command()@@ -31,7 +30,7 @@ class ConsumerThread(Thread):def chunked(size, source):for i in range(0, len(source), size):- yield source[i:i+size]+ yield source[i : i + size]class ConsumerMixin:@@ -41,7 +40,9 @@ class ConsumerMixin:def setUp(self) -> None:super(ConsumerMixin, self).setUp()self.t = None- patcher = mock.patch("documents.management.commands.document_consumer.async_task")+ patcher = mock.patch(+ "documents.management.commands.document_consumer.async_task"+ )self.task_mock = patcher.start()self.addCleanup(patcher.stop)@@ -81,13 +82,13 @@ class ConsumerMixin:print("Consumed a perfectly valid file.")def slow_write_file(self, target, incomplete=False):- with open(self.sample_file, 'rb') as f:+ with open(self.sample_file, "rb") as f:pdf_bytes = f.read()if incomplete:- pdf_bytes = pdf_bytes[:len(pdf_bytes) - 100]+ pdf_bytes = pdf_bytes[: len(pdf_bytes) - 100]- with open(target, 'wb') as f:+ with open(target, "wb") as f:# this will take 2 seconds, since the file is about 20k.print("Start writing file.")for b in chunked(1000, pdf_bytes):@@ -97,7 +98,6 @@ class ConsumerMixin:class TestConsumer(DirectoriesMixin, ConsumerMixin, TransactionTestCase):-def test_consume_file(self):self.t_start()@@ -195,23 +195,35 @@ class TestConsumer(DirectoriesMixin, ConsumerMixin, TransactionTestCase):@override_settings(CONSUMPTION_DIR="does_not_exist")def test_consumption_directory_invalid(self):- self.assertRaises(CommandError, call_command, 'document_consumer', '--oneshot')+ self.assertRaises(CommandError, call_command, "document_consumer", "--oneshot")@override_settings(CONSUMPTION_DIR="")def test_consumption_directory_unset(self):- self.assertRaises(CommandError, call_command, 'document_consumer', '--oneshot')+ self.assertRaises(CommandError, call_command, "document_consumer", "--oneshot")def test_mac_write(self):self.task_mock.side_effect = self.bogus_taskself.t_start()- shutil.copy(self.sample_file, os.path.join(self.dirs.consumption_dir, ".DS_STORE"))- shutil.copy(self.sample_file, os.path.join(self.dirs.consumption_dir, "my_file.pdf"))- shutil.copy(self.sample_file, os.path.join(self.dirs.consumption_dir, "._my_file.pdf"))- shutil.copy(self.sample_file, os.path.join(self.dirs.consumption_dir, "my_second_file.pdf"))- shutil.copy(self.sample_file, os.path.join(self.dirs.consumption_dir, "._my_second_file.pdf"))+ shutil.copy(+ self.sample_file, os.path.join(self.dirs.consumption_dir, ".DS_STORE")+ )+ shutil.copy(+ self.sample_file, os.path.join(self.dirs.consumption_dir, "my_file.pdf")+ )+ shutil.copy(+ self.sample_file, os.path.join(self.dirs.consumption_dir, "._my_file.pdf")+ )+ shutil.copy(+ self.sample_file,+ os.path.join(self.dirs.consumption_dir, "my_second_file.pdf"),+ )+ shutil.copy(+ self.sample_file,+ os.path.join(self.dirs.consumption_dir, "._my_second_file.pdf"),+ )sleep(5)@@ -219,15 +231,20 @@ class TestConsumer(DirectoriesMixin, ConsumerMixin, TransactionTestCase):self.assertEqual(2, self.task_mock.call_count)- fnames = [os.path.basename(args[1]) for args, _ in self.task_mock.call_args_list]+ fnames = [+ os.path.basename(args[1]) for args, _ in self.task_mock.call_args_list+ ]self.assertCountEqual(fnames, ["my_file.pdf", "my_second_file.pdf"])def test_is_ignored(self):test_paths = [(os.path.join(self.dirs.consumption_dir, "foo.pdf"), False),- (os.path.join(self.dirs.consumption_dir, "foo","bar.pdf"), False),+ (os.path.join(self.dirs.consumption_dir, "foo", "bar.pdf"), False),(os.path.join(self.dirs.consumption_dir, ".DS_STORE", "foo.pdf"), True),- (os.path.join(self.dirs.consumption_dir, "foo", ".DS_STORE", "bar.pdf"), True),+ (+ os.path.join(self.dirs.consumption_dir, "foo", ".DS_STORE", "bar.pdf"),+ True,+ ),(os.path.join(self.dirs.consumption_dir, ".stfolder", "foo.pdf"), True),(os.path.join(self.dirs.consumption_dir, "._foo.pdf"), True),(os.path.join(self.dirs.consumption_dir, "._foo", "bar.pdf"), False),@@ -236,10 +253,13 @@ class TestConsumer(DirectoriesMixin, ConsumerMixin, TransactionTestCase):self.assertEqual(expected_ignored,document_consumer._is_ignored(file_path),- f'_is_ignored("{file_path}") != {expected_ignored}')+ f'_is_ignored("{file_path}") != {expected_ignored}',+ )-@override_settings(CONSUMER_POLLING=1, CONSUMER_POLLING_DELAY=3, CONSUMER_POLLING_RETRY_COUNT=20)+@override_settings(+ CONSUMER_POLLING=1, CONSUMER_POLLING_DELAY=3, CONSUMER_POLLING_RETRY_COUNT=20+)class TestConsumerPolling(TestConsumer):# just do all the tests with pollingpass@@ -251,21 +271,27 @@ class TestConsumerRecursive(TestConsumer):pass-@override_settings(CONSUMER_RECURSIVE=True, CONSUMER_POLLING=1, CONSUMER_POLLING_DELAY=3, CONSUMER_POLLING_RETRY_COUNT=20)+@override_settings(+ CONSUMER_RECURSIVE=True,+ CONSUMER_POLLING=1,+ CONSUMER_POLLING_DELAY=3,+ CONSUMER_POLLING_RETRY_COUNT=20,+)class TestConsumerRecursivePolling(TestConsumer):# just do all the tests with polling and recursivepassclass TestConsumerTags(DirectoriesMixin, ConsumerMixin, TransactionTestCase):-@override_settings(CONSUMER_RECURSIVE=True)@override_settings(CONSUMER_SUBDIRS_AS_TAGS=True)def test_consume_file_with_path_tags(self):tag_names = ("existingTag", "Space Tag")# Create a Tag prior to consuming a file using it in path- tag_ids = [Tag.objects.create(name="existingtag").pk,]+ tag_ids = [+ Tag.objects.create(name="existingtag").pk,+ ]self.t_start()@@ -292,6 +318,8 @@ class TestConsumerTags(DirectoriesMixin, ConsumerMixin, TransactionTestCase):# their order.self.assertCountEqual(kwargs["override_tag_ids"], tag_ids)- @override_settings(CONSUMER_POLLING=1, CONSUMER_POLLING_DELAY=1, CONSUMER_POLLING_RETRY_COUNT=20)+ @override_settings(+ CONSUMER_POLLING=1, CONSUMER_POLLING_DELAY=1, CONSUMER_POLLING_RETRY_COUNT=20+ )def test_consume_file_with_path_tags_polling(self):self.test_consume_file_with_path_tags()diff --git a/src/documents/tests/test_management_exporter.py b/src/documents/tests/test_management_exporter.pyindex 9e2dd0804..e833b0eef 100644--- a/src/documents/tests/test_management_exporter.py+++ b/src/documents/tests/test_management_exporter.py@@ -17,15 +17,41 @@ from documents.tests.utils import DirectoriesMixin, paperless_environmentclass TestExportImport(DirectoriesMixin, TestCase):-def setUp(self) -> None:self.target = tempfile.mkdtemp()self.addCleanup(shutil.rmtree, self.target)- self.d1 = Document.objects.create(content="Content", checksum="42995833e01aea9b3edee44bbfdd7ce1", archive_checksum="62acb0bcbfbcaa62ca6ad3668e4e404b", title="wow1", filename="0000001.pdf", mime_type="application/pdf", archive_filename="0000001.pdf")- self.d2 = Document.objects.create(content="Content", checksum="9c9691e51741c1f4f41a20896af31770", title="wow2", filename="0000002.pdf", mime_type="application/pdf")- self.d3 = Document.objects.create(content="Content", checksum="d38d7ed02e988e072caf924e0f3fcb76", title="wow2", filename="0000003.pdf", mime_type="application/pdf")- self.d4 = Document.objects.create(content="Content", checksum="82186aaa94f0b98697d704b90fd1c072", title="wow_dec", filename="0000004.pdf.gpg", mime_type="application/pdf", storage_type=Document.STORAGE_TYPE_GPG)+ self.d1 = Document.objects.create(+ content="Content",+ checksum="42995833e01aea9b3edee44bbfdd7ce1",+ archive_checksum="62acb0bcbfbcaa62ca6ad3668e4e404b",+ title="wow1",+ filename="0000001.pdf",+ mime_type="application/pdf",+ archive_filename="0000001.pdf",+ )+ self.d2 = Document.objects.create(+ content="Content",+ checksum="9c9691e51741c1f4f41a20896af31770",+ title="wow2",+ filename="0000002.pdf",+ mime_type="application/pdf",+ )+ self.d3 = Document.objects.create(+ content="Content",+ checksum="d38d7ed02e988e072caf924e0f3fcb76",+ title="wow2",+ filename="0000003.pdf",+ mime_type="application/pdf",+ )+ self.d4 = Document.objects.create(+ content="Content",+ checksum="82186aaa94f0b98697d704b90fd1c072",+ title="wow_dec",+ filename="0000004.pdf.gpg",+ mime_type="application/pdf",+ storage_type=Document.STORAGE_TYPE_GPG,+ )self.t1 = Tag.objects.create(name="t")self.dt1 = DocumentType.objects.create(name="dt")@@ -38,17 +64,21 @@ class TestExportImport(DirectoriesMixin, TestCase):super(TestExportImport, self).setUp()def _get_document_from_manifest(self, manifest, id):- f = list(filter(lambda d: d['model'] == "documents.document" and d['pk'] == id, manifest))+ f = list(+ filter(+ lambda d: d["model"] == "documents.document" and d["pk"] == id, manifest+ )+ )if len(f) == 1:return f[0]else:raise ValueError(f"document with id {id} does not exist in manifest")- @override_settings(- PASSPHRASE="test"- )- def _do_export(self, use_filename_format=False, compare_checksums=False, delete=False):- args = ['document_exporter', self.target]+ @override_settings(PASSPHRASE="test")+ def _do_export(+ self, use_filename_format=False, compare_checksums=False, delete=False+ ):+ args = ["document_exporter", self.target]if use_filename_format:args += ["--use-filename-format"]if compare_checksums:@@ -65,39 +95,69 @@ class TestExportImport(DirectoriesMixin, TestCase):def test_exporter(self, use_filename_format=False):shutil.rmtree(os.path.join(self.dirs.media_dir, "documents"))- shutil.copytree(os.path.join(os.path.dirname(__file__), "samples", "documents"), os.path.join(self.dirs.media_dir, "documents"))+ shutil.copytree(+ os.path.join(os.path.dirname(__file__), "samples", "documents"),+ os.path.join(self.dirs.media_dir, "documents"),+ )manifest = self._do_export(use_filename_format=use_filename_format)self.assertEqual(len(manifest), 8)- self.assertEqual(len(list(filter(lambda e: e['model'] == 'documents.document', manifest))), 4)+ self.assertEqual(+ len(list(filter(lambda e: e["model"] == "documents.document", manifest))), 4+ )self.assertTrue(os.path.exists(os.path.join(self.target, "manifest.json")))- self.assertEqual(self._get_document_from_manifest(manifest, self.d1.id)['fields']['title'], "wow1")- self.assertEqual(self._get_document_from_manifest(manifest, self.d2.id)['fields']['title'], "wow2")- self.assertEqual(self._get_document_from_manifest(manifest, self.d3.id)['fields']['title'], "wow2")- self.assertEqual(self._get_document_from_manifest(manifest, self.d4.id)['fields']['title'], "wow_dec")+ self.assertEqual(+ self._get_document_from_manifest(manifest, self.d1.id)["fields"]["title"],+ "wow1",+ )+ self.assertEqual(+ self._get_document_from_manifest(manifest, self.d2.id)["fields"]["title"],+ "wow2",+ )+ self.assertEqual(+ self._get_document_from_manifest(manifest, self.d3.id)["fields"]["title"],+ "wow2",+ )+ self.assertEqual(+ self._get_document_from_manifest(manifest, self.d4.id)["fields"]["title"],+ "wow_dec",+ )for element in manifest:- if element['model'] == 'documents.document':- fname = os.path.join(self.target, element[document_exporter.EXPORTER_FILE_NAME])+ if element["model"] == "documents.document":+ fname = os.path.join(+ self.target, element[document_exporter.EXPORTER_FILE_NAME]+ )self.assertTrue(os.path.exists(fname))- self.assertTrue(os.path.exists(os.path.join(self.target, element[document_exporter.EXPORTER_THUMBNAIL_NAME])))+ self.assertTrue(+ os.path.exists(+ os.path.join(+ self.target,+ element[document_exporter.EXPORTER_THUMBNAIL_NAME],+ )+ )+ )with open(fname, "rb") as f:checksum = hashlib.md5(f.read()).hexdigest()- self.assertEqual(checksum, element['fields']['checksum'])+ self.assertEqual(checksum, element["fields"]["checksum"])- self.assertEqual(element['fields']['storage_type'], Document.STORAGE_TYPE_UNENCRYPTED)+ self.assertEqual(+ element["fields"]["storage_type"], Document.STORAGE_TYPE_UNENCRYPTED+ )if document_exporter.EXPORTER_ARCHIVE_NAME in element:- fname = os.path.join(self.target, element[document_exporter.EXPORTER_ARCHIVE_NAME])+ fname = os.path.join(+ self.target, element[document_exporter.EXPORTER_ARCHIVE_NAME]+ )self.assertTrue(os.path.exists(fname))with open(fname, "rb") as f:checksum = hashlib.md5(f.read()).hexdigest()- self.assertEqual(checksum, element['fields']['archive_checksum'])+ self.assertEqual(checksum, element["fields"]["archive_checksum"])with paperless_environment() as dirs:self.assertEqual(Document.objects.count(), 4)@@ -107,7 +167,7 @@ class TestExportImport(DirectoriesMixin, TestCase):Tag.objects.all().delete()self.assertEqual(Document.objects.count(), 0)- call_command('document_importer', self.target)+ call_command("document_importer", self.target)self.assertEqual(Document.objects.count(), 4)self.assertEqual(Tag.objects.count(), 1)self.assertEqual(Correspondent.objects.count(), 1)@@ -122,21 +182,31 @@ class TestExportImport(DirectoriesMixin, TestCase):def test_exporter_with_filename_format(self):shutil.rmtree(os.path.join(self.dirs.media_dir, "documents"))- shutil.copytree(os.path.join(os.path.dirname(__file__), "samples", "documents"), os.path.join(self.dirs.media_dir, "documents"))-- with override_settings(PAPERLESS_FILENAME_FORMAT="{created_year}/{correspondent}/{title}"):+ shutil.copytree(+ os.path.join(os.path.dirname(__file__), "samples", "documents"),+ os.path.join(self.dirs.media_dir, "documents"),+ )++ with override_settings(+ PAPERLESS_FILENAME_FORMAT="{created_year}/{correspondent}/{title}"+ ):self.test_exporter(use_filename_format=True)def test_update_export_changed_time(self):shutil.rmtree(os.path.join(self.dirs.media_dir, "documents"))- shutil.copytree(os.path.join(os.path.dirname(__file__), "samples", "documents"), os.path.join(self.dirs.media_dir, "documents"))+ shutil.copytree(+ os.path.join(os.path.dirname(__file__), "samples", "documents"),+ os.path.join(self.dirs.media_dir, "documents"),+ )self._do_export()self.assertTrue(os.path.exists(os.path.join(self.target, "manifest.json")))st_mtime_1 = os.stat(os.path.join(self.target, "manifest.json")).st_mtime- with mock.patch("documents.management.commands.document_exporter.shutil.copy2") as m:+ with mock.patch(+ "documents.management.commands.document_exporter.shutil.copy2"+ ) as m:self._do_export()m.assert_not_called()@@ -145,7 +215,9 @@ class TestExportImport(DirectoriesMixin, TestCase):Path(self.d1.source_path).touch()- with mock.patch("documents.management.commands.document_exporter.shutil.copy2") as m:+ with mock.patch(+ "documents.management.commands.document_exporter.shutil.copy2"+ ) as m:self._do_export()self.assertEqual(m.call_count, 1)@@ -157,13 +229,18 @@ class TestExportImport(DirectoriesMixin, TestCase):def test_update_export_changed_checksum(self):shutil.rmtree(os.path.join(self.dirs.media_dir, "documents"))- shutil.copytree(os.path.join(os.path.dirname(__file__), "samples", "documents"), os.path.join(self.dirs.media_dir, "documents"))+ shutil.copytree(+ os.path.join(os.path.dirname(__file__), "samples", "documents"),+ os.path.join(self.dirs.media_dir, "documents"),+ )self._do_export()self.assertTrue(os.path.exists(os.path.join(self.target, "manifest.json")))- with mock.patch("documents.management.commands.document_exporter.shutil.copy2") as m:+ with mock.patch(+ "documents.management.commands.document_exporter.shutil.copy2"+ ) as m:self._do_export()m.assert_not_called()@@ -172,7 +249,9 @@ class TestExportImport(DirectoriesMixin, TestCase):self.d2.checksum = "asdfasdgf3"self.d2.save()- with mock.patch("documents.management.commands.document_exporter.shutil.copy2") as m:+ with mock.patch(+ "documents.management.commands.document_exporter.shutil.copy2"+ ) as m:self._do_export(compare_checksums=True)self.assertEqual(m.call_count, 1)@@ -180,28 +259,48 @@ class TestExportImport(DirectoriesMixin, TestCase):def test_update_export_deleted_document(self):shutil.rmtree(os.path.join(self.dirs.media_dir, "documents"))- shutil.copytree(os.path.join(os.path.dirname(__file__), "samples", "documents"), os.path.join(self.dirs.media_dir, "documents"))+ shutil.copytree(+ os.path.join(os.path.dirname(__file__), "samples", "documents"),+ os.path.join(self.dirs.media_dir, "documents"),+ )manifest = self._do_export()self.assertTrue(len(manifest), 7)doc_from_manifest = self._get_document_from_manifest(manifest, self.d3.id)- self.assertTrue(os.path.isfile(os.path.join(self.target, doc_from_manifest[EXPORTER_FILE_NAME])))+ self.assertTrue(+ os.path.isfile(+ os.path.join(self.target, doc_from_manifest[EXPORTER_FILE_NAME])+ )+ )self.d3.delete()manifest = self._do_export()- self.assertRaises(ValueError, self._get_document_from_manifest, manifest, self.d3.id)- self.assertTrue(os.path.isfile(os.path.join(self.target, doc_from_manifest[EXPORTER_FILE_NAME])))+ self.assertRaises(+ ValueError, self._get_document_from_manifest, manifest, self.d3.id+ )+ self.assertTrue(+ os.path.isfile(+ os.path.join(self.target, doc_from_manifest[EXPORTER_FILE_NAME])+ )+ )manifest = self._do_export(delete=True)- self.assertFalse(os.path.isfile(os.path.join(self.target, doc_from_manifest[EXPORTER_FILE_NAME])))+ self.assertFalse(+ os.path.isfile(+ os.path.join(self.target, doc_from_manifest[EXPORTER_FILE_NAME])+ )+ )self.assertTrue(len(manifest), 6)@override_settings(PAPERLESS_FILENAME_FORMAT="{title}/{correspondent}")def test_update_export_changed_location(self):shutil.rmtree(os.path.join(self.dirs.media_dir, "documents"))- shutil.copytree(os.path.join(os.path.dirname(__file__), "samples", "documents"), os.path.join(self.dirs.media_dir, "documents"))+ shutil.copytree(+ os.path.join(os.path.dirname(__file__), "samples", "documents"),+ os.path.join(self.dirs.media_dir, "documents"),+ )m = self._do_export(use_filename_format=True)self.assertTrue(os.path.isfile(os.path.join(self.target, "wow1", "c.pdf")))@@ -216,11 +315,18 @@ class TestExportImport(DirectoriesMixin, TestCase):self.assertTrue(os.path.isfile(os.path.join(self.target, "new_title", "c.pdf")))self.assertTrue(os.path.exists(os.path.join(self.target, "manifest.json")))self.assertTrue(os.path.isfile(os.path.join(self.target, "wow2", "none.pdf")))- self.assertTrue(os.path.isfile(os.path.join(self.target, "wow2", "none_01.pdf")))+ self.assertTrue(+ os.path.isfile(os.path.join(self.target, "wow2", "none_01.pdf"))+ )def test_export_missing_files(self):target = tempfile.mkdtemp()self.addCleanup(shutil.rmtree, target)- Document.objects.create(checksum="AAAAAAAAAAAAAAAAA", title="wow", filename="0000004.pdf", mime_type="application/pdf")- self.assertRaises(FileNotFoundError, call_command, 'document_exporter', target)+ Document.objects.create(+ checksum="AAAAAAAAAAAAAAAAA",+ title="wow",+ filename="0000004.pdf",+ mime_type="application/pdf",+ )+ self.assertRaises(FileNotFoundError, call_command, "document_exporter", target)diff --git a/src/documents/tests/test_management_retagger.py b/src/documents/tests/test_management_retagger.pyindex 39e9c80b7..77fc9d2ad 100644--- a/src/documents/tests/test_management_retagger.py+++ b/src/documents/tests/test_management_retagger.py@@ -6,44 +6,64 @@ from documents.tests.utils import DirectoriesMixinclass TestRetagger(DirectoriesMixin, TestCase):-def make_models(self):- self.d1 = Document.objects.create(checksum="A", title="A", content="first document")- self.d2 = Document.objects.create(checksum="B", title="B", content="second document")- self.d3 = Document.objects.create(checksum="C", title="C", content="unrelated document")- self.d4 = Document.objects.create(checksum="D", title="D", content="auto document")-- self.tag_first = Tag.objects.create(name="tag1", match="first", matching_algorithm=Tag.MATCH_ANY)- self.tag_second = Tag.objects.create(name="tag2", match="second", matching_algorithm=Tag.MATCH_ANY)+ self.d1 = Document.objects.create(+ checksum="A", title="A", content="first document"+ )+ self.d2 = Document.objects.create(+ checksum="B", title="B", content="second document"+ )+ self.d3 = Document.objects.create(+ checksum="C", title="C", content="unrelated document"+ )+ self.d4 = Document.objects.create(+ checksum="D", title="D", content="auto document"+ )++ self.tag_first = Tag.objects.create(+ name="tag1", match="first", matching_algorithm=Tag.MATCH_ANY+ )+ self.tag_second = Tag.objects.create(+ name="tag2", match="second", matching_algorithm=Tag.MATCH_ANY+ )self.tag_inbox = Tag.objects.create(name="test", is_inbox_tag=True)self.tag_no_match = Tag.objects.create(name="test2")- self.tag_auto = Tag.objects.create(name="tagauto", matching_algorithm=Tag.MATCH_AUTO)+ self.tag_auto = Tag.objects.create(+ name="tagauto", matching_algorithm=Tag.MATCH_AUTO+ )self.d3.tags.add(self.tag_inbox)self.d3.tags.add(self.tag_no_match)self.d4.tags.add(self.tag_auto)-self.correspondent_first = Correspondent.objects.create(- name="c1", match="first", matching_algorithm=Correspondent.MATCH_ANY)+ name="c1", match="first", matching_algorithm=Correspondent.MATCH_ANY+ )self.correspondent_second = Correspondent.objects.create(- name="c2", match="second", matching_algorithm=Correspondent.MATCH_ANY)+ name="c2", match="second", matching_algorithm=Correspondent.MATCH_ANY+ )self.doctype_first = DocumentType.objects.create(- name="dt1", match="first", matching_algorithm=DocumentType.MATCH_ANY)+ name="dt1", match="first", matching_algorithm=DocumentType.MATCH_ANY+ )self.doctype_second = DocumentType.objects.create(- name="dt2", match="second", matching_algorithm=DocumentType.MATCH_ANY)+ name="dt2", match="second", matching_algorithm=DocumentType.MATCH_ANY+ )def get_updated_docs(self):- return Document.objects.get(title="A"), Document.objects.get(title="B"), \- Document.objects.get(title="C"), Document.objects.get(title="D")+ return (+ Document.objects.get(title="A"),+ Document.objects.get(title="B"),+ Document.objects.get(title="C"),+ Document.objects.get(title="D"),+ )def setUp(self) -> None:super(TestRetagger, self).setUp()self.make_models()def test_add_tags(self):- call_command('document_retagger', '--tags')+ call_command("document_retagger", "--tags")d_first, d_second, d_unrelated, d_auto = self.get_updated_docs()self.assertEqual(d_first.tags.count(), 1)@@ -55,14 +75,14 @@ class TestRetagger(DirectoriesMixin, TestCase):self.assertEqual(d_second.tags.first(), self.tag_second)def test_add_type(self):- call_command('document_retagger', '--document_type')+ call_command("document_retagger", "--document_type")d_first, d_second, d_unrelated, d_auto = self.get_updated_docs()self.assertEqual(d_first.document_type, self.doctype_first)self.assertEqual(d_second.document_type, self.doctype_second)def test_add_correspondent(self):- call_command('document_retagger', '--correspondent')+ call_command("document_retagger", "--correspondent")d_first, d_second, d_unrelated, d_auto = self.get_updated_docs()self.assertEqual(d_first.correspondent, self.correspondent_first)@@ -71,19 +91,26 @@ class TestRetagger(DirectoriesMixin, TestCase):def test_overwrite_preserve_inbox(self):self.d1.tags.add(self.tag_second)- call_command('document_retagger', '--tags', '--overwrite')+ call_command("document_retagger", "--tags", "--overwrite")d_first, d_second, d_unrelated, d_auto = self.get_updated_docs()self.assertIsNotNone(Tag.objects.get(id=self.tag_second.id))- self.assertCountEqual([tag.id for tag in d_first.tags.all()], [self.tag_first.id])- self.assertCountEqual([tag.id for tag in d_second.tags.all()], [self.tag_second.id])- self.assertCountEqual([tag.id for tag in d_unrelated.tags.all()], [self.tag_inbox.id, self.tag_no_match.id])+ self.assertCountEqual(+ [tag.id for tag in d_first.tags.all()], [self.tag_first.id]+ )+ self.assertCountEqual(+ [tag.id for tag in d_second.tags.all()], [self.tag_second.id]+ )+ self.assertCountEqual(+ [tag.id for tag in d_unrelated.tags.all()],+ [self.tag_inbox.id, self.tag_no_match.id],+ )self.assertEqual(d_auto.tags.count(), 0)def test_add_tags_suggest(self):- call_command('document_retagger', '--tags', '--suggest')+ call_command("document_retagger", "--tags", "--suggest")d_first, d_second, d_unrelated, d_auto = self.get_updated_docs()self.assertEqual(d_first.tags.count(), 0)@@ -91,21 +118,23 @@ class TestRetagger(DirectoriesMixin, TestCase):self.assertEqual(d_auto.tags.count(), 1)def test_add_type_suggest(self):- call_command('document_retagger', '--document_type', '--suggest')+ call_command("document_retagger", "--document_type", "--suggest")d_first, d_second, d_unrelated, d_auto = self.get_updated_docs()self.assertEqual(d_first.document_type, None)self.assertEqual(d_second.document_type, None)def test_add_correspondent_suggest(self):- call_command('document_retagger', '--correspondent', '--suggest')+ call_command("document_retagger", "--correspondent", "--suggest")d_first, d_second, d_unrelated, d_auto = self.get_updated_docs()self.assertEqual(d_first.correspondent, None)self.assertEqual(d_second.correspondent, None)def test_add_tags_suggest_url(self):- call_command('document_retagger', '--tags', '--suggest', '--base-url=http://localhost')+ call_command(+ "document_retagger", "--tags", "--suggest", "--base-url=http://localhost"+ )d_first, d_second, d_unrelated, d_auto = self.get_updated_docs()self.assertEqual(d_first.tags.count(), 0)@@ -113,14 +142,24 @@ class TestRetagger(DirectoriesMixin, TestCase):self.assertEqual(d_auto.tags.count(), 1)def test_add_type_suggest_url(self):- call_command('document_retagger', '--document_type', '--suggest', '--base-url=http://localhost')+ call_command(+ "document_retagger",+ "--document_type",+ "--suggest",+ "--base-url=http://localhost",+ )d_first, d_second, d_unrelated, d_auto = self.get_updated_docs()self.assertEqual(d_first.document_type, None)self.assertEqual(d_second.document_type, None)def test_add_correspondent_suggest_url(self):- call_command('document_retagger', '--correspondent', '--suggest', '--base-url=http://localhost')+ call_command(+ "document_retagger",+ "--correspondent",+ "--suggest",+ "--base-url=http://localhost",+ )d_first, d_second, d_unrelated, d_auto = self.get_updated_docs()self.assertEqual(d_first.correspondent, None)diff --git a/src/documents/tests/test_management_superuser.py b/src/documents/tests/test_management_superuser.pyindex ca28db89c..fa62a9f14 100644--- a/src/documents/tests/test_management_superuser.py+++ b/src/documents/tests/test_management_superuser.py@@ -12,7 +12,6 @@ from documents.tests.utils import DirectoriesMixinclass TestManageSuperUser(DirectoriesMixin, TestCase):-def reset_environment(self):if "PAPERLESS_ADMIN_USER" in os.environ:del os.environ["PAPERLESS_ADMIN_USER"]diff --git a/src/documents/tests/test_management_thumbnails.py b/src/documents/tests/test_management_thumbnails.pyindex 7ecdf0489..6af94ce99 100644--- a/src/documents/tests/test_management_thumbnails.py+++ b/src/documents/tests/test_management_thumbnails.py@@ -11,13 +11,30 @@ from documents.tests.utils import DirectoriesMixinclass TestMakeThumbnails(DirectoriesMixin, TestCase):-def make_models(self):- self.d1 = Document.objects.create(checksum="A", title="A", content="first document", mime_type="application/pdf", filename="test.pdf")- shutil.copy(os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), self.d1.source_path)+ self.d1 = Document.objects.create(+ checksum="A",+ title="A",+ content="first document",+ mime_type="application/pdf",+ filename="test.pdf",+ )+ shutil.copy(+ os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),+ self.d1.source_path,+ )- self.d2 = Document.objects.create(checksum="Ass", title="A", content="first document", mime_type="application/pdf", filename="test2.pdf")- shutil.copy(os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), self.d2.source_path)+ self.d2 = Document.objects.create(+ checksum="Ass",+ title="A",+ content="first document",+ mime_type="application/pdf",+ filename="test2.pdf",+ )+ shutil.copy(+ os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),+ self.d2.source_path,+ )def setUp(self) -> None:super(TestMakeThumbnails, self).setUp()@@ -40,13 +57,13 @@ class TestMakeThumbnails(DirectoriesMixin, TestCase):def test_command(self):self.assertFalse(os.path.isfile(self.d1.thumbnail_path))self.assertFalse(os.path.isfile(self.d2.thumbnail_path))- call_command('document_thumbnails')+ call_command("document_thumbnails")self.assertTrue(os.path.isfile(self.d1.thumbnail_path))self.assertTrue(os.path.isfile(self.d2.thumbnail_path))def test_command_documentid(self):self.assertFalse(os.path.isfile(self.d1.thumbnail_path))self.assertFalse(os.path.isfile(self.d2.thumbnail_path))- call_command('document_thumbnails', '-d', f"{self.d1.id}")+ call_command("document_thumbnails", "-d", f"{self.d1.id}")self.assertTrue(os.path.isfile(self.d1.thumbnail_path))self.assertFalse(os.path.isfile(self.d2.thumbnail_path))diff --git a/src/documents/tests/test_matchables.py b/src/documents/tests/test_matchables.pyindex da0fa66ea..df47db9ba 100644--- a/src/documents/tests/test_matchables.py+++ b/src/documents/tests/test_matchables.py@@ -12,25 +12,24 @@ from ..signals import document_consumption_finishedclass TestMatching(TestCase):-def _test_matching(self, text, algorithm, true, false):for klass in (Tag, Correspondent, DocumentType):instance = klass.objects.create(name=str(randint(10000, 99999)),match=text,- matching_algorithm=getattr(klass, algorithm)+ matching_algorithm=getattr(klass, algorithm),)for string in true:doc = Document(content=string)self.assertTrue(matching.matches(instance, doc),- '"%s" should match "%s" but it does not' % (text, string)+ '"%s" should match "%s" but it does not' % (text, string),)for string in false:doc = Document(content=string)self.assertFalse(matching.matches(instance, doc),- '"%s" should not match "%s" but it does' % (text, string)+ '"%s" should not match "%s" but it does' % (text, string),)def test_match_all(self):@@ -47,15 +46,13 @@ class TestMatching(TestCase):"I have alphas, charlie, and gamma in me","I have alphas in me","I have bravo in me",- )+ ),)self._test_matching("12 34 56","MATCH_ALL",- (- "I have 12 34, and 56 in me",- ),+ ("I have 12 34, and 56 in me",),("I have 12 in me","I have 34 in me",@@ -64,7 +61,7 @@ class TestMatching(TestCase):"I have 120, 34, and 56 in me","I have 123456 in me","I have 01234567 in me",- )+ ),)self._test_matching(@@ -79,7 +76,7 @@ class TestMatching(TestCase):"the quick brown wolf jumped over the lazy dogs","the quick brown fox jumped over the fat dogs","the quick brown fox jumped over the lazy... dogs",- )+ ),)def test_match_any(self):@@ -97,7 +94,7 @@ class TestMatching(TestCase):("I have alphas in me","I have bravo in me",- )+ ),)self._test_matching(@@ -114,7 +111,7 @@ class TestMatching(TestCase):("I have 123456 in me","I have 01234567 in me",- )+ ),)self._test_matching(@@ -124,9 +121,7 @@ class TestMatching(TestCase):"the quick brown fox","jumped over the lazy dogs.",),- (- "the lazy fox jumped over the brown dogs",- )+ ("the lazy fox jumped over the brown dogs",),)def test_match_literal(self):@@ -134,9 +129,7 @@ class TestMatching(TestCase):self._test_matching("alpha charlie gamma","MATCH_LITERAL",- (- "I have 'alpha charlie gamma' in me",- ),+ ("I have 'alpha charlie gamma' in me",),("I have alpha in me","I have charlie in me",@@ -146,15 +139,13 @@ class TestMatching(TestCase):"I have alphas, charlie, and gamma in me","I have alphas in me","I have bravo in me",- )+ ),)self._test_matching("12 34 56","MATCH_LITERAL",- (- "I have 12 34 56 in me",- ),+ ("I have 12 34 56 in me",),("I have 12 in me","I have 34 in me",@@ -165,7 +156,7 @@ class TestMatching(TestCase):"I have 120, 340, and 560 in me","I have 123456 in me","I have 01234567 in me",- )+ ),)def test_match_regex(self):@@ -186,18 +177,11 @@ class TestMatching(TestCase):"I have alpha, charlie, and gamma in me","I have alphas, charlie, and gamma in me","I have alphas in me",- )+ ),)def test_tach_invalid_regex(self):- self._test_matching(- "[[",- "MATCH_REGEX",- [],- [- "Don't match this"- ]- )+ self._test_matching("[[", "MATCH_REGEX", [], ["Don't match this"])def test_match_fuzzy(self):@@ -210,9 +194,7 @@ class TestMatching(TestCase):"1220 Main Street, Springfeld, Miss.","1220 Main Street Springfield Miss",),- (- "1220 Main Street, Springfield, Mich.",- )+ ("1220 Main Street, Springfield, Mich.",),)@@ -225,9 +207,10 @@ class TestDocumentConsumptionFinishedSignal(TestCase):def setUp(self):TestCase.setUp(self)- User.objects.create_user(username='test_consumer', password='12345')+ User.objects.create_user(username="test_consumer", password="12345")self.doc_contains = Document.objects.create(- content="I contain the keyword.", mime_type="application/pdf")+ content="I contain the keyword.", mime_type="application/pdf"+ )self.index_dir = tempfile.mkdtemp()# TODO: we should not need the index here.@@ -238,40 +221,43 @@ class TestDocumentConsumptionFinishedSignal(TestCase):def test_tag_applied_any(self):t1 = Tag.objects.create(- name="test", match="keyword", matching_algorithm=Tag.MATCH_ANY)+ name="test", match="keyword", matching_algorithm=Tag.MATCH_ANY+ )document_consumption_finished.send(- sender=self.__class__, document=self.doc_contains)+ sender=self.__class__, document=self.doc_contains+ )self.assertTrue(list(self.doc_contains.tags.all()) == [t1])def test_tag_not_applied(self):Tag.objects.create(- name="test", match="no-match", matching_algorithm=Tag.MATCH_ANY)+ name="test", match="no-match", matching_algorithm=Tag.MATCH_ANY+ )document_consumption_finished.send(- sender=self.__class__, document=self.doc_contains)+ sender=self.__class__, document=self.doc_contains+ )self.assertTrue(list(self.doc_contains.tags.all()) == [])def test_correspondent_applied(self):correspondent = Correspondent.objects.create(- name="test",- match="keyword",- matching_algorithm=Correspondent.MATCH_ANY+ name="test", match="keyword", matching_algorithm=Correspondent.MATCH_ANY)document_consumption_finished.send(- sender=self.__class__, document=self.doc_contains)+ sender=self.__class__, document=self.doc_contains+ )self.assertTrue(self.doc_contains.correspondent == correspondent)def test_correspondent_not_applied(self):Tag.objects.create(- name="test",- match="no-match",- matching_algorithm=Correspondent.MATCH_ANY+ name="test", match="no-match", matching_algorithm=Correspondent.MATCH_ANY)document_consumption_finished.send(- sender=self.__class__, document=self.doc_contains)+ sender=self.__class__, document=self.doc_contains+ )self.assertEqual(self.doc_contains.correspondent, None)def test_logentry_created(self):document_consumption_finished.send(- sender=self.__class__, document=self.doc_contains)+ sender=self.__class__, document=self.doc_contains+ )self.assertEqual(LogEntry.objects.count(), 1)diff --git a/src/documents/tests/test_migration_archive_files.py b/src/documents/tests/test_migration_archive_files.pyindex 6217ae05f..97f8899bc 100644--- a/src/documents/tests/test_migration_archive_files.py+++ b/src/documents/tests/test_migration_archive_files.py@@ -24,20 +24,14 @@ def archive_path_old(self):else:fname = "{:07}.pdf".format(self.pk)- return os.path.join(- settings.ARCHIVE_DIR,- fname- )+ return os.path.join(settings.ARCHIVE_DIR, fname)def archive_path_new(doc):- if doc.archive_filename is not None:- return os.path.join(- settings.ARCHIVE_DIR,- str(doc.archive_filename)- )- else:- return None+ if doc.archive_filename is not None:+ return os.path.join(settings.ARCHIVE_DIR, str(doc.archive_filename))+ else:+ return Nonedef source_path(doc):@@ -48,10 +42,7 @@ def source_path(doc):if doc.storage_type == STORAGE_TYPE_GPG:fname += ".gpg" # pragma: no cover- return os.path.join(- settings.ORIGINALS_DIR,- fname- )+ return os.path.join(settings.ORIGINALS_DIR, fname)def thumbnail_path(doc):@@ -59,13 +50,18 @@ def thumbnail_path(doc):if doc.storage_type == STORAGE_TYPE_GPG:file_name += ".gpg"- return os.path.join(- settings.THUMBNAIL_DIR,- file_name- )+ return os.path.join(settings.THUMBNAIL_DIR, file_name)-def make_test_document(document_class, title: str, mime_type: str, original: str, original_filename: str, archive: str = None, archive_filename: str = None):+def make_test_document(+ document_class,+ title: str,+ mime_type: str,+ original: str,+ original_filename: str,+ archive: str = None,+ archive_filename: str = None,+):doc = document_class()doc.filename = original_filenamedoc.title = title@@ -96,8 +92,12 @@ def make_test_document(document_class, title: str, mime_type: str, original: strsimple_jpg = os.path.join(os.path.dirname(__file__), "samples", "simple.jpg")simple_pdf = os.path.join(os.path.dirname(__file__), "samples", "simple.pdf")-simple_pdf2 = os.path.join(os.path.dirname(__file__), "samples", "documents", "originals", "0000002.pdf")-simple_pdf3 = os.path.join(os.path.dirname(__file__), "samples", "documents", "originals", "0000003.pdf")+simple_pdf2 = os.path.join(+ os.path.dirname(__file__), "samples", "documents", "originals", "0000002.pdf"+)+simple_pdf3 = os.path.join(+ os.path.dirname(__file__), "samples", "documents", "originals", "0000003.pdf"+)simple_txt = os.path.join(os.path.dirname(__file__), "samples", "simple.txt")simple_png = os.path.join(os.path.dirname(__file__), "samples", "simple-noalpha.png")simple_png2 = os.path.join(os.path.dirname(__file__), "examples", "no-text.png")@@ -106,26 +106,52 @@ simple_png2 = os.path.join(os.path.dirname(__file__), "examples", "no-text.png")@override_settings(PAPERLESS_FILENAME_FORMAT="")class TestMigrateArchiveFiles(DirectoriesMixin, TestMigrations):- migrate_from = '1011_auto_20210101_2340'- migrate_to = '1012_fix_archive_files'+ migrate_from = "1011_auto_20210101_2340"+ migrate_to = "1012_fix_archive_files"def setUpBeforeMigration(self, apps):Document = apps.get_model("documents", "Document")- self.unrelated = make_test_document(Document, "unrelated", "application/pdf", simple_pdf3, "unrelated.pdf", simple_pdf)- self.no_text = make_test_document(Document, "no-text", "image/png", simple_png2, "no-text.png", simple_pdf)- self.doc_no_archive = make_test_document(Document, "no_archive", "text/plain", simple_txt, "no_archive.txt")- self.clash1 = make_test_document(Document, "clash", "application/pdf", simple_pdf, "clash.pdf", simple_pdf)- self.clash2 = make_test_document(Document, "clash", "image/jpeg", simple_jpg, "clash.jpg", simple_pdf)- self.clash3 = make_test_document(Document, "clash", "image/png", simple_png, "clash.png", simple_pdf)- self.clash4 = make_test_document(Document, "clash.png", "application/pdf", simple_pdf2, "clash.png.pdf", simple_pdf2)+ self.unrelated = make_test_document(+ Document,+ "unrelated",+ "application/pdf",+ simple_pdf3,+ "unrelated.pdf",+ simple_pdf,+ )+ self.no_text = make_test_document(+ Document, "no-text", "image/png", simple_png2, "no-text.png", simple_pdf+ )+ self.doc_no_archive = make_test_document(+ Document, "no_archive", "text/plain", simple_txt, "no_archive.txt"+ )+ self.clash1 = make_test_document(+ Document, "clash", "application/pdf", simple_pdf, "clash.pdf", simple_pdf+ )+ self.clash2 = make_test_document(+ Document, "clash", "image/jpeg", simple_jpg, "clash.jpg", simple_pdf+ )+ self.clash3 = make_test_document(+ Document, "clash", "image/png", simple_png, "clash.png", simple_pdf+ )+ self.clash4 = make_test_document(+ Document,+ "clash.png",+ "application/pdf",+ simple_pdf2,+ "clash.png.pdf",+ simple_pdf2,+ )self.assertEqual(archive_path_old(self.clash1), archive_path_old(self.clash2))self.assertEqual(archive_path_old(self.clash1), archive_path_old(self.clash3))- self.assertNotEqual(archive_path_old(self.clash1), archive_path_old(self.clash4))+ self.assertNotEqual(+ archive_path_old(self.clash1), archive_path_old(self.clash4)+ )def testArchiveFilesMigrated(self):- Document = self.apps.get_model('documents', 'Document')+ Document = self.apps.get_model("documents", "Document")for doc in Document.objects.all():if doc.archive_checksum:@@ -144,31 +170,65 @@ class TestMigrateArchiveFiles(DirectoriesMixin, TestMigrations):archive_checksum = hashlib.md5(f.read()).hexdigest()self.assertEqual(archive_checksum, doc.archive_checksum)- self.assertEqual(Document.objects.filter(archive_checksum__isnull=False).count(), 6)+ self.assertEqual(+ Document.objects.filter(archive_checksum__isnull=False).count(), 6+ )def test_filenames(self):- Document = self.apps.get_model('documents', 'Document')- self.assertEqual(Document.objects.get(id=self.unrelated.id).archive_filename, "unrelated.pdf")- self.assertEqual(Document.objects.get(id=self.no_text.id).archive_filename, "no-text.pdf")- self.assertEqual(Document.objects.get(id=self.doc_no_archive.id).archive_filename, None)- self.assertEqual(Document.objects.get(id=self.clash1.id).archive_filename, f"{self.clash1.id:07}.pdf")- self.assertEqual(Document.objects.get(id=self.clash2.id).archive_filename, f"{self.clash2.id:07}.pdf")- self.assertEqual(Document.objects.get(id=self.clash3.id).archive_filename, f"{self.clash3.id:07}.pdf")- self.assertEqual(Document.objects.get(id=self.clash4.id).archive_filename, "clash.png.pdf")+ Document = self.apps.get_model("documents", "Document")+ self.assertEqual(+ Document.objects.get(id=self.unrelated.id).archive_filename, "unrelated.pdf"+ )+ self.assertEqual(+ Document.objects.get(id=self.no_text.id).archive_filename, "no-text.pdf"+ )+ self.assertEqual(+ Document.objects.get(id=self.doc_no_archive.id).archive_filename, None+ )+ self.assertEqual(+ Document.objects.get(id=self.clash1.id).archive_filename,+ f"{self.clash1.id:07}.pdf",+ )+ self.assertEqual(+ Document.objects.get(id=self.clash2.id).archive_filename,+ f"{self.clash2.id:07}.pdf",+ )+ self.assertEqual(+ Document.objects.get(id=self.clash3.id).archive_filename,+ f"{self.clash3.id:07}.pdf",+ )+ self.assertEqual(+ Document.objects.get(id=self.clash4.id).archive_filename, "clash.png.pdf"+ )@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{title}")class TestMigrateArchiveFilesWithFilenameFormat(TestMigrateArchiveFiles):-def test_filenames(self):- Document = self.apps.get_model('documents', 'Document')- self.assertEqual(Document.objects.get(id=self.unrelated.id).archive_filename, "unrelated.pdf")- self.assertEqual(Document.objects.get(id=self.no_text.id).archive_filename, "no-text.pdf")- self.assertEqual(Document.objects.get(id=self.doc_no_archive.id).archive_filename, None)- self.assertEqual(Document.objects.get(id=self.clash1.id).archive_filename, "none/clash.pdf")- self.assertEqual(Document.objects.get(id=self.clash2.id).archive_filename, "none/clash_01.pdf")- self.assertEqual(Document.objects.get(id=self.clash3.id).archive_filename, "none/clash_02.pdf")- self.assertEqual(Document.objects.get(id=self.clash4.id).archive_filename, "clash.png.pdf")+ Document = self.apps.get_model("documents", "Document")+ self.assertEqual(+ Document.objects.get(id=self.unrelated.id).archive_filename, "unrelated.pdf"+ )+ self.assertEqual(+ Document.objects.get(id=self.no_text.id).archive_filename, "no-text.pdf"+ )+ self.assertEqual(+ Document.objects.get(id=self.doc_no_archive.id).archive_filename, None+ )+ self.assertEqual(+ Document.objects.get(id=self.clash1.id).archive_filename, "none/clash.pdf"+ )+ self.assertEqual(+ Document.objects.get(id=self.clash2.id).archive_filename,+ "none/clash_01.pdf",+ )+ self.assertEqual(+ Document.objects.get(id=self.clash3.id).archive_filename,+ "none/clash_02.pdf",+ )+ self.assertEqual(+ Document.objects.get(id=self.clash4.id).archive_filename, "clash.png.pdf"+ )def fake_parse_wrapper(parser, path, mime_type, file_name):@@ -179,34 +239,63 @@ def fake_parse_wrapper(parser, path, mime_type, file_name):@override_settings(PAPERLESS_FILENAME_FORMAT="")class TestMigrateArchiveFilesErrors(DirectoriesMixin, TestMigrations):- migrate_from = '1011_auto_20210101_2340'- migrate_to = '1012_fix_archive_files'+ migrate_from = "1011_auto_20210101_2340"+ migrate_to = "1012_fix_archive_files"auto_migrate = Falsedef test_archive_missing(self):Document = self.apps.get_model("documents", "Document")- doc = make_test_document(Document, "clash", "application/pdf", simple_pdf, "clash.pdf", simple_pdf)+ doc = make_test_document(+ Document, "clash", "application/pdf", simple_pdf, "clash.pdf", simple_pdf+ )os.unlink(archive_path_old(doc))- self.assertRaisesMessage(ValueError, "does not exist at: ", self.performMigration)+ self.assertRaisesMessage(+ ValueError, "does not exist at: ", self.performMigration+ )def test_parser_missing(self):Document = self.apps.get_model("documents", "Document")- doc1 = make_test_document(Document, "document", "invalid/typesss768", simple_png, "document.png", simple_pdf)- doc2 = make_test_document(Document, "document", "invalid/typesss768", simple_jpg, "document.jpg", simple_pdf)-- self.assertRaisesMessage(ValueError, "no parsers are available", self.performMigration)+ doc1 = make_test_document(+ Document,+ "document",+ "invalid/typesss768",+ simple_png,+ "document.png",+ simple_pdf,+ )+ doc2 = make_test_document(+ Document,+ "document",+ "invalid/typesss768",+ simple_jpg,+ "document.jpg",+ simple_pdf,+ )++ self.assertRaisesMessage(+ ValueError, "no parsers are available", self.performMigration+ )@mock.patch("documents.migrations.1012_fix_archive_files.parse_wrapper")def test_parser_error(self, m):m.side_effect = ParseError()Document = self.apps.get_model("documents", "Document")- doc1 = make_test_document(Document, "document", "image/png", simple_png, "document.png", simple_pdf)- doc2 = make_test_document(Document, "document", "application/pdf", simple_jpg, "document.jpg", simple_pdf)+ doc1 = make_test_document(+ Document, "document", "image/png", simple_png, "document.png", simple_pdf+ )+ doc2 = make_test_document(+ Document,+ "document",+ "application/pdf",+ simple_jpg,+ "document.jpg",+ simple_pdf,+ )self.assertIsNotNone(doc1.archive_checksum)self.assertIsNotNone(doc2.archive_checksum)@@ -217,12 +306,29 @@ class TestMigrateArchiveFilesErrors(DirectoriesMixin, TestMigrations):self.assertEqual(m.call_count, 6)self.assertEqual(- len(list(filter(lambda log: "Parse error, will try again in 5 seconds" in log, capture.output))),- 4)+ len(+ list(+ filter(+ lambda log: "Parse error, will try again in 5 seconds" in log,+ capture.output,+ )+ )+ ),+ 4,+ )self.assertEqual(- len(list(filter(lambda log: "Unable to regenerate archive document for ID:" in log, capture.output))),- 2)+ len(+ list(+ filter(+ lambda log: "Unable to regenerate archive document for ID:"+ in log,+ capture.output,+ )+ )+ ),+ 2,+ )Document = self.apps.get_model("documents", "Document")@@ -240,15 +346,33 @@ class TestMigrateArchiveFilesErrors(DirectoriesMixin, TestMigrations):Document = self.apps.get_model("documents", "Document")- doc1 = make_test_document(Document, "document", "image/png", simple_png, "document.png", simple_pdf)- doc2 = make_test_document(Document, "document", "application/pdf", simple_jpg, "document.jpg", simple_pdf)+ doc1 = make_test_document(+ Document, "document", "image/png", simple_png, "document.png", simple_pdf+ )+ doc2 = make_test_document(+ Document,+ "document",+ "application/pdf",+ simple_jpg,+ "document.jpg",+ simple_pdf,+ )with self.assertLogs() as capture:self.performMigration()self.assertEqual(- len(list(filter(lambda log: "Parser did not return an archive document for document" in log, capture.output))),- 2)+ len(+ list(+ filter(+ lambda log: "Parser did not return an archive document for document"+ in log,+ capture.output,+ )+ )+ ),+ 2,+ )Document = self.apps.get_model("documents", "Document")@@ -264,19 +388,37 @@ class TestMigrateArchiveFilesErrors(DirectoriesMixin, TestMigrations):@override_settings(PAPERLESS_FILENAME_FORMAT="")class TestMigrateArchiveFilesBackwards(DirectoriesMixin, TestMigrations):- migrate_from = '1012_fix_archive_files'- migrate_to = '1011_auto_20210101_2340'+ migrate_from = "1012_fix_archive_files"+ migrate_to = "1011_auto_20210101_2340"def setUpBeforeMigration(self, apps):Document = apps.get_model("documents", "Document")- doc_unrelated = make_test_document(Document, "unrelated", "application/pdf", simple_pdf2, "unrelated.txt", simple_pdf2, "unrelated.pdf")- doc_no_archive = make_test_document(Document, "no_archive", "text/plain", simple_txt, "no_archive.txt")- clashB = make_test_document(Document, "clash", "image/jpeg", simple_jpg, "clash.jpg", simple_pdf, "clash_02.pdf")+ doc_unrelated = make_test_document(+ Document,+ "unrelated",+ "application/pdf",+ simple_pdf2,+ "unrelated.txt",+ simple_pdf2,+ "unrelated.pdf",+ )+ doc_no_archive = make_test_document(+ Document, "no_archive", "text/plain", simple_txt, "no_archive.txt"+ )+ clashB = make_test_document(+ Document,+ "clash",+ "image/jpeg",+ simple_jpg,+ "clash.jpg",+ simple_pdf,+ "clash_02.pdf",+ )def testArchiveFilesReverted(self):- Document = self.apps.get_model('documents', 'Document')+ Document = self.apps.get_model("documents", "Document")for doc in Document.objects.all():if doc.archive_checksum:@@ -291,35 +433,77 @@ class TestMigrateArchiveFilesBackwards(DirectoriesMixin, TestMigrations):archive_checksum = hashlib.md5(f.read()).hexdigest()self.assertEqual(archive_checksum, doc.archive_checksum)- self.assertEqual(Document.objects.filter(archive_checksum__isnull=False).count(), 2)+ self.assertEqual(+ Document.objects.filter(archive_checksum__isnull=False).count(), 2+ )@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{title}")-class TestMigrateArchiveFilesBackwardsWithFilenameFormat(TestMigrateArchiveFilesBackwards):+class TestMigrateArchiveFilesBackwardsWithFilenameFormat(+ TestMigrateArchiveFilesBackwards+):pass@override_settings(PAPERLESS_FILENAME_FORMAT="")class TestMigrateArchiveFilesBackwardsErrors(DirectoriesMixin, TestMigrations):- migrate_from = '1012_fix_archive_files'- migrate_to = '1011_auto_20210101_2340'+ migrate_from = "1012_fix_archive_files"+ migrate_to = "1011_auto_20210101_2340"auto_migrate = Falsedef test_filename_clash(self):Document = self.apps.get_model("documents", "Document")- self.clashA = make_test_document(Document, "clash", "application/pdf", simple_pdf, "clash.pdf", simple_pdf, "clash_02.pdf")- self.clashB = make_test_document(Document, "clash", "image/jpeg", simple_jpg, "clash.jpg", simple_pdf, "clash_01.pdf")-- self.assertRaisesMessage(ValueError, "would clash with another archive filename", self.performMigration)+ self.clashA = make_test_document(+ Document,+ "clash",+ "application/pdf",+ simple_pdf,+ "clash.pdf",+ simple_pdf,+ "clash_02.pdf",+ )+ self.clashB = make_test_document(+ Document,+ "clash",+ "image/jpeg",+ simple_jpg,+ "clash.jpg",+ simple_pdf,+ "clash_01.pdf",+ )++ self.assertRaisesMessage(+ ValueError,+ "would clash with another archive filename",+ self.performMigration,+ )def test_filename_exists(self):Document = self.apps.get_model("documents", "Document")- self.clashA = make_test_document(Document, "clash", "application/pdf", simple_pdf, "clash.pdf", simple_pdf, "clash.pdf")- self.clashB = make_test_document(Document, "clash", "image/jpeg", simple_jpg, "clash.jpg", simple_pdf, "clash_01.pdf")-- self.assertRaisesMessage(ValueError, "file already exists.", self.performMigration)+ self.clashA = make_test_document(+ Document,+ "clash",+ "application/pdf",+ simple_pdf,+ "clash.pdf",+ simple_pdf,+ "clash.pdf",+ )+ self.clashB = make_test_document(+ Document,+ "clash",+ "image/jpeg",+ simple_jpg,+ "clash.jpg",+ simple_pdf,+ "clash_01.pdf",+ )++ self.assertRaisesMessage(+ ValueError, "file already exists.", self.performMigration+ )diff --git a/src/documents/tests/test_migration_mime_type.py b/src/documents/tests/test_migration_mime_type.pyindex 5e825e89d..57cb84ad4 100644--- a/src/documents/tests/test_migration_mime_type.py+++ b/src/documents/tests/test_migration_mime_type.py@@ -19,10 +19,7 @@ def source_path_before(self):if self.storage_type == STORAGE_TYPE_GPG:fname += ".gpg"- return os.path.join(- settings.ORIGINALS_DIR,- fname- )+ return os.path.join(settings.ORIGINALS_DIR, fname)def file_type_after(self):@@ -37,30 +34,43 @@ def source_path_after(doc):if doc.storage_type == STORAGE_TYPE_GPG:fname += ".gpg" # pragma: no cover- return os.path.join(- settings.ORIGINALS_DIR,- fname- )+ return os.path.join(settings.ORIGINALS_DIR, fname)@override_settings(PASSPHRASE="test")class TestMigrateMimeType(DirectoriesMixin, TestMigrations):- migrate_from = '1002_auto_20201111_1105'- migrate_to = '1003_mime_types'+ migrate_from = "1002_auto_20201111_1105"+ migrate_to = "1003_mime_types"def setUpBeforeMigration(self, apps):Document = apps.get_model("documents", "Document")- doc = Document.objects.create(title="test", file_type="pdf", filename="file1.pdf")+ doc = Document.objects.create(+ title="test", file_type="pdf", filename="file1.pdf"+ )self.doc_id = doc.id- shutil.copy(os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), source_path_before(doc))-- doc2 = Document.objects.create(checksum="B", file_type="pdf", storage_type=STORAGE_TYPE_GPG)+ shutil.copy(+ os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),+ source_path_before(doc),+ )++ doc2 = Document.objects.create(+ checksum="B", file_type="pdf", storage_type=STORAGE_TYPE_GPG+ )self.doc2_id = doc2.id- shutil.copy(os.path.join(os.path.dirname(__file__), "samples", "documents", "originals", "0000004.pdf.gpg"), source_path_before(doc2))+ shutil.copy(+ os.path.join(+ os.path.dirname(__file__),+ "samples",+ "documents",+ "originals",+ "0000004.pdf.gpg",+ ),+ source_path_before(doc2),+ )def testMimeTypesMigrated(self):- Document = self.apps.get_model('documents', 'Document')+ Document = self.apps.get_model("documents", "Document")doc = Document.objects.get(id=self.doc_id)self.assertEqual(doc.mime_type, "application/pdf")@@ -72,17 +82,22 @@ class TestMigrateMimeType(DirectoriesMixin, TestMigrations):@override_settings(PASSPHRASE="test")class TestMigrateMimeTypeBackwards(DirectoriesMixin, TestMigrations):- migrate_from = '1003_mime_types'- migrate_to = '1002_auto_20201111_1105'+ migrate_from = "1003_mime_types"+ migrate_to = "1002_auto_20201111_1105"def setUpBeforeMigration(self, apps):Document = apps.get_model("documents", "Document")- doc = Document.objects.create(title="test", mime_type="application/pdf", filename="file1.pdf")+ doc = Document.objects.create(+ title="test", mime_type="application/pdf", filename="file1.pdf"+ )self.doc_id = doc.id- shutil.copy(os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), source_path_after(doc))+ shutil.copy(+ os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),+ source_path_after(doc),+ )def testMimeTypesReverted(self):- Document = self.apps.get_model('documents', 'Document')+ Document = self.apps.get_model("documents", "Document")doc = Document.objects.get(id=self.doc_id)self.assertEqual(doc.file_type, "pdf")diff --git a/src/documents/tests/test_migration_remove_null_characters.py b/src/documents/tests/test_migration_remove_null_characters.pyindex ba6f18539..9c8000550 100644--- a/src/documents/tests/test_migration_remove_null_characters.py+++ b/src/documents/tests/test_migration_remove_null_characters.py@@ -3,13 +3,13 @@ from documents.tests.utils import DirectoriesMixin, TestMigrationsclass TestMigrateNullCharacters(DirectoriesMixin, TestMigrations):- migrate_from = '1014_auto_20210228_1614'- migrate_to = '1015_remove_null_characters'+ migrate_from = "1014_auto_20210228_1614"+ migrate_to = "1015_remove_null_characters"def setUpBeforeMigration(self, apps):Document = apps.get_model("documents", "Document")self.doc = Document.objects.create(content="aaa\0bbb")def testMimeTypesMigrated(self):- Document = self.apps.get_model('documents', 'Document')+ Document = self.apps.get_model("documents", "Document")self.assertNotIn("\0", Document.objects.get(id=self.doc.id).content)diff --git a/src/documents/tests/test_migration_tag_colors.py b/src/documents/tests/test_migration_tag_colors.pyindex 2c4b35925..e209ce5e6 100644--- a/src/documents/tests/test_migration_tag_colors.py+++ b/src/documents/tests/test_migration_tag_colors.py@@ -3,8 +3,8 @@ from documents.tests.utils import DirectoriesMixin, TestMigrationsclass TestMigrateTagColor(DirectoriesMixin, TestMigrations):- migrate_from = '1012_fix_archive_files'- migrate_to = '1013_migrate_tag_colour'+ migrate_from = "1012_fix_archive_files"+ migrate_to = "1013_migrate_tag_colour"def setUpBeforeMigration(self, apps):Tag = apps.get_model("documents", "Tag")@@ -13,7 +13,7 @@ class TestMigrateTagColor(DirectoriesMixin, TestMigrations):self.t3_id = Tag.objects.create(name="tag3", colour=5).iddef testMimeTypesMigrated(self):- Tag = self.apps.get_model('documents', 'Tag')+ Tag = self.apps.get_model("documents", "Tag")self.assertEqual(Tag.objects.get(id=self.t1_id).color, "#a6cee3")self.assertEqual(Tag.objects.get(id=self.t2_id).color, "#a6cee3")self.assertEqual(Tag.objects.get(id=self.t3_id).color, "#fb9a99")@@ -21,8 +21,8 @@ class TestMigrateTagColor(DirectoriesMixin, TestMigrations):class TestMigrateTagColorBackwards(DirectoriesMixin, TestMigrations):- migrate_from = '1013_migrate_tag_colour'- migrate_to = '1012_fix_archive_files'+ migrate_from = "1013_migrate_tag_colour"+ migrate_to = "1012_fix_archive_files"def setUpBeforeMigration(self, apps):Tag = apps.get_model("documents", "Tag")@@ -31,7 +31,7 @@ class TestMigrateTagColorBackwards(DirectoriesMixin, TestMigrations):self.t3_id = Tag.objects.create(name="tag3", color="#123456").iddef testMimeTypesReverted(self):- Tag = self.apps.get_model('documents', 'Tag')+ Tag = self.apps.get_model("documents", "Tag")self.assertEqual(Tag.objects.get(id=self.t1_id).colour, 1)self.assertEqual(Tag.objects.get(id=self.t2_id).colour, 9)self.assertEqual(Tag.objects.get(id=self.t3_id).colour, 1)diff --git a/src/documents/tests/test_models.py b/src/documents/tests/test_models.pyindex 37b088c7f..77bb507f5 100644--- a/src/documents/tests/test_models.py+++ b/src/documents/tests/test_models.py@@ -5,7 +5,6 @@ from ..models import Document, Correspondentclass CorrespondentTestCase(TestCase):-def test___str__(self):for s in ("test", "οχι", "test with fun_charÅc'\"terß"):correspondent = CorrespondentFactory.create(name=s)@@ -13,7 +12,6 @@ class CorrespondentTestCase(TestCase):class DocumentTestCase(TestCase):-def test_correspondent_deletion_does_not_cascade(self):self.assertEqual(Correspondent.objects.all().count(), 0)diff --git a/src/documents/tests/test_parsers.py b/src/documents/tests/test_parsers.pyindex 9dd74313f..a914bbf93 100644--- a/src/documents/tests/test_parsers.py+++ b/src/documents/tests/test_parsers.py@@ -6,8 +6,14 @@ from unittest import mockfrom django.test import TestCase, override_settings-from documents.parsers import get_parser_class, get_supported_file_extensions, get_default_file_extension, \- get_parser_class_for_mime_type, DocumentParser, is_file_ext_supported+from documents.parsers import (+ get_parser_class,+ get_supported_file_extensions,+ get_default_file_extension,+ get_parser_class_for_mime_type,+ DocumentParser,+ is_file_ext_supported,+)from paperless_tesseract.parsers import RasterisedDocumentParserfrom paperless_text.parsers import TextDocumentParser@@ -25,24 +31,26 @@ def fake_magic_from_file(file, mime=False):@mock.patch("documents.parsers.magic.from_file", fake_magic_from_file)class TestParserDiscovery(TestCase):-@mock.patch("documents.parsers.document_consumer_declaration.send")def test__get_parser_class_1_parser(self, m, *args):class DummyParser(object):passm.return_value = (- (None, {"weight": 0, "parser": DummyParser, "mime_types": {"application/pdf": ".pdf"}}),+ (+ None,+ {+ "weight": 0,+ "parser": DummyParser,+ "mime_types": {"application/pdf": ".pdf"},+ },+ ),)- self.assertEqual(- get_parser_class("doc.pdf"),- DummyParser- )+ self.assertEqual(get_parser_class("doc.pdf"), DummyParser)@mock.patch("documents.parsers.document_consumer_declaration.send")def test__get_parser_class_n_parsers(self, m, *args):-class DummyParser1(object):pass@@ -50,22 +58,31 @@ class TestParserDiscovery(TestCase):passm.return_value = (- (None, {"weight": 0, "parser": DummyParser1, "mime_types": {"application/pdf": ".pdf"}}),- (None, {"weight": 1, "parser": DummyParser2, "mime_types": {"application/pdf": ".pdf"}}),+ (+ None,+ {+ "weight": 0,+ "parser": DummyParser1,+ "mime_types": {"application/pdf": ".pdf"},+ },+ ),+ (+ None,+ {+ "weight": 1,+ "parser": DummyParser2,+ "mime_types": {"application/pdf": ".pdf"},+ },+ ),)- self.assertEqual(- get_parser_class("doc.pdf"),- DummyParser2- )+ self.assertEqual(get_parser_class("doc.pdf"), DummyParser2)@mock.patch("documents.parsers.document_consumer_declaration.send")def test__get_parser_class_0_parsers(self, m, *args):m.return_value = []with TemporaryDirectory() as tmpdir:- self.assertIsNone(- get_parser_class("doc.pdf")- )+ self.assertIsNone(get_parser_class("doc.pdf"))def fake_get_thumbnail(self, path, mimetype, file_name):@@ -73,13 +90,10 @@ def fake_get_thumbnail(self, path, mimetype, file_name):class TestBaseParser(TestCase):-def setUp(self) -> None:self.scratch = tempfile.mkdtemp()- override_settings(- SCRATCH_DIR=self.scratch- ).enable()+ override_settings(SCRATCH_DIR=self.scratch).enable()def tearDown(self) -> None:shutil.rmtree(self.scratch)@@ -101,23 +115,28 @@ class TestBaseParser(TestCase):class TestParserAvailability(TestCase):-def test_file_extensions(self):for ext in [".pdf", ".jpe", ".jpg", ".jpeg", ".txt", ".csv"]:self.assertIn(ext, get_supported_file_extensions())- self.assertEqual(get_default_file_extension('application/pdf'), ".pdf")- self.assertEqual(get_default_file_extension('image/png'), ".png")- self.assertEqual(get_default_file_extension('image/jpeg'), ".jpg")- self.assertEqual(get_default_file_extension('text/plain'), ".txt")- self.assertEqual(get_default_file_extension('text/csv'), ".csv")- self.assertEqual(get_default_file_extension('application/zip'), ".zip")- self.assertEqual(get_default_file_extension('aasdasd/dgfgf'), "")-- self.assertIsInstance(get_parser_class_for_mime_type('application/pdf')(logging_group=None), RasterisedDocumentParser)- self.assertIsInstance(get_parser_class_for_mime_type('text/plain')(logging_group=None), TextDocumentParser)- self.assertEqual(get_parser_class_for_mime_type('text/sdgsdf'), None)-- self.assertTrue(is_file_ext_supported('.pdf'))- self.assertFalse(is_file_ext_supported('.hsdfh'))- self.assertFalse(is_file_ext_supported(''))+ self.assertEqual(get_default_file_extension("application/pdf"), ".pdf")+ self.assertEqual(get_default_file_extension("image/png"), ".png")+ self.assertEqual(get_default_file_extension("image/jpeg"), ".jpg")+ self.assertEqual(get_default_file_extension("text/plain"), ".txt")+ self.assertEqual(get_default_file_extension("text/csv"), ".csv")+ self.assertEqual(get_default_file_extension("application/zip"), ".zip")+ self.assertEqual(get_default_file_extension("aasdasd/dgfgf"), "")++ self.assertIsInstance(+ get_parser_class_for_mime_type("application/pdf")(logging_group=None),+ RasterisedDocumentParser,+ )+ self.assertIsInstance(+ get_parser_class_for_mime_type("text/plain")(logging_group=None),+ TextDocumentParser,+ )+ self.assertEqual(get_parser_class_for_mime_type("text/sdgsdf"), None)++ self.assertTrue(is_file_ext_supported(".pdf"))+ self.assertFalse(is_file_ext_supported(".hsdfh"))+ self.assertFalse(is_file_ext_supported(""))diff --git a/src/documents/tests/test_sanity_check.py b/src/documents/tests/test_sanity_check.pyindex b07ba930a..f3953bab9 100644--- a/src/documents/tests/test_sanity_check.py+++ b/src/documents/tests/test_sanity_check.py@@ -13,7 +13,6 @@ from documents.tests.utils import DirectoriesMixinclass TestSanityCheckMessages(TestCase):-def test_no_messages(self):messages = SanityCheckMessages()self.assertEqual(len(messages), 0)@@ -23,7 +22,9 @@ class TestSanityCheckMessages(TestCase):messages.log_messages()self.assertEqual(len(capture.output), 1)self.assertEqual(capture.records[0].levelno, logging.INFO)- self.assertEqual(capture.records[0].message, "Sanity checker detected no issues.")+ self.assertEqual(+ capture.records[0].message, "Sanity checker detected no issues."+ )def test_info(self):messages = SanityCheckMessages()@@ -61,22 +62,58 @@ class TestSanityCheckMessages(TestCase):self.assertEqual(capture.records[0].levelno, logging.ERROR)self.assertEqual(capture.records[0].message, "Something is seriously wrong")-class TestSanityCheck(DirectoriesMixin, TestCase):+class TestSanityCheck(DirectoriesMixin, TestCase):def make_test_data(self):with filelock.FileLock(settings.MEDIA_LOCK):# just make sure that the lockfile is present.- shutil.copy(os.path.join(os.path.dirname(__file__), "samples", "documents", "originals", "0000001.pdf"), os.path.join(self.dirs.originals_dir, "0000001.pdf"))- shutil.copy(os.path.join(os.path.dirname(__file__), "samples", "documents", "archive", "0000001.pdf"), os.path.join(self.dirs.archive_dir, "0000001.pdf"))- shutil.copy(os.path.join(os.path.dirname(__file__), "samples", "documents", "thumbnails", "0000001.png"), os.path.join(self.dirs.thumbnail_dir, "0000001.png"))-- return Document.objects.create(title="test", checksum="42995833e01aea9b3edee44bbfdd7ce1", archive_checksum="62acb0bcbfbcaa62ca6ad3668e4e404b", content="test", pk=1, filename="0000001.pdf", mime_type="application/pdf", archive_filename="0000001.pdf")+ shutil.copy(+ os.path.join(+ os.path.dirname(__file__),+ "samples",+ "documents",+ "originals",+ "0000001.pdf",+ ),+ os.path.join(self.dirs.originals_dir, "0000001.pdf"),+ )+ shutil.copy(+ os.path.join(+ os.path.dirname(__file__),+ "samples",+ "documents",+ "archive",+ "0000001.pdf",+ ),+ os.path.join(self.dirs.archive_dir, "0000001.pdf"),+ )+ shutil.copy(+ os.path.join(+ os.path.dirname(__file__),+ "samples",+ "documents",+ "thumbnails",+ "0000001.png",+ ),+ os.path.join(self.dirs.thumbnail_dir, "0000001.png"),+ )++ return Document.objects.create(+ title="test",+ checksum="42995833e01aea9b3edee44bbfdd7ce1",+ archive_checksum="62acb0bcbfbcaa62ca6ad3668e4e404b",+ content="test",+ pk=1,+ filename="0000001.pdf",+ mime_type="application/pdf",+ archive_filename="0000001.pdf",+ )def assertSanityError(self, messageRegex):messages = check_sanity()self.assertTrue(messages.has_error())- self.assertRegex(messages[0]['message'], messageRegex)+ self.assertRegex(messages[0]["message"], messageRegex)def test_no_docs(self):self.assertEqual(len(check_sanity()), 0)@@ -138,7 +175,7 @@ class TestSanityCheck(DirectoriesMixin, TestCase):self.assertFalse(messages.has_error())self.assertFalse(messages.has_warning())self.assertEqual(len(messages), 1)- self.assertRegex(messages[0]['message'], "Document .* has no content.")+ self.assertRegex(messages[0]["message"], "Document .* has no content.")def test_orphaned_file(self):doc = self.make_test_data()@@ -147,7 +184,7 @@ class TestSanityCheck(DirectoriesMixin, TestCase):self.assertFalse(messages.has_error())self.assertTrue(messages.has_warning())self.assertEqual(len(messages), 1)- self.assertRegex(messages[0]['message'], "Orphaned file in media dir")+ self.assertRegex(messages[0]["message"], "Orphaned file in media dir")def test_archive_filename_no_checksum(self):doc = self.make_test_data()diff --git a/src/documents/tests/test_settings.py b/src/documents/tests/test_settings.pyindex 0036daee7..25fe0e317 100644--- a/src/documents/tests/test_settings.py+++ b/src/documents/tests/test_settings.py@@ -7,7 +7,6 @@ from paperless.settings import default_task_workers, default_threads_per_workerclass TestSettings(TestCase):-@mock.patch("paperless.settings.multiprocessing.cpu_count")def test_single_core(self, cpu_count):cpu_count.return_value = 1@@ -21,7 +20,9 @@ class TestSettings(TestCase):def test_workers_threads(self):for i in range(1, 64):- with mock.patch("paperless.settings.multiprocessing.cpu_count") as cpu_count:+ with mock.patch(+ "paperless.settings.multiprocessing.cpu_count"+ ) as cpu_count:cpu_count.return_value = idefault_workers = default_task_workers()diff --git a/src/documents/tests/test_tasks.py b/src/documents/tests/test_tasks.pyindex dab8ebfb9..a0cae7307 100644--- a/src/documents/tests/test_tasks.py+++ b/src/documents/tests/test_tasks.py@@ -12,14 +12,27 @@ from documents.tests.utils import DirectoriesMixinclass TestTasks(DirectoriesMixin, TestCase):-def test_index_reindex(self):- Document.objects.create(title="test", content="my document", checksum="wow", added=timezone.now(), created=timezone.now(), modified=timezone.now())+ Document.objects.create(+ title="test",+ content="my document",+ checksum="wow",+ added=timezone.now(),+ created=timezone.now(),+ modified=timezone.now(),+ )tasks.index_reindex()def test_index_optimize(self):- Document.objects.create(title="test", content="my document", checksum="wow", added=timezone.now(), created=timezone.now(), modified=timezone.now())+ Document.objects.create(+ title="test",+ content="my document",+ checksum="wow",+ added=timezone.now(),+ created=timezone.now(),+ modified=timezone.now(),+ )tasks.index_optimize()@@ -92,7 +105,9 @@ class TestTasks(DirectoriesMixin, TestCase):messages = SanityCheckMessages()messages.warning("Some warning")m.return_value = messages- self.assertEqual(tasks.sanity_check(), "Sanity check exited with warnings. See log.")+ self.assertEqual(+ tasks.sanity_check(), "Sanity check exited with warnings. See log."+ )m.assert_called_once()@mock.patch("documents.tasks.sanity_checker.check_sanity")@@ -100,11 +115,19 @@ class TestTasks(DirectoriesMixin, TestCase):messages = SanityCheckMessages()messages.info("Some info")m.return_value = messages- self.assertEqual(tasks.sanity_check(), "Sanity check exited with infos. See log.")+ self.assertEqual(+ tasks.sanity_check(), "Sanity check exited with infos. See log."+ )m.assert_called_once()def test_bulk_update_documents(self):- doc1 = Document.objects.create(title="test", content="my document", checksum="wow", added=timezone.now(),- created=timezone.now(), modified=timezone.now())+ doc1 = Document.objects.create(+ title="test",+ content="my document",+ checksum="wow",+ added=timezone.now(),+ created=timezone.now(),+ modified=timezone.now(),+ )tasks.bulk_update_documents([doc1.pk])diff --git a/src/documents/tests/test_views.py b/src/documents/tests/test_views.pyindex 7f7a07a61..dcae72797 100644--- a/src/documents/tests/test_views.py+++ b/src/documents/tests/test_views.py@@ -4,27 +4,52 @@ from django.test import TestCaseclass TestViews(TestCase):-def setUp(self) -> None:self.user = User.objects.create_user("testuser")def test_login_redirect(self):- response = self.client.get('/')+ response = self.client.get("/")self.assertEqual(response.status_code, 302)self.assertEqual(response.url, "/accounts/login/?next=/")def test_index(self):self.client.force_login(self.user)- for (language_given, language_actual) in [("", "en-US"), ("en-US", "en-US"), ("de", "de-DE"), ("en", "en-US"), ("en-us", "en-US"), ("fr", "fr-FR"), ("jp", "en-US")]:+ for (language_given, language_actual) in [+ ("", "en-US"),+ ("en-US", "en-US"),+ ("de", "de-DE"),+ ("en", "en-US"),+ ("en-us", "en-US"),+ ("fr", "fr-FR"),+ ("jp", "en-US"),+ ]:if language_given:- self.client.cookies.load({settings.LANGUAGE_COOKIE_NAME: language_given})+ self.client.cookies.load(+ {settings.LANGUAGE_COOKIE_NAME: language_given}+ )elif settings.LANGUAGE_COOKIE_NAME in self.client.cookies.keys():self.client.cookies.pop(settings.LANGUAGE_COOKIE_NAME)- response = self.client.get('/', )+ response = self.client.get(+ "/",+ )self.assertEqual(response.status_code, 200)- self.assertEqual(response.context_data['webmanifest'], f"frontend/{language_actual}/manifest.webmanifest")- self.assertEqual(response.context_data['styles_css'], f"frontend/{language_actual}/styles.css")- self.assertEqual(response.context_data['runtime_js'], f"frontend/{language_actual}/runtime.js")- self.assertEqual(response.context_data['polyfills_js'], f"frontend/{language_actual}/polyfills.js")- self.assertEqual(response.context_data['main_js'], f"frontend/{language_actual}/main.js")+ self.assertEqual(+ response.context_data["webmanifest"],+ f"frontend/{language_actual}/manifest.webmanifest",+ )+ self.assertEqual(+ response.context_data["styles_css"],+ f"frontend/{language_actual}/styles.css",+ )+ self.assertEqual(+ response.context_data["runtime_js"],+ f"frontend/{language_actual}/runtime.js",+ )+ self.assertEqual(+ response.context_data["polyfills_js"],+ f"frontend/{language_actual}/polyfills.js",+ )+ self.assertEqual(+ response.context_data["main_js"], f"frontend/{language_actual}/main.js"+ )diff --git a/src/documents/tests/utils.py b/src/documents/tests/utils.pyindex da8d3d429..3aa9cf880 100644--- a/src/documents/tests/utils.py+++ b/src/documents/tests/utils.py@@ -42,8 +42,7 @@ def setup_directories():LOGGING_DIR=dirs.logging_dir,INDEX_DIR=dirs.index_dir,MODEL_FILE=os.path.join(dirs.data_dir, "classification_model.pickle"),- MEDIA_LOCK=os.path.join(dirs.media_dir, "media.lock")-+ MEDIA_LOCK=os.path.join(dirs.media_dir, "media.lock"),)dirs.settings_override.enable()@@ -70,7 +69,6 @@ def paperless_environment():class DirectoriesMixin:-def __init__(self, *args, **kwargs):super().__init__(*args, **kwargs)self.dirs = None@@ -85,7 +83,6 @@ class DirectoriesMixin:class TestMigrations(TransactionTestCase):-@propertydef app(self):return apps.get_containing_app_config(type(self).__module__).name@@ -97,8 +94,11 @@ class TestMigrations(TransactionTestCase):def setUp(self):super(TestMigrations, self).setUp()- assert self.migrate_from and self.migrate_to, \- "TestCase '{}' must define migrate_from and migrate_to properties".format(type(self).__name__)+ assert (+ self.migrate_from and self.migrate_to+ ), "TestCase '{}' must define migrate_from and migrate_to properties".format(+ type(self).__name__+ )self.migrate_from = [(self.app, self.migrate_from)]self.migrate_to = [(self.app, self.migrate_to)]executor = MigrationExecutor(connection)diff --git a/src/documents/views.py b/src/documents/views.pyindex 6b4d02e49..9e4d960ab 100644--- a/src/documents/views.py+++ b/src/documents/views.py@@ -26,27 +26,26 @@ from rest_framework.mixins import (DestroyModelMixin,ListModelMixin,RetrieveModelMixin,- UpdateModelMixin+ UpdateModelMixin,)from rest_framework.permissions import IsAuthenticatedfrom rest_framework.response import Responsefrom rest_framework.views import APIView-from rest_framework.viewsets import (- GenericViewSet,- ModelViewSet,- ViewSet-)+from rest_framework.viewsets import GenericViewSet, ModelViewSet, ViewSetfrom paperless.db import GnuPGfrom paperless.views import StandardPagination-from .bulk_download import OriginalAndArchiveStrategy, OriginalsOnlyStrategy, \- ArchiveOnlyStrategy+from .bulk_download import (+ OriginalAndArchiveStrategy,+ OriginalsOnlyStrategy,+ ArchiveOnlyStrategy,+)from .classifier import load_classifierfrom .filters import (CorrespondentFilterSet,DocumentFilterSet,TagFilterSet,- DocumentTypeFilterSet+ DocumentTypeFilterSet,)from .matching import match_correspondents, match_tags, match_document_typesfrom .models import Correspondent, Document, Tag, DocumentType, SavedView@@ -61,7 +60,7 @@ from .serialisers import (SavedViewSerializer,BulkEditSerializer,DocumentListSerializer,- BulkDownloadSerializer+ BulkDownloadSerializer,)logger = logging.getLogger("paperless.api")@@ -77,23 +76,29 @@ class IndexView(TemplateView):# this translates between these two forms.lang = get_language()if "-" in lang:- first = lang[:lang.index("-")]- second = lang[lang.index("-")+1:]+ first = lang[: lang.index("-")]+ second = lang[lang.index("-") + 1 :]return f"{first}-{second.upper()}"else:return langdef get_context_data(self, **kwargs):context = super().get_context_data(**kwargs)- context['cookie_prefix'] = settings.COOKIE_PREFIX- context['username'] = self.request.user.username- context['full_name'] = self.request.user.get_full_name()- context['styles_css'] = f"frontend/{self.get_language()}/styles.css"- context['runtime_js'] = f"frontend/{self.get_language()}/runtime.js"- context['polyfills_js'] = f"frontend/{self.get_language()}/polyfills.js" # NOQA: E501- context['main_js'] = f"frontend/{self.get_language()}/main.js"- context['webmanifest'] = f"frontend/{self.get_language()}/manifest.webmanifest" # NOQA: E501- context['apple_touch_icon'] = f"frontend/{self.get_language()}/apple-touch-icon.png" # NOQA: E501+ context["cookie_prefix"] = settings.COOKIE_PREFIX+ context["username"] = self.request.user.username+ context["full_name"] = self.request.user.get_full_name()+ context["styles_css"] = f"frontend/{self.get_language()}/styles.css"+ context["runtime_js"] = f"frontend/{self.get_language()}/runtime.js"+ context[+ "polyfills_js"+ ] = f"frontend/{self.get_language()}/polyfills.js" # NOQA: E501+ context["main_js"] = f"frontend/{self.get_language()}/main.js"+ context[+ "webmanifest"+ ] = f"frontend/{self.get_language()}/manifest.webmanifest" # NOQA: E501+ context[+ "apple_touch_icon"+ ] = f"frontend/{self.get_language()}/apple-touch-icon.png" # NOQA: E501return context@@ -101,8 +106,8 @@ class CorrespondentViewSet(ModelViewSet):model = Correspondentqueryset = Correspondent.objects.annotate(- document_count=Count('documents'),- last_correspondence=Max('documents__created')).order_by(Lower('name'))+ document_count=Count("documents"), last_correspondence=Max("documents__created")+ ).order_by(Lower("name"))serializer_class = CorrespondentSerializerpagination_class = StandardPagination@@ -114,14 +119,16 @@ class CorrespondentViewSet(ModelViewSet):"matching_algorithm","match","document_count",- "last_correspondence")+ "last_correspondence",+ )class TagViewSet(ModelViewSet):model = Tag- queryset = Tag.objects.annotate(- document_count=Count('documents')).order_by(Lower('name'))+ queryset = Tag.objects.annotate(document_count=Count("documents")).order_by(+ Lower("name")+ )def get_serializer_class(self):if int(self.request.version) == 1:@@ -140,7 +147,8 @@ class DocumentTypeViewSet(ModelViewSet):model = DocumentTypequeryset = DocumentType.objects.annotate(- document_count=Count('documents')).order_by(Lower('name'))+ document_count=Count("documents")+ ).order_by(Lower("name"))serializer_class = DocumentTypeSerializerpagination_class = StandardPagination@@ -150,11 +158,13 @@ class DocumentTypeViewSet(ModelViewSet):ordering_fields = ("name", "matching_algorithm", "match", "document_count")-class DocumentViewSet(RetrieveModelMixin,- UpdateModelMixin,- DestroyModelMixin,- ListModelMixin,- GenericViewSet):+class DocumentViewSet(+ RetrieveModelMixin,+ UpdateModelMixin,+ DestroyModelMixin,+ ListModelMixin,+ GenericViewSet,+):model = Documentqueryset = Document.objects.all()serializer_class = DocumentSerializer@@ -171,47 +181,51 @@ class DocumentViewSet(RetrieveModelMixin,"created","modified","added",- "archive_serial_number")+ "archive_serial_number",+ )def get_queryset(self):return Document.objects.distinct()def get_serializer(self, *args, **kwargs):- fields_param = self.request.query_params.get('fields', None)+ fields_param = self.request.query_params.get("fields", None)if fields_param:fields = fields_param.split(",")else:fields = Noneserializer_class = self.get_serializer_class()- kwargs.setdefault('context', self.get_serializer_context())- kwargs.setdefault('fields', fields)+ kwargs.setdefault("context", self.get_serializer_context())+ kwargs.setdefault("fields", fields)return serializer_class(*args, **kwargs)def update(self, request, *args, **kwargs):- response = super(DocumentViewSet, self).update(- request, *args, **kwargs)+ response = super(DocumentViewSet, self).update(request, *args, **kwargs)from documents import index+index.add_or_update_document(self.get_object())return responsedef destroy(self, request, *args, **kwargs):from documents import index+index.remove_document_from_index(self.get_object())return super(DocumentViewSet, self).destroy(request, *args, **kwargs)@staticmethoddef original_requested(request):return (- 'original' in request.query_params and- request.query_params['original'] == 'true'+ "original" in request.query_params+ and request.query_params["original"] == "true")def file_response(self, pk, request, disposition):doc = Document.objects.get(id=pk)- if not self.original_requested(request) and doc.has_archive_version: # NOQA: E501+ if (+ not self.original_requested(request) and doc.has_archive_version+ ): # NOQA: E501file_handle = doc.archive_filefilename = doc.get_public_filename(archive=True)- mime_type = 'application/pdf'+ mime_type = "application/pdf"else:file_handle = doc.source_filefilename = doc.get_public_filename()@@ -224,12 +238,13 @@ class DocumentViewSet(RetrieveModelMixin,# Firefox is not able to handle unicode characters in filename field# RFC 5987 addresses this issue# see https://datatracker.ietf.org/doc/html/rfc5987#section-4.2- filename_normalized = normalize("NFKD", filename)\- .encode('ascii', 'ignore')+ filename_normalized = normalize("NFKD", filename).encode("ascii", "ignore")filename_encoded = quote_plus(filename)- content_disposition = f'{disposition}; ' \- f'filename="{filename_normalized}"; ' \- f'filename*=utf-8\'\'{filename_encoded}'+ content_disposition = (+ f"{disposition}; "+ f'filename="{filename_normalized}"; '+ f"filename*=utf-8''{filename_encoded}"+ )response["Content-Disposition"] = content_dispositionreturn response@@ -255,7 +270,7 @@ class DocumentViewSet(RetrieveModelMixin,else:return None- @action(methods=['get'], detail=True)+ @action(methods=["get"], detail=True)def metadata(self, request, pk=None):try:doc = Document.objects.get(pk=pk)@@ -268,23 +283,23 @@ class DocumentViewSet(RetrieveModelMixin,"original_mime_type": doc.mime_type,"media_filename": doc.filename,"has_archive_version": doc.has_archive_version,- "original_metadata": self.get_metadata(- doc.source_path, doc.mime_type),+ "original_metadata": self.get_metadata(doc.source_path, doc.mime_type),"archive_checksum": doc.archive_checksum,- "archive_media_filename": doc.archive_filename+ "archive_media_filename": doc.archive_filename,}if doc.has_archive_version:- meta['archive_size'] = self.get_filesize(doc.archive_path)- meta['archive_metadata'] = self.get_metadata(- doc.archive_path, "application/pdf")+ meta["archive_size"] = self.get_filesize(doc.archive_path)+ meta["archive_metadata"] = self.get_metadata(+ doc.archive_path, "application/pdf"+ )else:- meta['archive_size'] = None- meta['archive_metadata'] = None+ meta["archive_size"] = None+ meta["archive_metadata"] = Nonereturn Response(meta)- @action(methods=['get'], detail=True)+ @action(methods=["get"], detail=True)def suggestions(self, request, pk=None):try:doc = Document.objects.get(pk=pk)@@ -293,26 +308,25 @@ class DocumentViewSet(RetrieveModelMixin,classifier = load_classifier()- return Response({- "correspondents": [- c.id for c in match_correspondents(doc, classifier)- ],- "tags": [t.id for t in match_tags(doc, classifier)],- "document_types": [- dt.id for dt in match_document_types(doc, classifier)- ]- })-- @action(methods=['get'], detail=True)+ return Response(+ {+ "correspondents": [c.id for c in match_correspondents(doc, classifier)],+ "tags": [t.id for t in match_tags(doc, classifier)],+ "document_types": [+ dt.id for dt in match_document_types(doc, classifier)+ ],+ }+ )++ @action(methods=["get"], detail=True)def preview(self, request, pk=None):try:- response = self.file_response(- pk, request, "inline")+ response = self.file_response(pk, request, "inline")return responseexcept (FileNotFoundError, Document.DoesNotExist):raise Http404()- @action(methods=['get'], detail=True)+ @action(methods=["get"], detail=True)@cache_control(public=False, max_age=315360000)def thumb(self, request, pk=None):try:@@ -323,37 +337,34 @@ class DocumentViewSet(RetrieveModelMixin,handle = doc.thumbnail_file# TODO: Send ETag information and use that to send new thumbnails# if available- return HttpResponse(handle,- content_type='image/png')+ return HttpResponse(handle, content_type="image/png")except (FileNotFoundError, Document.DoesNotExist):raise Http404()- @action(methods=['get'], detail=True)+ @action(methods=["get"], detail=True)def download(self, request, pk=None):try:- return self.file_response(- pk, request, "attachment")+ return self.file_response(pk, request, "attachment")except (FileNotFoundError, Document.DoesNotExist):raise Http404()class SearchResultSerializer(DocumentSerializer):-def to_representation(self, instance):- doc = Document.objects.get(id=instance['id'])+ doc = Document.objects.get(id=instance["id"])r = super(SearchResultSerializer, self).to_representation(doc)- r['__search_hit__'] = {+ r["__search_hit__"] = {"score": instance.score,- "highlights": instance.highlights("content",- text=doc.content) if doc else None, # NOQA: E501- "rank": instance.rank+ "highlights": instance.highlights("content", text=doc.content)+ if doc+ else None, # NOQA: E501+ "rank": instance.rank,}return rclass UnifiedSearchViewSet(DocumentViewSet):-def __init__(self, *args, **kwargs):super(UnifiedSearchViewSet, self).__init__(*args, **kwargs)self.searcher = None@@ -365,8 +376,10 @@ class UnifiedSearchViewSet(DocumentViewSet):return DocumentSerializerdef _is_search_request(self):- return ("query" in self.request.query_params or- "more_like_id" in self.request.query_params)+ return (+ "query" in self.request.query_params+ or "more_like_id" in self.request.query_params+ )def filter_queryset(self, queryset):if self._is_search_request():@@ -382,13 +395,15 @@ class UnifiedSearchViewSet(DocumentViewSet):return query_class(self.searcher,self.request.query_params,- self.paginator.get_page_size(self.request))+ self.paginator.get_page_size(self.request),+ )else:return super(UnifiedSearchViewSet, self).filter_queryset(queryset)def list(self, request, *args, **kwargs):if self._is_search_request():from documents import index+try:with index.open_index_searcher() as s:self.searcher = s@@ -474,34 +489,36 @@ class PostDocumentView(GenericAPIView):serializer = self.get_serializer(data=request.data)serializer.is_valid(raise_exception=True)- doc_name, doc_data = serializer.validated_data.get('document')- correspondent_id = serializer.validated_data.get('correspondent')- document_type_id = serializer.validated_data.get('document_type')- tag_ids = serializer.validated_data.get('tags')- title = serializer.validated_data.get('title')+ doc_name, doc_data = serializer.validated_data.get("document")+ correspondent_id = serializer.validated_data.get("correspondent")+ document_type_id = serializer.validated_data.get("document_type")+ tag_ids = serializer.validated_data.get("tags")+ title = serializer.validated_data.get("title")t = int(mktime(datetime.now().timetuple()))os.makedirs(settings.SCRATCH_DIR, exist_ok=True)- with tempfile.NamedTemporaryFile(prefix="paperless-upload-",- dir=settings.SCRATCH_DIR,- delete=False) as f:+ with tempfile.NamedTemporaryFile(+ prefix="paperless-upload-", dir=settings.SCRATCH_DIR, delete=False+ ) as f:f.write(doc_data)os.utime(f.name, times=(t, t))temp_filename = f.nametask_id = str(uuid.uuid4())- async_task("documents.tasks.consume_file",- temp_filename,- override_filename=doc_name,- override_title=title,- override_correspondent_id=correspondent_id,- override_document_type_id=document_type_id,- override_tag_ids=tag_ids,- task_id=task_id,- task_name=os.path.basename(doc_name)[:100])+ async_task(+ "documents.tasks.consume_file",+ temp_filename,+ override_filename=doc_name,+ override_title=title,+ override_correspondent_id=correspondent_id,+ override_document_type_id=document_type_id,+ override_tag_ids=tag_ids,+ task_id=task_id,+ task_name=os.path.basename(doc_name)[:100],+ )return Response("OK")@@ -516,38 +533,40 @@ class SelectionDataView(GenericAPIView):serializer = self.get_serializer(data=request.data)serializer.is_valid(raise_exception=True)- ids = serializer.validated_data.get('documents')+ ids = serializer.validated_data.get("documents")correspondents = Correspondent.objects.annotate(- document_count=Count(Case(- When(documents__id__in=ids, then=1),- output_field=IntegerField()- )))-- tags = Tag.objects.annotate(document_count=Count(Case(- When(documents__id__in=ids, then=1),- output_field=IntegerField()- )))-- types = DocumentType.objects.annotate(document_count=Count(Case(- When(documents__id__in=ids, then=1),- output_field=IntegerField()- )))-- r = Response({- "selected_correspondents": [{- "id": t.id,- "document_count": t.document_count- } for t in correspondents],- "selected_tags": [{- "id": t.id,- "document_count": t.document_count- } for t in tags],- "selected_document_types": [{- "id": t.id,- "document_count": t.document_count- } for t in types]- })+ document_count=Count(+ Case(When(documents__id__in=ids, then=1), output_field=IntegerField())+ )+ )++ tags = Tag.objects.annotate(+ document_count=Count(+ Case(When(documents__id__in=ids, then=1), output_field=IntegerField())+ )+ )++ types = DocumentType.objects.annotate(+ document_count=Count(+ Case(When(documents__id__in=ids, then=1), output_field=IntegerField())+ )+ )++ r = Response(+ {+ "selected_correspondents": [+ {"id": t.id, "document_count": t.document_count}+ for t in correspondents+ ],+ "selected_tags": [+ {"id": t.id, "document_count": t.document_count} for t in tags+ ],+ "selected_document_types": [+ {"id": t.id, "document_count": t.document_count} for t in types+ ],+ }+ )return r@@ -557,13 +576,13 @@ class SearchAutoCompleteView(APIView):permission_classes = (IsAuthenticated,)def get(self, request, format=None):- if 'term' in request.query_params:- term = request.query_params['term']+ if "term" in request.query_params:+ term = request.query_params["term"]else:return HttpResponseBadRequest("Term required")- if 'limit' in request.query_params:- limit = int(request.query_params['limit'])+ if "limit" in request.query_params:+ limit = int(request.query_params["limit"])if limit <= 0:return HttpResponseBadRequest("Invalid limit")else:@@ -583,15 +602,18 @@ class StatisticsView(APIView):def get(self, request, format=None):documents_total = Document.objects.all().count()if Tag.objects.filter(is_inbox_tag=True).exists():- documents_inbox = Document.objects.filter(- tags__is_inbox_tag=True).distinct().count()+ documents_inbox = (+ Document.objects.filter(tags__is_inbox_tag=True).distinct().count()+ )else:documents_inbox = None- return Response({- 'documents_total': documents_total,- 'documents_inbox': documents_inbox,- })+ return Response(+ {+ "documents_total": documents_total,+ "documents_inbox": documents_inbox,+ }+ )class BulkDownloadView(GenericAPIView):@@ -604,19 +626,18 @@ class BulkDownloadView(GenericAPIView):serializer = self.get_serializer(data=request.data)serializer.is_valid(raise_exception=True)- ids = serializer.validated_data.get('documents')- compression = serializer.validated_data.get('compression')- content = serializer.validated_data.get('content')+ ids = serializer.validated_data.get("documents")+ compression = serializer.validated_data.get("compression")+ content = serializer.validated_data.get("content")os.makedirs(settings.SCRATCH_DIR, exist_ok=True)temp = tempfile.NamedTemporaryFile(- dir=settings.SCRATCH_DIR,- suffix="-compressed-archive",- delete=False)+ dir=settings.SCRATCH_DIR, suffix="-compressed-archive", delete=False+ )- if content == 'both':+ if content == "both":strategy_class = OriginalAndArchiveStrategy- elif content == 'originals':+ elif content == "originals":strategy_class = OriginalsOnlyStrategyelse:strategy_class = ArchiveOnlyStrategy@@ -630,6 +651,7 @@ class BulkDownloadView(GenericAPIView):with open(temp.name, "rb") as f:response = HttpResponse(f, content_type="application/zip")response["Content-Disposition"] = '{}; filename="{}"'.format(- "attachment", "documents.zip")+ "attachment", "documents.zip"+ )return responsediff --git a/src/paperless/asgi.py b/src/paperless/asgi.pyindex 2f6cc2d5f..a3bc386ce 100644--- a/src/paperless/asgi.py+++ b/src/paperless/asgi.py@@ -1,6 +1,7 @@import osfrom django.core.asgi import get_asgi_application+# Fetch Django ASGI application early to ensure AppRegistry is populated# before importing consumers and AuthMiddlewareStack that may import ORM# models.@@ -13,11 +14,9 @@ from channels.routing import ProtocolTypeRouter, URLRouter # NOQA: E402from paperless.urls import websocket_urlpatterns # NOQA: E402-application = ProtocolTypeRouter({- "http": get_asgi_application(),- "websocket": AuthMiddlewareStack(- URLRouter(- websocket_urlpatterns- )- ),-})+application = ProtocolTypeRouter(+ {+ "http": get_asgi_application(),+ "websocket": AuthMiddlewareStack(URLRouter(websocket_urlpatterns)),+ }+)diff --git a/src/paperless/auth.py b/src/paperless/auth.pyindex 79deb5c37..7af4f3590 100644--- a/src/paperless/auth.py+++ b/src/paperless/auth.py@@ -7,23 +7,25 @@ from django.contrib.auth.middleware import RemoteUserMiddlewareclass AutoLoginMiddleware(MiddlewareMixin):-def process_request(self, request):try:- request.user = User.objects.get(- username=settings.AUTO_LOGIN_USERNAME)+ request.user = User.objects.get(username=settings.AUTO_LOGIN_USERNAME)auth.login(request, request.user)except User.DoesNotExist:passclass AngularApiAuthenticationOverride(authentication.BaseAuthentication):- """ This class is here to provide authentication to the angular dev server- during development. This is disabled in production.+ """This class is here to provide authentication to the angular dev server+ during development. This is disabled in production."""def authenticate(self, request):- if settings.DEBUG and 'Referer' in request.headers and request.headers['Referer'].startswith('http://localhost:4200/'): # NOQA: E501+ if (+ settings.DEBUG+ and "Referer" in request.headers+ and request.headers["Referer"].startswith("http://localhost:4200/")+ ): # NOQA: E501user = User.objects.filter(is_staff=True).first()print("Auto-Login with user {}".format(user))return (user, None)@@ -32,7 +34,8 @@ class AngularApiAuthenticationOverride(authentication.BaseAuthentication):class HttpRemoteUserMiddleware(RemoteUserMiddleware):- """ This class allows authentication via HTTP_REMOTE_USER which is set for- example by certain SSO applications.+ """This class allows authentication via HTTP_REMOTE_USER which is set for+ example by certain SSO applications."""+header = settings.HTTP_REMOTE_USER_HEADER_NAMEdiff --git a/src/paperless/checks.py b/src/paperless/checks.pyindex 24830a9e0..1adc8b149 100644--- a/src/paperless/checks.py+++ b/src/paperless/checks.py@@ -18,24 +18,26 @@ def path_check(var, directory):messages = []if directory:if not os.path.isdir(directory):- messages.append(Error(- exists_message.format(var),- exists_hint.format(directory)- ))+ messages.append(+ Error(exists_message.format(var), exists_hint.format(directory))+ )else:test_file = os.path.join(- directory, f'__paperless_write_test_{os.getpid()}__'+ directory, f"__paperless_write_test_{os.getpid()}__")try:- with open(test_file, 'w'):+ with open(test_file, "w"):passexcept PermissionError:- messages.append(Error(- writeable_message.format(var),- writeable_hint.format(- f'\n{stat.filemode(os.stat(directory).st_mode)} '- f'{directory}\n')- ))+ messages.append(+ Error(+ writeable_message.format(var),+ writeable_hint.format(+ f"\n{stat.filemode(os.stat(directory).st_mode)} "+ f"{directory}\n"+ ),+ )+ )finally:if os.path.isfile(test_file):os.remove(test_file)@@ -49,10 +51,12 @@ def paths_check(app_configs, **kwargs):Check the various paths for existence, readability and writeability"""- return path_check("PAPERLESS_DATA_DIR", settings.DATA_DIR) + \- path_check("PAPERLESS_TRASH_DIR", settings.TRASH_DIR) + \- path_check("PAPERLESS_MEDIA_ROOT", settings.MEDIA_ROOT) + \- path_check("PAPERLESS_CONSUMPTION_DIR", settings.CONSUMPTION_DIR)+ return (+ path_check("PAPERLESS_DATA_DIR", settings.DATA_DIR)+ + path_check("PAPERLESS_TRASH_DIR", settings.TRASH_DIR)+ + path_check("PAPERLESS_MEDIA_ROOT", settings.MEDIA_ROOT)+ + path_check("PAPERLESS_CONSUMPTION_DIR", settings.CONSUMPTION_DIR)+ )@register()@@ -65,11 +69,7 @@ def binaries_check(app_configs, **kwargs):error = "Paperless can't find {}. Without it, consumption is impossible."hint = "Either it's not in your ${PATH} or it's not installed."- binaries = (- settings.CONVERT_BINARY,- settings.OPTIPNG_BINARY,- "tesseract"- )+ binaries = (settings.CONVERT_BINARY, settings.OPTIPNG_BINARY, "tesseract")check_messages = []for binary in binaries:@@ -82,11 +82,14 @@ def binaries_check(app_configs, **kwargs):@register()def debug_mode_check(app_configs, **kwargs):if settings.DEBUG:- return [Warning(- "DEBUG mode is enabled. Disable Debug mode. This is a serious "- "security issue, since it puts security overides in place which "- "are meant to be only used during development. This "- "also means that paperless will tell anyone various "- "debugging information when something goes wrong.")]+ return [+ Warning(+ "DEBUG mode is enabled. Disable Debug mode. This is a serious "+ "security issue, since it puts security overides in place which "+ "are meant to be only used during development. This "+ "also means that paperless will tell anyone various "+ "debugging information when something goes wrong."+ )+ ]else:return []diff --git a/src/paperless/consumers.py b/src/paperless/consumers.pyindex 45f6ad9c5..8b8e8c6dc 100644--- a/src/paperless/consumers.py+++ b/src/paperless/consumers.py@@ -6,24 +6,25 @@ from channels.generic.websocket import WebsocketConsumerclass StatusConsumer(WebsocketConsumer):-def _authenticated(self):- return 'user' in self.scope and self.scope['user'].is_authenticated+ return "user" in self.scope and self.scope["user"].is_authenticateddef connect(self):if not self._authenticated():raise DenyConnection()else:async_to_sync(self.channel_layer.group_add)(- 'status_updates', self.channel_name)+ "status_updates", self.channel_name+ )raise AcceptConnection()def disconnect(self, close_code):async_to_sync(self.channel_layer.group_discard)(- 'status_updates', self.channel_name)+ "status_updates", self.channel_name+ )def status_update(self, event):if not self._authenticated():self.close()else:- self.send(json.dumps(event['data']))+ self.send(json.dumps(event["data"]))diff --git a/src/paperless/middleware.py b/src/paperless/middleware.pyindex 2e9d2793c..bb634adf8 100644--- a/src/paperless/middleware.py+++ b/src/paperless/middleware.py@@ -4,17 +4,14 @@ from paperless import versionclass ApiVersionMiddleware:-def __init__(self, get_response):self.get_response = get_responsedef __call__(self, request):response = self.get_response(request)if request.user.is_authenticated:- versions = settings.REST_FRAMEWORK['ALLOWED_VERSIONS']- response['X-Api-Version'] = versions[len(versions)-1]- response['X-Version'] = ".".join(- [str(_) for _ in version.__version__]- )+ versions = settings.REST_FRAMEWORK["ALLOWED_VERSIONS"]+ response["X-Api-Version"] = versions[len(versions) - 1]+ response["X-Version"] = ".".join([str(_) for _ in version.__version__])return responsediff --git a/src/paperless/settings.py b/src/paperless/settings.pyindex 124f13ccb..e3fb5d155 100644--- a/src/paperless/settings.py+++ b/src/paperless/settings.py@@ -27,7 +27,7 @@ elif os.path.exists("/usr/local/etc/paperless.conf"):# OCR threads may exceed the number of available cpu cores, which will# dramatically slow down the consumption process. This settings limits each# Tesseract process to one thread.-os.environ['OMP_THREAD_LIMIT'] = "1"+os.environ["OMP_THREAD_LIMIT"] = "1"def __get_boolean(key, default="NO"):@@ -50,14 +50,14 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))STATIC_ROOT = os.getenv("PAPERLESS_STATICDIR", os.path.join(BASE_DIR, "..", "static"))-MEDIA_ROOT = os.getenv('PAPERLESS_MEDIA_ROOT', os.path.join(BASE_DIR, "..", "media"))+MEDIA_ROOT = os.getenv("PAPERLESS_MEDIA_ROOT", os.path.join(BASE_DIR, "..", "media"))ORIGINALS_DIR = os.path.join(MEDIA_ROOT, "documents", "originals")ARCHIVE_DIR = os.path.join(MEDIA_ROOT, "documents", "archive")THUMBNAIL_DIR = os.path.join(MEDIA_ROOT, "documents", "thumbnails")-DATA_DIR = os.getenv('PAPERLESS_DATA_DIR', os.path.join(BASE_DIR, "..", "data"))+DATA_DIR = os.getenv("PAPERLESS_DATA_DIR", os.path.join(BASE_DIR, "..", "data"))-TRASH_DIR = os.getenv('PAPERLESS_TRASH_DIR')+TRASH_DIR = os.getenv("PAPERLESS_TRASH_DIR")# Lock file for synchronizing changes to the MEDIA directory across multiple# threads.@@ -65,9 +65,11 @@ MEDIA_LOCK = os.path.join(MEDIA_ROOT, "media.lock")INDEX_DIR = os.path.join(DATA_DIR, "index")MODEL_FILE = os.path.join(DATA_DIR, "classification_model.pickle")-LOGGING_DIR = os.getenv('PAPERLESS_LOGGING_DIR', os.path.join(DATA_DIR, "log"))+LOGGING_DIR = os.getenv("PAPERLESS_LOGGING_DIR", os.path.join(DATA_DIR, "log"))-CONSUMPTION_DIR = os.getenv("PAPERLESS_CONSUMPTION_DIR", os.path.join(BASE_DIR, "..", "consume"))+CONSUMPTION_DIR = os.getenv(+ "PAPERLESS_CONSUMPTION_DIR", os.path.join(BASE_DIR, "..", "consume")+)# This will be created if it doesn't existSCRATCH_DIR = os.getenv("PAPERLESS_SCRATCH_DIR", "/tmp/paperless")@@ -80,75 +82,68 @@ env_apps = os.getenv("PAPERLESS_APPS").split(",") if os.getenv("PAPERLESS_APPS")INSTALLED_APPS = ["whitenoise.runserver_nostatic",-"django.contrib.auth","django.contrib.contenttypes","django.contrib.sessions","django.contrib.messages","django.contrib.staticfiles",-"corsheaders","django_extensions",-"paperless","documents.apps.DocumentsConfig","paperless_tesseract.apps.PaperlessTesseractConfig","paperless_text.apps.PaperlessTextConfig","paperless_mail.apps.PaperlessMailConfig",-"django.contrib.admin",-"rest_framework","rest_framework.authtoken","django_filters",-"django_q",-] + env_appsif DEBUG:INSTALLED_APPS.append("channels")REST_FRAMEWORK = {- 'DEFAULT_AUTHENTICATION_CLASSES': [- 'rest_framework.authentication.BasicAuthentication',- 'rest_framework.authentication.SessionAuthentication',- 'rest_framework.authentication.TokenAuthentication'+ "DEFAULT_AUTHENTICATION_CLASSES": [+ "rest_framework.authentication.BasicAuthentication",+ "rest_framework.authentication.SessionAuthentication",+ "rest_framework.authentication.TokenAuthentication",],- 'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.AcceptHeaderVersioning',- 'DEFAULT_VERSION': '1',+ "DEFAULT_VERSIONING_CLASS": "rest_framework.versioning.AcceptHeaderVersioning",+ "DEFAULT_VERSION": "1",# Make sure these are ordered and that the most recent version appears# last- 'ALLOWED_VERSIONS': ['1', '2']+ "ALLOWED_VERSIONS": ["1", "2"],}if DEBUG:- REST_FRAMEWORK['DEFAULT_AUTHENTICATION_CLASSES'].append(- 'paperless.auth.AngularApiAuthenticationOverride'+ REST_FRAMEWORK["DEFAULT_AUTHENTICATION_CLASSES"].append(+ "paperless.auth.AngularApiAuthenticationOverride")MIDDLEWARE = [- 'django.middleware.security.SecurityMiddleware',- 'whitenoise.middleware.WhiteNoiseMiddleware',- 'django.contrib.sessions.middleware.SessionMiddleware',- 'corsheaders.middleware.CorsMiddleware',- 'django.middleware.locale.LocaleMiddleware',- 'django.middleware.common.CommonMiddleware',- 'django.middleware.csrf.CsrfViewMiddleware',- 'paperless.middleware.ApiVersionMiddleware',- 'django.contrib.auth.middleware.AuthenticationMiddleware',- 'django.contrib.messages.middleware.MessageMiddleware',- 'django.middleware.clickjacking.XFrameOptionsMiddleware',+ "django.middleware.security.SecurityMiddleware",+ "whitenoise.middleware.WhiteNoiseMiddleware",+ "django.contrib.sessions.middleware.SessionMiddleware",+ "corsheaders.middleware.CorsMiddleware",+ "django.middleware.locale.LocaleMiddleware",+ "django.middleware.common.CommonMiddleware",+ "django.middleware.csrf.CsrfViewMiddleware",+ "paperless.middleware.ApiVersionMiddleware",+ "django.contrib.auth.middleware.AuthenticationMiddleware",+ "django.contrib.messages.middleware.MessageMiddleware",+ "django.middleware.clickjacking.XFrameOptionsMiddleware",]-ROOT_URLCONF = 'paperless.urls'+ROOT_URLCONF = "paperless.urls"FORCE_SCRIPT_NAME = os.getenv("PAPERLESS_FORCE_SCRIPT_NAME")BASE_URL = (FORCE_SCRIPT_NAME or "") + "/"LOGIN_URL = BASE_URL + "accounts/login/"LOGOUT_REDIRECT_URL = os.getenv("PAPERLESS_LOGOUT_REDIRECT_URL")-WSGI_APPLICATION = 'paperless.wsgi.application'+WSGI_APPLICATION = "paperless.wsgi.application"ASGI_APPLICATION = "paperless.asgi.application"STATIC_URL = os.getenv("PAPERLESS_STATIC_URL", BASE_URL + "static/")@@ -157,15 +152,15 @@ WHITENOISE_STATIC_PREFIX = "/static/"# TODO: what is this used for?TEMPLATES = [{- 'BACKEND': 'django.template.backends.django.DjangoTemplates',- 'DIRS': [],- 'APP_DIRS': True,- 'OPTIONS': {- 'context_processors': [- 'django.template.context_processors.debug',- 'django.template.context_processors.request',- 'django.contrib.auth.context_processors.auth',- 'django.contrib.messages.context_processors.messages',+ "BACKEND": "django.template.backends.django.DjangoTemplates",+ "DIRS": [],+ "APP_DIRS": True,+ "OPTIONS": {+ "context_processors": [+ "django.template.context_processors.debug",+ "django.template.context_processors.request",+ "django.contrib.auth.context_processors.auth",+ "django.contrib.messages.context_processors.messages",],},},@@ -189,45 +184,46 @@ CHANNEL_LAYERS = {AUTO_LOGIN_USERNAME = os.getenv("PAPERLESS_AUTO_LOGIN_USERNAME")if AUTO_LOGIN_USERNAME:- _index = MIDDLEWARE.index('django.contrib.auth.middleware.AuthenticationMiddleware')+ _index = MIDDLEWARE.index("django.contrib.auth.middleware.AuthenticationMiddleware")# This overrides everything the auth middleware is doing but still allows# regular login in case the provided user does not exist.- MIDDLEWARE.insert(_index+1, 'paperless.auth.AutoLoginMiddleware')+ MIDDLEWARE.insert(_index + 1, "paperless.auth.AutoLoginMiddleware")ENABLE_HTTP_REMOTE_USER = __get_boolean("PAPERLESS_ENABLE_HTTP_REMOTE_USER")-HTTP_REMOTE_USER_HEADER_NAME = os.getenv("PAPERLESS_HTTP_REMOTE_USER_HEADER_NAME", "HTTP_REMOTE_USER")+HTTP_REMOTE_USER_HEADER_NAME = os.getenv(+ "PAPERLESS_HTTP_REMOTE_USER_HEADER_NAME", "HTTP_REMOTE_USER"+)if ENABLE_HTTP_REMOTE_USER:- MIDDLEWARE.append(- 'paperless.auth.HttpRemoteUserMiddleware'- )+ MIDDLEWARE.append("paperless.auth.HttpRemoteUserMiddleware")AUTHENTICATION_BACKENDS = [- 'django.contrib.auth.backends.RemoteUserBackend',- 'django.contrib.auth.backends.ModelBackend'+ "django.contrib.auth.backends.RemoteUserBackend",+ "django.contrib.auth.backends.ModelBackend",]- REST_FRAMEWORK['DEFAULT_AUTHENTICATION_CLASSES'].append(- 'rest_framework.authentication.RemoteUserAuthentication'+ REST_FRAMEWORK["DEFAULT_AUTHENTICATION_CLASSES"].append(+ "rest_framework.authentication.RemoteUserAuthentication")# X-Frame options for embedded PDF display:if DEBUG:- X_FRAME_OPTIONS = 'ANY'+ X_FRAME_OPTIONS = "ANY"else:- X_FRAME_OPTIONS = 'SAMEORIGIN'+ X_FRAME_OPTIONS = "SAMEORIGIN"# We allow CORS from localhost:8080-CORS_ALLOWED_ORIGINS = tuple(os.getenv("PAPERLESS_CORS_ALLOWED_HOSTS", "http://localhost:8000").split(","))+CORS_ALLOWED_ORIGINS = tuple(+ os.getenv("PAPERLESS_CORS_ALLOWED_HOSTS", "http://localhost:8000").split(",")+)if DEBUG:# Allow access from the angular development server during debugging- CORS_ALLOWED_ORIGINS += ('http://localhost:4200',)+ CORS_ALLOWED_ORIGINS += ("http://localhost:4200",)# The secret key has a default that should be fine so long as you're hosting# Paperless on a closed network. However, if you're putting this anywhere# public, you should change the key to something unique and verbose.SECRET_KEY = os.getenv(- "PAPERLESS_SECRET_KEY",- "e11fl1oa-*ytql8p)(06fbj4ukrlo+n7k&q5+$1md7i+mge=ee"+ "PAPERLESS_SECRET_KEY", "e11fl1oa-*ytql8p)(06fbj4ukrlo+n7k&q5+$1md7i+mge=ee")_allowed_hosts = os.getenv("PAPERLESS_ALLOWED_HOSTS")@@ -238,16 +234,16 @@ else:AUTH_PASSWORD_VALIDATORS = [{- 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',+ "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",},{- 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',+ "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",},{- 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',+ "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",},{- 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',+ "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",},]@@ -271,17 +267,14 @@ LANGUAGE_COOKIE_NAME = f"{COOKIE_PREFIX}django_language"DATABASES = {"default": {"ENGINE": "django.db.backends.sqlite3",- "NAME": os.path.join(- DATA_DIR,- "db.sqlite3"- )+ "NAME": os.path.join(DATA_DIR, "db.sqlite3"),}}if os.getenv("PAPERLESS_DBHOST"):# Have sqlite available as a second option for management commands# This is important when migrating to/from sqlite- DATABASES['sqlite'] = DATABASES['default'].copy()+ DATABASES["sqlite"] = DATABASES["default"].copy()DATABASES["default"] = {"ENGINE": "django.db.backends.postgresql_psycopg2",@@ -289,21 +282,21 @@ if os.getenv("PAPERLESS_DBHOST"):"NAME": os.getenv("PAPERLESS_DBNAME", "paperless"),"USER": os.getenv("PAPERLESS_DBUSER", "paperless"),"PASSWORD": os.getenv("PAPERLESS_DBPASS", "paperless"),- 'OPTIONS': {'sslmode': os.getenv("PAPERLESS_DBSSLMODE", "prefer")},+ "OPTIONS": {"sslmode": os.getenv("PAPERLESS_DBSSLMODE", "prefer")},}if os.getenv("PAPERLESS_DBPORT"):DATABASES["default"]["PORT"] = os.getenv("PAPERLESS_DBPORT")-DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'+DEFAULT_AUTO_FIELD = "django.db.models.AutoField"################################################################################ Internationalization ################################################################################-LANGUAGE_CODE = 'en-us'+LANGUAGE_CODE = "en-us"LANGUAGES = [- ("en-us", _("English (US)")), # needs to be first to act as fallback language+ ("en-us", _("English (US)")), # needs to be first to act as fallback language("cs-cz", _("Czech")),("da-dk", _("Danish")),("de-de", _("German")),@@ -321,9 +314,7 @@ LANGUAGES = [("sv-se", _("Swedish")),]-LOCALE_PATHS = [- os.path.join(BASE_DIR, "locale")-]+LOCALE_PATHS = [os.path.join(BASE_DIR, "locale")]TIME_ZONE = os.getenv("PAPERLESS_TIME_ZONE", "UTC")@@ -341,20 +332,20 @@ setup_logging_queues()os.makedirs(LOGGING_DIR, exist_ok=True)-LOGROTATE_MAX_SIZE = os.getenv("PAPERLESS_LOGROTATE_MAX_SIZE", 1024*1024)+LOGROTATE_MAX_SIZE = os.getenv("PAPERLESS_LOGROTATE_MAX_SIZE", 1024 * 1024)LOGROTATE_MAX_BACKUPS = os.getenv("PAPERLESS_LOGROTATE_MAX_BACKUPS", 20)LOGGING = {"version": 1,"disable_existing_loggers": False,- 'formatters': {- 'verbose': {- 'format': '[{asctime}] [{levelname}] [{name}] {message}',- 'style': '{',+ "formatters": {+ "verbose": {+ "format": "[{asctime}] [{levelname}] [{name}] {message}",+ "style": "{",},- 'simple': {- 'format': '{levelname} {message}',- 'style': '{',+ "simple": {+ "format": "{levelname} {message}",+ "style": "{",},},"handlers": {@@ -368,29 +359,21 @@ LOGGING = {"formatter": "verbose","filename": os.path.join(LOGGING_DIR, "paperless.log"),"maxBytes": LOGROTATE_MAX_SIZE,- "backupCount": LOGROTATE_MAX_BACKUPS+ "backupCount": LOGROTATE_MAX_BACKUPS,},"file_mail": {"class": "concurrent_log_handler.ConcurrentRotatingFileHandler","formatter": "verbose","filename": os.path.join(LOGGING_DIR, "mail.log"),"maxBytes": LOGROTATE_MAX_SIZE,- "backupCount": LOGROTATE_MAX_BACKUPS- }- },- "root": {- "handlers": ["console"]+ "backupCount": LOGROTATE_MAX_BACKUPS,+ },},+ "root": {"handlers": ["console"]},"loggers": {- "paperless": {- "handlers": ["file_paperless"],- "level": "DEBUG"- },- "paperless_mail": {- "handlers": ["file_mail"],- "level": "DEBUG"- }- }+ "paperless": {"handlers": ["file_paperless"], "level": "DEBUG"},+ "paperless_mail": {"handlers": ["file_mail"], "level": "DEBUG"},+ },}###############################################################################@@ -412,10 +395,7 @@ def default_task_workers():try:if available_cores < 4:return available_cores- return max(- math.floor(math.sqrt(available_cores)),- 1- )+ return max(math.floor(math.sqrt(available_cores)), 1)except NotImplementedError:return 1@@ -423,13 +403,13 @@ def default_task_workers():TASK_WORKERS = int(os.getenv("PAPERLESS_TASK_WORKERS", default_task_workers()))Q_CLUSTER = {- 'name': 'paperless',- 'catch_up': False,- 'recycle': 1,- 'retry': 1800,- 'timeout': int(os.getenv("PAPERLESS_WORKER_TIMEOUT", 1800)),- 'workers': TASK_WORKERS,- 'redis': os.getenv("PAPERLESS_REDIS", "redis://localhost:6379")+ "name": "paperless",+ "catch_up": False,+ "recycle": 1,+ "retry": 1800,+ "timeout": int(os.getenv("PAPERLESS_WORKER_TIMEOUT", 1800)),+ "workers": TASK_WORKERS,+ "redis": os.getenv("PAPERLESS_REDIS", "redis://localhost:6379"),}@@ -437,15 +417,14 @@ def default_threads_per_worker(task_workers):# always leave one core openavailable_cores = max(multiprocessing.cpu_count(), 1)try:- return max(- math.floor(available_cores / task_workers),- 1- )+ return max(math.floor(available_cores / task_workers), 1)except NotImplementedError:return 1-THREADS_PER_WORKER = os.getenv("PAPERLESS_THREADS_PER_WORKER", default_threads_per_worker(TASK_WORKERS))+THREADS_PER_WORKER = os.getenv(+ "PAPERLESS_THREADS_PER_WORKER", default_threads_per_worker(TASK_WORKERS)+)################################################################################ Paperless Specific Settings #@@ -466,14 +445,18 @@ CONSUMER_RECURSIVE = __get_boolean("PAPERLESS_CONSUMER_RECURSIVE")# Ignore glob patterns, relative to PAPERLESS_CONSUMPTION_DIRCONSUMER_IGNORE_PATTERNS = list(json.loads(- os.getenv("PAPERLESS_CONSUMER_IGNORE_PATTERNS",- '[".DS_STORE/*", "._*", ".stfolder/*"]')))+ os.getenv(+ "PAPERLESS_CONSUMER_IGNORE_PATTERNS",+ '[".DS_STORE/*", "._*", ".stfolder/*"]',+ )+ )+)CONSUMER_SUBDIRS_AS_TAGS = __get_boolean("PAPERLESS_CONSUMER_SUBDIRS_AS_TAGS")OPTIMIZE_THUMBNAILS = __get_boolean("PAPERLESS_OPTIMIZE_THUMBNAILS", "true")-OCR_PAGES = int(os.getenv('PAPERLESS_OCR_PAGES', 0))+OCR_PAGES = int(os.getenv("PAPERLESS_OCR_PAGES", 0))# The default language that tesseract will attempt to use when parsing# documents. It should be a 3-letter language code consistent with ISO 639.@@ -495,7 +478,9 @@ OCR_DESKEW = __get_boolean("PAPERLESS_OCR_DESKEW", "true")OCR_ROTATE_PAGES = __get_boolean("PAPERLESS_OCR_ROTATE_PAGES", "true")-OCR_ROTATE_PAGES_THRESHOLD = float(os.getenv("PAPERLESS_OCR_ROTATE_PAGES_THRESHOLD", 12.0))+OCR_ROTATE_PAGES_THRESHOLD = float(+ os.getenv("PAPERLESS_OCR_ROTATE_PAGES_THRESHOLD", 12.0)+)OCR_USER_ARGS = os.getenv("PAPERLESS_OCR_USER_ARGS", "{}")@@ -542,7 +527,10 @@ for t in json.loads(os.getenv("PAPERLESS_FILENAME_PARSE_TRANSFORMS", "[]")):# Specify the filename format for out filesPAPERLESS_FILENAME_FORMAT = os.getenv("PAPERLESS_FILENAME_FORMAT")-THUMBNAIL_FONT_NAME = os.getenv("PAPERLESS_THUMBNAIL_FONT_NAME", "/usr/share/fonts/liberation/LiberationSerif-Regular.ttf")+THUMBNAIL_FONT_NAME = os.getenv(+ "PAPERLESS_THUMBNAIL_FONT_NAME",+ "/usr/share/fonts/liberation/LiberationSerif-Regular.ttf",+)# Tika settingsPAPERLESS_TIKA_ENABLED = __get_boolean("PAPERLESS_TIKA_ENABLED", "NO")diff --git a/src/paperless/tests/test_checks.py b/src/paperless/tests/test_checks.pyindex e1525cab8..b0301be0e 100644--- a/src/paperless/tests/test_checks.py+++ b/src/paperless/tests/test_checks.py@@ -9,7 +9,6 @@ from paperless.checks import debug_mode_checkclass TestChecks(DirectoriesMixin, TestCase):-def test_binaries(self):self.assertEqual(binaries_check(None), [])@@ -20,9 +19,9 @@ class TestChecks(DirectoriesMixin, TestCase):def test_paths_check(self):self.assertEqual(paths_check(None), [])- @override_settings(MEDIA_ROOT="uuh",- DATA_DIR="whatever",- CONSUMPTION_DIR="idontcare")+ @override_settings(+ MEDIA_ROOT="uuh", DATA_DIR="whatever", CONSUMPTION_DIR="idontcare"+ )def test_paths_check_dont_exist(self):msgs = paths_check(None)self.assertEqual(len(msgs), 3, str(msgs))diff --git a/src/paperless/tests/test_websockets.py b/src/paperless/tests/test_websockets.pyindex e1b948edf..c8cc269fe 100644--- a/src/paperless/tests/test_websockets.py+++ b/src/paperless/tests/test_websockets.py@@ -8,14 +8,13 @@ from paperless.asgi import applicationTEST_CHANNEL_LAYERS = {- 'default': {- 'BACKEND': 'channels.layers.InMemoryChannelLayer',+ "default": {+ "BACKEND": "channels.layers.InMemoryChannelLayer",},}class TestWebSockets(TestCase):-@override_settings(CHANNEL_LAYERS=TEST_CHANNEL_LAYERS)async def test_no_auth(self):communicator = WebsocketCommunicator(application, "/ws/status/")@@ -43,15 +42,12 @@ class TestWebSockets(TestCase):connected, subprotocol = await communicator.connect()self.assertTrue(connected)- message = {- "task_id": "test"- }+ message = {"task_id": "test"}channel_layer = get_channel_layer()- await channel_layer.group_send("status_updates", {- "type": "status_update",- "data": message- })+ await channel_layer.group_send(+ "status_updates", {"type": "status_update", "data": message}+ )response = await communicator.receive_json_from()diff --git a/src/paperless/urls.py b/src/paperless/urls.pyindex eee04af9f..a8c1ed645 100644--- a/src/paperless/urls.py+++ b/src/paperless/urls.py@@ -25,7 +25,7 @@ from documents.views import (SavedViewViewSet,BulkEditView,SelectionDataView,- BulkDownloadView+ BulkDownloadView,)from paperless.views import FaviconView@@ -39,82 +39,101 @@ api_router.register(r"saved_views", SavedViewViewSet)urlpatterns = [- re_path(r"^api/", include([- re_path(r"^auth/",- include(('rest_framework.urls', 'rest_framework'),- namespace="rest_framework")),-- re_path(r"^search/autocomplete/",- SearchAutoCompleteView.as_view(),- name="autocomplete"),-- re_path(r"^statistics/",- StatisticsView.as_view(),- name="statistics"),-- re_path(r"^documents/post_document/", PostDocumentView.as_view(),- name="post_document"),-- re_path(r"^documents/bulk_edit/", BulkEditView.as_view(),- name="bulk_edit"),-- re_path(r"^documents/selection_data/", SelectionDataView.as_view(),- name="selection_data"),-- re_path(r"^documents/bulk_download/", BulkDownloadView.as_view(),- name="bulk_download"),-- path('token/', views.obtain_auth_token)-- ] + api_router.urls)),-+ re_path(+ r"^api/",+ include(+ [+ re_path(+ r"^auth/",+ include(+ ("rest_framework.urls", "rest_framework"),+ namespace="rest_framework",+ ),+ ),+ re_path(+ r"^search/autocomplete/",+ SearchAutoCompleteView.as_view(),+ name="autocomplete",+ ),+ re_path(r"^statistics/", StatisticsView.as_view(), name="statistics"),+ re_path(+ r"^documents/post_document/",+ PostDocumentView.as_view(),+ name="post_document",+ ),+ re_path(+ r"^documents/bulk_edit/", BulkEditView.as_view(), name="bulk_edit"+ ),+ re_path(+ r"^documents/selection_data/",+ SelectionDataView.as_view(),+ name="selection_data",+ ),+ re_path(+ r"^documents/bulk_download/",+ BulkDownloadView.as_view(),+ name="bulk_download",+ ),+ path("token/", views.obtain_auth_token),+ ]+ + api_router.urls+ ),+ ),re_path(r"^favicon.ico$", FaviconView.as_view(), name="favicon"),-re_path(r"admin/", admin.site.urls),-- re_path(r"^fetch/", include([- re_path(- r"^doc/(?P<pk>\d+)$",- RedirectView.as_view(url=settings.BASE_URL +- 'api/documents/%(pk)s/download/'),+ re_path(+ r"^fetch/",+ include(+ [+ re_path(+ r"^doc/(?P<pk>\d+)$",+ RedirectView.as_view(+ url=settings.BASE_URL + "api/documents/%(pk)s/download/"+ ),+ ),+ re_path(+ r"^thumb/(?P<pk>\d+)$",+ RedirectView.as_view(+ url=settings.BASE_URL + "api/documents/%(pk)s/thumb/"+ ),+ ),+ re_path(+ r"^preview/(?P<pk>\d+)$",+ RedirectView.as_view(+ url=settings.BASE_URL + "api/documents/%(pk)s/preview/"+ ),+ ),+ ]),- re_path(- r"^thumb/(?P<pk>\d+)$",- RedirectView.as_view(url=settings.BASE_URL +- 'api/documents/%(pk)s/thumb/'),+ ),+ re_path(+ r"^push$",+ csrf_exempt(+ RedirectView.as_view(url=settings.BASE_URL + "api/documents/post_document/")),- re_path(- r"^preview/(?P<pk>\d+)$",- RedirectView.as_view(url=settings.BASE_URL +- 'api/documents/%(pk)s/preview/'),- ),- ])),-- re_path(r"^push$", csrf_exempt(- RedirectView.as_view(url=settings.BASE_URL +- 'api/documents/post_document/'))),-+ ),# Frontend assets TODO: this is pretty bad, but it works.- path('assets/<path:path>',- RedirectView.as_view(url=settings.STATIC_URL +- 'frontend/en-US/assets/%(path)s')),+ path(+ "assets/<path:path>",+ RedirectView.as_view(+ url=settings.STATIC_URL + "frontend/en-US/assets/%(path)s"+ ),+ ),# TODO: with localization, this is even worse! :/-# login, logout- path('accounts/', include('django.contrib.auth.urls')),-+ path("accounts/", include("django.contrib.auth.urls")),# Root of the Frontent- re_path(r".*", login_required(IndexView.as_view()), name='base'),+ re_path(r".*", login_required(IndexView.as_view()), name="base"),]websocket_urlpatterns = [- re_path(r'ws/status/$', StatusConsumer.as_asgi()),+ re_path(r"ws/status/$", StatusConsumer.as_asgi()),]# Text in each page's <h1> (and above login form).-admin.site.site_header = 'Paperless-ng'+admin.site.site_header = "Paperless-ng"# Text at the end of each page's <title>.-admin.site.site_title = 'Paperless-ng'+admin.site.site_title = "Paperless-ng"# Text at the top of the admin index page.-admin.site.index_title = _('Paperless-ng administration')+admin.site.index_title = _("Paperless-ng administration")diff --git a/src/paperless/views.py b/src/paperless/views.pyindex 560a27980..a6a37a679 100644--- a/src/paperless/views.py+++ b/src/paperless/views.py@@ -12,14 +12,9 @@ class StandardPagination(PageNumberPagination):class FaviconView(View):-def get(self, request, *args, **kwargs):favicon = os.path.join(- os.path.dirname(__file__),- "static",- "paperless",- "img",- "favicon.ico"+ os.path.dirname(__file__), "static", "paperless", "img", "favicon.ico")with open(favicon, "rb") as f:return HttpResponse(f, content_type="image/x-icon")diff --git a/src/paperless_mail/admin.py b/src/paperless_mail/admin.pyindex c7eaa0a8c..3c9ae0f56 100644--- a/src/paperless_mail/admin.py+++ b/src/paperless_mail/admin.py@@ -15,9 +15,9 @@ class MailAccountAdminForm(forms.ModelForm):model = MailAccountwidgets = {- 'password': forms.PasswordInput(),+ "password": forms.PasswordInput(),}- fields = '__all__'+ fields = "__all__"class MailAccountAdmin(admin.ModelAdmin):@@ -25,15 +25,9 @@ class MailAccountAdmin(admin.ModelAdmin):list_display = ("name", "imap_server", "username")fieldsets = [- (None, {- 'fields': ['name', 'imap_server', 'imap_port']- }),- (_("Authentication"), {- 'fields': ['imap_security', 'username', 'password']- }),- (_("Advanced settings"), {- 'fields': ['character_set']- })+ (None, {"fields": ["name", "imap_server", "imap_port"]}),+ (_("Authentication"), {"fields": ["imap_security", "username", "password"]}),+ (_("Advanced settings"), {"fields": ["character_set"]}),]form = MailAccountAdminForm@@ -44,56 +38,66 @@ class MailRuleAdmin(admin.ModelAdmin):"attachment_type": admin.VERTICAL,"action": admin.VERTICAL,"assign_title_from": admin.VERTICAL,- "assign_correspondent_from": admin.VERTICAL+ "assign_correspondent_from": admin.VERTICAL,}fieldsets = (- (None, {- 'fields': ('name', 'order', 'account', 'folder')- }),- (_("Filter"), {- 'description':- _("Paperless will only process mails that match ALL of the "- "filters given below."),- 'fields':- ('filter_from',- 'filter_subject',- 'filter_body',- 'filter_attachment_filename',- 'maximum_age',- 'attachment_type')- }),- (_("Actions"), {- 'description':- _("The action applied to the mail. This action is only "- "performed when documents were consumed from the mail. "- "Mails without attachments will remain entirely untouched."),- 'fields': (- 'action',- 'action_parameter')- }),- (_("Metadata"), {- 'description':- _("Assign metadata to documents consumed from this rule "- "automatically. If you do not assign tags, types or "- "correspondents here, paperless will still process all "- "matching rules that you have defined."),- "fields": (- 'assign_title_from',- 'assign_tag',- 'assign_document_type',- 'assign_correspondent_from',- 'assign_correspondent')- })+ (None, {"fields": ("name", "order", "account", "folder")}),+ (+ _("Filter"),+ {+ "description": _(+ "Paperless will only process mails that match ALL of the "+ "filters given below."+ ),+ "fields": (+ "filter_from",+ "filter_subject",+ "filter_body",+ "filter_attachment_filename",+ "maximum_age",+ "attachment_type",+ ),+ },+ ),+ (+ _("Actions"),+ {+ "description": _(+ "The action applied to the mail. This action is only "+ "performed when documents were consumed from the mail. "+ "Mails without attachments will remain entirely untouched."+ ),+ "fields": ("action", "action_parameter"),+ },+ ),+ (+ _("Metadata"),+ {+ "description": _(+ "Assign metadata to documents consumed from this rule "+ "automatically. If you do not assign tags, types or "+ "correspondents here, paperless will still process all "+ "matching rules that you have defined."+ ),+ "fields": (+ "assign_title_from",+ "assign_tag",+ "assign_document_type",+ "assign_correspondent_from",+ "assign_correspondent",+ ),+ },+ ),)list_filter = ("account",)list_display = ("order", "name", "account", "folder", "action")- list_editable = ("order", )+ list_editable = ("order",)- list_display_links = ("name", )+ list_display_links = ("name",)sortable_by = []diff --git a/src/paperless_mail/apps.py b/src/paperless_mail/apps.pyindex efbea745b..f55240852 100644--- a/src/paperless_mail/apps.py+++ b/src/paperless_mail/apps.py@@ -4,6 +4,6 @@ from django.utils.translation import gettext_lazy as _class PaperlessMailConfig(AppConfig):- name = 'paperless_mail'+ name = "paperless_mail"- verbose_name = _('Paperless mail')+ verbose_name = _("Paperless mail")diff --git a/src/paperless_mail/mail.py b/src/paperless_mail/mail.pyindex 327731a84..bc35f0e0d 100644--- a/src/paperless_mail/mail.py+++ b/src/paperless_mail/mail.py@@ -8,8 +8,13 @@ import pathvalidatefrom django.conf import settingsfrom django.db import DatabaseErrorfrom django_q.tasks import async_task-from imap_tools import MailBox, MailBoxUnencrypted, AND, MailMessageFlags, \- MailboxFolderSelectError+from imap_tools import (+ MailBox,+ MailBoxUnencrypted,+ AND,+ MailMessageFlags,+ MailboxFolderSelectError,+)from documents.loggers import LoggingMixinfrom documents.models import Correspondent@@ -22,7 +27,6 @@ class MailError(Exception):class BaseMailAction:-def get_criteria(self):return {}@@ -31,30 +35,26 @@ class BaseMailAction:class DeleteMailAction(BaseMailAction):-def post_consume(self, M, message_uids, parameter):M.delete(message_uids)class MarkReadMailAction(BaseMailAction):-def get_criteria(self):- return {'seen': False}+ return {"seen": False}def post_consume(self, M, message_uids, parameter):M.seen(message_uids, True)class MoveMailAction(BaseMailAction):-def post_consume(self, M, message_uids, parameter):M.move(message_uids, parameter)class FlagMailAction(BaseMailAction):-def get_criteria(self):- return {'flagged': False}+ return {"flagged": False}def post_consume(self, M, message_uids, parameter):M.flag(message_uids, [MailMessageFlags.FLAGGED], True)@@ -108,10 +108,7 @@ class MailAccountHandler(LoggingMixin):try:return Correspondent.objects.get_or_create(name=name)[0]except DatabaseError as e:- self.log(- "error",- f"Error while retrieving correspondent {name}: {e}"- )+ self.log("error", f"Error while retrieving correspondent {name}: {e}")return Nonedef get_title(self, message, att, rule):@@ -122,7 +119,9 @@ class MailAccountHandler(LoggingMixin):return os.path.splitext(os.path.basename(att.filename))[0]else:- raise NotImplementedError("Unknown title selector.") # pragma: nocover # NOQA: E501+ raise NotImplementedError(+ "Unknown title selector."+ ) # pragma: nocover # NOQA: E501def get_correspondent(self, message, rule):c_from = rule.assign_correspondent_from@@ -134,9 +133,12 @@ class MailAccountHandler(LoggingMixin):return self._correspondent_from_name(message.from_)elif c_from == MailRule.CORRESPONDENT_FROM_NAME:- if message.from_values and 'name' in message.from_values and message.from_values['name']: # NOQA: E501- return self._correspondent_from_name(- message.from_values['name'])+ if (+ message.from_values+ and "name" in message.from_values+ and message.from_values["name"]+ ): # NOQA: E501+ return self._correspondent_from_name(message.from_values["name"])else:return self._correspondent_from_name(message.from_)@@ -144,69 +146,71 @@ class MailAccountHandler(LoggingMixin):return rule.assign_correspondentelse:- raise NotImplementedError("Unknwown correspondent selector") # pragma: nocover # NOQA: E501+ raise NotImplementedError(+ "Unknwown correspondent selector"+ ) # pragma: nocover # NOQA: E501def handle_mail_account(self, account):self.renew_logging_group()- self.log('debug', f"Processing mail account {account}")+ self.log("debug", f"Processing mail account {account}")total_processed_files = 0- with get_mailbox(account.imap_server,- account.imap_port,- account.imap_security) as M:+ with get_mailbox(+ account.imap_server, account.imap_port, account.imap_security+ ) as M:try:M.login(account.username, account.password)except Exception:- raise MailError(- f"Error while authenticating account {account}")+ raise MailError(f"Error while authenticating account {account}")- self.log('debug', f"Account {account}: Processing "- f"{account.rules.count()} rule(s)")+ self.log(+ "debug",+ f"Account {account}: Processing " f"{account.rules.count()} rule(s)",+ )- for rule in account.rules.order_by('order'):+ for rule in account.rules.order_by("order"):try:total_processed_files += self.handle_mail_rule(M, rule)except Exception as e:self.log("error",f"Rule {rule}: Error while processing rule: {e}",- exc_info=True+ exc_info=True,)return total_processed_filesdef handle_mail_rule(self, M, rule):- self.log(- 'debug',- f"Rule {rule}: Selecting folder {rule.folder}")+ self.log("debug", f"Rule {rule}: Selecting folder {rule.folder}")try:M.folder.set(rule.folder)except MailboxFolderSelectError:raise MailError(f"Rule {rule}: Folder {rule.folder} "- f"does not exist in account {rule.account}")+ f"does not exist in account {rule.account}"+ )criterias = make_criterias(rule)self.log(- 'debug',- f"Rule {rule}: Searching folder with criteria "- f"{str(AND(**criterias))}")+ "debug",+ f"Rule {rule}: Searching folder with criteria " f"{str(AND(**criterias))}",+ )try:messages = M.fetch(criteria=AND(**criterias),mark_seen=False,- charset=rule.account.character_set)+ charset=rule.account.character_set,+ )except Exception:- raise MailError(- f"Rule {rule}: Error while fetching folder {rule.folder}")+ raise MailError(f"Rule {rule}: Error while fetching folder {rule.folder}")post_consume_messages = []@@ -224,29 +228,27 @@ class MailAccountHandler(LoggingMixin):except Exception as e:self.log("error",- f"Rule {rule}: Error while processing mail "- f"{message.uid}: {e}",- exc_info=True)+ f"Rule {rule}: Error while processing mail " f"{message.uid}: {e}",+ exc_info=True,+ )- self.log(- 'debug',- f"Rule {rule}: Processed {mails_processed} matching mail(s)")+ self.log("debug", f"Rule {rule}: Processed {mails_processed} matching mail(s)")self.log(- 'debug',+ "debug",f"Rule {rule}: Running mail actions on "- f"{len(post_consume_messages)} mails")+ f"{len(post_consume_messages)} mails",+ )try:get_rule_action(rule).post_consume(- M,- post_consume_messages,- rule.action_parameter)+ M, post_consume_messages, rule.action_parameter+ )except Exception as e:raise MailError(- f"Rule {rule}: Error while processing post-consume actions: "- f"{e}")+ f"Rule {rule}: Error while processing post-consume actions: " f"{e}"+ )return total_processed_files@@ -255,10 +257,11 @@ class MailAccountHandler(LoggingMixin):return 0self.log(- 'debug',+ "debug",f"Rule {rule}: "f"Processing mail {message.subject} from {message.from_} with "- f"{len(message.attachments)} attachment(s)")+ f"{len(message.attachments)} attachment(s)",+ )correspondent = self.get_correspondent(message, rule)tag = rule.assign_tag@@ -268,12 +271,16 @@ class MailAccountHandler(LoggingMixin):for att in message.attachments:- if not att.content_disposition == "attachment" and rule.attachment_type == MailRule.ATTACHMENT_TYPE_ATTACHMENTS_ONLY: # NOQA: E501+ if (+ not att.content_disposition == "attachment"+ and rule.attachment_type == MailRule.ATTACHMENT_TYPE_ATTACHMENTS_ONLY+ ): # NOQA: E501self.log(- 'debug',+ "debug",f"Rule {rule}: "f"Skipping attachment {att.filename} "- f"with content disposition {att.content_disposition}")+ f"with content disposition {att.content_disposition}",+ )continueif rule.filter_attachment_filename:@@ -289,35 +296,44 @@ class MailAccountHandler(LoggingMixin):if is_mime_type_supported(mime_type):os.makedirs(settings.SCRATCH_DIR, exist_ok=True)- _, temp_filename = tempfile.mkstemp(prefix="paperless-mail-",- dir=settings.SCRATCH_DIR)- with open(temp_filename, 'wb') as f:+ _, temp_filename = tempfile.mkstemp(+ prefix="paperless-mail-", dir=settings.SCRATCH_DIR+ )+ with open(temp_filename, "wb") as f:f.write(att.payload)self.log(- 'info',+ "info",f"Rule {rule}: "f"Consuming attachment {att.filename} from mail "- f"{message.subject} from {message.from_}")+ f"{message.subject} from {message.from_}",+ )async_task("documents.tasks.consume_file",path=temp_filename,- override_filename=pathvalidate.sanitize_filename(att.filename), # NOQA: E501+ override_filename=pathvalidate.sanitize_filename(+ att.filename+ ), # NOQA: E501override_title=title,- override_correspondent_id=correspondent.id if correspondent else None, # NOQA: E501- override_document_type_id=doc_type.id if doc_type else None, # NOQA: E501+ override_correspondent_id=correspondent.id+ if correspondent+ else None, # NOQA: E501+ override_document_type_id=doc_type.id+ if doc_type+ else None, # NOQA: E501override_tag_ids=[tag.id] if tag else None,- task_name=att.filename[:100]+ task_name=att.filename[:100],)processed_attachments += 1else:self.log(- 'debug',+ "debug",f"Rule {rule}: "f"Skipping attachment {att.filename} "f"since guessed mime type {mime_type} is not supported "- f"by paperless")+ f"by paperless",+ )return processed_attachmentsdiff --git a/src/paperless_mail/management/commands/mail_fetcher.py b/src/paperless_mail/management/commands/mail_fetcher.pyindex b11b5b70d..642633660 100644--- a/src/paperless_mail/management/commands/mail_fetcher.py+++ b/src/paperless_mail/management/commands/mail_fetcher.py@@ -6,7 +6,9 @@ from paperless_mail import tasksclass Command(BaseCommand):help = """- """.replace(" ", "")+ """.replace(+ " ", ""+ )def handle(self, *args, **options):diff --git a/src/paperless_mail/migrations/0001_initial.py b/src/paperless_mail/migrations/0001_initial.pyindex bb6328c60..dbc6e467f 100644--- a/src/paperless_mail/migrations/0001_initial.py+++ b/src/paperless_mail/migrations/0001_initial.py@@ -9,40 +9,146 @@ class Migration(migrations.Migration):initial = Truedependencies = [- ('documents', '1002_auto_20201111_1105'),+ ("documents", "1002_auto_20201111_1105"),]operations = [migrations.CreateModel(- name='MailAccount',+ name="MailAccount",fields=[- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),- ('name', models.CharField(max_length=256, unique=True)),- ('imap_server', models.CharField(max_length=256)),- ('imap_port', models.IntegerField(blank=True, null=True)),- ('imap_security', models.PositiveIntegerField(choices=[(1, 'No encryption'), (2, 'Use SSL'), (3, 'Use STARTTLS')], default=2)),- ('username', models.CharField(max_length=256)),- ('password', models.CharField(max_length=256)),+ (+ "id",+ models.AutoField(+ auto_created=True,+ primary_key=True,+ serialize=False,+ verbose_name="ID",+ ),+ ),+ ("name", models.CharField(max_length=256, unique=True)),+ ("imap_server", models.CharField(max_length=256)),+ ("imap_port", models.IntegerField(blank=True, null=True)),+ (+ "imap_security",+ models.PositiveIntegerField(+ choices=[+ (1, "No encryption"),+ (2, "Use SSL"),+ (3, "Use STARTTLS"),+ ],+ default=2,+ ),+ ),+ ("username", models.CharField(max_length=256)),+ ("password", models.CharField(max_length=256)),],),migrations.CreateModel(- name='MailRule',+ name="MailRule",fields=[- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),- ('name', models.CharField(max_length=256)),- ('folder', models.CharField(default='INBOX', max_length=256)),- ('filter_from', models.CharField(blank=True, max_length=256, null=True)),- ('filter_subject', models.CharField(blank=True, max_length=256, null=True)),- ('filter_body', models.CharField(blank=True, max_length=256, null=True)),- ('maximum_age', models.PositiveIntegerField(default=30)),- ('action', models.PositiveIntegerField(choices=[(1, 'Delete'), (2, 'Move to specified folder'), (3, "Mark as read, don't process read mails"), (4, "Flag the mail, don't process flagged mails")], default=3, help_text='The action applied to the mail. This action is only performed when documents were consumed from the mail. Mails without attachments will remain entirely untouched.')),- ('action_parameter', models.CharField(blank=True, help_text='Additional parameter for the action selected above, i.e., the target folder of the move to folder action.', max_length=256, null=True)),- ('assign_title_from', models.PositiveIntegerField(choices=[(1, 'Use subject as title'), (2, 'Use attachment filename as title')], default=1)),- ('assign_correspondent_from', models.PositiveIntegerField(choices=[(1, 'Do not assign a correspondent'), (2, 'Use mail address'), (3, 'Use name (or mail address if not available)'), (4, 'Use correspondent selected below')], default=1)),- ('account', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='rules', to='paperless_mail.mailaccount')),- ('assign_correspondent', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='documents.correspondent')),- ('assign_document_type', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='documents.documenttype')),- ('assign_tag', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='documents.tag')),+ (+ "id",+ models.AutoField(+ auto_created=True,+ primary_key=True,+ serialize=False,+ verbose_name="ID",+ ),+ ),+ ("name", models.CharField(max_length=256)),+ ("folder", models.CharField(default="INBOX", max_length=256)),+ (+ "filter_from",+ models.CharField(blank=True, max_length=256, null=True),+ ),+ (+ "filter_subject",+ models.CharField(blank=True, max_length=256, null=True),+ ),+ (+ "filter_body",+ models.CharField(blank=True, max_length=256, null=True),+ ),+ ("maximum_age", models.PositiveIntegerField(default=30)),+ (+ "action",+ models.PositiveIntegerField(+ choices=[+ (1, "Delete"),+ (2, "Move to specified folder"),+ (3, "Mark as read, don't process read mails"),+ (4, "Flag the mail, don't process flagged mails"),+ ],+ default=3,+ help_text="The action applied to the mail. This action is only performed when documents were consumed from the mail. Mails without attachments will remain entirely untouched.",+ ),+ ),+ (+ "action_parameter",+ models.CharField(+ blank=True,+ help_text="Additional parameter for the action selected above, i.e., the target folder of the move to folder action.",+ max_length=256,+ null=True,+ ),+ ),+ (+ "assign_title_from",+ models.PositiveIntegerField(+ choices=[+ (1, "Use subject as title"),+ (2, "Use attachment filename as title"),+ ],+ default=1,+ ),+ ),+ (+ "assign_correspondent_from",+ models.PositiveIntegerField(+ choices=[+ (1, "Do not assign a correspondent"),+ (2, "Use mail address"),+ (3, "Use name (or mail address if not available)"),+ (4, "Use correspondent selected below"),+ ],+ default=1,+ ),+ ),+ (+ "account",+ models.ForeignKey(+ on_delete=django.db.models.deletion.CASCADE,+ related_name="rules",+ to="paperless_mail.mailaccount",+ ),+ ),+ (+ "assign_correspondent",+ models.ForeignKey(+ blank=True,+ null=True,+ on_delete=django.db.models.deletion.SET_NULL,+ to="documents.correspondent",+ ),+ ),+ (+ "assign_document_type",+ models.ForeignKey(+ blank=True,+ null=True,+ on_delete=django.db.models.deletion.SET_NULL,+ to="documents.documenttype",+ ),+ ),+ (+ "assign_tag",+ models.ForeignKey(+ blank=True,+ null=True,+ on_delete=django.db.models.deletion.SET_NULL,+ to="documents.tag",+ ),+ ),],),]diff --git a/src/paperless_mail/migrations/0002_auto_20201117_1334.py b/src/paperless_mail/migrations/0002_auto_20201117_1334.pyindex 7a29ba248..5b29b3072 100644--- a/src/paperless_mail/migrations/0002_auto_20201117_1334.py+++ b/src/paperless_mail/migrations/0002_auto_20201117_1334.py@@ -7,26 +7,23 @@ from django_q.tasks import scheduledef add_schedules(apps, schema_editor):- schedule('paperless_mail.tasks.process_mail_accounts',- name="Check all e-mail accounts",- schedule_type=Schedule.MINUTES,- minutes=10)+ schedule(+ "paperless_mail.tasks.process_mail_accounts",+ name="Check all e-mail accounts",+ schedule_type=Schedule.MINUTES,+ minutes=10,+ )def remove_schedules(apps, schema_editor):- Schedule.objects.filter(- func='paperless_mail.tasks.process_mail_accounts').delete()+ Schedule.objects.filter(func="paperless_mail.tasks.process_mail_accounts").delete()class Migration(migrations.Migration):dependencies = [- ('paperless_mail', '0001_initial'),- ('django_q', '0013_task_attempt_count'),+ ("paperless_mail", "0001_initial"),+ ("django_q", "0013_task_attempt_count"),]- operations = [- RunPython(add_schedules, remove_schedules)- ]--+ operations = [RunPython(add_schedules, remove_schedules)]diff --git a/src/paperless_mail/migrations/0003_auto_20201118_1940.py b/src/paperless_mail/migrations/0003_auto_20201118_1940.pyindex 3339a6d7f..30a882b03 100644--- a/src/paperless_mail/migrations/0003_auto_20201118_1940.py+++ b/src/paperless_mail/migrations/0003_auto_20201118_1940.py@@ -6,18 +6,22 @@ from django.db import migrations, modelsclass Migration(migrations.Migration):dependencies = [- ('paperless_mail', '0002_auto_20201117_1334'),+ ("paperless_mail", "0002_auto_20201117_1334"),]operations = [migrations.AlterField(- model_name='mailaccount',- name='imap_port',- field=models.IntegerField(blank=True, help_text='This is usually 143 for unencrypted and STARTTLS connections, and 993 for SSL connections.', null=True),+ model_name="mailaccount",+ name="imap_port",+ field=models.IntegerField(+ blank=True,+ help_text="This is usually 143 for unencrypted and STARTTLS connections, and 993 for SSL connections.",+ null=True,+ ),),migrations.AlterField(- model_name='mailrule',- name='name',+ model_name="mailrule",+ name="name",field=models.CharField(max_length=256, unique=True),),]diff --git a/src/paperless_mail/migrations/0004_mailrule_order.py b/src/paperless_mail/migrations/0004_mailrule_order.pyindex 498f280a1..71b404185 100644--- a/src/paperless_mail/migrations/0004_mailrule_order.py+++ b/src/paperless_mail/migrations/0004_mailrule_order.py@@ -6,13 +6,13 @@ from django.db import migrations, modelsclass Migration(migrations.Migration):dependencies = [- ('paperless_mail', '0003_auto_20201118_1940'),+ ("paperless_mail", "0003_auto_20201118_1940"),]operations = [migrations.AddField(- model_name='mailrule',- name='order',+ model_name="mailrule",+ name="order",field=models.IntegerField(default=0),),]diff --git a/src/paperless_mail/migrations/0005_help_texts.py b/src/paperless_mail/migrations/0005_help_texts.pyindex 71899c8ef..ba1ab397c 100644--- a/src/paperless_mail/migrations/0005_help_texts.py+++ b/src/paperless_mail/migrations/0005_help_texts.py@@ -6,18 +6,28 @@ from django.db import migrations, modelsclass Migration(migrations.Migration):dependencies = [- ('paperless_mail', '0004_mailrule_order'),+ ("paperless_mail", "0004_mailrule_order"),]operations = [migrations.AlterField(- model_name='mailrule',- name='action',- field=models.PositiveIntegerField(choices=[(3, "Mark as read, don't process read mails"), (4, "Flag the mail, don't process flagged mails"), (2, 'Move to specified folder'), (1, 'Delete')], default=3),+ model_name="mailrule",+ name="action",+ field=models.PositiveIntegerField(+ choices=[+ (3, "Mark as read, don't process read mails"),+ (4, "Flag the mail, don't process flagged mails"),+ (2, "Move to specified folder"),+ (1, "Delete"),+ ],+ default=3,+ ),),migrations.AlterField(- model_name='mailrule',- name='maximum_age',- field=models.PositiveIntegerField(default=30, help_text='Specified in days.'),+ model_name="mailrule",+ name="maximum_age",+ field=models.PositiveIntegerField(+ default=30, help_text="Specified in days."+ ),),]diff --git a/src/paperless_mail/migrations/0006_auto_20210101_2340.py b/src/paperless_mail/migrations/0006_auto_20210101_2340.pyindex c03ee627d..1776ce4d0 100644--- a/src/paperless_mail/migrations/0006_auto_20210101_2340.py+++ b/src/paperless_mail/migrations/0006_auto_20210101_2340.py@@ -7,122 +7,198 @@ import django.db.models.deletionclass Migration(migrations.Migration):dependencies = [- ('documents', '1011_auto_20210101_2340'),- ('paperless_mail', '0005_help_texts'),+ ("documents", "1011_auto_20210101_2340"),+ ("paperless_mail", "0005_help_texts"),]operations = [migrations.AlterModelOptions(- name='mailaccount',- options={'verbose_name': 'mail account', 'verbose_name_plural': 'mail accounts'},+ name="mailaccount",+ options={+ "verbose_name": "mail account",+ "verbose_name_plural": "mail accounts",+ },),migrations.AlterModelOptions(- name='mailrule',- options={'verbose_name': 'mail rule', 'verbose_name_plural': 'mail rules'},- ),- migrations.AlterField(- model_name='mailaccount',- name='imap_port',- field=models.IntegerField(blank=True, help_text='This is usually 143 for unencrypted and STARTTLS connections, and 993 for SSL connections.', null=True, verbose_name='IMAP port'),- ),- migrations.AlterField(- model_name='mailaccount',- name='imap_security',- field=models.PositiveIntegerField(choices=[(1, 'No encryption'), (2, 'Use SSL'), (3, 'Use STARTTLS')], default=2, verbose_name='IMAP security'),- ),- migrations.AlterField(- model_name='mailaccount',- name='imap_server',- field=models.CharField(max_length=256, verbose_name='IMAP server'),- ),- migrations.AlterField(- model_name='mailaccount',- name='name',- field=models.CharField(max_length=256, unique=True, verbose_name='name'),- ),- migrations.AlterField(- model_name='mailaccount',- name='password',- field=models.CharField(max_length=256, verbose_name='password'),- ),- migrations.AlterField(- model_name='mailaccount',- name='username',- field=models.CharField(max_length=256, verbose_name='username'),- ),- migrations.AlterField(- model_name='mailrule',- name='account',- field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='rules', to='paperless_mail.mailaccount', verbose_name='account'),- ),- migrations.AlterField(- model_name='mailrule',- name='action',- field=models.PositiveIntegerField(choices=[(3, "Mark as read, don't process read mails"), (4, "Flag the mail, don't process flagged mails"), (2, 'Move to specified folder'), (1, 'Delete')], default=3, verbose_name='action'),- ),- migrations.AlterField(- model_name='mailrule',- name='action_parameter',- field=models.CharField(blank=True, help_text='Additional parameter for the action selected above, i.e., the target folder of the move to folder action.', max_length=256, null=True, verbose_name='action parameter'),- ),- migrations.AlterField(- model_name='mailrule',- name='assign_correspondent',- field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='documents.correspondent', verbose_name='assign this correspondent'),- ),- migrations.AlterField(- model_name='mailrule',- name='assign_correspondent_from',- field=models.PositiveIntegerField(choices=[(1, 'Do not assign a correspondent'), (2, 'Use mail address'), (3, 'Use name (or mail address if not available)'), (4, 'Use correspondent selected below')], default=1, verbose_name='assign correspondent from'),- ),- migrations.AlterField(- model_name='mailrule',- name='assign_document_type',- field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='documents.documenttype', verbose_name='assign this document type'),- ),- migrations.AlterField(- model_name='mailrule',- name='assign_tag',- field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='documents.tag', verbose_name='assign this tag'),- ),- migrations.AlterField(- model_name='mailrule',- name='assign_title_from',- field=models.PositiveIntegerField(choices=[(1, 'Use subject as title'), (2, 'Use attachment filename as title')], default=1, verbose_name='assign title from'),- ),- migrations.AlterField(- model_name='mailrule',- name='filter_body',- field=models.CharField(blank=True, max_length=256, null=True, verbose_name='filter body'),- ),- migrations.AlterField(- model_name='mailrule',- name='filter_from',- field=models.CharField(blank=True, max_length=256, null=True, verbose_name='filter from'),- ),- migrations.AlterField(- model_name='mailrule',- name='filter_subject',- field=models.CharField(blank=True, max_length=256, null=True, verbose_name='filter subject'),- ),- migrations.AlterField(- model_name='mailrule',- name='folder',- field=models.CharField(default='INBOX', max_length=256, verbose_name='folder'),- ),- migrations.AlterField(- model_name='mailrule',- name='maximum_age',- field=models.PositiveIntegerField(default=30, help_text='Specified in days.', verbose_name='maximum age'),- ),- migrations.AlterField(- model_name='mailrule',- name='name',- field=models.CharField(max_length=256, unique=True, verbose_name='name'),- ),- migrations.AlterField(- model_name='mailrule',- name='order',- field=models.IntegerField(default=0, verbose_name='order'),+ name="mailrule",+ options={"verbose_name": "mail rule", "verbose_name_plural": "mail rules"},+ ),+ migrations.AlterField(+ model_name="mailaccount",+ name="imap_port",+ field=models.IntegerField(+ blank=True,+ help_text="This is usually 143 for unencrypted and STARTTLS connections, and 993 for SSL connections.",+ null=True,+ verbose_name="IMAP port",+ ),+ ),+ migrations.AlterField(+ model_name="mailaccount",+ name="imap_security",+ field=models.PositiveIntegerField(+ choices=[(1, "No encryption"), (2, "Use SSL"), (3, "Use STARTTLS")],+ default=2,+ verbose_name="IMAP security",+ ),+ ),+ migrations.AlterField(+ model_name="mailaccount",+ name="imap_server",+ field=models.CharField(max_length=256, verbose_name="IMAP server"),+ ),+ migrations.AlterField(+ model_name="mailaccount",+ name="name",+ field=models.CharField(max_length=256, unique=True, verbose_name="name"),+ ),+ migrations.AlterField(+ model_name="mailaccount",+ name="password",+ field=models.CharField(max_length=256, verbose_name="password"),+ ),+ migrations.AlterField(+ model_name="mailaccount",+ name="username",+ field=models.CharField(max_length=256, verbose_name="username"),+ ),+ migrations.AlterField(+ model_name="mailrule",+ name="account",+ field=models.ForeignKey(+ on_delete=django.db.models.deletion.CASCADE,+ related_name="rules",+ to="paperless_mail.mailaccount",+ verbose_name="account",+ ),+ ),+ migrations.AlterField(+ model_name="mailrule",+ name="action",+ field=models.PositiveIntegerField(+ choices=[+ (3, "Mark as read, don't process read mails"),+ (4, "Flag the mail, don't process flagged mails"),+ (2, "Move to specified folder"),+ (1, "Delete"),+ ],+ default=3,+ verbose_name="action",+ ),+ ),+ migrations.AlterField(+ model_name="mailrule",+ name="action_parameter",+ field=models.CharField(+ blank=True,+ help_text="Additional parameter for the action selected above, i.e., the target folder of the move to folder action.",+ max_length=256,+ null=True,+ verbose_name="action parameter",+ ),+ ),+ migrations.AlterField(+ model_name="mailrule",+ name="assign_correspondent",+ field=models.ForeignKey(+ blank=True,+ null=True,+ on_delete=django.db.models.deletion.SET_NULL,+ to="documents.correspondent",+ verbose_name="assign this correspondent",+ ),+ ),+ migrations.AlterField(+ model_name="mailrule",+ name="assign_correspondent_from",+ field=models.PositiveIntegerField(+ choices=[+ (1, "Do not assign a correspondent"),+ (2, "Use mail address"),+ (3, "Use name (or mail address if not available)"),+ (4, "Use correspondent selected below"),+ ],+ default=1,+ verbose_name="assign correspondent from",+ ),+ ),+ migrations.AlterField(+ model_name="mailrule",+ name="assign_document_type",+ field=models.ForeignKey(+ blank=True,+ null=True,+ on_delete=django.db.models.deletion.SET_NULL,+ to="documents.documenttype",+ verbose_name="assign this document type",+ ),+ ),+ migrations.AlterField(+ model_name="mailrule",+ name="assign_tag",+ field=models.ForeignKey(+ blank=True,+ null=True,+ on_delete=django.db.models.deletion.SET_NULL,+ to="documents.tag",+ verbose_name="assign this tag",+ ),+ ),+ migrations.AlterField(+ model_name="mailrule",+ name="assign_title_from",+ field=models.PositiveIntegerField(+ choices=[+ (1, "Use subject as title"),+ (2, "Use attachment filename as title"),+ ],+ default=1,+ verbose_name="assign title from",+ ),+ ),+ migrations.AlterField(+ model_name="mailrule",+ name="filter_body",+ field=models.CharField(+ blank=True, max_length=256, null=True, verbose_name="filter body"+ ),+ ),+ migrations.AlterField(+ model_name="mailrule",+ name="filter_from",+ field=models.CharField(+ blank=True, max_length=256, null=True, verbose_name="filter from"+ ),+ ),+ migrations.AlterField(+ model_name="mailrule",+ name="filter_subject",+ field=models.CharField(+ blank=True, max_length=256, null=True, verbose_name="filter subject"+ ),+ ),+ migrations.AlterField(+ model_name="mailrule",+ name="folder",+ field=models.CharField(+ default="INBOX", max_length=256, verbose_name="folder"+ ),+ ),+ migrations.AlterField(+ model_name="mailrule",+ name="maximum_age",+ field=models.PositiveIntegerField(+ default=30, help_text="Specified in days.", verbose_name="maximum age"+ ),+ ),+ migrations.AlterField(+ model_name="mailrule",+ name="name",+ field=models.CharField(max_length=256, unique=True, verbose_name="name"),+ ),+ migrations.AlterField(+ model_name="mailrule",+ name="order",+ field=models.IntegerField(default=0, verbose_name="order"),),]diff --git a/src/paperless_mail/migrations/0007_auto_20210106_0138.py b/src/paperless_mail/migrations/0007_auto_20210106_0138.pyindex 2da9eecc9..3325a86f7 100644--- a/src/paperless_mail/migrations/0007_auto_20210106_0138.py+++ b/src/paperless_mail/migrations/0007_auto_20210106_0138.py@@ -6,18 +6,32 @@ from django.db import migrations, modelsclass Migration(migrations.Migration):dependencies = [- ('paperless_mail', '0006_auto_20210101_2340'),+ ("paperless_mail", "0006_auto_20210101_2340"),]operations = [migrations.AddField(- model_name='mailrule',- name='attachment_type',- field=models.PositiveIntegerField(choices=[(1, 'Only process attachments.'), (2, "Process all files, including 'inline' attachments.")], default=1, help_text="Inline attachments include embedded images, so it's best to combine this option with a filename filter.", verbose_name='attachment type'),+ model_name="mailrule",+ name="attachment_type",+ field=models.PositiveIntegerField(+ choices=[+ (1, "Only process attachments."),+ (2, "Process all files, including 'inline' attachments."),+ ],+ default=1,+ help_text="Inline attachments include embedded images, so it's best to combine this option with a filename filter.",+ verbose_name="attachment type",+ ),),migrations.AddField(- model_name='mailrule',- name='filter_attachment_filename',- field=models.CharField(blank=True, help_text='Only consume documents which entirely match this filename if specified. Wildcards such as *.pdf or *invoice* are allowed. Case insensitive.', max_length=256, null=True, verbose_name='filter attachment filename'),+ model_name="mailrule",+ name="filter_attachment_filename",+ field=models.CharField(+ blank=True,+ help_text="Only consume documents which entirely match this filename if specified. Wildcards such as *.pdf or *invoice* are allowed. Case insensitive.",+ max_length=256,+ null=True,+ verbose_name="filter attachment filename",+ ),),]diff --git a/src/paperless_mail/migrations/0008_auto_20210516_0940.py b/src/paperless_mail/migrations/0008_auto_20210516_0940.pyindex 3cb98752c..c25852c7d 100644--- a/src/paperless_mail/migrations/0008_auto_20210516_0940.py+++ b/src/paperless_mail/migrations/0008_auto_20210516_0940.py@@ -6,23 +6,39 @@ from django.db import migrations, modelsclass Migration(migrations.Migration):dependencies = [- ('paperless_mail', '0007_auto_20210106_0138'),+ ("paperless_mail", "0007_auto_20210106_0138"),]operations = [migrations.AddField(- model_name='mailaccount',- name='character_set',- field=models.CharField(default='UTF-8', help_text="The character set to use when communicating with the mail server, such as 'UTF-8' or 'US-ASCII'.", max_length=256, verbose_name='character set'),+ model_name="mailaccount",+ name="character_set",+ field=models.CharField(+ default="UTF-8",+ help_text="The character set to use when communicating with the mail server, such as 'UTF-8' or 'US-ASCII'.",+ max_length=256,+ verbose_name="character set",+ ),),migrations.AlterField(- model_name='mailrule',- name='action_parameter',- field=models.CharField(blank=True, help_text='Additional parameter for the action selected above, i.e., the target folder of the move to folder action. Subfolders must be separated by dots.', max_length=256, null=True, verbose_name='action parameter'),+ model_name="mailrule",+ name="action_parameter",+ field=models.CharField(+ blank=True,+ help_text="Additional parameter for the action selected above, i.e., the target folder of the move to folder action. Subfolders must be separated by dots.",+ max_length=256,+ null=True,+ verbose_name="action parameter",+ ),),migrations.AlterField(- model_name='mailrule',- name='folder',- field=models.CharField(default='INBOX', help_text='Subfolders must be separated by dots.', max_length=256, verbose_name='folder'),+ model_name="mailrule",+ name="folder",+ field=models.CharField(+ default="INBOX",+ help_text="Subfolders must be separated by dots.",+ max_length=256,+ verbose_name="folder",+ ),),]diff --git a/src/paperless_mail/models.py b/src/paperless_mail/models.pyindex 47921800e..08048d352 100644--- a/src/paperless_mail/models.py+++ b/src/paperless_mail/models.py@@ -6,7 +6,6 @@ from django.utils.translation import gettext_lazy as _class MailAccount(models.Model):-class Meta:verbose_name = _("mail account")verbose_name_plural = _("mail accounts")@@ -21,41 +20,36 @@ class MailAccount(models.Model):(IMAP_SECURITY_STARTTLS, _("Use STARTTLS")),)- name = models.CharField(- _("name"),- max_length=256, unique=True)+ name = models.CharField(_("name"), max_length=256, unique=True)- imap_server = models.CharField(- _("IMAP server"),- max_length=256)+ imap_server = models.CharField(_("IMAP server"), max_length=256)imap_port = models.IntegerField(_("IMAP port"),blank=True,null=True,- help_text=_("This is usually 143 for unencrypted and STARTTLS "- "connections, and 993 for SSL connections."))+ help_text=_(+ "This is usually 143 for unencrypted and STARTTLS "+ "connections, and 993 for SSL connections."+ ),+ )imap_security = models.PositiveIntegerField(- _("IMAP security"),- choices=IMAP_SECURITY_OPTIONS,- default=IMAP_SECURITY_SSL+ _("IMAP security"), choices=IMAP_SECURITY_OPTIONS, default=IMAP_SECURITY_SSL)- username = models.CharField(- _("username"),- max_length=256)+ username = models.CharField(_("username"), max_length=256)- password = models.CharField(- _("password"),- max_length=256)+ password = models.CharField(_("password"), max_length=256)character_set = models.CharField(_("character set"),max_length=256,default="UTF-8",- help_text=_("The character set to use when communicating with the "- "mail server, such as 'UTF-8' or 'US-ASCII'.")+ help_text=_(+ "The character set to use when communicating with the "+ "mail server, such as 'UTF-8' or 'US-ASCII'."+ ),)def __str__(self):@@ -63,7 +57,6 @@ class MailAccount(models.Model):class MailRule(models.Model):-class Meta:verbose_name = _("mail rule")verbose_name_plural = _("mail rules")@@ -73,8 +66,10 @@ class MailRule(models.Model):ATTACHMENT_TYPES = ((ATTACHMENT_TYPE_ATTACHMENTS_ONLY, _("Only process attachments.")),- (ATTACHMENT_TYPE_EVERYTHING, _("Process all files, including 'inline' "- "attachments."))+ (+ ATTACHMENT_TYPE_EVERYTHING,+ _("Process all files, including 'inline' " "attachments."),+ ),)ACTION_DELETE = 1@@ -94,7 +89,7 @@ class MailRule(models.Model):TITLE_SELECTOR = ((TITLE_FROM_SUBJECT, _("Use subject as title")),- (TITLE_FROM_FILENAME, _("Use attachment filename as title"))+ (TITLE_FROM_FILENAME, _("Use attachment filename as title")),)CORRESPONDENT_FROM_NOTHING = 1@@ -103,66 +98,64 @@ class MailRule(models.Model):CORRESPONDENT_FROM_CUSTOM = 4CORRESPONDENT_SELECTOR = (- (CORRESPONDENT_FROM_NOTHING,- _("Do not assign a correspondent")),- (CORRESPONDENT_FROM_EMAIL,- _("Use mail address")),- (CORRESPONDENT_FROM_NAME,- _("Use name (or mail address if not available)")),- (CORRESPONDENT_FROM_CUSTOM,- _("Use correspondent selected below"))+ (CORRESPONDENT_FROM_NOTHING, _("Do not assign a correspondent")),+ (CORRESPONDENT_FROM_EMAIL, _("Use mail address")),+ (CORRESPONDENT_FROM_NAME, _("Use name (or mail address if not available)")),+ (CORRESPONDENT_FROM_CUSTOM, _("Use correspondent selected below")),)- name = models.CharField(- _("name"),- max_length=256, unique=True)+ name = models.CharField(_("name"), max_length=256, unique=True)- order = models.IntegerField(- _("order"),- default=0)+ order = models.IntegerField(_("order"), default=0)account = models.ForeignKey(MailAccount,related_name="rules",on_delete=models.CASCADE,- verbose_name=_("account")+ verbose_name=_("account"),)folder = models.CharField(_("folder"),- default='INBOX', max_length=256,- help_text=_("Subfolders must be separated by dots.")+ default="INBOX",+ max_length=256,+ help_text=_("Subfolders must be separated by dots."),)filter_from = models.CharField(- _("filter from"),- max_length=256, null=True, blank=True)+ _("filter from"), max_length=256, null=True, blank=True+ )filter_subject = models.CharField(- _("filter subject"),- max_length=256, null=True, blank=True)+ _("filter subject"), max_length=256, null=True, blank=True+ )filter_body = models.CharField(- _("filter body"),- max_length=256, null=True, blank=True)+ _("filter body"), max_length=256, null=True, blank=True+ )filter_attachment_filename = models.CharField(_("filter attachment filename"),- max_length=256, null=True, blank=True,- help_text=_("Only consume documents which entirely match this "- "filename if specified. Wildcards such as *.pdf or "- "*invoice* are allowed. Case insensitive.")+ max_length=256,+ null=True,+ blank=True,+ help_text=_(+ "Only consume documents which entirely match this "+ "filename if specified. Wildcards such as *.pdf or "+ "*invoice* are allowed. Case insensitive."+ ),)maximum_age = models.PositiveIntegerField(- _("maximum age"),- default=30,- help_text=_("Specified in days."))+ _("maximum age"), default=30, help_text=_("Specified in days.")+ )attachment_type = models.PositiveIntegerField(_("attachment type"),choices=ATTACHMENT_TYPES,default=ATTACHMENT_TYPE_ATTACHMENTS_ONLY,- help_text=_("Inline attachments include embedded images, so it's best "- "to combine this option with a filename filter.")+ help_text=_(+ "Inline attachments include embedded images, so it's best "+ "to combine this option with a filename filter."+ ),)action = models.PositiveIntegerField(@@ -173,17 +166,19 @@ class MailRule(models.Model):action_parameter = models.CharField(_("action parameter"),- max_length=256, blank=True, null=True,- help_text=_("Additional parameter for the action selected above, "- "i.e., "- "the target folder of the move to folder action. "- "Subfolders must be separated by dots.")+ max_length=256,+ blank=True,+ null=True,+ help_text=_(+ "Additional parameter for the action selected above, "+ "i.e., "+ "the target folder of the move to folder action. "+ "Subfolders must be separated by dots."+ ),)assign_title_from = models.PositiveIntegerField(- _("assign title from"),- choices=TITLE_SELECTOR,- default=TITLE_FROM_SUBJECT+ _("assign title from"), choices=TITLE_SELECTOR, default=TITLE_FROM_SUBJECT)assign_tag = models.ForeignKey(@@ -205,7 +200,7 @@ class MailRule(models.Model):assign_correspondent_from = models.PositiveIntegerField(_("assign correspondent from"),choices=CORRESPONDENT_SELECTOR,- default=CORRESPONDENT_FROM_NOTHING+ default=CORRESPONDENT_FROM_NOTHING,)assign_correspondent = models.ForeignKey(@@ -213,7 +208,7 @@ class MailRule(models.Model):null=True,blank=True,on_delete=models.SET_NULL,- verbose_name=_("assign this correspondent")+ verbose_name=_("assign this correspondent"),)def __str__(self):diff --git a/src/paperless_mail/tasks.py b/src/paperless_mail/tasks.pyindex c591f04b9..bbb163ff1 100644--- a/src/paperless_mail/tasks.py+++ b/src/paperless_mail/tasks.py@@ -11,8 +11,7 @@ def process_mail_accounts():total_new_documents = 0for account in MailAccount.objects.all():try:- total_new_documents += MailAccountHandler().handle_mail_account(- account)+ total_new_documents += MailAccountHandler().handle_mail_account(account)except MailError:logger.exception(f"Error while processing mail account {account}")diff --git a/src/paperless_mail/tests/test_mail.py b/src/paperless_mail/tests/test_mail.pyindex bec0ff4b4..b1f6ef807 100644--- a/src/paperless_mail/tests/test_mail.py+++ b/src/paperless_mail/tests/test_mail.py@@ -38,7 +38,7 @@ class BogusMailBox(ContextManager):self.messages_spam = []def login(self, username, password):- if not (username == 'admin' and password == 'secret'):+ if not (username == "admin" and password == "secret"):raise Exception()folder = BogusFolderManager()@@ -46,24 +46,24 @@ class BogusMailBox(ContextManager):def fetch(self, criteria, mark_seen, charset=""):msg = self.messages- criteria = str(criteria).strip('()').split(" ")+ criteria = str(criteria).strip("()").split(" ")- if 'UNSEEN' in criteria:+ if "UNSEEN" in criteria:msg = filter(lambda m: not m.seen, msg)- if 'SUBJECT' in criteria:- subject = criteria[criteria.index('SUBJECT') + 1].strip('"')+ if "SUBJECT" in criteria:+ subject = criteria[criteria.index("SUBJECT") + 1].strip('"')msg = filter(lambda m: subject in m.subject, msg)- if 'BODY' in criteria:- body = criteria[criteria.index('BODY') + 1].strip('"')+ if "BODY" in criteria:+ body = criteria[criteria.index("BODY") + 1].strip('"')msg = filter(lambda m: body in m.body, msg)- if 'FROM' in criteria:- from_ = criteria[criteria.index('FROM') + 1].strip('"')+ if "FROM" in criteria:+ from_ = criteria[criteria.index("FROM") + 1].strip('"')msg = filter(lambda m: from_ in m.from_, msg)- if 'UNFLAGGED' in criteria:+ if "UNFLAGGED" in criteria:msg = filter(lambda m: not m.flagged, msg)return list(msg)@@ -88,15 +88,20 @@ class BogusMailBox(ContextManager):self.messages_spam.append(filter(lambda m: m.uid in uid_list, self.messages))- self.messages = list(- filter(lambda m: m.uid not in uid_list, self.messages)- )+ self.messages = list(filter(lambda m: m.uid not in uid_list, self.messages))else:raise Exception()-def create_message(num_attachments=1, body="", subject="the suject", from_="[email protected]", seen=False, flagged=False):- message = namedtuple('MailMessage', [])+def create_message(+ num_attachments=1,+ body="",+ subject="the suject",+ from_="[email protected]",+ seen=False,+ flagged=False,+):+ message = namedtuple("MailMessage", [])message.uid = uuid.uuid4()message.subject = subject@@ -112,8 +117,10 @@ def create_message(num_attachments=1, body="", subject="the suject", from_="noonreturn message-def create_attachment(filename="the_file.pdf", content_disposition="attachment", payload=b"a PDF document"):- attachment = namedtuple('Attachment', [])+def create_attachment(+ filename="the_file.pdf", content_disposition="attachment", payload=b"a PDF document"+):+ attachment = namedtuple("Attachment", [])attachment.filename = filenameattachment.content_disposition = content_dispositionattachment.payload = payload@@ -123,25 +130,24 @@ def create_attachment(filename="the_file.pdf", content_disposition="attachment",def fake_magic_from_buffer(buffer, mime=False):if mime:- if 'PDF' in str(buffer):- return 'application/pdf'+ if "PDF" in str(buffer):+ return "application/pdf"else:- return 'unknown/type'+ return "unknown/type"else:- return 'Some verbose file description'+ return "Some verbose file description"[email protected]('paperless_mail.mail.magic.from_buffer', fake_magic_from_buffer)[email protected]("paperless_mail.mail.magic.from_buffer", fake_magic_from_buffer)class TestMail(DirectoriesMixin, TestCase):-def setUp(self):- patcher = mock.patch('paperless_mail.mail.MailBox')+ patcher = mock.patch("paperless_mail.mail.MailBox")m = patcher.start()self.bogus_mailbox = BogusMailBox()m.return_value = self.bogus_mailboxself.addCleanup(patcher.stop)- patcher = mock.patch('paperless_mail.mail.async_task')+ patcher = mock.patch("paperless_mail.mail.async_task")self.async_task = patcher.start()self.addCleanup(patcher.stop)@@ -153,28 +159,53 @@ class TestMail(DirectoriesMixin, TestCase):def reset_bogus_mailbox(self):self.bogus_mailbox.messages = []self.bogus_mailbox.messages_spam = []- self.bogus_mailbox.messages.append(create_message(subject="Invoice 1", from_="[email protected]", body="cables", seen=True, flagged=False))- self.bogus_mailbox.messages.append(create_message(subject="Invoice 2", body="from my favorite electronic store", seen=False, flagged=True))- self.bogus_mailbox.messages.append(create_message(subject="Claim your $10M price now!", from_="[email protected]", seen=False))+ self.bogus_mailbox.messages.append(+ create_message(+ subject="Invoice 1",+ from_="[email protected]",+ body="cables",+ seen=True,+ flagged=False,+ )+ )+ self.bogus_mailbox.messages.append(+ create_message(+ subject="Invoice 2",+ body="from my favorite electronic store",+ seen=False,+ flagged=True,+ )+ )+ self.bogus_mailbox.messages.append(+ create_message(+ subject="Claim your $10M price now!",+ from_="[email protected]",+ seen=False,+ )+ )def test_get_correspondent(self):- message = namedtuple('MailMessage', [])+ message = namedtuple("MailMessage", [])message.from_ = "[email protected]"- message.from_values = {'name': "Someone!", 'email': "[email protected]"}+ message.from_values = {"name": "Someone!", "email": "[email protected]"}- message2 = namedtuple('MailMessage', [])+ message2 = namedtuple("MailMessage", [])message2.from_ = "[email protected]"- message2.from_values = {'name': "", 'email': "[email protected]"}+ message2.from_values = {"name": "", "email": "[email protected]"}me_localhost = Correspondent.objects.create(name=message2.from_)someone_else = Correspondent.objects.create(name="someone else")handler = MailAccountHandler()- rule = MailRule(name="a", assign_correspondent_from=MailRule.CORRESPONDENT_FROM_NOTHING)+ rule = MailRule(+ name="a", assign_correspondent_from=MailRule.CORRESPONDENT_FROM_NOTHING+ )self.assertIsNone(handler.get_correspondent(message, rule))- rule = MailRule(name="b", assign_correspondent_from=MailRule.CORRESPONDENT_FROM_EMAIL)+ rule = MailRule(+ name="b", assign_correspondent_from=MailRule.CORRESPONDENT_FROM_EMAIL+ )c = handler.get_correspondent(message, rule)self.assertIsNotNone(c)self.assertEqual(c.name, "[email protected]")@@ -183,7 +214,9 @@ class TestMail(DirectoriesMixin, TestCase):self.assertEqual(c.name, "[email protected]")self.assertEqual(c.id, me_localhost.id)- rule = MailRule(name="c", assign_correspondent_from=MailRule.CORRESPONDENT_FROM_NAME)+ rule = MailRule(+ name="c", assign_correspondent_from=MailRule.CORRESPONDENT_FROM_NAME+ )c = handler.get_correspondent(message, rule)self.assertIsNotNone(c)self.assertEqual(c.name, "Someone!")@@ -191,14 +224,18 @@ class TestMail(DirectoriesMixin, TestCase):self.assertIsNotNone(c)self.assertEqual(c.id, me_localhost.id)- rule = MailRule(name="d", assign_correspondent_from=MailRule.CORRESPONDENT_FROM_CUSTOM, assign_correspondent=someone_else)+ rule = MailRule(+ name="d",+ assign_correspondent_from=MailRule.CORRESPONDENT_FROM_CUSTOM,+ assign_correspondent=someone_else,+ )c = handler.get_correspondent(message, rule)self.assertEqual(c, someone_else)def test_get_title(self):- message = namedtuple('MailMessage', [])+ message = namedtuple("MailMessage", [])message.subject = "the message title"- att = namedtuple('Attachment', [])+ att = namedtuple("Attachment", [])att.filename = "this_is_the_file.pdf"handler = MailAccountHandler()@@ -209,7 +246,9 @@ class TestMail(DirectoriesMixin, TestCase):self.assertEqual(handler.get_title(message, att, rule), "the message title")def test_handle_message(self):- message = create_message(subject="the message title", from_="Myself", num_attachments=2)+ message = create_message(+ subject="the message title", from_="Myself", num_attachments=2+ )account = MailAccount()rule = MailRule(assign_title_from=MailRule.TITLE_FROM_FILENAME, account=account)@@ -223,18 +262,18 @@ class TestMail(DirectoriesMixin, TestCase):args1, kwargs1 = self.async_task.call_args_list[0]args2, kwargs2 = self.async_task.call_args_list[1]- self.assertTrue(os.path.isfile(kwargs1['path']), kwargs1['path'])+ self.assertTrue(os.path.isfile(kwargs1["path"]), kwargs1["path"])- self.assertEqual(kwargs1['override_title'], "file_0")- self.assertEqual(kwargs1['override_filename'], "file_0.pdf")+ self.assertEqual(kwargs1["override_title"], "file_0")+ self.assertEqual(kwargs1["override_filename"], "file_0.pdf")- self.assertTrue(os.path.isfile(kwargs2['path']), kwargs1['path'])+ self.assertTrue(os.path.isfile(kwargs2["path"]), kwargs1["path"])- self.assertEqual(kwargs2['override_title'], "file_1")- self.assertEqual(kwargs2['override_filename'], "file_1.pdf")+ self.assertEqual(kwargs2["override_title"], "file_1")+ self.assertEqual(kwargs2["override_filename"], "file_1.pdf")def test_handle_empty_message(self):- message = namedtuple('MailMessage', [])+ message = namedtuple("MailMessage", [])message.attachments = []rule = MailRule()@@ -248,7 +287,10 @@ class TestMail(DirectoriesMixin, TestCase):message = create_message()message.attachments = [create_attachment(filename="f1.pdf"),- create_attachment(filename="f2.json", payload=b"{'much': 'payload.', 'so': 'json', 'wow': true}")+ create_attachment(+ filename="f2.json",+ payload=b"{'much': 'payload.', 'so': 'json', 'wow': true}",+ ),]account = MailAccount()@@ -260,14 +302,14 @@ class TestMail(DirectoriesMixin, TestCase):self.assertEqual(self.async_task.call_count, 1)args, kwargs = self.async_task.call_args- self.assertTrue(os.path.isfile(kwargs['path']), kwargs['path'])- self.assertEqual(kwargs['override_filename'], "f1.pdf")+ self.assertTrue(os.path.isfile(kwargs["path"]), kwargs["path"])+ self.assertEqual(kwargs["override_filename"], "f1.pdf")def test_handle_disposition(self):message = create_message()message.attachments = [- create_attachment(filename="f1.pdf", content_disposition='inline'),- create_attachment(filename="f2.pdf", content_disposition='attachment')+ create_attachment(filename="f1.pdf", content_disposition="inline"),+ create_attachment(filename="f2.pdf", content_disposition="attachment"),]account = MailAccount()@@ -279,17 +321,21 @@ class TestMail(DirectoriesMixin, TestCase):self.assertEqual(self.async_task.call_count, 1)args, kwargs = self.async_task.call_args- self.assertEqual(kwargs['override_filename'], "f2.pdf")+ self.assertEqual(kwargs["override_filename"], "f2.pdf")def test_handle_inline_files(self):message = create_message()message.attachments = [- create_attachment(filename="f1.pdf", content_disposition='inline'),- create_attachment(filename="f2.pdf", content_disposition='attachment')+ create_attachment(filename="f1.pdf", content_disposition="inline"),+ create_attachment(filename="f2.pdf", content_disposition="attachment"),]account = MailAccount()- rule = MailRule(assign_title_from=MailRule.TITLE_FROM_FILENAME, account=account, attachment_type=MailRule.ATTACHMENT_TYPE_EVERYTHING)+ rule = MailRule(+ assign_title_from=MailRule.TITLE_FROM_FILENAME,+ account=account,+ attachment_type=MailRule.ATTACHMENT_TYPE_EVERYTHING,+ )result = self.mail_account_handler.handle_message(message, rule)@@ -316,19 +362,29 @@ class TestMail(DirectoriesMixin, TestCase):for (pattern, matches) in tests:self.async_task.reset_mock()account = MailAccount()- rule = MailRule(assign_title_from=MailRule.TITLE_FROM_FILENAME, account=account, filter_attachment_filename=pattern)+ rule = MailRule(+ assign_title_from=MailRule.TITLE_FROM_FILENAME,+ account=account,+ filter_attachment_filename=pattern,+ )result = self.mail_account_handler.handle_message(message, rule)self.assertEqual(result, len(matches))- filenames = [a[1]['override_filename'] for a in self.async_task.call_args_list]+ filenames = [+ a[1]["override_filename"] for a in self.async_task.call_args_list+ ]self.assertCountEqual(filenames, matches)def test_handle_mail_account_mark_read(self):- account = MailAccount.objects.create(name="test", imap_server="", username="admin", password="secret")+ account = MailAccount.objects.create(+ name="test", imap_server="", username="admin", password="secret"+ )- rule = MailRule.objects.create(name="testrule", account=account, action=MailRule.ACTION_MARK_READ)+ rule = MailRule.objects.create(+ name="testrule", account=account, action=MailRule.ACTION_MARK_READ+ )self.assertEqual(len(self.bogus_mailbox.messages), 3)self.assertEqual(self.async_task.call_count, 0)@@ -340,9 +396,16 @@ class TestMail(DirectoriesMixin, TestCase):def test_handle_mail_account_delete(self):- account = MailAccount.objects.create(name="test", imap_server="", username="admin", password="secret")+ account = MailAccount.objects.create(+ name="test", imap_server="", username="admin", password="secret"+ )- rule = MailRule.objects.create(name="testrule", account=account, action=MailRule.ACTION_DELETE, filter_subject="Invoice")+ rule = MailRule.objects.create(+ name="testrule",+ account=account,+ action=MailRule.ACTION_DELETE,+ filter_subject="Invoice",+ )self.assertEqual(self.async_task.call_count, 0)self.assertEqual(len(self.bogus_mailbox.messages), 3)@@ -351,9 +414,16 @@ class TestMail(DirectoriesMixin, TestCase):self.assertEqual(len(self.bogus_mailbox.messages), 1)def test_handle_mail_account_flag(self):- account = MailAccount.objects.create(name="test", imap_server="", username="admin", password="secret")+ account = MailAccount.objects.create(+ name="test", imap_server="", username="admin", password="secret"+ )- rule = MailRule.objects.create(name="testrule", account=account, action=MailRule.ACTION_FLAG, filter_subject="Invoice")+ rule = MailRule.objects.create(+ name="testrule",+ account=account,+ action=MailRule.ACTION_FLAG,+ filter_subject="Invoice",+ )self.assertEqual(len(self.bogus_mailbox.messages), 3)self.assertEqual(self.async_task.call_count, 0)@@ -364,9 +434,17 @@ class TestMail(DirectoriesMixin, TestCase):self.assertEqual(len(self.bogus_mailbox.messages), 3)def test_handle_mail_account_move(self):- account = MailAccount.objects.create(name="test", imap_server="", username="admin", password="secret")+ account = MailAccount.objects.create(+ name="test", imap_server="", username="admin", password="secret"+ )- rule = MailRule.objects.create(name="testrule", account=account, action=MailRule.ACTION_MOVE, action_parameter="spam", filter_subject="Claim")+ rule = MailRule.objects.create(+ name="testrule",+ account=account,+ action=MailRule.ACTION_MOVE,+ action_parameter="spam",+ filter_subject="Claim",+ )self.assertEqual(self.async_task.call_count, 0)self.assertEqual(len(self.bogus_mailbox.messages), 3)@@ -377,7 +455,9 @@ class TestMail(DirectoriesMixin, TestCase):self.assertEqual(len(self.bogus_mailbox.messages_spam), 1)def test_error_login(self):- account = MailAccount.objects.create(name="test", imap_server="", username="admin", password="wrong")+ account = MailAccount.objects.create(+ name="test", imap_server="", username="admin", password="wrong"+ )try:self.mail_account_handler.handle_mail_account(account)@@ -387,11 +467,20 @@ class TestMail(DirectoriesMixin, TestCase):self.fail("Should raise exception")def test_error_skip_account(self):- account_faulty = MailAccount.objects.create(name="test", imap_server="", username="admin", password="wroasdng")+ account_faulty = MailAccount.objects.create(+ name="test", imap_server="", username="admin", password="wroasdng"+ )- account = MailAccount.objects.create(name="test2", imap_server="", username="admin", password="secret")- rule = MailRule.objects.create(name="testrule", account=account, action=MailRule.ACTION_MOVE,- action_parameter="spam", filter_subject="Claim")+ account = MailAccount.objects.create(+ name="test2", imap_server="", username="admin", password="secret"+ )+ rule = MailRule.objects.create(+ name="testrule",+ account=account,+ action=MailRule.ACTION_MOVE,+ action_parameter="spam",+ filter_subject="Claim",+ )tasks.process_mail_accounts()self.assertEqual(self.async_task.call_count, 1)@@ -400,31 +489,51 @@ class TestMail(DirectoriesMixin, TestCase):def test_error_skip_rule(self):- account = MailAccount.objects.create(name="test2", imap_server="", username="admin", password="secret")- rule = MailRule.objects.create(name="testrule", account=account, action=MailRule.ACTION_MOVE,- action_parameter="spam", filter_subject="Claim", order=1, folder="uuuhhhh")- rule2 = MailRule.objects.create(name="testrule2", account=account, action=MailRule.ACTION_MOVE,- action_parameter="spam", filter_subject="Claim", order=2)+ account = MailAccount.objects.create(+ name="test2", imap_server="", username="admin", password="secret"+ )+ rule = MailRule.objects.create(+ name="testrule",+ account=account,+ action=MailRule.ACTION_MOVE,+ action_parameter="spam",+ filter_subject="Claim",+ order=1,+ folder="uuuhhhh",+ )+ rule2 = MailRule.objects.create(+ name="testrule2",+ account=account,+ action=MailRule.ACTION_MOVE,+ action_parameter="spam",+ filter_subject="Claim",+ order=2,+ )self.mail_account_handler.handle_mail_account(account)self.assertEqual(self.async_task.call_count, 1)self.assertEqual(len(self.bogus_mailbox.messages), 2)self.assertEqual(len(self.bogus_mailbox.messages_spam), 1)-@mock.patch("paperless_mail.mail.MailAccountHandler.get_correspondent")def test_error_skip_mail(self, m):-def get_correspondent_fake(message, rule):- if message.from_ == '[email protected]':+ if message.from_ == "[email protected]":raise ValueError("Does not compute.")else:return Nonem.side_effect = get_correspondent_fake- account = MailAccount.objects.create(name="test2", imap_server="", username="admin", password="secret")- rule = MailRule.objects.create(name="testrule", account=account, action=MailRule.ACTION_MOVE, action_parameter="spam")+ account = MailAccount.objects.create(+ name="test2", imap_server="", username="admin", password="secret"+ )+ rule = MailRule.objects.create(+ name="testrule",+ account=account,+ action=MailRule.ACTION_MOVE,+ action_parameter="spam",+ )self.mail_account_handler.handle_mail_account(account)@@ -433,15 +542,21 @@ class TestMail(DirectoriesMixin, TestCase):# faulty mail still in inbox, untouchedself.assertEqual(len(self.bogus_mailbox.messages), 1)- self.assertEqual(self.bogus_mailbox.messages[0].from_, '[email protected]')+ self.assertEqual(self.bogus_mailbox.messages[0].from_, "[email protected]")def test_error_create_correspondent(self):- account = MailAccount.objects.create(name="test2", imap_server="", username="admin", password="secret")+ account = MailAccount.objects.create(+ name="test2", imap_server="", username="admin", password="secret"+ )rule = MailRule.objects.create(- name="testrule", filter_from="[email protected]",- account=account, action=MailRule.ACTION_MOVE, action_parameter="spam",- assign_correspondent_from=MailRule.CORRESPONDENT_FROM_EMAIL)+ name="testrule",+ filter_from="[email protected]",+ account=account,+ action=MailRule.ACTION_MOVE,+ action_parameter="spam",+ assign_correspondent_from=MailRule.CORRESPONDENT_FROM_EMAIL,+ )self.mail_account_handler.handle_mail_account(account)@@ -450,7 +565,7 @@ class TestMail(DirectoriesMixin, TestCase):c = Correspondent.objects.get(name="[email protected]")# should work- self.assertEqual(kwargs['override_correspondent_id'], c.id)+ self.assertEqual(kwargs["override_correspondent_id"], c.id)self.async_task.reset_mock()self.reset_bogus_mailbox()@@ -462,13 +577,19 @@ class TestMail(DirectoriesMixin, TestCase):args, kwargs = self.async_task.call_argsself.async_task.assert_called_once()- self.assertEqual(kwargs['override_correspondent_id'], None)-+ self.assertEqual(kwargs["override_correspondent_id"], None)def test_filters(self):- account = MailAccount.objects.create(name="test3", imap_server="", username="admin", password="secret")- rule = MailRule.objects.create(name="testrule3", account=account, action=MailRule.ACTION_DELETE, filter_subject="Claim")+ account = MailAccount.objects.create(+ name="test3", imap_server="", username="admin", password="secret"+ )+ rule = MailRule.objects.create(+ name="testrule3",+ account=account,+ action=MailRule.ACTION_DELETE,+ filter_subject="Claim",+ )self.assertEqual(self.async_task.call_count, 0)@@ -508,23 +629,29 @@ class TestMail(DirectoriesMixin, TestCase):self.assertEqual(len(self.bogus_mailbox.messages), 2)self.assertEqual(self.async_task.call_count, 5)-class TestManagementCommand(TestCase):- @mock.patch("paperless_mail.management.commands.mail_fetcher.tasks.process_mail_accounts")+class TestManagementCommand(TestCase):+ @mock.patch(+ "paperless_mail.management.commands.mail_fetcher.tasks.process_mail_accounts"+ )def test_mail_fetcher(self, m):call_command("mail_fetcher")m.assert_called_once()-class TestTasks(TestCase):+class TestTasks(TestCase):@mock.patch("paperless_mail.tasks.MailAccountHandler.handle_mail_account")def test_all_accounts(self, m):m.side_effect = lambda account: 6- MailAccount.objects.create(name="A", imap_server="A", username="A", password="A")- MailAccount.objects.create(name="B", imap_server="A", username="A", password="A")+ MailAccount.objects.create(+ name="A", imap_server="A", username="A", password="A"+ )+ MailAccount.objects.create(+ name="B", imap_server="A", username="A", password="A"+ )result = tasks.process_mail_accounts()@@ -538,7 +665,9 @@ class TestTasks(TestCase):@mock.patch("paperless_mail.tasks.MailAccountHandler.handle_mail_account")def test_single_accounts(self, m):- MailAccount.objects.create(name="A", imap_server="A", username="A", password="A")+ MailAccount.objects.create(+ name="A", imap_server="A", username="A", password="A"+ )tasks.process_mail_account("A")diff --git a/src/paperless_tesseract/checks.py b/src/paperless_tesseract/checks.pyindex d58b7ac6d..e627aa0ac 100644--- a/src/paperless_tesseract/checks.py+++ b/src/paperless_tesseract/checks.py@@ -5,8 +5,7 @@ from django.core.checks import Error, Warning, registerdef get_tesseract_langs():- with subprocess.Popen(['tesseract', '--list-langs'],- stdout=subprocess.PIPE) as p:+ with subprocess.Popen(["tesseract", "--list-langs"], stdout=subprocess.PIPE) as p:stdout, stderr = p.communicate()return stdout.decode().strip().split("\n")[1:]@@ -17,18 +16,23 @@ def check_default_language_available(app_configs, **kwargs):installed_langs = get_tesseract_langs()if not settings.OCR_LANGUAGE:- return [Warning(- "No OCR language has been specified with PAPERLESS_OCR_LANGUAGE. "- "This means that tesseract will fallback to english."- )]+ return [+ Warning(+ "No OCR language has been specified with PAPERLESS_OCR_LANGUAGE. "+ "This means that tesseract will fallback to english."+ )+ ]specified_langs = settings.OCR_LANGUAGE.split("+")for lang in specified_langs:if lang not in installed_langs:- return [Error(- f"The selected ocr language {lang} is "- f"not installed. Paperless cannot OCR your documents "- f"without it. Please fix PAPERLESS_OCR_LANGUAGE.")]+ return [+ Error(+ f"The selected ocr language {lang} is "+ f"not installed. Paperless cannot OCR your documents "+ f"without it. Please fix PAPERLESS_OCR_LANGUAGE."+ )+ ]return []diff --git a/src/paperless_tesseract/parsers.py b/src/paperless_tesseract/parsers.pyindex c1eddcefe..c27b598c8 100644--- a/src/paperless_tesseract/parsers.py+++ b/src/paperless_tesseract/parsers.py@@ -5,8 +5,7 @@ import refrom PIL import Imagefrom django.conf import settings-from documents.parsers import DocumentParser, ParseError, \- make_thumbnail_from_pdf+from documents.parsers import DocumentParser, ParseError, make_thumbnail_from_pdfclass NoTextFoundException(Exception):@@ -24,7 +23,7 @@ class RasterisedDocumentParser(DocumentParser):def extract_metadata(self, document_path, mime_type):result = []- if mime_type == 'application/pdf':+ if mime_type == "application/pdf":import pikepdfnamespace_pattern = re.compile(r"\{(.*)\}(.*)")@@ -37,25 +36,25 @@ class RasterisedDocumentParser(DocumentParser):value = str(value)try:m = namespace_pattern.match(key)- result.append({- "namespace": m.group(1),- "prefix": meta.REVERSE_NS[m.group(1)],- "key": m.group(2),- "value": value- })+ result.append(+ {+ "namespace": m.group(1),+ "prefix": meta.REVERSE_NS[m.group(1)],+ "key": m.group(2),+ "value": value,+ }+ )except Exception as e:self.log("warning",- f"Error while reading metadata {key}: {value}. Error: "- f"{e}"+ f"Error while reading metadata {key}: {value}. Error: " f"{e}",)return resultdef get_thumbnail(self, document_path, mime_type, file_name=None):return make_thumbnail_from_pdf(- self.archive_path or document_path,- self.tempdir,- self.logging_group)+ self.archive_path or document_path, self.tempdir, self.logging_group+ )def is_image(self, mime_type):return mime_type in [@@ -68,17 +67,15 @@ class RasterisedDocumentParser(DocumentParser):def has_alpha(self, image):with Image.open(image) as im:- return im.mode in ('RGBA', 'LA')+ return im.mode in ("RGBA", "LA")def get_dpi(self, image):try:with Image.open(image) as im:- x, y = im.info['dpi']+ x, y = im.info["dpi"]return round(x)except Exception as e:- self.log(- 'warning',- f"Error while getting DPI from image {image}: {e}")+ self.log("warning", f"Error while getting DPI from image {image}: {e}")return Nonedef calculate_a4_dpi(self, image):@@ -87,16 +84,11 @@ class RasterisedDocumentParser(DocumentParser):width, height = im.size# divide image width by A4 width (210mm) in inches.dpi = int(width / (21 / 2.54))- self.log(- 'debug',- f"Estimated DPI {dpi} based on image width {width}"- )+ self.log("debug", f"Estimated DPI {dpi} based on image width {width}")return dpiexcept Exception as e:- self.log(- 'warning',- f"Error while calculating DPI for image {image}: {e}")+ self.log("warning", f"Error while calculating DPI for image {image}: {e}")return Nonedef extract_text(self, sidecar_file, pdf_file):@@ -128,60 +120,60 @@ class RasterisedDocumentParser(DocumentParser):except Exception:# TODO catch all for various issues with PDFminer.six.# If PDFminer fails, fall back to OCR.- self.log("warn",- "Error while getting text from PDF document with "- "pdfminer.six", exc_info=True)+ self.log(+ "warn",+ "Error while getting text from PDF document with " "pdfminer.six",+ exc_info=True,+ )# probably not a PDF file.return None- def construct_ocrmypdf_parameters(self,- input_file,- mime_type,- output_file,- sidecar_file,- safe_fallback=False):+ def construct_ocrmypdf_parameters(+ self, input_file, mime_type, output_file, sidecar_file, safe_fallback=False+ ):ocrmypdf_args = {- 'input_file': input_file,- 'output_file': output_file,+ "input_file": input_file,+ "output_file": output_file,# need to use threads, since this will be run in daemonized# processes by django-q.- 'use_threads': True,- 'jobs': settings.THREADS_PER_WORKER,- 'language': settings.OCR_LANGUAGE,- 'output_type': settings.OCR_OUTPUT_TYPE,- 'progress_bar': False+ "use_threads": True,+ "jobs": settings.THREADS_PER_WORKER,+ "language": settings.OCR_LANGUAGE,+ "output_type": settings.OCR_OUTPUT_TYPE,+ "progress_bar": False,}- if settings.OCR_MODE == 'force' or safe_fallback:- ocrmypdf_args['force_ocr'] = True- elif settings.OCR_MODE in ['skip', 'skip_noarchive']:- ocrmypdf_args['skip_text'] = True- elif settings.OCR_MODE == 'redo':- ocrmypdf_args['redo_ocr'] = True+ if settings.OCR_MODE == "force" or safe_fallback:+ ocrmypdf_args["force_ocr"] = True+ elif settings.OCR_MODE in ["skip", "skip_noarchive"]:+ ocrmypdf_args["skip_text"] = True+ elif settings.OCR_MODE == "redo":+ ocrmypdf_args["redo_ocr"] = Trueelse:- raise ParseError(- f"Invalid ocr mode: {settings.OCR_MODE}")-- if settings.OCR_CLEAN == 'clean':- ocrmypdf_args['clean'] = True- elif settings.OCR_CLEAN == 'clean-final':- if settings.OCR_MODE == 'redo':- ocrmypdf_args['clean'] = True+ raise ParseError(f"Invalid ocr mode: {settings.OCR_MODE}")++ if settings.OCR_CLEAN == "clean":+ ocrmypdf_args["clean"] = True+ elif settings.OCR_CLEAN == "clean-final":+ if settings.OCR_MODE == "redo":+ ocrmypdf_args["clean"] = Trueelse:- ocrmypdf_args['clean_final'] = True+ ocrmypdf_args["clean_final"] = True- if settings.OCR_DESKEW and not settings.OCR_MODE == 'redo':- ocrmypdf_args['deskew'] = True+ if settings.OCR_DESKEW and not settings.OCR_MODE == "redo":+ ocrmypdf_args["deskew"] = Trueif settings.OCR_ROTATE_PAGES:- ocrmypdf_args['rotate_pages'] = True- ocrmypdf_args['rotate_pages_threshold'] = settings.OCR_ROTATE_PAGES_THRESHOLD # NOQA: E501+ ocrmypdf_args["rotate_pages"] = True+ ocrmypdf_args[+ "rotate_pages_threshold"+ ] = settings.OCR_ROTATE_PAGES_THRESHOLD # NOQA: E501if settings.OCR_PAGES > 0:- ocrmypdf_args['pages'] = f"1-{settings.OCR_PAGES}"+ ocrmypdf_args["pages"] = f"1-{settings.OCR_PAGES}"else:# sidecar is incompatible with pages- ocrmypdf_args['sidecar'] = sidecar_file+ ocrmypdf_args["sidecar"] = sidecar_fileif self.is_image(mime_type):dpi = self.get_dpi(input_file)@@ -191,29 +183,27 @@ class RasterisedDocumentParser(DocumentParser):self.log("info",f"Removing alpha layer from {input_file} "- "for compatibility with img2pdf"+ "for compatibility with img2pdf",)with Image.open(input_file) as im:- background = Image.new('RGBA', im.size, (255, 255, 255))+ background = Image.new("RGBA", im.size, (255, 255, 255))background.alpha_composite(im)- background = background.convert('RGB')+ background = background.convert("RGB")background.save(input_file, format=im.format)if dpi:- self.log(- "debug",- f"Detected DPI for image {input_file}: {dpi}"- )- ocrmypdf_args['image_dpi'] = dpi+ self.log("debug", f"Detected DPI for image {input_file}: {dpi}")+ ocrmypdf_args["image_dpi"] = dpielif settings.OCR_IMAGE_DPI:- ocrmypdf_args['image_dpi'] = settings.OCR_IMAGE_DPI+ ocrmypdf_args["image_dpi"] = settings.OCR_IMAGE_DPIelif a4_dpi:- ocrmypdf_args['image_dpi'] = a4_dpi+ ocrmypdf_args["image_dpi"] = a4_dpielse:raise ParseError(f"Cannot produce archive PDF for image {input_file}, "f"no DPI information is present in this image and "- f"OCR_IMAGE_DPI is not set.")+ f"OCR_IMAGE_DPI is not set."+ )if settings.OCR_USER_ARGS and not safe_fallback:try:@@ -223,13 +213,14 @@ class RasterisedDocumentParser(DocumentParser):self.log("warning",f"There is an issue with PAPERLESS_OCR_USER_ARGS, so "- f"they will not be used. Error: {e}")+ f"they will not be used. Error: {e}",+ )return ocrmypdf_argsdef parse(self, document_path, mime_type, file_name=None):# This forces tesseract to use one core per page.- os.environ['OMP_THREAD_LIMIT'] = "1"+ os.environ["OMP_THREAD_LIMIT"] = "1"if mime_type == "application/pdf":text_original = self.extract_text(None, document_path)@@ -239,8 +230,7 @@ class RasterisedDocumentParser(DocumentParser):original_has_text = Falseif settings.OCR_MODE == "skip_noarchive" and original_has_text:- self.log("debug",- "Document has text, skipping OCRmyPDF entirely.")+ self.log("debug", "Document has text, skipping OCRmyPDF entirely.")self.text = text_originalreturn@@ -251,7 +241,8 @@ class RasterisedDocumentParser(DocumentParser):sidecar_file = os.path.join(self.tempdir, "sidecar.txt")args = self.construct_ocrmypdf_parameters(- document_path, mime_type, archive_path, sidecar_file)+ document_path, mime_type, archive_path, sidecar_file+ )try:self.log("debug", f"Calling OCRmyPDF with args: {args}")@@ -261,42 +252,45 @@ class RasterisedDocumentParser(DocumentParser):self.text = self.extract_text(sidecar_file, archive_path)if not self.text:- raise NoTextFoundException(- "No text was found in the original document")+ raise NoTextFoundException("No text was found in the original document")except EncryptedPdfError:- self.log("warning",- "This file is encrypted, OCR is impossible. Using "- "any text present in the original file.")+ self.log(+ "warning",+ "This file is encrypted, OCR is impossible. Using "+ "any text present in the original file.",+ )if original_has_text:self.text = text_originalexcept (NoTextFoundException, InputFileError) as e:- self.log("warning",- f"Encountered an error while running OCR: {str(e)}. "- f"Attempting force OCR to get the text.")+ self.log(+ "warning",+ f"Encountered an error while running OCR: {str(e)}. "+ f"Attempting force OCR to get the text.",+ )- archive_path_fallback = os.path.join(- self.tempdir, "archive-fallback.pdf")- sidecar_file_fallback = os.path.join(- self.tempdir, "sidecar-fallback.txt")+ archive_path_fallback = os.path.join(self.tempdir, "archive-fallback.pdf")+ sidecar_file_fallback = os.path.join(self.tempdir, "sidecar-fallback.txt")# Attempt to run OCR with safe settings.args = self.construct_ocrmypdf_parameters(- document_path, mime_type,- archive_path_fallback, sidecar_file_fallback,- safe_fallback=True+ document_path,+ mime_type,+ archive_path_fallback,+ sidecar_file_fallback,+ safe_fallback=True,)try:- self.log("debug",- f"Fallback: Calling OCRmyPDF with args: {args}")+ self.log("debug", f"Fallback: Calling OCRmyPDF with args: {args}")ocrmypdf.ocr(**args)# Don't return the archived file here, since this file# is bigger and blurry due to --force-ocr.self.text = self.extract_text(- sidecar_file_fallback, archive_path_fallback)+ sidecar_file_fallback, archive_path_fallback+ )except Exception as e:# If this fails, we have a serious issue at hand.@@ -315,7 +309,7 @@ class RasterisedDocumentParser(DocumentParser):self.log("warning",f"No text was found in {document_path}, the content will "- f"be empty."+ f"be empty.",)self.text = ""@@ -325,10 +319,8 @@ def post_process_text(text):return Nonecollapsed_spaces = re.sub(r"([^\S\r\n]+)", " ", text)- no_leading_whitespace = re.sub(- r"([\n\r]+)([^\S\n\r]+)", '\\1', collapsed_spaces)- no_trailing_whitespace = re.sub(- r"([^\S\n\r]+)$", '', no_leading_whitespace)+ no_leading_whitespace = re.sub(r"([\n\r]+)([^\S\n\r]+)", "\\1", collapsed_spaces)+ no_trailing_whitespace = re.sub(r"([^\S\n\r]+)$", "", no_leading_whitespace)# TODO: this needs a rework# replace \0 prevents issues with saving to postgres.diff --git a/src/paperless_tesseract/signals.py b/src/paperless_tesseract/signals.pyindex fedd08a92..85f2cab9f 100644--- a/src/paperless_tesseract/signals.py+++ b/src/paperless_tesseract/signals.py@@ -1,4 +1,3 @@-def get_parser(*args, **kwargs):from .parsers import RasterisedDocumentParser@@ -16,5 +15,5 @@ def tesseract_consumer_declaration(sender, **kwargs):"image/tiff": ".tif","image/gif": ".gif","image/bmp": ".bmp",- }+ },}diff --git a/src/paperless_tesseract/tests/test_checks.py b/src/paperless_tesseract/tests/test_checks.pyindex c4f15764e..31d60f4ee 100644--- a/src/paperless_tesseract/tests/test_checks.py+++ b/src/paperless_tesseract/tests/test_checks.py@@ -7,7 +7,6 @@ from paperless_tesseract import check_default_language_availableclass TestChecks(TestCase):-def test_default_language(self):msgs = check_default_language_available(None)@@ -15,7 +14,11 @@ class TestChecks(TestCase):def test_no_language(self):msgs = check_default_language_available(None)self.assertEqual(len(msgs), 1)- self.assertTrue(msgs[0].msg.startswith("No OCR language has been specified with PAPERLESS_OCR_LANGUAGE"))+ self.assertTrue(+ msgs[0].msg.startswith(+ "No OCR language has been specified with PAPERLESS_OCR_LANGUAGE"+ )+ )@override_settings(OCR_LANGUAGE="ita")@mock.patch("paperless_tesseract.checks.get_tesseract_langs")diff --git a/src/paperless_tesseract/tests/test_parser.py b/src/paperless_tesseract/tests/test_parser.pyindex 1ee295edf..9b59c324d 100644--- a/src/paperless_tesseract/tests/test_parser.py+++ b/src/paperless_tesseract/tests/test_parser.py@@ -33,7 +33,6 @@ class FakeImageFile(ContextManager):class TestParser(DirectoriesMixin, TestCase):-def assertContainsStrings(self, content, strings):# Asserts that all strings appear in content, in the given order.indices = []@@ -46,14 +45,8 @@ class TestParser(DirectoriesMixin, TestCase):text_cases = [("simple string", "simple string"),- (- "simple newline\n testing string",- "simple newline\ntesting string"- ),- (- "utf-8 строка с пробелами в конце ",- "utf-8 строка с пробелами в конце"- )+ ("simple newline\n testing string", "simple newline\ntesting string"),+ ("utf-8 строка с пробелами в конце ", "utf-8 строка с пробелами в конце"),]def test_post_process_text(self):@@ -63,28 +56,29 @@ class TestParser(DirectoriesMixin, TestCase):result,actual_result,"strip_exceess_whitespace({}) != '{}', but '{}'".format(- source,- result,- actual_result- )+ source, result, actual_result+ ),)SAMPLE_FILES = os.path.join(os.path.dirname(__file__), "samples")def test_get_text_from_pdf(self):parser = RasterisedDocumentParser(uuid.uuid4())- text = parser.extract_text(None, os.path.join(self.SAMPLE_FILES, 'simple-digital.pdf'))+ text = parser.extract_text(+ None, os.path.join(self.SAMPLE_FILES, "simple-digital.pdf")+ )self.assertContainsStrings(text.strip(), ["This is a test document."])def test_thumbnail(self):parser = RasterisedDocumentParser(uuid.uuid4())- thumb = parser.get_thumbnail(os.path.join(self.SAMPLE_FILES, 'simple-digital.pdf'), "application/pdf")+ thumb = parser.get_thumbnail(+ os.path.join(self.SAMPLE_FILES, "simple-digital.pdf"), "application/pdf"+ )self.assertTrue(os.path.isfile(thumb))@mock.patch("documents.parsers.run_convert")def test_thumbnail_fallback(self, m):-def call_convert(input_file, output_file, **kwargs):if ".pdf" in input_file:raise ParseError("Does not compute.")@@ -94,12 +88,16 @@ class TestParser(DirectoriesMixin, TestCase):m.side_effect = call_convertparser = RasterisedDocumentParser(uuid.uuid4())- thumb = parser.get_thumbnail(os.path.join(self.SAMPLE_FILES, 'simple-digital.pdf'), "application/pdf")+ thumb = parser.get_thumbnail(+ os.path.join(self.SAMPLE_FILES, "simple-digital.pdf"), "application/pdf"+ )self.assertTrue(os.path.isfile(thumb))def test_thumbnail_encrypted(self):parser = RasterisedDocumentParser(uuid.uuid4())- thumb = parser.get_thumbnail(os.path.join(self.SAMPLE_FILES, 'encrypted.pdf'), "application/pdf")+ thumb = parser.get_thumbnail(+ os.path.join(self.SAMPLE_FILES, "encrypted.pdf"), "application/pdf"+ )self.assertTrue(os.path.isfile(thumb))def test_get_dpi(self):@@ -114,7 +112,9 @@ class TestParser(DirectoriesMixin, TestCase):def test_simple_digital(self):parser = RasterisedDocumentParser(None)- parser.parse(os.path.join(self.SAMPLE_FILES, "simple-digital.pdf"), "application/pdf")+ parser.parse(+ os.path.join(self.SAMPLE_FILES, "simple-digital.pdf"), "application/pdf"+ )self.assertTrue(os.path.isfile(parser.archive_path))@@ -123,20 +123,30 @@ class TestParser(DirectoriesMixin, TestCase):def test_with_form(self):parser = RasterisedDocumentParser(None)- parser.parse(os.path.join(self.SAMPLE_FILES, "with-form.pdf"), "application/pdf")+ parser.parse(+ os.path.join(self.SAMPLE_FILES, "with-form.pdf"), "application/pdf"+ )self.assertTrue(os.path.isfile(parser.archive_path))- self.assertContainsStrings(parser.get_text(), ["Please enter your name in here:", "This is a PDF document with a form."])+ self.assertContainsStrings(+ parser.get_text(),+ ["Please enter your name in here:", "This is a PDF document with a form."],+ )@override_settings(OCR_MODE="redo")def test_with_form_error(self):parser = RasterisedDocumentParser(None)- parser.parse(os.path.join(self.SAMPLE_FILES, "with-form.pdf"), "application/pdf")+ parser.parse(+ os.path.join(self.SAMPLE_FILES, "with-form.pdf"), "application/pdf"+ )self.assertIsNone(parser.archive_path)- self.assertContainsStrings(parser.get_text(), ["Please enter your name in here:", "This is a PDF document with a form."])+ self.assertContainsStrings(+ parser.get_text(),+ ["Please enter your name in here:", "This is a PDF document with a form."],+ )@override_settings(OCR_MODE="skip")def test_signed(self):@@ -145,32 +155,49 @@ class TestParser(DirectoriesMixin, TestCase):parser.parse(os.path.join(self.SAMPLE_FILES, "signed.pdf"), "application/pdf")self.assertIsNone(parser.archive_path)- self.assertContainsStrings(parser.get_text(), ["This is a digitally signed PDF, created with Acrobat Pro for the Paperless project to enable", "automated testing of signed/encrypted PDFs"])+ self.assertContainsStrings(+ parser.get_text(),+ [+ "This is a digitally signed PDF, created with Acrobat Pro for the Paperless project to enable",+ "automated testing of signed/encrypted PDFs",+ ],+ )@override_settings(OCR_MODE="skip")def test_encrypted(self):parser = RasterisedDocumentParser(None)- parser.parse(os.path.join(self.SAMPLE_FILES, "encrypted.pdf"), "application/pdf")+ parser.parse(+ os.path.join(self.SAMPLE_FILES, "encrypted.pdf"), "application/pdf"+ )self.assertIsNone(parser.archive_path)self.assertEqual(parser.get_text(), "")-@override_settings(OCR_MODE="redo")def test_with_form_error_notext(self):parser = RasterisedDocumentParser(None)- parser.parse(os.path.join(self.SAMPLE_FILES, "with-form.pdf"), "application/pdf")+ parser.parse(+ os.path.join(self.SAMPLE_FILES, "with-form.pdf"), "application/pdf"+ )- self.assertContainsStrings(parser.get_text(), ["Please enter your name in here:", "This is a PDF document with a form."])+ self.assertContainsStrings(+ parser.get_text(),+ ["Please enter your name in here:", "This is a PDF document with a form."],+ )@override_settings(OCR_MODE="force")def test_with_form_force(self):parser = RasterisedDocumentParser(None)- parser.parse(os.path.join(self.SAMPLE_FILES, "with-form.pdf"), "application/pdf")+ parser.parse(+ os.path.join(self.SAMPLE_FILES, "with-form.pdf"), "application/pdf"+ )- self.assertContainsStrings(parser.get_text(), ["Please enter your name in here:", "This is a PDF document with a form."])+ self.assertContainsStrings(+ parser.get_text(),+ ["Please enter your name in here:", "This is a PDF document with a form."],+ )def test_image_simple(self):parser = RasterisedDocumentParser(None)@@ -193,7 +220,9 @@ class TestParser(DirectoriesMixin, TestCase):def test_image_calc_a4_dpi(self):parser = RasterisedDocumentParser(None)- dpi = parser.calculate_a4_dpi(os.path.join(self.SAMPLE_FILES, "simple-no-dpi.png"))+ dpi = parser.calculate_a4_dpi(+ os.path.join(self.SAMPLE_FILES, "simple-no-dpi.png")+ )self.assertEqual(dpi, 62)@@ -203,7 +232,9 @@ class TestParser(DirectoriesMixin, TestCase):parser = RasterisedDocumentParser(None)def f():- parser.parse(os.path.join(self.SAMPLE_FILES, "simple-no-dpi.png"), "image/png")+ parser.parse(+ os.path.join(self.SAMPLE_FILES, "simple-no-dpi.png"), "image/png"+ )self.assertRaises(ParseError, f)@@ -215,46 +246,70 @@ class TestParser(DirectoriesMixin, TestCase):self.assertTrue(os.path.isfile(parser.archive_path))- self.assertContainsStrings(parser.get_text().lower(), ["this is a test document."])+ self.assertContainsStrings(+ parser.get_text().lower(), ["this is a test document."]+ )def test_multi_page(self):parser = RasterisedDocumentParser(None)- parser.parse(os.path.join(self.SAMPLE_FILES, "multi-page-digital.pdf"), "application/pdf")+ parser.parse(+ os.path.join(self.SAMPLE_FILES, "multi-page-digital.pdf"), "application/pdf"+ )self.assertTrue(os.path.isfile(parser.archive_path))- self.assertContainsStrings(parser.get_text().lower(), ["page 1", "page 2", "page 3"])+ self.assertContainsStrings(+ parser.get_text().lower(), ["page 1", "page 2", "page 3"]+ )@override_settings(OCR_PAGES=2, OCR_MODE="skip")def test_multi_page_pages_skip(self):parser = RasterisedDocumentParser(None)- parser.parse(os.path.join(self.SAMPLE_FILES, "multi-page-digital.pdf"), "application/pdf")+ parser.parse(+ os.path.join(self.SAMPLE_FILES, "multi-page-digital.pdf"), "application/pdf"+ )self.assertTrue(os.path.isfile(parser.archive_path))- self.assertContainsStrings(parser.get_text().lower(), ["page 1", "page 2", "page 3"])+ self.assertContainsStrings(+ parser.get_text().lower(), ["page 1", "page 2", "page 3"]+ )@override_settings(OCR_PAGES=2, OCR_MODE="redo")def test_multi_page_pages_redo(self):parser = RasterisedDocumentParser(None)- parser.parse(os.path.join(self.SAMPLE_FILES, "multi-page-digital.pdf"), "application/pdf")+ parser.parse(+ os.path.join(self.SAMPLE_FILES, "multi-page-digital.pdf"), "application/pdf"+ )self.assertTrue(os.path.isfile(parser.archive_path))- self.assertContainsStrings(parser.get_text().lower(), ["page 1", "page 2", "page 3"])+ self.assertContainsStrings(+ parser.get_text().lower(), ["page 1", "page 2", "page 3"]+ )@override_settings(OCR_PAGES=2, OCR_MODE="force")def test_multi_page_pages_force(self):parser = RasterisedDocumentParser(None)- parser.parse(os.path.join(self.SAMPLE_FILES, "multi-page-digital.pdf"), "application/pdf")+ parser.parse(+ os.path.join(self.SAMPLE_FILES, "multi-page-digital.pdf"), "application/pdf"+ )self.assertTrue(os.path.isfile(parser.archive_path))- self.assertContainsStrings(parser.get_text().lower(), ["page 1", "page 2", "page 3"])+ self.assertContainsStrings(+ parser.get_text().lower(), ["page 1", "page 2", "page 3"]+ )@override_settings(OOCR_MODE="skip")def test_multi_page_analog_pages_skip(self):parser = RasterisedDocumentParser(None)- parser.parse(os.path.join(self.SAMPLE_FILES, "multi-page-images.pdf"), "application/pdf")+ parser.parse(+ os.path.join(self.SAMPLE_FILES, "multi-page-images.pdf"), "application/pdf"+ )self.assertTrue(os.path.isfile(parser.archive_path))- self.assertContainsStrings(parser.get_text().lower(), ["page 1", "page 2", "page 3"])+ self.assertContainsStrings(+ parser.get_text().lower(), ["page 1", "page 2", "page 3"]+ )@override_settings(OCR_PAGES=2, OCR_MODE="redo")def test_multi_page_analog_pages_redo(self):parser = RasterisedDocumentParser(None)- parser.parse(os.path.join(self.SAMPLE_FILES, "multi-page-images.pdf"), "application/pdf")+ parser.parse(+ os.path.join(self.SAMPLE_FILES, "multi-page-images.pdf"), "application/pdf"+ )self.assertTrue(os.path.isfile(parser.archive_path))self.assertContainsStrings(parser.get_text().lower(), ["page 1", "page 2"])self.assertFalse("page 3" in parser.get_text().lower())@@ -262,7 +317,9 @@ class TestParser(DirectoriesMixin, TestCase):@override_settings(OCR_PAGES=1, OCR_MODE="force")def test_multi_page_analog_pages_force(self):parser = RasterisedDocumentParser(None)- parser.parse(os.path.join(self.SAMPLE_FILES, "multi-page-images.pdf"), "application/pdf")+ parser.parse(+ os.path.join(self.SAMPLE_FILES, "multi-page-images.pdf"), "application/pdf"+ )self.assertTrue(os.path.isfile(parser.archive_path))self.assertContainsStrings(parser.get_text().lower(), ["page 1"])self.assertFalse("page 2" in parser.get_text().lower())@@ -271,23 +328,36 @@ class TestParser(DirectoriesMixin, TestCase):@override_settings(OCR_MODE="skip_noarchive")def test_skip_noarchive_withtext(self):parser = RasterisedDocumentParser(None)- parser.parse(os.path.join(self.SAMPLE_FILES, "multi-page-digital.pdf"), "application/pdf")+ parser.parse(+ os.path.join(self.SAMPLE_FILES, "multi-page-digital.pdf"), "application/pdf"+ )self.assertIsNone(parser.archive_path)- self.assertContainsStrings(parser.get_text().lower(), ["page 1", "page 2", "page 3"])+ self.assertContainsStrings(+ parser.get_text().lower(), ["page 1", "page 2", "page 3"]+ )@override_settings(OCR_MODE="skip_noarchive")def test_skip_noarchive_notext(self):parser = RasterisedDocumentParser(None)- parser.parse(os.path.join(self.SAMPLE_FILES, "multi-page-images.pdf"), "application/pdf")+ parser.parse(+ os.path.join(self.SAMPLE_FILES, "multi-page-images.pdf"), "application/pdf"+ )self.assertTrue(os.path.isfile(parser.archive_path))- self.assertContainsStrings(parser.get_text().lower(), ["page 1", "page 2", "page 3"])+ self.assertContainsStrings(+ parser.get_text().lower(), ["page 1", "page 2", "page 3"]+ )@override_settings(OCR_MODE="skip")def test_multi_page_mixed(self):parser = RasterisedDocumentParser(None)- parser.parse(os.path.join(self.SAMPLE_FILES, "multi-page-mixed.pdf"), "application/pdf")+ parser.parse(+ os.path.join(self.SAMPLE_FILES, "multi-page-mixed.pdf"), "application/pdf"+ )self.assertTrue(os.path.isfile(parser.archive_path))- self.assertContainsStrings(parser.get_text().lower(), ["page 1", "page 2", "page 3", "page 4", "page 5", "page 6"])+ self.assertContainsStrings(+ parser.get_text().lower(),+ ["page 1", "page 2", "page 3", "page 4", "page 5", "page 6"],+ )with open(os.path.join(parser.tempdir, "sidecar.txt")) as f:sidecar = f.read()@@ -297,30 +367,41 @@ class TestParser(DirectoriesMixin, TestCase):@override_settings(OCR_MODE="skip_noarchive")def test_multi_page_mixed_no_archive(self):parser = RasterisedDocumentParser(None)- parser.parse(os.path.join(self.SAMPLE_FILES, "multi-page-mixed.pdf"), "application/pdf")+ parser.parse(+ os.path.join(self.SAMPLE_FILES, "multi-page-mixed.pdf"), "application/pdf"+ )self.assertIsNone(parser.archive_path)- self.assertContainsStrings(parser.get_text().lower(), ["page 4", "page 5", "page 6"])+ self.assertContainsStrings(+ parser.get_text().lower(), ["page 4", "page 5", "page 6"]+ )@override_settings(OCR_MODE="skip", OCR_ROTATE_PAGES=True)def test_rotate(self):parser = RasterisedDocumentParser(None)parser.parse(os.path.join(self.SAMPLE_FILES, "rotated.pdf"), "application/pdf")- self.assertContainsStrings(parser.get_text(), [- "This is the text that appears on the first page. It’s a lot of text.",- "Even if the pages are rotated, OCRmyPDF still gets the job done.",- "This is a really weird file with lots of nonsense text.",- "If you read this, it’s your own fault. Also check your screen orientation."- ])+ self.assertContainsStrings(+ parser.get_text(),+ [+ "This is the text that appears on the first page. It’s a lot of text.",+ "Even if the pages are rotated, OCRmyPDF still gets the job done.",+ "This is a really weird file with lots of nonsense text.",+ "If you read this, it’s your own fault. Also check your screen orientation.",+ ],+ )def test_ocrmypdf_parameters(self):parser = RasterisedDocumentParser(None)- params = parser.construct_ocrmypdf_parameters(input_file="input.pdf", output_file="output.pdf",- sidecar_file="sidecar.txt", mime_type="application/pdf",- safe_fallback=False)+ params = parser.construct_ocrmypdf_parameters(+ input_file="input.pdf",+ output_file="output.pdf",+ sidecar_file="sidecar.txt",+ mime_type="application/pdf",+ safe_fallback=False,+ )- self.assertEqual(params['input_file'], "input.pdf")- self.assertEqual(params['output_file'], "output.pdf")- self.assertEqual(params['sidecar'], "sidecar.txt")+ self.assertEqual(params["input_file"], "input.pdf")+ self.assertEqual(params["output_file"], "output.pdf")+ self.assertEqual(params["sidecar"], "sidecar.txt")with override_settings(OCR_CLEAN="none"):params = parser.construct_ocrmypdf_parameters("", "", "", "")@@ -329,30 +410,31 @@ class TestParser(DirectoriesMixin, TestCase):with override_settings(OCR_CLEAN="clean"):params = parser.construct_ocrmypdf_parameters("", "", "", "")- self.assertTrue(params['clean'])+ self.assertTrue(params["clean"])self.assertNotIn("clean_final", params)with override_settings(OCR_CLEAN="clean-final", OCR_MODE="skip"):params = parser.construct_ocrmypdf_parameters("", "", "", "")- self.assertTrue(params['clean_final'])+ self.assertTrue(params["clean_final"])self.assertNotIn("clean", params)with override_settings(OCR_CLEAN="clean-final", OCR_MODE="redo"):params = parser.construct_ocrmypdf_parameters("", "", "", "")- self.assertTrue(params['clean'])+ self.assertTrue(params["clean"])self.assertNotIn("clean_final", params)with override_settings(OCR_DESKEW=True, OCR_MODE="skip"):params = parser.construct_ocrmypdf_parameters("", "", "", "")- self.assertTrue(params['deskew'])+ self.assertTrue(params["deskew"])with override_settings(OCR_DESKEW=True, OCR_MODE="redo"):params = parser.construct_ocrmypdf_parameters("", "", "", "")- self.assertNotIn('deskew', params)+ self.assertNotIn("deskew", params)with override_settings(OCR_DESKEW=False, OCR_MODE="skip"):params = parser.construct_ocrmypdf_parameters("", "", "", "")- self.assertNotIn('deskew', params)+ self.assertNotIn("deskew", params)+class TestParserFileTypes(DirectoriesMixin, TestCase):diff --git a/src/paperless_text/parsers.py b/src/paperless_text/parsers.pyindex 837f05c9f..86d4e8d43 100644--- a/src/paperless_text/parsers.py+++ b/src/paperless_text/parsers.py@@ -14,9 +14,8 @@ class TextDocumentParser(DocumentParser):logging_name = "paperless.parsing.text"def get_thumbnail(self, document_path, mime_type, file_name=None):-def read_text():- with open(document_path, 'r') as src:+ with open(document_path, "r") as src:lines = [line.strip() for line in src.readlines()]text = "\n".join(lines[:50])return text@@ -26,7 +25,8 @@ class TextDocumentParser(DocumentParser):font = ImageFont.truetype(font=settings.THUMBNAIL_FONT_NAME,size=20,- layout_engine=ImageFont.LAYOUT_BASIC)+ layout_engine=ImageFont.LAYOUT_BASIC,+ )draw.text((5, 5), read_text(), font=font, fill="black")out_path = os.path.join(self.tempdir, "thumb.png")@@ -35,5 +35,5 @@ class TextDocumentParser(DocumentParser):return out_pathdef parse(self, document_path, mime_type, file_name=None):- with open(document_path, 'r') as f:+ with open(document_path, "r") as f:self.text = f.read()diff --git a/src/paperless_text/signals.py b/src/paperless_text/signals.pyindex 833d0be28..3d025c14e 100644--- a/src/paperless_text/signals.py+++ b/src/paperless_text/signals.py@@ -1,4 +1,3 @@-def get_parser(*args, **kwargs):from .parsers import TextDocumentParser@@ -12,5 +11,5 @@ def text_consumer_declaration(sender, **kwargs):"mime_types": {"text/plain": ".txt","text/csv": ".csv",- }+ },}diff --git a/src/paperless_text/tests/test_parser.py b/src/paperless_text/tests/test_parser.pyindex 413aa91cf..f63c327cb 100644--- a/src/paperless_text/tests/test_parser.py+++ b/src/paperless_text/tests/test_parser.py@@ -7,20 +7,23 @@ from paperless_text.parsers import TextDocumentParserclass TestTextParser(DirectoriesMixin, TestCase):-def test_thumbnail(self):parser = TextDocumentParser(None)# just make sure that it does not crash- f = parser.get_thumbnail(os.path.join(os.path.dirname(__file__), "samples", "test.txt"), "text/plain")+ f = parser.get_thumbnail(+ os.path.join(os.path.dirname(__file__), "samples", "test.txt"), "text/plain"+ )self.assertTrue(os.path.isfile(f))def test_parse(self):parser = TextDocumentParser(None)- parser.parse(os.path.join(os.path.dirname(__file__), "samples", "test.txt"), "text/plain")+ parser.parse(+ os.path.join(os.path.dirname(__file__), "samples", "test.txt"), "text/plain"+ )self.assertEqual(parser.get_text(), "This is a test file.\n")self.assertIsNone(parser.get_archive_path())diff --git a/src/paperless_tika/parsers.py b/src/paperless_tika/parsers.pyindex e6924ed92..5dff20098 100644--- a/src/paperless_tika/parsers.py+++ b/src/paperless_tika/parsers.py@@ -4,8 +4,7 @@ import dateutil.parserfrom django.conf import settings-from documents.parsers import DocumentParser, ParseError, \- make_thumbnail_from_pdf+from documents.parsers import DocumentParser, ParseError, make_thumbnail_from_pdffrom tika import parser@@ -21,15 +20,18 @@ class TikaDocumentParser(DocumentParser):self.archive_path = self.convert_to_pdf(document_path, file_name)return make_thumbnail_from_pdf(- self.archive_path, self.tempdir, self.logging_group)+ self.archive_path, self.tempdir, self.logging_group+ )def extract_metadata(self, document_path, mime_type):tika_server = settings.PAPERLESS_TIKA_ENDPOINTtry:parsed = parser.from_file(document_path, tika_server)except Exception as e:- self.log("warning", f"Error while fetching document metadata for "- f"{document_path}: {e}")+ self.log(+ "warning",+ f"Error while fetching document metadata for " f"{document_path}: {e}",+ )return []return [@@ -37,8 +39,9 @@ class TikaDocumentParser(DocumentParser):"namespace": "","prefix": "","key": key,- "value": parsed['metadata'][key]- } for key in parsed['metadata']+ "value": parsed["metadata"][key],+ }+ for key in parsed["metadata"]]def parse(self, document_path, mime_type, file_name=None):@@ -56,11 +59,12 @@ class TikaDocumentParser(DocumentParser):self.text = parsed["content"].strip()try:- self.date = dateutil.parser.isoparse(- parsed["metadata"]["Creation-Date"])+ self.date = dateutil.parser.isoparse(parsed["metadata"]["Creation-Date"])except Exception as e:- self.log("warning", f"Unable to extract date for document "- f"{document_path}: {e}")+ self.log(+ "warning",+ f"Unable to extract date for document " f"{document_path}: {e}",+ )self.archive_path = self.convert_to_pdf(document_path, file_name)@@ -70,17 +74,19 @@ class TikaDocumentParser(DocumentParser):url = gotenberg_server + "/forms/libreoffice/convert"self.log("info", f"Converting {document_path} to PDF as {pdf_path}")- files = {"files": (file_name or os.path.basename(document_path),- open(document_path, "rb"))}+ files = {+ "files": (+ file_name or os.path.basename(document_path),+ open(document_path, "rb"),+ )+ }headers = {}try:response = requests.post(url, files=files, headers=headers)response.raise_for_status() # ensure we notice bad responsesexcept Exception as err:- raise ParseError(- f"Error while converting document to PDF: {err}"- )+ raise ParseError(f"Error while converting document to PDF: {err}")file = open(pdf_path, "wb")file.write(response.content)diff --git a/src/paperless_tika/tests/test_tika_parser.py b/src/paperless_tika/tests/test_tika_parser.pyindex 67563e5fe..7eaaab25e 100644--- a/src/paperless_tika/tests/test_tika_parser.py+++ b/src/paperless_tika/tests/test_tika_parser.py@@ -10,7 +10,6 @@ from paperless_tika.parsers import TikaDocumentParserclass TestTikaParser(TestCase):-def setUp(self) -> None:self.parser = TikaDocumentParser(logging_group=None)@@ -22,9 +21,7 @@ class TestTikaParser(TestCase):def test_parse(self, post, from_file):from_file.return_value = {"content": "the content",- "metadata": {- "Creation-Date": "2020-11-21"- }+ "metadata": {"Creation-Date": "2020-11-21"},}response = Response()response._content = b"PDF document"@@ -45,16 +42,15 @@ class TestTikaParser(TestCase):@mock.patch("paperless_tika.parsers.parser.from_file")def test_metadata(self, from_file):from_file.return_value = {- "metadata": {- "Creation-Date": "2020-11-21",- "Some-key": "value"- }+ "metadata": {"Creation-Date": "2020-11-21", "Some-key": "value"}}file = os.path.join(self.parser.tempdir, "input.odt")Path(file).touch()- metadata = self.parser.extract_metadata(file, "application/vnd.oasis.opendocument.text")+ metadata = self.parser.extract_metadata(+ file, "application/vnd.oasis.opendocument.text"+ )- self.assertTrue("Creation-Date" in [m['key'] for m in metadata])- self.assertTrue("Some-key" in [m['key'] for m in metadata])+ self.assertTrue("Creation-Date" in [m["key"] for m in metadata])+ self.assertTrue("Some-key" in [m["key"] for m in metadata])