]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Enhancement: allow multiple filename attachment exclusion patterns for a mail rule...
authorDennis Melzer <dennis.melzer@de.bosch.com>
Tue, 27 Aug 2024 16:31:46 +0000 (18:31 +0200)
committerGitHub <noreply@github.com>
Tue, 27 Aug 2024 16:31:46 +0000 (09:31 -0700)
---------

Co-authored-by: shamoon <4887959+shamoon@users.noreply.github.com>
docs/usage.md
src/paperless_mail/mail.py
src/paperless_mail/tests/test_mail.py

index a20e5ea4a18d13fa47a3b957e636eb2823adc1fd..bf45d4982036950bd8a148fbceff1f7bdd03c2f1 100644 (file)
@@ -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:
index 50435de5dceffa78b38c3b7efbd7c82f502e7cec..83771dbf52bd44149b774a8c30ec71e9a1ae8dba 100644 (file)
@@ -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} "
index 26130b643225fece15e948a826552156ddd316e0..0920f033c9a5aa71f6de9358a9a4241df64be634 100644 (file)
@@ -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="*",