Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

changes to filename generation, partially addresses #90

jonaswinklera079c310b4b1
diff --git a/src/documents/file_handling.py b/src/documents/file_handling.py
index 85ee37d4d..78d0d7efc 100644
--- a/src/documents/file_handling.py
+++ b/src/documents/file_handling.py
@@ -1,11 +1,14 @@
+import datetime
import logging
import os
from collections import defaultdict
+import pathvalidate
from django.conf import settings
from django.template.defaultfilters import slugify
+
def create_source_path_directory(source_path):
os.makedirs(os.path.dirname(source_path), exist_ok=True)
@@ -75,14 +78,31 @@ def generate_filename(doc):
if settings.PAPERLESS_FILENAME_FORMAT is not None:
tags = defaultdict(lambda: slugify(None),
many_to_dictionary(doc.tags))
+
+ if doc.correspondent:
+ correspondent = pathvalidate.sanitize_filename(
+ doc.correspondent.name, replacement_text="-"
+ )
+ else:
+ correspondent = "none"
+
+ if doc.document_type:
+ document_type = pathvalidate.sanitize_filename(
+ doc.document_type.name, replacement_text="-"
+ )
+ else:
+ document_type = "none"
+
path = settings.PAPERLESS_FILENAME_FORMAT.format(
- correspondent=slugify(doc.correspondent),
- title=slugify(doc.title),
- created=slugify(doc.created),
+ title=pathvalidate.sanitize_filename(
+ doc.title, replacement_text="-"),
+ correspondent=correspondent,
+ document_type=document_type,
+ created=datetime.date.isoformat(doc.created),
created_year=doc.created.year if doc.created else "none",
created_month=doc.created.month if doc.created else "none",
created_day=doc.created.day if doc.created else "none",
- added=slugify(doc.added),
+ added=datetime.date.isoformat(doc.added),
added_year=doc.added.year if doc.added else "none",
added_month=doc.added.month if doc.added else "none",
added_day=doc.added.day if doc.added else "none",
diff --git a/src/documents/tests/test_consumer.py b/src/documents/tests/test_consumer.py
index 992d450db..f785bc695 100644
--- a/src/documents/tests/test_consumer.py
+++ b/src/documents/tests/test_consumer.py
@@ -598,7 +598,7 @@ class TestConsumer(DirectoriesMixin, TestCase):
self.assertEqual(document.title, "new docs")
self.assertEqual(document.correspondent.name, "Bank")
- self.assertEqual(document.filename, "bank/new-docs-0000001.pdf")
+ self.assertEqual(document.filename, "Bank/new docs-0000001.pdf")
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{title}")
@mock.patch("documents.signals.handlers.generate_filename")
diff --git a/src/documents/tests/test_file_handling.py b/src/documents/tests/test_file_handling.py
index cc4bf8053..4ed93d1d4 100644
--- a/src/documents/tests/test_file_handling.py
+++ b/src/documents/tests/test_file_handling.py
@@ -1,5 +1,5 @@
+import datetime
import os
-import shutil
from pathlib import Path
from unittest import mock
@@ -485,3 +485,23 @@ class TestFileHandlingWithArchive(DirectoriesMixin, TestCase):
self.assertTrue(os.path.isfile(archive))
self.assertTrue(os.path.isfile(doc.source_path))
self.assertTrue(os.path.isfile(doc.archive_path))
+
+class TestFilenameGeneration(TestCase):
+
+ @override_settings(
+ PAPERLESS_FILENAME_FORMAT="{title}"
+ )
+ def test_invalid_characters(self):
+
+ doc = Document.objects.create(title="This. is the title.", mime_type="application/pdf", pk=1, checksum="1")
+ self.assertEqual(generate_filename(doc), "This. is the title-0000001.pdf")
+
+ doc = Document.objects.create(title="my\\invalid/../title:yay", mime_type="application/pdf", pk=2, checksum="2")
+ self.assertEqual(generate_filename(doc), "my-invalid-..-title-yay-0000002.pdf")
+
+ @override_settings(
+ PAPERLESS_FILENAME_FORMAT="{created}"
+ )
+ def test_date(self):
+ doc = Document.objects.create(title="does not matter", created=datetime.datetime(2020,5,21, 7,36,51, 153), mime_type="application/pdf", pk=2, checksum="2")
+ self.assertEqual(generate_filename(doc), "2020-05-21-0000002.pdf")