mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-07-28 18:24:38 -05:00
Format Python code with black
This commit is contained in:
@@ -13,9 +13,11 @@ from django.utils import termcolors, timezone
|
||||
from filelock import FileLock
|
||||
|
||||
from .. 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 color
|
||||
else 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 color
|
||||
else 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 = selected
|
||||
document.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 color
|
||||
else 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 color
|
||||
else 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 = selected
|
||||
document.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:
|
||||
return
|
||||
if base_url:
|
||||
print(
|
||||
termcolors.colorize(str(document), fg='green')
|
||||
termcolors.colorize(str(document), fg="green")
|
||||
if color
|
||||
else 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 color
|
||||
else 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: E501
|
||||
else:
|
||||
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):
|
||||
|
Reference in New Issue
Block a user