Skip to content

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

added ASN to filename format #519

jonaswinkler1d002149dc9b
diff --git a/docs/advanced_usage.rst b/docs/advanced_usage.rst
index 8f6b91b4c..c64df4317 100644
--- a/docs/advanced_usage.rst
+++ b/docs/advanced_usage.rst
@@ -217,6 +217,7 @@ will create a directory structure as follows:
Paperless provides the following placeholders withing filenames:
+* ``{asn}``: The archive serial number of the document, or "none".
* ``{correspondent}``: The name of the correspondent, or "none".
* ``{document_type}``: The name of the document type, or "none".
* ``{tag_list}``: A comma separated list of all tags assigned to the document.
diff --git a/src/documents/file_handling.py b/src/documents/file_handling.py
index 535aa3d2c..8c0e2148b 100644
--- a/src/documents/file_handling.py
+++ b/src/documents/file_handling.py
@@ -131,6 +131,11 @@ def generate_filename(doc, counter=0, append_gpg=True, archive_filename=False):
else:
document_type = "none"
+ if doc.archive_serial_number:
+ asn = str(doc.archive_serial_number)
+ else:
+ asn = "none"
+
path = settings.PAPERLESS_FILENAME_FORMAT.format(
title=pathvalidate.sanitize_filename(
doc.title, replacement_text="-"),
@@ -144,6 +149,7 @@ def generate_filename(doc, counter=0, append_gpg=True, archive_filename=False):
added_year=doc.added.year 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",
+ asn=asn,
tags=tags,
tag_list=tag_list
).strip()
diff --git a/src/documents/tests/test_file_handling.py b/src/documents/tests/test_file_handling.py
index fd74aad7d..927c865c6 100644
--- a/src/documents/tests/test_file_handling.py
+++ b/src/documents/tests/test_file_handling.py
@@ -201,6 +201,13 @@ class TestFileHandling(DirectoriesMixin, TestCase):
self.assertEqual(generate_filename(d), "my_doc_type - the_doc.pdf")
+ @override_settings(PAPERLESS_FILENAME_FORMAT="{asn} - {title}")
+ def test_asn(self):
+ d1 = Document.objects.create(title="the_doc", mime_type="application/pdf", archive_serial_number=652, checksum="A")
+ d2 = Document.objects.create(title="the_doc", mime_type="application/pdf", archive_serial_number=None, checksum="B")
+ self.assertEqual(generate_filename(d1), "652 - the_doc.pdf")
+ self.assertEqual(generate_filename(d2), "none - the_doc.pdf")
+
@override_settings(PAPERLESS_FILENAME_FORMAT="{tags[type]}")
def test_tags_with_underscore(self):
document = Document()