A community-supported supercharged document management system: scan, index and archive all your documents
Python aiangulararchivingdjangodmsdocument-managementdocument-management-systemllmmachine-learningocroptical-character-recognitionpdf
Fix: dont defer tag assignment when barcode will not result in a split (#14497)
shamoon20b7a116fdf9
diff --git a/src/documents/barcodes.py b/src/documents/barcodes.pyindex 324d65d61..65cba9a71 100644--- a/src/documents/barcodes.py+++ b/src/documents/barcodes.py@@ -165,17 +165,19 @@ class BarcodePlugin(ConsumeTaskPlugin):# Locate any barcodes in the filesself.detect()- # try reading tags from barcodes- # If tag splitting is enabled, skip this on the original document - let each split document extract its own tags- # However, if we're processing a split document (original_path is set), extract tags+ separator_pages = (+ self.get_separation_pages() if self.settings.barcodes_enabled else {}+ )++ # Defer tags to split documents only when a split will actually happen.if (self.settings.barcode_enable_tagand (not self.settings.barcode_tag_splitor self.input_doc.original_path is not None+ or not separator_pages)- and (tags := self.tags) is not None- and len(tags) > 0+ and (tags := self.tags)):if self.metadata.tag_ids:self.metadata.tag_ids += tags@@ -184,9 +186,7 @@ class BarcodePlugin(ConsumeTaskPlugin):logger.info(f"Found tags in barcode: {tags}")# Lastly attempt to split documents- if self.settings.barcodes_enabled and (- separator_pages := self.get_separation_pages()- ):+ if separator_pages:# We have pages to split against# Note this does NOT use the base_temp_dir, as that will be removeddiff --git a/src/documents/tests/test_barcodes.py b/src/documents/tests/test_barcodes.pyindex 521b87a6d..36322d2ab 100644--- a/src/documents/tests/test_barcodes.py+++ b/src/documents/tests/test_barcodes.py@@ -4,6 +4,7 @@ import shutilfrom contextlib import contextmanagerfrom pathlib import Pathfrom typing import TYPE_CHECKING+from unittest.mock import patchimport pytestimport zxingcpp@@ -13,6 +14,7 @@ from django.test import override_settingsfrom rest_framework import statusfrom documents import tasks+from documents.barcodes import Barcodefrom documents.barcodes import BarcodePluginfrom documents.barcodes import read_barcode_valuesfrom documents.consumer import ConsumerError@@ -844,6 +846,89 @@ class TestTagBarcode(DirectoriesMixin, SampleDirMixin, GetReaderPluginMixin, Tesyield readerreader.cleanup()+ @override_settings(+ CONSUMER_ENABLE_BARCODES=True,+ CONSUMER_ENABLE_ASN_BARCODE=True,+ CONSUMER_ENABLE_TAG_BARCODE=True,+ CONSUMER_TAG_BARCODE_SPLIT=True,+ )+ def test_first_page_tags_without_split(self) -> None:+ test_file = self.BARCODE_SAMPLE_DIR / "barcode-39-asn-123.pdf"+ with self.get_reader(test_file) as reader:+ reader.barcodes = [+ Barcode(0, "ASN123,TAG:invoice", reader.settings),+ ]+ existing_tag = Tag.objects.create(name="existing")+ reader.metadata.tag_ids = [existing_tag.pk]++ reader.run()++ self.assertEqual(reader.metadata.asn, 123)+ self.assertCountEqual(+ reader.metadata.tag_ids,+ [existing_tag.pk, Tag.objects.get(name="invoice").pk],+ )++ @override_settings(+ CONSUMER_ENABLE_BARCODES=True,+ CONSUMER_ENABLE_ASN_BARCODE=True,+ CONSUMER_ENABLE_TAG_BARCODE=True,+ CONSUMER_TAG_BARCODE_SPLIT=False,+ )+ def test_asn_split_preserves_tags_when_tag_split_disabled(self) -> None:+ source = self.BARCODE_SAMPLE_DIR / "split-by-tag-basic.pdf"+ test_file = self.dirs.scratch_dir / "asn-split.pdf"+ shutil.copy(source, test_file)+ with self.get_reader(test_file) as reader:+ reader.barcodes = [+ Barcode(0, "ASN123", reader.settings),+ Barcode(0, "TAG:invoice", reader.settings),+ Barcode(1, "ASN456", reader.settings),+ Barcode(1, "TAG:receipt", reader.settings),+ ]+ existing_tag = Tag.objects.create(name="existing")+ reader.metadata.tag_ids = [existing_tag.pk]++ with patch("documents.tasks.consume_file.apply_async") as consume:+ with self.assertRaises(StopConsumeTaskError):+ reader.run()++ self.assertEqual(consume.call_count, 2)+ for call in consume.call_args_list:+ self.assertCountEqual(+ call.kwargs["kwargs"]["overrides"].tag_ids,+ [+ existing_tag.pk,+ Tag.objects.get(name="invoice").pk,+ Tag.objects.get(name="receipt").pk,+ ],+ )++ @override_settings(+ CONSUMER_ENABLE_BARCODES=True,+ CONSUMER_ENABLE_TAG_BARCODE=True,+ CONSUMER_TAG_BARCODE_SPLIT=False,+ CONSUMER_BARCODE_RETAIN_SPLIT_PAGES=False,+ )+ def test_discarded_separator_page_preserves_tags(self) -> None:+ source = self.BARCODE_SAMPLE_DIR / "split-by-tag-basic.pdf"+ test_file = self.dirs.scratch_dir / "separator-split.pdf"+ shutil.copy(source, test_file)+ with self.get_reader(test_file) as reader:+ reader.barcodes = [+ Barcode(0, reader.settings.barcode_string, reader.settings),+ Barcode(0, "TAG:invoice", reader.settings),+ ]+ with patch("documents.tasks.consume_file.apply_async") as consume:+ with self.assertRaises(StopConsumeTaskError):+ reader.run()++ consume.assert_called_once()+ self.assertEqual(+ consume.call_args.kwargs["kwargs"]["overrides"].tag_ids,+ [Tag.objects.get(name="invoice").pk],+ )+@override_settings(CONSUMER_ENABLE_TAG_BARCODE=True,CONSUMER_TAG_BARCODE_MAPPING={"TAG:(.*)": "\\g<1>"},