]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Fix: workflow assignment of customfield fails if field exists in v2.3.1 (#5302)
authorshamoon <4887959+shamoon@users.noreply.github.com>
Sun, 7 Jan 2024 22:27:57 +0000 (14:27 -0800)
committerGitHub <noreply@github.com>
Sun, 7 Jan 2024 22:27:57 +0000 (22:27 +0000)
src/documents/signals/handlers.py
src/documents/tests/test_workflows.py

index a86868462c7215aa4d33a2123ba6f04f12cf115a..b782cfa7f6025f2531ead6c7e516ab6cbe869a86 100644 (file)
@@ -610,10 +610,18 @@ def run_workflow(
 
                 if action.assign_custom_fields is not None:
                     for field in action.assign_custom_fields.all():
-                        CustomFieldInstance.objects.create(
-                            field=field,
-                            document=document,
-                        )  # adds to document
+                        if (
+                            CustomFieldInstance.objects.filter(
+                                field=field,
+                                document=document,
+                            ).count()
+                            == 0
+                        ):
+                            # can be triggered on existing docs, so only add the field if it doesnt already exist
+                            CustomFieldInstance.objects.create(
+                                field=field,
+                                document=document,
+                            )
 
             document.save()
 
index 92207742f1718ead5019303795f2db525a27361f..b4ad4aa57fb1351acf54f56ddb1e7a9711d0bad9 100644 (file)
@@ -13,6 +13,7 @@ from documents.data_models import DocumentSource
 from documents.matching import document_matches_workflow
 from documents.models import Correspondent
 from documents.models import CustomField
+from documents.models import CustomFieldInstance
 from documents.models import Document
 from documents.models import DocumentType
 from documents.models import MatchingModel
@@ -997,6 +998,47 @@ class TestWorkflows(DirectoriesMixin, FileSystemAssertsMixin, APITestCase):
 
         self.assertEqual(doc.custom_fields.all().count(), 1)
 
+    def test_document_updated_workflow_existing_custom_field(self):
+        """
+        GIVEN:
+            - Existing workflow with UPDATED trigger and action that adds a custom field
+        WHEN:
+            - Document is updated that already contains the field
+        THEN:
+            - Document update succeeds without trying to re-create the field
+        """
+        trigger = WorkflowTrigger.objects.create(
+            type=WorkflowTrigger.WorkflowTriggerType.DOCUMENT_UPDATED,
+            filter_has_document_type=self.dt,
+        )
+        action = WorkflowAction.objects.create()
+        action.assign_custom_fields.add(self.cf1)
+        w = Workflow.objects.create(
+            name="Workflow 1",
+            order=0,
+        )
+        w.triggers.add(trigger)
+        w.actions.add(action)
+        w.save()
+
+        doc = Document.objects.create(
+            title="sample test",
+            correspondent=self.c,
+            original_filename="sample.pdf",
+        )
+        CustomFieldInstance.objects.create(document=doc, field=self.cf1)
+
+        superuser = User.objects.create_superuser("superuser")
+        self.client.force_authenticate(user=superuser)
+
+        self.client.patch(
+            f"/api/documents/{doc.id}/",
+            {"document_type": self.dt.id},
+            format="json",
+        )
+
+        self.assertEqual(doc.custom_fields.all().count(), 1)
+
     def test_workflow_enabled_disabled(self):
         trigger = WorkflowTrigger.objects.create(
             type=WorkflowTrigger.WorkflowTriggerType.DOCUMENT_ADDED,