Skip to content

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

Fix: allow multiple consumption templates to assign the same custom field (#5142)

shamoonfbeb03c37798
diff --git a/src/documents/data_models.py b/src/documents/data_models.py
index 8b53e2c14..0d506cd6a 100644
--- a/src/documents/data_models.py
+++ b/src/documents/data_models.py
@@ -55,31 +55,37 @@ class DocumentMetadataOverrides:
self.tag_ids = other.tag_ids
elif other.tag_ids is not None:
self.tag_ids.extend(other.tag_ids)
+ self.tag_ids = list(set(self.tag_ids))
if self.view_users is None:
self.view_users = other.view_users
elif other.view_users is not None:
self.view_users.extend(other.view_users)
+ self.view_users = list(set(self.view_users))
if self.view_groups is None:
self.view_groups = other.view_groups
elif other.view_groups is not None:
self.view_groups.extend(other.view_groups)
+ self.view_groups = list(set(self.view_groups))
if self.change_users is None:
self.change_users = other.change_users
elif other.change_users is not None:
self.change_users.extend(other.change_users)
+ self.change_users = list(set(self.change_users))
if self.change_groups is None:
self.change_groups = other.change_groups
elif other.change_groups is not None:
self.change_groups.extend(other.change_groups)
+ self.change_groups = list(set(self.change_groups))
if self.custom_field_ids is None:
self.custom_field_ids = other.custom_field_ids
elif other.custom_field_ids is not None:
self.custom_field_ids.extend(other.custom_field_ids)
+ self.custom_field_ids = list(set(self.custom_field_ids))
return self
diff --git a/src/documents/tests/test_consumption_templates.py b/src/documents/tests/test_consumption_templates.py
index 3abbacf14..6f671bfc4 100644
--- a/src/documents/tests/test_consumption_templates.py
+++ b/src/documents/tests/test_consumption_templates.py
@@ -486,3 +486,54 @@ class TestConsumptionTemplates(DirectoriesMixin, FileSystemAssertsMixin, TestCas
self.assertIn(expected_str, cm.output[0])
expected_str = f"Document source {DocumentSource.ApiUpload.name} not in ['{DocumentSource.ConsumeFolder.name}', '{DocumentSource.MailFetch.name}']"
self.assertIn(expected_str, cm.output[1])
+
+ @mock.patch("documents.consumer.Consumer.try_consume_file")
+ def test_consumption_template_repeat_custom_fields(self, m):
+ """
+ GIVEN:
+ - Existing consumption templates which assign the same custom field
+ WHEN:
+ - File that matches is consumed
+ THEN:
+ - Custom field is added the first time successfully
+ """
+ ct = ConsumptionTemplate.objects.create(
+ name="Template 1",
+ order=0,
+ sources=f"{DocumentSource.ApiUpload},{DocumentSource.ConsumeFolder},{DocumentSource.MailFetch}",
+ filter_filename="*simple*",
+ )
+ ct.assign_custom_fields.add(self.cf1.pk)
+ ct.save()
+
+ ct2 = ConsumptionTemplate.objects.create(
+ name="Template 2",
+ order=1,
+ sources=f"{DocumentSource.ApiUpload},{DocumentSource.ConsumeFolder},{DocumentSource.MailFetch}",
+ filter_filename="*simple*",
+ )
+ ct2.assign_custom_fields.add(self.cf1.pk)
+ ct2.save()
+
+ test_file = self.SAMPLE_DIR / "simple.pdf"
+
+ with mock.patch("documents.tasks.async_to_sync"):
+ with self.assertLogs("paperless.matching", level="INFO") 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_custom_field_ids"],
+ [self.cf1.pk],
+ )
+
+ expected_str = f"Document matched template {ct}"
+ self.assertIn(expected_str, cm.output[0])
+ expected_str = f"Document matched template {ct2}"
+ self.assertIn(expected_str, cm.output[1])