]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
renaming of documents to align with front end
authorflorian on nixos (Florian Brandes) <florian.brandes@posteo.de>
Sat, 26 Mar 2022 07:41:50 +0000 (08:41 +0100)
committerFlorian Brandes <florian.brandes@posteo.de>
Thu, 7 Apr 2022 09:14:29 +0000 (11:14 +0200)
Signed-off-by: florian on nixos (Florian Brandes) <florian.brandes@posteo.de>
src/documents/tasks.py
src/documents/tests/test_tasks.py

index 23c81e0518fa7c702cc12e98080d618dc543cb9c..2b5ce8bdcadd929410d66374a766891ec237e85b 100644 (file)
@@ -21,6 +21,8 @@ from pdf2image import convert_from_path
 from pikepdf import Pdf
 from pyzbar import pyzbar
 from whoosh.writing import AsyncWriter
+from asgiref.sync import async_to_sync
+from channels.layers import get_channel_layer
 
 # barcode decoder
 
@@ -121,8 +123,6 @@ def separate_pages(filepath: str, pages_to_split_on: list) -> list:
     pdf = Pdf.open(filepath)
     document_paths = []
     logger.debug(f"Temp dir is {str(tempdir)}")
-    # TODO: Get the directory of the file and save the other files there
-    # TODO: Return list of new paths of the new files
     if len(pages_to_split_on) <= 0:
         logger.warning("No pages to split on!")
     else:
@@ -158,12 +158,19 @@ def separate_pages(filepath: str, pages_to_split_on: list) -> list:
     return document_paths
 
 
-def save_to_dir(filepath, target_dir=settings.CONSUMPTION_DIR):
+def save_to_dir(filepath, newname=None, target_dir=settings.CONSUMPTION_DIR):
     """
     Copies filepath to target_dir.
+    Optionally rename the file.
     """
+    logger.debug(f"filepath: {str(filepath)}")
+    logger.debug(f"newname: {str(newname)}")
+    logger.debug(f"target_dir: {str(target_dir)}")
     if os.path.isfile(filepath) and os.path.isdir(target_dir):
-        shutil.copy(filepath, target_dir)
+        dst = shutil.copy(filepath, target_dir)
+        if newname:
+            dst_new = os.path.join(target_dir, newname)
+            os.rename(dst, dst_new)
     else:
         logger.warning(f"{str(filepath)} or {str(target_dir)} don't exist.")
 
@@ -189,13 +196,29 @@ def consume_file(
     if document_list == []:
         pass
     else:
-        for document in document_list:
+        for n, document in enumerate(document_list):
             # save to consumption dir
-            save_to_dir(document)
+            # rename it to the original filename  with number prefix
+            newname = f"{str(n)}_" + override_filename
+            save_to_dir(document, newname=newname)
         # if we got here, the document was successfully split
         # and can safely be deleted
         logger.debug("Deleting file {}".format(path))
         os.unlink(path)
+        # notify the sender, otherwise the progress bar
+        # in the UI stays stuck
+        payload = {
+            "filename": override_filename,
+            "task_id": task_id,
+            "current_progress": 100,
+            "max_progress": 100,
+            "status": "SUCCESS",
+            "message": "finished"
+        }
+        async_to_sync(get_channel_layer().group_send)(
+            "status_updates",
+            {"type": "status_update", "data": payload},
+        )
         return "File successfully split"
 
     # continue with consumption if no barcode was found
index ab692f6b364b31318a7664f1c440785d6078772e..b5eabdd1a3045e1783232a41790af342d9448bc9 100644 (file)
@@ -144,7 +144,7 @@ class TestTasks(DirectoriesMixin, TestCase):
             "patch-code-t.pdf",
         )
         tempdir = tempfile.mkdtemp(prefix="paperless-", dir=settings.SCRATCH_DIR)
-        tasks.save_to_dir(test_file, tempdir)
+        tasks.save_to_dir(test_file, target_dir=tempdir)
         target_file = os.path.join(tempdir, "patch-code-t.pdf")
         self.assertTrue(os.path.isfile(target_file))
 
@@ -159,7 +159,7 @@ class TestTasks(DirectoriesMixin, TestCase):
             self.skipTest("non-existing dir exists")
         else:
             with self.assertLogs("paperless.tasks", level="WARNING") as cm:
-                tasks.save_to_dir(test_file, nonexistingdir)
+                tasks.save_to_dir(test_file, target_dir=nonexistingdir)
             self.assertEqual(
                 cm.output,
                 [
@@ -167,6 +167,17 @@ class TestTasks(DirectoriesMixin, TestCase):
                 ],
             )
 
+    def test_save_to_dir3(self):
+        test_file = os.path.join(
+            os.path.dirname(__file__),
+            "samples",
+            "patch-code-t.pdf",
+        )
+        tempdir = tempfile.mkdtemp(prefix="paperless-", dir=settings.SCRATCH_DIR)
+        tasks.save_to_dir(test_file, newname="newname.pdf", target_dir=tempdir)
+        target_file = os.path.join(tempdir, "newname.pdf")
+        self.assertTrue(os.path.isfile(target_file))
+
     def test_barcode_splitter(self):
         test_file = os.path.join(
             os.path.dirname(__file__),
@@ -179,7 +190,7 @@ class TestTasks(DirectoriesMixin, TestCase):
         document_list = tasks.separate_pages(test_file, separators)
         self.assertTrue(document_list != [])
         for document in document_list:
-            tasks.save_to_dir(document, tempdir)
+            tasks.save_to_dir(document, target_dir=tempdir)
         target_file1 = os.path.join(tempdir, "patch-code-t-middle_document_0.pdf")
         target_file2 = os.path.join(tempdir, "patch-code-t-middle_document_1.pdf")
         self.assertTrue(os.path.isfile(target_file1))