mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-08-16 00:36:22 +00:00
Configures ruff as the one stop linter and resolves warnings it raised
This commit is contained in:
@@ -18,7 +18,16 @@ class MailAccountAdminForm(forms.ModelForm):
|
||||
widgets = {
|
||||
"password": forms.PasswordInput(),
|
||||
}
|
||||
fields = "__all__"
|
||||
fields = [
|
||||
"name",
|
||||
"imap_server",
|
||||
"username",
|
||||
"imap_security",
|
||||
"username",
|
||||
"password",
|
||||
"is_token",
|
||||
"character_set",
|
||||
]
|
||||
|
||||
|
||||
class MailAccountAdmin(admin.ModelAdmin):
|
||||
@@ -27,7 +36,10 @@ class MailAccountAdmin(admin.ModelAdmin):
|
||||
|
||||
fieldsets = [
|
||||
(None, {"fields": ["name", "imap_server", "imap_port"]}),
|
||||
(_("Authentication"), {"fields": ["imap_security", "username", "password"]}),
|
||||
(
|
||||
_("Authentication"),
|
||||
{"fields": ["imap_security", "username", "password", "is_token"]},
|
||||
),
|
||||
(_("Advanced settings"), {"fields": ["character_set"]}),
|
||||
]
|
||||
form = MailAccountAdminForm
|
||||
|
@@ -94,7 +94,7 @@ class BaseMailAction:
|
||||
"""
|
||||
Perform mail action on the given mail uid in the mailbox.
|
||||
"""
|
||||
raise NotImplementedError()
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class DeleteMailAction(BaseMailAction):
|
||||
@@ -152,7 +152,7 @@ class TagMailAction(BaseMailAction):
|
||||
_, self.color = parameter.split(":")
|
||||
self.color = self.color.strip()
|
||||
|
||||
if not self.color.lower() in APPLE_MAIL_TAG_COLORS.keys():
|
||||
if self.color.lower() not in APPLE_MAIL_TAG_COLORS.keys():
|
||||
raise MailError("Not a valid AppleMail tag color.")
|
||||
|
||||
self.keyword = None
|
||||
@@ -274,7 +274,7 @@ def apply_mail_action(
|
||||
status="SUCCESS",
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
except Exception:
|
||||
ProcessedMail.objects.create(
|
||||
owner=rule.owner,
|
||||
rule=rule,
|
||||
@@ -285,7 +285,7 @@ def apply_mail_action(
|
||||
status="FAILED",
|
||||
error=traceback.format_exc(),
|
||||
)
|
||||
raise e
|
||||
raise
|
||||
|
||||
|
||||
@shared_task
|
||||
@@ -548,7 +548,7 @@ class MailAccountHandler(LoggingMixin):
|
||||
|
||||
self.log(
|
||||
"debug",
|
||||
f"Rule {rule}: Searching folder with criteria " f"{str(criterias)}",
|
||||
f"Rule {rule}: Searching folder with criteria {str(criterias)}",
|
||||
)
|
||||
|
||||
try:
|
||||
@@ -582,7 +582,7 @@ class MailAccountHandler(LoggingMixin):
|
||||
except Exception as e:
|
||||
self.log(
|
||||
"error",
|
||||
f"Rule {rule}: Error while processing mail " f"{message.uid}: {e}",
|
||||
f"Rule {rule}: Error while processing mail {message.uid}: {e}",
|
||||
exc_info=True,
|
||||
)
|
||||
|
||||
@@ -653,7 +653,7 @@ class MailAccountHandler(LoggingMixin):
|
||||
for att in message.attachments:
|
||||
|
||||
if (
|
||||
not att.content_disposition == "attachment"
|
||||
att.content_disposition != "attachment"
|
||||
and rule.attachment_type
|
||||
== MailRule.AttachmentProcessing.ATTACHMENTS_ONLY
|
||||
):
|
||||
@@ -665,14 +665,13 @@ class MailAccountHandler(LoggingMixin):
|
||||
)
|
||||
continue
|
||||
|
||||
if rule.filter_attachment_filename:
|
||||
if rule.filter_attachment_filename and not fnmatch(
|
||||
att.filename.lower(),
|
||||
rule.filter_attachment_filename.lower(),
|
||||
):
|
||||
# Force the filename and pattern to the lowercase
|
||||
# as this is system dependent otherwise
|
||||
if not fnmatch(
|
||||
att.filename.lower(),
|
||||
rule.filter_attachment_filename.lower(),
|
||||
):
|
||||
continue
|
||||
continue
|
||||
|
||||
title = self._get_title(message, att, rule)
|
||||
|
||||
|
@@ -27,7 +27,8 @@ class Migration(migrations.Migration):
|
||||
model_name="mailrule",
|
||||
name="maximum_age",
|
||||
field=models.PositiveIntegerField(
|
||||
default=30, help_text="Specified in days."
|
||||
default=30,
|
||||
help_text="Specified in days.",
|
||||
),
|
||||
),
|
||||
]
|
||||
|
@@ -160,35 +160,48 @@ class Migration(migrations.Migration):
|
||||
model_name="mailrule",
|
||||
name="filter_body",
|
||||
field=models.CharField(
|
||||
blank=True, max_length=256, null=True, verbose_name="filter body"
|
||||
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"
|
||||
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"
|
||||
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"
|
||||
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"
|
||||
default=30,
|
||||
help_text="Specified in days.",
|
||||
verbose_name="maximum age",
|
||||
),
|
||||
),
|
||||
migrations.AlterField(
|
||||
|
@@ -14,7 +14,9 @@ class Migration(migrations.Migration):
|
||||
model_name="mailrule",
|
||||
name="assign_tags",
|
||||
field=models.ManyToManyField(
|
||||
blank=True, to="documents.Tag", verbose_name="assign this tag"
|
||||
blank=True,
|
||||
to="documents.Tag",
|
||||
verbose_name="assign this tag",
|
||||
),
|
||||
),
|
||||
]
|
||||
|
@@ -29,19 +29,25 @@ class Migration(migrations.Migration):
|
||||
(
|
||||
"folder",
|
||||
models.CharField(
|
||||
editable=False, max_length=256, verbose_name="folder"
|
||||
editable=False,
|
||||
max_length=256,
|
||||
verbose_name="folder",
|
||||
),
|
||||
),
|
||||
(
|
||||
"uid",
|
||||
models.CharField(
|
||||
editable=False, max_length=256, verbose_name="uid"
|
||||
editable=False,
|
||||
max_length=256,
|
||||
verbose_name="uid",
|
||||
),
|
||||
),
|
||||
(
|
||||
"subject",
|
||||
models.CharField(
|
||||
editable=False, max_length=256, verbose_name="subject"
|
||||
editable=False,
|
||||
max_length=256,
|
||||
verbose_name="subject",
|
||||
),
|
||||
),
|
||||
(
|
||||
@@ -59,13 +65,18 @@ class Migration(migrations.Migration):
|
||||
(
|
||||
"status",
|
||||
models.CharField(
|
||||
editable=False, max_length=256, verbose_name="status"
|
||||
editable=False,
|
||||
max_length=256,
|
||||
verbose_name="status",
|
||||
),
|
||||
),
|
||||
(
|
||||
"error",
|
||||
models.TextField(
|
||||
blank=True, editable=False, null=True, verbose_name="error"
|
||||
blank=True,
|
||||
editable=False,
|
||||
null=True,
|
||||
verbose_name="error",
|
||||
),
|
||||
),
|
||||
(
|
||||
|
@@ -13,7 +13,10 @@ class Migration(migrations.Migration):
|
||||
model_name="mailrule",
|
||||
name="filter_to",
|
||||
field=models.CharField(
|
||||
blank=True, max_length=256, null=True, verbose_name="filter to"
|
||||
blank=True,
|
||||
max_length=256,
|
||||
null=True,
|
||||
verbose_name="filter to",
|
||||
),
|
||||
),
|
||||
]
|
||||
|
@@ -13,7 +13,8 @@ class Migration(migrations.Migration):
|
||||
model_name="mailaccount",
|
||||
name="is_token",
|
||||
field=models.BooleanField(
|
||||
default=False, verbose_name="Is token authentication"
|
||||
default=False,
|
||||
verbose_name="Is token authentication",
|
||||
),
|
||||
),
|
||||
]
|
||||
|
@@ -69,7 +69,7 @@ class MailRule(document_models.ModelWithOwner):
|
||||
|
||||
class AttachmentProcessing(models.IntegerChoices):
|
||||
ATTACHMENTS_ONLY = 1, _("Only process attachments.")
|
||||
EVERYTHING = 2, _("Process all files, including 'inline' " "attachments.")
|
||||
EVERYTHING = 2, _("Process all files, including 'inline' attachments.")
|
||||
|
||||
class MailAction(models.IntegerChoices):
|
||||
DELETE = 1, _("Delete")
|
||||
|
@@ -38,7 +38,7 @@ class MailDocumentParser(DocumentParser):
|
||||
except Exception as err:
|
||||
raise ParseError(
|
||||
f"Could not parse {document_path}: {err}",
|
||||
)
|
||||
) from err
|
||||
if not self._parsed.from_values:
|
||||
self._parsed = None
|
||||
raise ParseError(
|
||||
@@ -65,7 +65,7 @@ class MailDocumentParser(DocumentParser):
|
||||
except ParseError as e:
|
||||
self.log(
|
||||
"warning",
|
||||
f"Error while fetching document metadata for " f"{document_path}: {e}",
|
||||
f"Error while fetching document metadata for {document_path}: {e}",
|
||||
)
|
||||
return result
|
||||
|
||||
@@ -132,7 +132,7 @@ class MailDocumentParser(DocumentParser):
|
||||
|
||||
self.text += f"Attachments: {', '.join(att)}\n\n"
|
||||
|
||||
if mail.html != "":
|
||||
if mail.html:
|
||||
self.text += "HTML content: " + strip_text(self.tika_parse(mail.html))
|
||||
|
||||
self.text += f"\n\n{strip_text(mail.text)}"
|
||||
@@ -153,7 +153,7 @@ class MailDocumentParser(DocumentParser):
|
||||
raise ParseError(
|
||||
f"Could not parse content with tika server at "
|
||||
f"{self.tika_server}: {err}",
|
||||
)
|
||||
) from err
|
||||
if parsed["content"]:
|
||||
return parsed["content"]
|
||||
else:
|
||||
@@ -167,7 +167,7 @@ class MailDocumentParser(DocumentParser):
|
||||
|
||||
pdf_collection.append(("1_mail.pdf", self.generate_pdf_from_mail(mail)))
|
||||
|
||||
if mail.html == "":
|
||||
if not mail.html:
|
||||
with open(pdf_path, "wb") as file:
|
||||
file.write(pdf_collection[0][1])
|
||||
file.close()
|
||||
@@ -188,7 +188,7 @@ class MailDocumentParser(DocumentParser):
|
||||
response = requests.post(url_merge, files=files, headers=headers)
|
||||
response.raise_for_status() # ensure we notice bad responses
|
||||
except Exception as err:
|
||||
raise ParseError(f"Error while converting document to PDF: {err}")
|
||||
raise ParseError(f"Error while converting document to PDF: {err}") from err
|
||||
|
||||
with open(pdf_path, "wb") as file:
|
||||
file.write(response.content)
|
||||
@@ -212,26 +212,26 @@ class MailDocumentParser(DocumentParser):
|
||||
return text
|
||||
|
||||
data["subject"] = clean_html(mail.subject)
|
||||
if data["subject"] != "":
|
||||
if data["subject"]:
|
||||
data["subject_label"] = "Subject"
|
||||
data["from"] = clean_html(mail.from_values.full)
|
||||
if data["from"] != "":
|
||||
if data["from"]:
|
||||
data["from_label"] = "From"
|
||||
data["to"] = clean_html(", ".join(address.full for address in mail.to_values))
|
||||
if data["to"] != "":
|
||||
if data["to"]:
|
||||
data["to_label"] = "To"
|
||||
data["cc"] = clean_html(", ".join(address.full for address in mail.cc_values))
|
||||
if data["cc"] != "":
|
||||
if data["cc"]:
|
||||
data["cc_label"] = "CC"
|
||||
data["bcc"] = clean_html(", ".join(address.full for address in mail.bcc_values))
|
||||
if data["bcc"] != "":
|
||||
if data["bcc"]:
|
||||
data["bcc_label"] = "BCC"
|
||||
|
||||
att = []
|
||||
for a in mail.attachments:
|
||||
att.append(f"{a.filename} ({format_size(a.size, binary=True)})")
|
||||
data["attachments"] = clean_html(", ".join(att))
|
||||
if data["attachments"] != "":
|
||||
if data["attachments"]:
|
||||
data["attachments_label"] = "Attachments"
|
||||
|
||||
data["date"] = clean_html(mail.date.astimezone().strftime("%Y-%m-%d %H:%M"))
|
||||
@@ -290,7 +290,9 @@ class MailDocumentParser(DocumentParser):
|
||||
)
|
||||
response.raise_for_status() # ensure we notice bad responses
|
||||
except Exception as err:
|
||||
raise ParseError(f"Error while converting document to PDF: {err}")
|
||||
raise ParseError(
|
||||
f"Error while converting document to PDF: {err}",
|
||||
) from err
|
||||
|
||||
return response.content
|
||||
|
||||
@@ -344,6 +346,6 @@ class MailDocumentParser(DocumentParser):
|
||||
)
|
||||
response.raise_for_status() # ensure we notice bad responses
|
||||
except Exception as err:
|
||||
raise ParseError(f"Error while converting document to PDF: {err}")
|
||||
raise ParseError(f"Error while converting document to PDF: {err}") from err
|
||||
|
||||
return response.content
|
||||
|
@@ -38,9 +38,11 @@ class MailAccountSerializer(OwnedObjectSerializer):
|
||||
]
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
if "password" in validated_data:
|
||||
if len(validated_data.get("password").replace("*", "")) == 0:
|
||||
validated_data.pop("password")
|
||||
if (
|
||||
"password" in validated_data
|
||||
and len(validated_data.get("password").replace("*", "")) == 0
|
||||
):
|
||||
validated_data.pop("password")
|
||||
super().update(instance, validated_data)
|
||||
return instance
|
||||
|
||||
|
@@ -47,7 +47,7 @@ class TestMailLiveServer(TestCase):
|
||||
|
||||
except MailError as e:
|
||||
self.fail(f"Failure: {e}")
|
||||
except Exception as e:
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def test_process_non_gmail_server_tag(self):
|
||||
@@ -66,5 +66,5 @@ class TestMailLiveServer(TestCase):
|
||||
|
||||
except MailError as e:
|
||||
self.fail(f"Failure: {e}")
|
||||
except Exception as e:
|
||||
except Exception:
|
||||
pass
|
||||
|
@@ -12,11 +12,8 @@ from unittest import mock
|
||||
from django.core.management import call_command
|
||||
from django.db import DatabaseError
|
||||
from django.test import TestCase
|
||||
from documents.data_models import ConsumableDocument
|
||||
from documents.data_models import DocumentMetadataOverrides
|
||||
from documents.models import Correspondent
|
||||
from documents.tests.utils import DirectoriesMixin
|
||||
from documents.tests.utils import DocumentConsumeDelayMixin
|
||||
from documents.tests.utils import FileSystemAssertsMixin
|
||||
from imap_tools import EmailAddress
|
||||
from imap_tools import FolderInfo
|
||||
@@ -183,7 +180,7 @@ class BogusMailBox(ContextManager):
|
||||
)
|
||||
self.messages = list(filter(lambda m: m.uid not in uid_list, self.messages))
|
||||
else:
|
||||
raise Exception()
|
||||
raise Exception
|
||||
|
||||
|
||||
def fake_magic_from_buffer(buffer, mime=False):
|
||||
@@ -769,7 +766,7 @@ class TestMail(
|
||||
with self.assertRaisesRegex(
|
||||
MailError,
|
||||
"Error while authenticating account",
|
||||
) as context:
|
||||
):
|
||||
self.mail_account_handler.handle_mail_account(account)
|
||||
|
||||
def test_error_skip_account(self):
|
||||
|
@@ -65,9 +65,8 @@ class MailAccountTestView(GenericAPIView):
|
||||
try:
|
||||
mailbox_login(M, account)
|
||||
return Response({"success": True})
|
||||
except MailError as e:
|
||||
except MailError:
|
||||
logger.error(
|
||||
f"Mail account {account} test failed: {e}",
|
||||
exc_info=False,
|
||||
f"Mail account {account} test failed",
|
||||
)
|
||||
return HttpResponseBadRequest("Unable to connect to server")
|
||||
|
Reference in New Issue
Block a user