]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Add 'doc_pk' to PAPERLESS_FILENAME_FORMAT handling (#3861)
authormechanarchy <1166756+mechanarchy@users.noreply.github.com>
Sun, 30 Jul 2023 15:30:50 +0000 (01:30 +1000)
committerGitHub <noreply@github.com>
Sun, 30 Jul 2023 15:30:50 +0000 (08:30 -0700)
* Add 'doc_pk' to PAPERLESS_FILENAME_FORMAT handling

* Add test for 'doc_pk' filename formatter

docs/advanced_usage.md
src/documents/file_handling.py
src/documents/tests/test_file_handling.py

index 199931bb5924548b0478a44aaf5280c862d0ad76..89530db7f9037a213ac670e59473aeee213ca37b 100644 (file)
@@ -311,6 +311,7 @@ Paperless provides the following placeholders within filenames:
 - `{added_day}`: Day added only (number 01-31).
 - `{owner_username}`: Username of document owner, if any, or "none"
 - `{original_name}`: Document original filename, minus the extension, if any, or "none"
+- `{doc_pk}`: The paperless identifier (primary key) for the document.
 
 Paperless will try to conserve the information from your database as
 much as possible. However, some characters that you can use in document
index e382d496562901d9b597a17727d20bfb370716c7..d9601ccd92f68410eeef0928bc94b851f3c921c1 100644 (file)
@@ -218,6 +218,7 @@ def generate_filename(
                 tag_list=tag_list,
                 owner_username=owner_username_str,
                 original_name=original_name,
+                doc_pk=f"{doc.pk:07}",
             ).strip()
 
             if settings.FILENAME_FORMAT_REMOVE_NONE:
index b021f8aaf08db20cfbf2691dd4ae31f93eb4c0c1..9e27b9aad3c0cf2d849e3bf6eb02fa6fa38c83bd 100644 (file)
@@ -446,6 +446,19 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
         self.assertIsNotDir(os.path.join(settings.ORIGINALS_DIR, "none"))
         self.assertIsDir(settings.ORIGINALS_DIR)
 
+    @override_settings(FILENAME_FORMAT="{doc_pk}")
+    def test_format_doc_pk(self):
+        document = Document()
+        document.pk = 1
+        document.mime_type = "application/pdf"
+        document.storage_type = Document.STORAGE_TYPE_UNENCRYPTED
+
+        self.assertEqual(generate_filename(document), "0000001.pdf")
+
+        document.pk = 13579
+
+        self.assertEqual(generate_filename(document), "0013579.pdf")
+
     @override_settings(FILENAME_FORMAT=None)
     def test_format_none(self):
         document = Document()