mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-11-03 03:16:10 -06:00 
			
		
		
		
	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:
		@@ -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:
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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} "
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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="*",
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user