Skip to content

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

fixes #117

jonaswinklerbad7caa8b9cf
diff --git a/src/documents/file_handling.py b/src/documents/file_handling.py
index d28f9ffbb..861eb2a37 100644
--- a/src/documents/file_handling.py
+++ b/src/documents/file_handling.py
@@ -114,12 +114,12 @@ def generate_filename(doc, counter=0):
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",
+ created_month=f"{doc.created.month:02}" if doc.created else "none", # NOQA: E501
+ created_day=f"{doc.created.day:02}" if doc.created else "none",
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",
+ added_month=f"{doc.added.month:02}" if doc.added else "none",
+ added_day=f"{doc.added.day:02}" if doc.added else "none",
tags=tags,
tag_list=",".join([tag.name for tag in doc.tags.all()])
).strip()
diff --git a/src/documents/tests/test_file_handling.py b/src/documents/tests/test_file_handling.py
index 2b1022453..2f7f6efcf 100644
--- a/src/documents/tests/test_file_handling.py
+++ b/src/documents/tests/test_file_handling.py
@@ -287,6 +287,28 @@ class TestFileHandling(DirectoriesMixin, TestCase):
self.assertEqual(doc.source_path, os.path.join(settings.ORIGINALS_DIR, "etc", "something", "doc1.pdf"))
+ @override_settings(PAPERLESS_FILENAME_FORMAT="{created_year}-{created_month}-{created_day}")
+ def test_created_year_month_day(self):
+ d1 = datetime.datetime(2020, 3, 6, 1, 1, 1)
+ doc1 = Document.objects.create(title="doc1", mime_type="application/pdf", created=d1)
+
+ self.assertEqual(generate_filename(doc1), "2020-03-06.pdf")
+
+ doc1.created = datetime.datetime(2020, 11, 16, 1, 1, 1)
+
+ self.assertEqual(generate_filename(doc1), "2020-11-16.pdf")
+
+ @override_settings(PAPERLESS_FILENAME_FORMAT="{added_year}-{added_month}-{added_day}")
+ def test_added_year_month_day(self):
+ d1 = datetime.datetime(232, 1, 9, 1, 1, 1)
+ doc1 = Document.objects.create(title="doc1", mime_type="application/pdf", added=d1)
+
+ self.assertEqual(generate_filename(doc1), "232-01-09.pdf")
+
+ doc1.added = datetime.datetime(2020, 11, 16, 1, 1, 1)
+
+ self.assertEqual(generate_filename(doc1), "2020-11-16.pdf")
+
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{correspondent}/{correspondent}")
def test_nested_directory_cleanup(self):
document = Document()