From 8cc03363381231c2f6d8f45d11da1f78f461b483 Mon Sep 17 00:00:00 2001 From: jonaswinkler Date: Mon, 14 Dec 2020 03:01:50 +0100 Subject: [PATCH] prevent usage of {tags} directly. --- src/documents/file_handling.py | 10 ++++++++-- src/documents/tests/test_file_handling.py | 9 +++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/documents/file_handling.py b/src/documents/file_handling.py index 861eb2a37..c49493991 100644 --- a/src/documents/file_handling.py +++ b/src/documents/file_handling.py @@ -8,6 +8,12 @@ from django.conf import settings from django.template.defaultfilters import slugify +class defaultdictNoStr(defaultdict): + + def __str__(self): + raise ValueError("Don't use {tags} directly.") + + def create_source_path_directory(source_path): os.makedirs(os.path.dirname(source_path), exist_ok=True) @@ -90,8 +96,8 @@ def generate_filename(doc, counter=0): try: if settings.PAPERLESS_FILENAME_FORMAT is not None: - tags = defaultdict(lambda: slugify(None), - many_to_dictionary(doc.tags)) + tags = defaultdictNoStr(lambda: slugify(None), + many_to_dictionary(doc.tags)) if doc.correspondent: correspondent = pathvalidate.sanitize_filename( diff --git a/src/documents/tests/test_file_handling.py b/src/documents/tests/test_file_handling.py index 2f7f6efcf..dec89c45b 100644 --- a/src/documents/tests/test_file_handling.py +++ b/src/documents/tests/test_file_handling.py @@ -267,6 +267,15 @@ class TestFileHandling(DirectoriesMixin, TestCase): self.assertEqual(generate_filename(document), "none.pdf") + @override_settings(PAPERLESS_FILENAME_FORMAT="{tags}") + def test_tags_without_args(self): + document = Document() + document.mime_type = "application/pdf" + document.storage_type = Document.STORAGE_TYPE_UNENCRYPTED + document.save() + + self.assertEqual(generate_filename(document), f"{document.pk:07}.pdf") + @override_settings(PAPERLESS_FILENAME_FORMAT="{title} {tag_list}") def test_tag_list(self): doc = Document.objects.create(title="doc1", mime_type="application/pdf")