]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Change: Use fnmatch for workflow path matching (#5250)
authorshamoon <4887959+shamoon@users.noreply.github.com>
Fri, 5 Jan 2024 19:15:14 +0000 (11:15 -0800)
committerGitHub <noreply@github.com>
Fri, 5 Jan 2024 19:15:14 +0000 (19:15 +0000)
src/documents/matching.py
src/documents/tests/test_workflows.py

index ec28f80ca5daa20c6e1c3a7a56241bcc3984cc05..15d839db6655e654fead166fd2ba3cb9d3bf2c74 100644 (file)
@@ -296,7 +296,10 @@ def consumable_document_matches_workflow(
     if (
         trigger.filter_path is not None
         and len(trigger.filter_path) > 0
-        and not document.original_file.match(trigger.filter_path)
+        and not fnmatch(
+            document.original_file,
+            trigger.filter_path,
+        )
     ):
         reason = (
             f"Document path {document.original_file}"
index 2e516e24c120a44ee24546ff654cbfb77f8d9708..92207742f1718ead5019303795f2db525a27361f 100644 (file)
@@ -324,6 +324,53 @@ class TestWorkflows(DirectoriesMixin, FileSystemAssertsMixin, APITestCase):
         expected_str = f"Document matched {trigger2} from {w2}"
         self.assertIn(expected_str, cm.output[1])
 
+    @mock.patch("documents.consumer.Consumer.try_consume_file")
+    def test_workflow_fnmatch_path(self, m):
+        """
+        GIVEN:
+            - Existing workflow
+        WHEN:
+            - File that matches using fnmatch on path is consumed
+        THEN:
+            - Template overrides are applied
+            - Note: Test was added when path matching changed from pathlib.match to fnmatch
+        """
+        trigger = WorkflowTrigger.objects.create(
+            type=WorkflowTrigger.WorkflowTriggerType.CONSUMPTION,
+            sources=f"{DocumentSource.ApiUpload},{DocumentSource.ConsumeFolder},{DocumentSource.MailFetch}",
+            filter_path="*sample*",
+        )
+        action = WorkflowAction.objects.create(
+            assign_title="Doc fnmatch title",
+        )
+        action.save()
+
+        w = Workflow.objects.create(
+            name="Workflow 1",
+            order=0,
+        )
+        w.triggers.add(trigger)
+        w.actions.add(action)
+        w.save()
+
+        test_file = self.SAMPLE_DIR / "simple.pdf"
+
+        with mock.patch("documents.tasks.async_to_sync"):
+            with self.assertLogs("paperless.matching", level="DEBUG") as cm:
+                tasks.consume_file(
+                    ConsumableDocument(
+                        source=DocumentSource.ConsumeFolder,
+                        original_file=test_file,
+                    ),
+                    None,
+                )
+                m.assert_called_once()
+                _, overrides = m.call_args
+                self.assertEqual(overrides["override_title"], "Doc fnmatch title")
+
+        expected_str = f"Document matched {trigger} from {w}"
+        self.assertIn(expected_str, cm.output[0])
+
     @mock.patch("documents.consumer.Consumer.try_consume_file")
     def test_workflow_no_match_filename(self, m):
         """