Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

Change: Use fnmatch for workflow path matching (#5250)

shamoond623af9c419d
diff --git a/src/documents/matching.py b/src/documents/matching.py
index ec28f80ca..15d839db6 100644
--- a/src/documents/matching.py
+++ b/src/documents/matching.py
@@ -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}"
diff --git a/src/documents/tests/test_workflows.py b/src/documents/tests/test_workflows.py
index 2e516e24c..92207742f 100644
--- a/src/documents/tests/test_workflows.py
+++ b/src/documents/tests/test_workflows.py
@@ -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):
"""