From 502537915021fde6dc41e1958d2004f15906eed9 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Thu, 28 Dec 2023 09:33:56 -0800 Subject: [PATCH] Fix: allow multiple consumption templates to assign the same custom field --- src/documents/data_models.py | 1 + .../tests/test_consumption_templates.py | 51 +++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/documents/data_models.py b/src/documents/data_models.py index 8b53e2c14..12fa6db72 100644 --- a/src/documents/data_models.py +++ b/src/documents/data_models.py @@ -80,6 +80,7 @@ class DocumentMetadataOverrides: 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])