Unified PAPERLESS_DIRECTORY_FORMAT and PAPERLESS_FILENAME_FORMAT into

one configuration setting
This commit is contained in:
Wolf-Bastian Poettner 2020-02-01 20:09:36 +00:00
parent 86a51c6fa5
commit 6a671ebd22
4 changed files with 13 additions and 36 deletions

View File

@ -54,7 +54,7 @@ PAPERLESS_CONSUME_MAIL_PASS=""
# ignored.
PAPERLESS_EMAIL_SECRET=""
# Specify a filename format for an (optional) subdirectory and the document itself
# Specify a filename format for the document (directories are supported)
# Use the following placeholders:
# * {correspondent}
# * {title}
@ -63,7 +63,6 @@ PAPERLESS_EMAIL_SECRET=""
# * {tags[FILTER]}
# Uniqueness of filenames is ensured, as an incrementing counter is attached
# to each filename.
#PAPERLESS_DIRECTORY_FORMAT=""
#PAPERLESS_FILENAME_FORMAT=""
###############################################################################

View File

@ -299,31 +299,17 @@ class Document(models.Model):
return mydictionary
def generate_source_filename(self):
# Create directory name based on configured format
if settings.PAPERLESS_DIRECTORY_FORMAT is not None:
directory = settings.PAPERLESS_DIRECTORY_FORMAT.format(
correspondent=slugify(self.correspondent),
title=slugify(self.title),
created=slugify(self.created),
added=slugify(self.added),
tags=defaultdict(str,
self.many_to_dictionary(self.tags)))
else:
directory = ""
# Create filename based on configured format
if settings.PAPERLESS_FILENAME_FORMAT is not None:
filename = settings.PAPERLESS_FILENAME_FORMAT.format(
correspondent=slugify(self.correspondent),
title=slugify(self.title),
created=slugify(self.created),
added=slugify(self.added),
tags=defaultdict(str,
self.many_to_dictionary(self.tags)))
path = settings.PAPERLESS_FILENAME_FORMAT.format(
correspondent=slugify(self.correspondent),
title=slugify(self.title),
created=slugify(self.created),
added=slugify(self.added),
tags=defaultdict(str,
self.many_to_dictionary(self.tags)))
else:
filename = ""
path = os.path.join(directory, filename)
path = ""
# Always append the primary key to guarantee uniqueness of filename
if len(path) > 0:

View File

@ -14,7 +14,6 @@ from django.conf import settings
class TestDate(TestCase):
@override_settings(PAPERLESS_DIRECTORY_FORMAT="")
@override_settings(PAPERLESS_FILENAME_FORMAT="")
def test_source_filename(self):
document = Document()
@ -27,7 +26,6 @@ class TestDate(TestCase):
document.filename = "test.pdf"
self.assertEqual(document.source_filename, "test.pdf")
@override_settings(PAPERLESS_DIRECTORY_FORMAT="")
@override_settings(PAPERLESS_FILENAME_FORMAT="")
def test_generate_source_filename(self):
document = Document()
@ -43,8 +41,7 @@ class TestDate(TestCase):
@override_settings(MEDIA_ROOT="/tmp/paperless-tests-{}".
format(str(uuid4())[:8]))
@override_settings(PAPERLESS_DIRECTORY_FORMAT="{correspondent}")
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}")
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{correspondent}")
def test_file_renaming(self):
document = Document()
document.file_type = "pdf"
@ -89,8 +86,7 @@ class TestDate(TestCase):
@override_settings(MEDIA_ROOT="/tmp/paperless-tests-{}".
format(str(uuid4())[:8]))
@override_settings(PAPERLESS_DIRECTORY_FORMAT="{correspondent}")
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}")
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{correspondent}")
def test_document_delete(self):
document = Document()
document.file_type = "pdf"
@ -113,8 +109,7 @@ class TestDate(TestCase):
@override_settings(MEDIA_ROOT="/tmp/paperless-tests-{}".
format(str(uuid4())[:8]))
@override_settings(PAPERLESS_DIRECTORY_FORMAT="{correspondent}")
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}")
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{correspondent}")
def test_directory_not_empty(self):
document = Document()
document.file_type = "pdf"
@ -148,8 +143,7 @@ class TestDate(TestCase):
@override_settings(MEDIA_ROOT="/tmp/paperless-tests-{}".
format(str(uuid4())[:8]))
@override_settings(PAPERLESS_DIRECTORY_FORMAT="{correspondent}/{correspondent}")
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}")
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{correspondent}/{correspondent}")
def test_nested_directory_cleanup(self):
document = Document()
document.file_type = "pdf"
@ -178,7 +172,6 @@ class TestDate(TestCase):
@override_settings(MEDIA_ROOT="/tmp/paperless-tests-{}".
format(str(uuid4())[:8]))
@override_settings(PAPERLESS_DIRECTORY_FORMAT=None)
@override_settings(PAPERLESS_FILENAME_FORMAT=None)
def test_format_none(self):
document = Document()

View File

@ -336,5 +336,4 @@ PAPERLESS_RECENT_CORRESPONDENT_YEARS = int(os.getenv(
"PAPERLESS_RECENT_CORRESPONDENT_YEARS", 0))
# Specify the filename format for out files
PAPERLESS_DIRECTORY_FORMAT = os.getenv("PAPERLESS_DIRECTORY_FORMAT")
PAPERLESS_FILENAME_FORMAT = os.getenv("PAPERLESS_FILENAME_FORMAT")