diff --git a/src/documents/barcodes.py b/src/documents/barcodes.py index f3c693edf..75a196250 100644 --- a/src/documents/barcodes.py +++ b/src/documents/barcodes.py @@ -16,6 +16,7 @@ from pikepdf import Pdf from documents.converters import convert_from_tiff_to_pdf from documents.data_models import ConsumableDocument from documents.data_models import DocumentMetadataOverrides +from documents.models import Document from documents.models import Tag from documents.plugins.base import ConsumeTaskPlugin from documents.plugins.base import StopConsumeTaskError @@ -186,13 +187,19 @@ class BarcodePlugin(ConsumeTaskPlugin): # Update/overwrite an ASN if possible # After splitting, as otherwise each split document gets the same ASN - if ( - self.settings.barcode_enable_asn - and not self.metadata.skip_asn - and (located_asn := self.asn) is not None - ): - logger.info(f"Found ASN in barcode: {located_asn}") - self.metadata.asn = located_asn + if self.settings.barcode_enable_asn and (located_asn := self.asn) is not None: + if ( + self.metadata.skip_asn_if_exists + and Document.global_objects.filter( + archive_serial_number=located_asn, + ).exists() + ): + logger.info( + f"Found ASN in barcode {located_asn} but skipping because it already exists.", + ) + else: + logger.info(f"Found ASN in barcode: {located_asn}") + self.metadata.asn = located_asn def cleanup(self) -> None: self.temp_dir.cleanup() diff --git a/src/documents/bulk_edit.py b/src/documents/bulk_edit.py index bde3b1a8b..07abb84d9 100644 --- a/src/documents/bulk_edit.py +++ b/src/documents/bulk_edit.py @@ -473,6 +473,8 @@ def merge( if user is not None: overrides.owner_id = user.id + if not delete_originals: + overrides.skip_asn_if_exists = True if delete_originals and handoff_asn is not None: overrides.asn = handoff_asn @@ -543,6 +545,8 @@ def split( overrides.title = f"{doc.title} (split {idx + 1})" if user is not None: overrides.owner_id = user.id + if not delete_originals: + overrides.skip_asn_if_exists = True logger.info( f"Adding split document with pages {split_doc} to the task queue.", ) @@ -673,6 +677,8 @@ def edit_pdf( ) if user is not None: overrides.owner_id = user.id + if not delete_original: + overrides.skip_asn_if_exists = True if delete_original and len(pdf_docs) == 1: overrides.asn = doc.archive_serial_number for idx, pdf in enumerate(pdf_docs, start=1): diff --git a/src/documents/consumer.py b/src/documents/consumer.py index 2c1cf025b..56b1e6ade 100644 --- a/src/documents/consumer.py +++ b/src/documents/consumer.py @@ -696,7 +696,7 @@ class ConsumerPlugin( pk=self.metadata.storage_path_id, ) - if self.metadata.asn is not None and not self.metadata.skip_asn: + if self.metadata.asn is not None: document.archive_serial_number = self.metadata.asn if self.metadata.owner_id: @@ -812,8 +812,8 @@ class ConsumerPreflightPlugin( """ Check that if override_asn is given, it is unique and within a valid range """ - if self.metadata.skip_asn or self.metadata.asn is None: - # if skip is set or ASN is None + if self.metadata.asn is None: + # if ASN is None return # Validate the range is above zero and less than uint32_t max # otherwise, Whoosh can't handle it in the index diff --git a/src/documents/data_models.py b/src/documents/data_models.py index 3acc3f51b..661f946e0 100644 --- a/src/documents/data_models.py +++ b/src/documents/data_models.py @@ -30,7 +30,7 @@ class DocumentMetadataOverrides: change_users: list[int] | None = None change_groups: list[int] | None = None custom_fields: dict | None = None - skip_asn: bool = False + skip_asn_if_exists: bool = False def update(self, other: "DocumentMetadataOverrides") -> "DocumentMetadataOverrides": """ @@ -50,8 +50,8 @@ class DocumentMetadataOverrides: self.storage_path_id = other.storage_path_id if other.owner_id is not None: self.owner_id = other.owner_id - if other.skip_asn: - self.skip_asn = True + if other.skip_asn_if_exists: + self.skip_asn_if_exists = True # merge if self.tag_ids is None: diff --git a/src/documents/tests/test_bulk_edit.py b/src/documents/tests/test_bulk_edit.py index 717e47fda..347a372ac 100644 --- a/src/documents/tests/test_bulk_edit.py +++ b/src/documents/tests/test_bulk_edit.py @@ -602,7 +602,6 @@ class TestPDFActions(DirectoriesMixin, TestCase): expected_filename, ) self.assertEqual(consume_file_args[1].title, None) - self.assertFalse(consume_file_args[1].skip_asn) # No metadata_document_id, delete_originals False, so ASN should be None self.assertIsNone(consume_file_args[1].asn) @@ -611,7 +610,6 @@ class TestPDFActions(DirectoriesMixin, TestCase): consume_file_args, _ = mock_consume_file.call_args self.assertEqual(consume_file_args[1].title, "B (merged)") self.assertEqual(consume_file_args[1].created, self.doc2.created) - self.assertFalse(consume_file_args[1].skip_asn) self.assertEqual(result, "OK") @@ -657,7 +655,6 @@ class TestPDFActions(DirectoriesMixin, TestCase): expected_filename, ) self.assertEqual(consume_file_args[1].title, None) - self.assertFalse(consume_file_args[1].skip_asn) self.assertEqual(consume_file_args[1].asn, 101) delete_documents_args, _ = mock_delete_documents.call_args @@ -741,7 +738,6 @@ class TestPDFActions(DirectoriesMixin, TestCase): self.assertEqual(mock_consume_file.call_count, 2) consume_file_args, _ = mock_consume_file.call_args self.assertEqual(consume_file_args[1].title, "B (split 2)") - self.assertFalse(consume_file_args[1].skip_asn) self.assertIsNone(consume_file_args[1].asn) self.assertEqual(result, "OK") @@ -776,7 +772,6 @@ class TestPDFActions(DirectoriesMixin, TestCase): self.assertEqual(mock_consume_file.call_count, 2) consume_file_args, _ = mock_consume_file.call_args self.assertEqual(consume_file_args[1].title, "B (split 2)") - self.assertFalse(consume_file_args[1].skip_asn) mock_delete_documents.assert_called() mock_chord.assert_called_once() @@ -997,7 +992,6 @@ class TestPDFActions(DirectoriesMixin, TestCase): self.assertEqual(result, "OK") mock_chord.assert_called_once() consume_file_args, _ = mock_consume_file.call_args - self.assertFalse(consume_file_args[1].skip_asn) self.assertEqual(consume_file_args[1].asn, 250) self.doc2.refresh_from_db() self.assertIsNone(self.doc2.archive_serial_number) diff --git a/src/documents/tests/test_consumer.py b/src/documents/tests/test_consumer.py index 63d6f8f5b..6387b5e95 100644 --- a/src/documents/tests/test_consumer.py +++ b/src/documents/tests/test_consumer.py @@ -412,14 +412,6 @@ class TestConsumer( self.assertEqual(document.archive_serial_number, 123) self._assert_first_last_send_progress() - def testMetadataOverridesSkipAsnPropagation(self): - overrides = DocumentMetadataOverrides() - incoming = DocumentMetadataOverrides(skip_asn=True) - - overrides.update(incoming) - - self.assertTrue(overrides.skip_asn) - def testOverrideTitlePlaceholders(self): c = Correspondent.objects.create(name="Correspondent Name") dt = DocumentType.objects.create(name="DocType Name")