From 251fc582e93f30dae7dd3fc5fe83b93c5e863fa6 Mon Sep 17 00:00:00 2001 From: jonaswinkler Date: Mon, 14 Dec 2020 02:19:53 +0100 Subject: [PATCH] fixes #130 --- src/documents/file_handling.py | 6 +++++- src/documents/tests/test_file_handling.py | 22 +++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/documents/file_handling.py b/src/documents/file_handling.py index c5efc33e4..d28f9ffbb 100644 --- a/src/documents/file_handling.py +++ b/src/documents/file_handling.py @@ -121,7 +121,11 @@ def generate_filename(doc, counter=0): added_month=doc.added.month if doc.added else "none", added_day=doc.added.day if doc.added else "none", tags=tags, - ) + tag_list=",".join([tag.name for tag in doc.tags.all()]) + ).strip() + + path = path.strip(os.sep) + except (ValueError, KeyError, IndexError): logging.getLogger(__name__).warning( f"Invalid PAPERLESS_FILENAME_FORMAT: " diff --git a/src/documents/tests/test_file_handling.py b/src/documents/tests/test_file_handling.py index 719b0078a..2b1022453 100644 --- a/src/documents/tests/test_file_handling.py +++ b/src/documents/tests/test_file_handling.py @@ -13,7 +13,7 @@ from django.test import TestCase, override_settings from .utils import DirectoriesMixin from ..file_handling import generate_filename, create_source_path_directory, delete_empty_directories, \ generate_unique_filename -from ..models import Document, Correspondent +from ..models import Document, Correspondent, Tag class TestFileHandling(DirectoriesMixin, TestCase): @@ -267,6 +267,26 @@ class TestFileHandling(DirectoriesMixin, TestCase): self.assertEqual(generate_filename(document), "none.pdf") + @override_settings(PAPERLESS_FILENAME_FORMAT="{title} {tag_list}") + def test_tag_list(self): + doc = Document.objects.create(title="doc1", mime_type="application/pdf") + doc.tags.create(name="tag2") + doc.tags.create(name="tag1") + + self.assertEqual(generate_filename(doc), "doc1 tag1,tag2.pdf") + + doc = Document.objects.create(title="doc2", checksum="B", mime_type="application/pdf") + + self.assertEqual(generate_filename(doc), "doc2.pdf") + + @override_settings(PAPERLESS_FILENAME_FORMAT="//etc/something/{title}") + def test_filename_relative(self): + doc = Document.objects.create(title="doc1", mime_type="application/pdf") + doc.filename = generate_filename(doc) + doc.save() + + self.assertEqual(doc.source_path, os.path.join(settings.ORIGINALS_DIR, "etc", "something", "doc1.pdf")) + @override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{correspondent}/{correspondent}") def test_nested_directory_cleanup(self): document = Document()