Allow filename to use tags[KEY] and tags[INDEX]

This commit is contained in:
Wolf-Bastian Poettner 2020-03-28 21:04:55 +00:00
parent 845313db52
commit 07bb9aa723
3 changed files with 9 additions and 19 deletions

View File

@ -60,7 +60,7 @@ PAPERLESS_EMAIL_SECRET=""
# * {title}
# * {created}
# * {added}
# * {tag[KEY]} If your tags conform to key_value or key-value
# * {tags[KEY]} If your tags conform to key_value or key-value
# * {tags[INDEX]} If your tags are strings, select the tag by index
# Uniqueness of filenames is ensured, as an incrementing counter is attached
# to each filename.

View File

@ -289,7 +289,10 @@ class Document(models.Model):
# entries contain an _ or - which will be used as a delimiter
mydictionary = dict()
for t in field.all():
for index, t in enumerate(field.all()):
# Populate tag names by index
mydictionary[index] = slugify(t.name)
# Find delimiter
delimiter = t.name.find('_')
@ -306,16 +309,6 @@ class Document(models.Model):
return mydictionary
@staticmethod
def many_to_list(field):
# Converts ManyToManyField to list
mylist = list()
for t in field.all():
mylist.append(slugify(t.name))
return mylist
@staticmethod
def fill_list(input_list, length, filler):
while len(input_list) < length:
@ -326,16 +319,13 @@ class Document(models.Model):
def generate_source_filename(self):
# Create filename based on configured format
if settings.PAPERLESS_FILENAME_FORMAT is not None:
tag = defaultdict(lambda: slugify(None),
self.many_to_dictionary(self.tags))
tags = defaultdict(lambda: slugify(None),
enumerate(self.many_to_list(self.tags)))
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),
tag=tag,
tags=tags)
else:
path = ""

View File

@ -145,7 +145,7 @@ class TestDate(TestCase):
@override_settings(MEDIA_ROOT="/tmp/paperless-tests-{}".
format(str(uuid4())[:8]))
@override_settings(PAPERLESS_FILENAME_FORMAT="{tag[type]}")
@override_settings(PAPERLESS_FILENAME_FORMAT="{tags[type]}")
def test_tags_with_underscore(self):
document = Document()
document.file_type = "pdf"
@ -168,7 +168,7 @@ class TestDate(TestCase):
@override_settings(MEDIA_ROOT="/tmp/paperless-tests-{}".
format(str(uuid4())[:8]))
@override_settings(PAPERLESS_FILENAME_FORMAT="{tag[type]}")
@override_settings(PAPERLESS_FILENAME_FORMAT="{tags[type]}")
def test_tags_with_dash(self):
document = Document()
document.file_type = "pdf"
@ -191,7 +191,7 @@ class TestDate(TestCase):
@override_settings(MEDIA_ROOT="/tmp/paperless-tests-{}".
format(str(uuid4())[:8]))
@override_settings(PAPERLESS_FILENAME_FORMAT="{tag[type]}")
@override_settings(PAPERLESS_FILENAME_FORMAT="{tags[type]}")
def test_tags_malformed(self):
document = Document()
document.file_type = "pdf"