From: Dennis Melzer Date: Tue, 27 Aug 2024 16:31:46 +0000 (+0200) Subject: Enhancement: allow multiple filename attachment exclusion patterns for a mail rule... X-Git-Tag: v2.12.0~1^2~26 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=eaaaa575b8cb23cf463ffccaa1f0a6f6e2af29d4;p=thirdparty%2Fpaperless-ngx.git Enhancement: allow multiple filename attachment exclusion patterns for a mail rule (#5524) --------- Co-authored-by: shamoon <4887959+shamoon@users.noreply.github.com> --- diff --git a/docs/usage.md b/docs/usage.md index a20e5ea4a1..bf45d49820 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -136,7 +136,8 @@ These rules perform the following: 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 -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 different means. These are as follows: diff --git a/src/paperless_mail/mail.py b/src/paperless_mail/mail.py index 50435de5dc..83771dbf52 100644 --- a/src/paperless_mail/mail.py +++ b/src/paperless_mail/mail.py @@ -656,6 +656,24 @@ class MailAccountHandler(LoggingMixin): 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( self, message: MailMessage, @@ -692,12 +710,10 @@ class MailAccountHandler(LoggingMixin): f"does not match pattern {rule.filter_attachment_filename_include}", ) continue - elif rule.filter_attachment_filename_exclude and fnmatch( - att.filename.lower(), - rule.filter_attachment_filename_exclude.lower(), + elif self.filename_exclusion_matches( + rule.filter_attachment_filename_exclude, + att.filename, ): - # Force the filename and pattern to the lowercase - # as this is system dependent otherwise self.log.debug( f"Rule {rule}: " f"Skipping attachment {att.filename} " diff --git a/src/paperless_mail/tests/test_mail.py b/src/paperless_mail/tests/test_mail.py index 26130b6432..0920f033c9 100644 --- a/src/paperless_mail/tests/test_mail.py +++ b/src/paperless_mail/tests/test_mail.py @@ -592,6 +592,18 @@ class TestMail( exclude_pattern="f1*", 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( "All Files, no PNG", include_pattern="*",