mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-10-12 02:26:09 -05:00
Format Python code with black
This commit is contained in:
@@ -8,8 +8,13 @@ import pathvalidate
|
||||
from django.conf import settings
|
||||
from django.db import DatabaseError
|
||||
from 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 LoggingMixin
|
||||
from 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 None
|
||||
|
||||
def 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: E501
|
||||
|
||||
def 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_correspondent
|
||||
|
||||
else:
|
||||
raise NotImplementedError("Unknwown correspondent selector") # pragma: nocover # NOQA: E501
|
||||
raise NotImplementedError(
|
||||
"Unknwown correspondent selector"
|
||||
) # pragma: nocover # NOQA: E501
|
||||
|
||||
def 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_files
|
||||
|
||||
def 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 0
|
||||
|
||||
self.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: E501
|
||||
self.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}",
|
||||
)
|
||||
continue
|
||||
|
||||
if 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: E501
|
||||
override_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: E501
|
||||
override_tag_ids=[tag.id] if tag else None,
|
||||
task_name=att.filename[:100]
|
||||
task_name=att.filename[:100],
|
||||
)
|
||||
|
||||
processed_attachments += 1
|
||||
else:
|
||||
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_attachments
|
||||
|
Reference in New Issue
Block a user