]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Fix: Splitting on ASN barcodes even if not enabled (#5740)
authorTrenton H <797416+stumpylog@users.noreply.github.com>
Mon, 12 Feb 2024 20:58:37 +0000 (12:58 -0800)
committerGitHub <noreply@github.com>
Mon, 12 Feb 2024 20:58:37 +0000 (12:58 -0800)
* Fixes the barcodes always splitting on ASNs, even if splitting was disabled

src/documents/barcodes.py
src/documents/tests/test_barcodes.py

index 4bfb9b791f152752646ccc72b005d16e533a6bc8..5bf6226519f789991481c60e0ce224b90acfcf0e 100644 (file)
@@ -88,64 +88,71 @@ class BarcodePlugin(ConsumeTaskPlugin):
         self.detect()
 
         # Update/overwrite an ASN if possible
-        located_asn = self.asn
-        if located_asn is not None:
+        if (
+            settings.CONSUMER_ENABLE_ASN_BARCODE
+            and (located_asn := self.asn) is not None
+        ):
             logger.info(f"Found ASN in barcode: {located_asn}")
             self.metadata.asn = located_asn
 
         # try reading tags from barcodes
-        if settings.CONSUMER_ENABLE_TAG_BARCODE:
-            tags = self.tags
-            if tags is not None and len(tags) > 0:
-                if self.metadata.tag_ids:
-                    self.metadata.tag_ids += tags
-                else:
-                    self.metadata.tag_ids = tags
-                logger.info(f"Found tags in barcode: {tags}")
-
-        separator_pages = self.get_separation_pages()
-        if not separator_pages:
-            return "No pages to split on!"
-
-        # We have pages to split against
-
-        # Note this does NOT use the base_temp_dir, as that will be removed
-        tmp_dir = Path(
-            tempfile.mkdtemp(
-                dir=settings.SCRATCH_DIR,
-                prefix="paperless-barcode-split-",
-            ),
-        ).resolve()
-
-        from documents import tasks
-
-        # Create the split document tasks
-        for new_document in self.separate_pages(separator_pages):
-            copy_file_with_basic_stats(new_document, tmp_dir / new_document.name)
-
-            task = tasks.consume_file.delay(
-                ConsumableDocument(
-                    # Same source, for templates
-                    source=self.input_doc.source,
-                    mailrule_id=self.input_doc.mailrule_id,
-                    # Can't use same folder or the consume might grab it again
-                    original_file=(tmp_dir / new_document.name).resolve(),
+        if (
+            settings.CONSUMER_ENABLE_TAG_BARCODE
+            and (tags := self.tags) is not None
+            and len(tags) > 0
+        ):
+            if self.metadata.tag_ids:
+                self.metadata.tag_ids += tags
+            else:
+                self.metadata.tag_ids = tags
+            logger.info(f"Found tags in barcode: {tags}")
+
+        # Lastly attempt to split documents
+        if settings.CONSUMER_ENABLE_BARCODES:
+
+            separator_pages = self.get_separation_pages()
+            if not separator_pages:
+                return "No pages to split on!"
+
+            # We have pages to split against
+
+            # Note this does NOT use the base_temp_dir, as that will be removed
+            tmp_dir = Path(
+                tempfile.mkdtemp(
+                    dir=settings.SCRATCH_DIR,
+                    prefix="paperless-barcode-split-",
                 ),
-                # All the same metadata
-                self.metadata,
-            )
-            logger.info(f"Created new task {task.id} for {new_document.name}")
+            ).resolve()
+
+            from documents import tasks
+
+            # Create the split document tasks
+            for new_document in self.separate_pages(separator_pages):
+                copy_file_with_basic_stats(new_document, tmp_dir / new_document.name)
+
+                task = tasks.consume_file.delay(
+                    ConsumableDocument(
+                        # Same source, for templates
+                        source=self.input_doc.source,
+                        mailrule_id=self.input_doc.mailrule_id,
+                        # Can't use same folder or the consume might grab it again
+                        original_file=(tmp_dir / new_document.name).resolve(),
+                    ),
+                    # All the same metadata
+                    self.metadata,
+                )
+                logger.info(f"Created new task {task.id} for {new_document.name}")
 
-        # This file is now two or more files
-        self.input_doc.original_file.unlink()
+            # This file is now two or more files
+            self.input_doc.original_file.unlink()
 
-        msg = "Barcode splitting complete!"
+            msg = "Barcode splitting complete!"
 
-        # Update the progress to complete
-        self.status_mgr.send_progress(ProgressStatusOptions.SUCCESS, msg, 100, 100)
+            # Update the progress to complete
+            self.status_mgr.send_progress(ProgressStatusOptions.SUCCESS, msg, 100, 100)
 
-        # Request the consume task stops
-        raise StopConsumeTaskError(msg)
+            # Request the consume task stops
+            raise StopConsumeTaskError(msg)
 
     def cleanup(self) -> None:
         self.temp_dir.cleanup()
index 3dd6d62ff366486cc79ff36a7e0f4ad46c8ce06d..732c2e1675cd5e3e466afe2c03f892da18e3f912 100644 (file)
@@ -402,6 +402,7 @@ class TestBarcode(
 
             self.assertEqual(len(documents), 2)
 
+    @override_settings(CONSUMER_ENABLE_BARCODES=True)
     def test_separate_pages_no_list(self):
         """
         GIVEN: