From e89ef5de25ebea1565ceaed2ecbd33df29e09e78 Mon Sep 17 00:00:00 2001 From: "florian on nixos (Florian Brandes)" Date: Sat, 26 Mar 2022 08:41:50 +0100 Subject: [PATCH] renaming of documents to align with front end Signed-off-by: florian on nixos (Florian Brandes) --- src/documents/tasks.py | 35 +++++++++++++++++++++++++------ src/documents/tests/test_tasks.py | 17 ++++++++++++--- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/documents/tasks.py b/src/documents/tasks.py index 23c81e051..2b5ce8bdc 100644 --- a/src/documents/tasks.py +++ b/src/documents/tasks.py @@ -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 diff --git a/src/documents/tests/test_tasks.py b/src/documents/tests/test_tasks.py index ab692f6b3..b5eabdd1a 100644 --- a/src/documents/tests/test_tasks.py +++ b/src/documents/tests/test_tasks.py @@ -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))