mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-04-02 13:45:10 -05:00
renaming of documents to align with front end
Signed-off-by: florian on nixos (Florian Brandes) <florian.brandes@posteo.de>
This commit is contained in:
parent
06cac44d02
commit
e89ef5de25
@ -21,6 +21,8 @@ from pdf2image import convert_from_path
|
|||||||
from pikepdf import Pdf
|
from pikepdf import Pdf
|
||||||
from pyzbar import pyzbar
|
from pyzbar import pyzbar
|
||||||
from whoosh.writing import AsyncWriter
|
from whoosh.writing import AsyncWriter
|
||||||
|
from asgiref.sync import async_to_sync
|
||||||
|
from channels.layers import get_channel_layer
|
||||||
|
|
||||||
# barcode decoder
|
# barcode decoder
|
||||||
|
|
||||||
@ -121,8 +123,6 @@ def separate_pages(filepath: str, pages_to_split_on: list) -> list:
|
|||||||
pdf = Pdf.open(filepath)
|
pdf = Pdf.open(filepath)
|
||||||
document_paths = []
|
document_paths = []
|
||||||
logger.debug(f"Temp dir is {str(tempdir)}")
|
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:
|
if len(pages_to_split_on) <= 0:
|
||||||
logger.warning("No pages to split on!")
|
logger.warning("No pages to split on!")
|
||||||
else:
|
else:
|
||||||
@ -158,12 +158,19 @@ def separate_pages(filepath: str, pages_to_split_on: list) -> list:
|
|||||||
return document_paths
|
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.
|
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):
|
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:
|
else:
|
||||||
logger.warning(f"{str(filepath)} or {str(target_dir)} don't exist.")
|
logger.warning(f"{str(filepath)} or {str(target_dir)} don't exist.")
|
||||||
|
|
||||||
@ -189,13 +196,29 @@ def consume_file(
|
|||||||
if document_list == []:
|
if document_list == []:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
for document in document_list:
|
for n, document in enumerate(document_list):
|
||||||
# save to consumption dir
|
# 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
|
# if we got here, the document was successfully split
|
||||||
# and can safely be deleted
|
# and can safely be deleted
|
||||||
logger.debug("Deleting file {}".format(path))
|
logger.debug("Deleting file {}".format(path))
|
||||||
os.unlink(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"
|
return "File successfully split"
|
||||||
|
|
||||||
# continue with consumption if no barcode was found
|
# continue with consumption if no barcode was found
|
||||||
|
@ -144,7 +144,7 @@ class TestTasks(DirectoriesMixin, TestCase):
|
|||||||
"patch-code-t.pdf",
|
"patch-code-t.pdf",
|
||||||
)
|
)
|
||||||
tempdir = tempfile.mkdtemp(prefix="paperless-", dir=settings.SCRATCH_DIR)
|
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")
|
target_file = os.path.join(tempdir, "patch-code-t.pdf")
|
||||||
self.assertTrue(os.path.isfile(target_file))
|
self.assertTrue(os.path.isfile(target_file))
|
||||||
|
|
||||||
@ -159,7 +159,7 @@ class TestTasks(DirectoriesMixin, TestCase):
|
|||||||
self.skipTest("non-existing dir exists")
|
self.skipTest("non-existing dir exists")
|
||||||
else:
|
else:
|
||||||
with self.assertLogs("paperless.tasks", level="WARNING") as cm:
|
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(
|
self.assertEqual(
|
||||||
cm.output,
|
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):
|
def test_barcode_splitter(self):
|
||||||
test_file = os.path.join(
|
test_file = os.path.join(
|
||||||
os.path.dirname(__file__),
|
os.path.dirname(__file__),
|
||||||
@ -179,7 +190,7 @@ class TestTasks(DirectoriesMixin, TestCase):
|
|||||||
document_list = tasks.separate_pages(test_file, separators)
|
document_list = tasks.separate_pages(test_file, separators)
|
||||||
self.assertTrue(document_list != [])
|
self.assertTrue(document_list != [])
|
||||||
for document in 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_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")
|
target_file2 = os.path.join(tempdir, "patch-code-t-middle_document_1.pdf")
|
||||||
self.assertTrue(os.path.isfile(target_file1))
|
self.assertTrue(os.path.isfile(target_file1))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user