Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

Commit

Permalink
add exception handler for invalid filename formats.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Winkler committed Nov 13, 2020
1 parent 4862ce7 commit 82168e1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 21 deletions.
40 changes: 22 additions & 18 deletions src/documents/file_handling.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import os
from collections import defaultdict

Expand Down Expand Up @@ -66,24 +67,27 @@ def many_to_dictionary(field):

def generate_filename(document):
# Create filename based on configured format
if settings.PAPERLESS_FILENAME_FORMAT is not None:
tags = defaultdict(lambda: slugify(None),
many_to_dictionary(document.tags))
path = settings.PAPERLESS_FILENAME_FORMAT.format(
correspondent=slugify(document.correspondent),
title=slugify(document.title),
created=slugify(document.created),
created_year=document.created.year if document.created else "none",
created_month=document.created.month if document.created else "none",
created_day=document.created.day if document.created else "none",
added=slugify(document.added),
added_year=document.added.year if document.added else "none",
added_month=document.added.month if document.added else "none",
added_day=document.added.day if document.added else "none",
tags=tags,
)
else:
path = ""
path = ""

try:
if settings.PAPERLESS_FILENAME_FORMAT is not None:
tags = defaultdict(lambda: slugify(None),
many_to_dictionary(document.tags))
path = settings.PAPERLESS_FILENAME_FORMAT.format(
correspondent=slugify(document.correspondent),
title=slugify(document.title),
created=slugify(document.created),
created_year=document.created.year if document.created else "none",
created_month=document.created.month if document.created else "none",
created_day=document.created.day if document.created else "none",
added=slugify(document.added),
added_year=document.added.year if document.added else "none",
added_month=document.added.month if document.added else "none",
added_day=document.added.day if document.added else "none",
tags=tags,
)
except (ValueError, KeyError, IndexError) as e:
logging.getLogger(__name__).warning("Invalid PAPERLESS_FILENAME_FORMAT: {}, falling back to default,".format(settings.PAPERLESS_FILENAME_FORMAT))

# Always append the primary key to guarantee uniqueness of filename
if len(path) > 0:
Expand Down
6 changes: 3 additions & 3 deletions src/documents/signals/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.dispatch import Signal

document_consumption_started = Signal(providing_args=["filename"])
document_consumption_finished = Signal(providing_args=["document"])
document_consumer_declaration = Signal(providing_args=[])
document_consumption_started = Signal()
document_consumption_finished = Signal()
document_consumer_declaration = Signal()
18 changes: 18 additions & 0 deletions src/documents/tests/test_file_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,21 @@ def test_try_delete_empty_directories(self):
os.path.join(tmp, "notempty", "file")), True)
self.assertEqual(os.path.isdir(
os.path.join(tmp, "notempty", "empty")), False)

@override_settings(PAPERLESS_FILENAME_FORMAT="{created/[title]")
def test_invalid_format(self):
document = Document()
document.pk = 1
document.file_type = "pdf"
document.storage_type = Document.STORAGE_TYPE_UNENCRYPTED

self.assertEqual(generate_filename(document), "0000001.pdf")

@override_settings(PAPERLESS_FILENAME_FORMAT="{created__year}")
def test_invalid_format_key(self):
document = Document()
document.pk = 1
document.file_type = "pdf"
document.storage_type = Document.STORAGE_TYPE_UNENCRYPTED

self.assertEqual(generate_filename(document), "0000001.pdf")

0 comments on commit 82168e1

Please sign in to comment.