Enhancement: allow multiple filename attachment exclusion patterns for a mail rule (#5524)

---------

Co-authored-by: shamoon <4887959+shamoon@users.noreply.github.com>
This commit is contained in:
Dennis Melzer 2024-08-27 18:31:46 +02:00 committed by GitHub
parent e21552e053
commit eaaaa575b8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 35 additions and 6 deletions

View File

@ -136,7 +136,8 @@ These rules perform the following:
Paperless will check all emails only once and completely ignore messages Paperless will check all emails only once and completely ignore messages
that do not match your filters. It will also only perform the rule action that do not match your filters. It will also only perform the rule action
on e-mails that it has consumed documents from. on e-mails that it has consumed documents from. The filename attachment
exclusion pattern can include multiple patterns separated by a comma.
The actions all ensure that the same mail is not consumed twice by The actions all ensure that the same mail is not consumed twice by
different means. These are as follows: different means. These are as follows:

View File

@ -656,6 +656,24 @@ class MailAccountHandler(LoggingMixin):
return processed_elements return processed_elements
def filename_exclusion_matches(
self,
filter_attachment_filename_exclude: Optional[str],
filename: str,
) -> bool:
if filter_attachment_filename_exclude:
filter_attachment_filename_exclusions = (
filter_attachment_filename_exclude.split(",")
)
# Force the filename and pattern to the lowercase
# as this is system dependent otherwise
filename = filename.lower()
for filename_exclude in filter_attachment_filename_exclusions:
if filename_exclude and fnmatch(filename, filename_exclude.lower()):
return True
return False
def _process_attachments( def _process_attachments(
self, self,
message: MailMessage, message: MailMessage,
@ -692,12 +710,10 @@ class MailAccountHandler(LoggingMixin):
f"does not match pattern {rule.filter_attachment_filename_include}", f"does not match pattern {rule.filter_attachment_filename_include}",
) )
continue continue
elif rule.filter_attachment_filename_exclude and fnmatch( elif self.filename_exclusion_matches(
att.filename.lower(), rule.filter_attachment_filename_exclude,
rule.filter_attachment_filename_exclude.lower(), att.filename,
): ):
# Force the filename and pattern to the lowercase
# as this is system dependent otherwise
self.log.debug( self.log.debug(
f"Rule {rule}: " f"Rule {rule}: "
f"Skipping attachment {att.filename} " f"Skipping attachment {att.filename} "

View File

@ -592,6 +592,18 @@ class TestMail(
exclude_pattern="f1*", exclude_pattern="f1*",
expected_matches=["f2.pdf", "f3.pdf", "file.PDf"], expected_matches=["f2.pdf", "f3.pdf", "file.PDf"],
), ),
FilterTestCase(
"PDF Files without f1 and f2",
include_pattern="*.pdf",
exclude_pattern="f1*,f2*",
expected_matches=["f3.pdf", "file.PDf"],
),
FilterTestCase(
"PDF Files without f1 and f2 and f3",
include_pattern="*.pdf",
exclude_pattern="f1*,f2*,f3*",
expected_matches=["file.PDf"],
),
FilterTestCase( FilterTestCase(
"All Files, no PNG", "All Files, no PNG",
include_pattern="*", include_pattern="*",