]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Adds more options for the filename formatting
authorTrenton Holmes <holmes.trenton@gmail.com>
Tue, 1 Nov 2022 01:49:37 +0000 (18:49 -0700)
committerTrenton H <holmes.trenton@gmail.com>
Tue, 1 Nov 2022 15:44:30 +0000 (08:44 -0700)
docs/advanced_usage.rst
src/documents/file_handling.py
src/documents/tests/test_file_handling.py

index 844a73817f447df04a30aaa4a1a2b78d11728910..0dd7e9601fae0ca3d9c344ea574a1d0d0655cac5 100644 (file)
@@ -258,12 +258,18 @@ Paperless provides the following placeholders within filenames:
 * ``{tag_list}``: A comma separated list of all tags assigned to the document.
 * ``{title}``: The title of the document.
 * ``{created}``: The full date (ISO format) the document was created.
-* ``{created_year}``: Year created only.
+* ``{created_year}``: Year created only, formatted as the year with century.
+* ``{created_year_short}``: Year created only, formatted as the year without century, zero padded.
 * ``{created_month}``: Month created only (number 01-12).
+* ``{created_month_name}``: Month created name, as per locale
+* ``{created_month_name_short}``: Month created abbreviated name, as per locale
 * ``{created_day}``: Day created only (number 01-31).
 * ``{added}``: The full date (ISO format) the document was added to paperless.
 * ``{added_year}``: Year added only.
+* ``{added_year_short}``: Year added only, formatted as the year without century, zero padded.
 * ``{added_month}``: Month added only (number 01-12).
+* ``{added_month_name}``: Month added name, as per locale
+* ``{added_month_name_short}``: Month added abbreviated name, as per locale
 * ``{added_day}``: Day added only (number 01-31).
 
 
index e3f13c077c3ed11d93070394616234a5fbb9e440..2036cf44306152d0ebd940d55689397a43e7123d 100644 (file)
@@ -181,13 +181,19 @@ def generate_filename(doc, counter=0, append_gpg=True, archive_filename=False):
                 correspondent=correspondent,
                 document_type=document_type,
                 created=datetime.date.isoformat(local_created),
-                created_year=local_created.year,
-                created_month=f"{local_created.month:02}",
-                created_day=f"{local_created.day:02}",
+                created_year=local_created.strftime("%Y"),
+                created_year_short=local_created.strftime("%y"),
+                created_month=local_created.strftime("%m"),
+                created_month_name=local_created.strftime("%B"),
+                created_month_name_short=local_created.strftime("%b"),
+                created_day=local_created.strftime("%d"),
                 added=datetime.date.isoformat(local_added),
-                added_year=local_added.year,
-                added_month=f"{local_added.month:02}",
-                added_day=f"{local_added.day:02}",
+                added_year=local_added.strftime("%Y"),
+                added_year_short=local_added.strftime("%y"),
+                added_month=local_added.strftime("%m"),
+                added_month_name=local_added.strftime("%B"),
+                added_month_name_short=local_added.strftime("%b"),
+                added_day=local_added.strftime("%d"),
                 asn=asn,
                 tags=tags,
                 tag_list=tag_list,
index 400c1741331e38b9619dd7525361a4138a8bd348..8d726b339dbe8609914c875b45cd12d99f7fbd19 100644 (file)
@@ -1036,6 +1036,34 @@ class TestFilenameGeneration(TestCase):
         self.assertEqual(generate_filename(doc_a), "0000002.pdf")
         self.assertEqual(generate_filename(doc_b), "SomeImportantNone/2020-07-25.pdf")
 
+    @override_settings(
+        FILENAME_FORMAT="{created_year_short}/{created_month_name_short}/{created_month_name}/{title}",
+    )
+    def test_short_names_created(self):
+        doc = Document.objects.create(
+            title="The Title",
+            created=timezone.make_aware(
+                datetime.datetime(1989, 12, 21, 7, 36, 51, 153),
+            ),
+            mime_type="application/pdf",
+            pk=2,
+            checksum="2",
+        )
+        self.assertEqual(generate_filename(doc), "89/Dec/December/The Title.pdf")
+
+    @override_settings(
+        FILENAME_FORMAT="{added_year_short}/{added_month_name}/{added_month_name_short}/{title}",
+    )
+    def test_short_names_added(self):
+        doc = Document.objects.create(
+            title="The Title",
+            added=timezone.make_aware(datetime.datetime(1984, 8, 21, 7, 36, 51, 153)),
+            mime_type="application/pdf",
+            pk=2,
+            checksum="2",
+        )
+        self.assertEqual(generate_filename(doc), "84/August/Aug/The Title.pdf")
+
 
 def run():
     doc = Document.objects.create(