mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-04-19 10:19:27 -05:00
Tags in filenames can now either be found by key (in case - or _ are
used as delimeter) or by index. Added some more tests.
This commit is contained in:
parent
05da4a559f
commit
d8fda64cd0
@ -60,7 +60,8 @@ PAPERLESS_EMAIL_SECRET=""
|
|||||||
# * {title}
|
# * {title}
|
||||||
# * {created}
|
# * {created}
|
||||||
# * {added}
|
# * {added}
|
||||||
# * {tags[FILTER]}
|
# * {tag[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
|
# Uniqueness of filenames is ensured, as an incrementing counter is attached
|
||||||
# to each filename.
|
# to each filename.
|
||||||
#PAPERLESS_FILENAME_FORMAT=""
|
#PAPERLESS_FILENAME_FORMAT=""
|
||||||
|
@ -283,21 +283,39 @@ class Document(models.Model):
|
|||||||
|
|
||||||
return self.filename
|
return self.filename
|
||||||
|
|
||||||
def many_to_dictionary(self, field):
|
@staticmethod
|
||||||
|
def many_to_dictionary(field):
|
||||||
|
# Converts ManyToManyField to dictionary by assuming, that field
|
||||||
|
# entries contain an _ or - which will be used as a delimeter
|
||||||
mydictionary = dict()
|
mydictionary = dict()
|
||||||
|
|
||||||
for t in field.all():
|
for t in field.all():
|
||||||
|
# Find delimeter
|
||||||
delimeter = t.name.find('_')
|
delimeter = t.name.find('_')
|
||||||
|
|
||||||
|
if delimeter is -1:
|
||||||
|
delimeter = t.name.find('-')
|
||||||
|
|
||||||
if delimeter is -1:
|
if delimeter is -1:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
key = t.name[:delimeter]
|
key = t.name[:delimeter]
|
||||||
value = t.name[delimeter+1:]
|
value = t.name[delimeter+1:]
|
||||||
|
|
||||||
mydictionary[key] = slugify(value)
|
mydictionary[slugify(key)] = slugify(value)
|
||||||
|
|
||||||
return mydictionary
|
return mydictionary
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def many_to_list(field):
|
||||||
|
# Converts ManyToManyField to list
|
||||||
|
mylist = list()
|
||||||
|
|
||||||
|
for t in field.all():
|
||||||
|
mylist.append(slugify(t))
|
||||||
|
|
||||||
|
return mylist
|
||||||
|
|
||||||
def generate_source_filename(self):
|
def generate_source_filename(self):
|
||||||
# Create filename based on configured format
|
# Create filename based on configured format
|
||||||
if settings.PAPERLESS_FILENAME_FORMAT is not None:
|
if settings.PAPERLESS_FILENAME_FORMAT is not None:
|
||||||
@ -306,8 +324,8 @@ class Document(models.Model):
|
|||||||
title=slugify(self.title),
|
title=slugify(self.title),
|
||||||
created=slugify(self.created),
|
created=slugify(self.created),
|
||||||
added=slugify(self.added),
|
added=slugify(self.added),
|
||||||
tags=defaultdict(str,
|
tag=defaultdict(str, self.many_to_dictionary(self.tags)),
|
||||||
self.many_to_dictionary(self.tags)))
|
tags=self.many_to_list(self.tags))
|
||||||
else:
|
else:
|
||||||
path = ""
|
path = ""
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ from dateutil import tz
|
|||||||
from django.test import TestCase, override_settings
|
from django.test import TestCase, override_settings
|
||||||
|
|
||||||
from django.utils.text import slugify
|
from django.utils.text import slugify
|
||||||
from ..models import Document, Correspondent
|
from ..models import Tag, Document, Correspondent
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
||||||
|
|
||||||
@ -41,7 +41,8 @@ class TestDate(TestCase):
|
|||||||
|
|
||||||
@override_settings(MEDIA_ROOT="/tmp/paperless-tests-{}".
|
@override_settings(MEDIA_ROOT="/tmp/paperless-tests-{}".
|
||||||
format(str(uuid4())[:8]))
|
format(str(uuid4())[:8]))
|
||||||
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{correspondent}")
|
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/" +
|
||||||
|
"{correspondent}")
|
||||||
def test_file_renaming(self):
|
def test_file_renaming(self):
|
||||||
document = Document()
|
document = Document()
|
||||||
document.file_type = "pdf"
|
document.file_type = "pdf"
|
||||||
@ -86,7 +87,8 @@ class TestDate(TestCase):
|
|||||||
|
|
||||||
@override_settings(MEDIA_ROOT="/tmp/paperless-tests-{}".
|
@override_settings(MEDIA_ROOT="/tmp/paperless-tests-{}".
|
||||||
format(str(uuid4())[:8]))
|
format(str(uuid4())[:8]))
|
||||||
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{correspondent}")
|
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/" +
|
||||||
|
"{correspondent}")
|
||||||
def test_document_delete(self):
|
def test_document_delete(self):
|
||||||
document = Document()
|
document = Document()
|
||||||
document.file_type = "pdf"
|
document.file_type = "pdf"
|
||||||
@ -109,7 +111,8 @@ class TestDate(TestCase):
|
|||||||
|
|
||||||
@override_settings(MEDIA_ROOT="/tmp/paperless-tests-{}".
|
@override_settings(MEDIA_ROOT="/tmp/paperless-tests-{}".
|
||||||
format(str(uuid4())[:8]))
|
format(str(uuid4())[:8]))
|
||||||
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{correspondent}")
|
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/" +
|
||||||
|
"{correspondent}")
|
||||||
def test_directory_not_empty(self):
|
def test_directory_not_empty(self):
|
||||||
document = Document()
|
document = Document()
|
||||||
document.file_type = "pdf"
|
document.file_type = "pdf"
|
||||||
@ -140,10 +143,101 @@ class TestDate(TestCase):
|
|||||||
"/documents/originals/none/none-0000001.pdftest")
|
"/documents/originals/none/none-0000001.pdftest")
|
||||||
os.rmdir(settings.MEDIA_ROOT + "/documents/originals/none")
|
os.rmdir(settings.MEDIA_ROOT + "/documents/originals/none")
|
||||||
|
|
||||||
|
@override_settings(MEDIA_ROOT="/tmp/paperless-tests-{}".
|
||||||
|
format(str(uuid4())[:8]))
|
||||||
|
@override_settings(PAPERLESS_FILENAME_FORMAT="{tag[type]}")
|
||||||
|
def test_tags_with_underscore(self):
|
||||||
|
document = Document()
|
||||||
|
document.file_type = "pdf"
|
||||||
|
document.storage_type = Document.STORAGE_TYPE_UNENCRYPTED
|
||||||
|
document.save()
|
||||||
|
|
||||||
|
# Add tag to document
|
||||||
|
document.tags.create(name="type_demo")
|
||||||
|
document.tags.create(name="foo_bar")
|
||||||
|
document.save()
|
||||||
|
|
||||||
|
# Ensure that filename is properly generated
|
||||||
|
tmp = document.source_filename
|
||||||
|
self.assertEqual(document.generate_source_filename(),
|
||||||
|
"demo-0000001.pdf")
|
||||||
|
document.create_source_directory()
|
||||||
|
Path(document.source_path).touch()
|
||||||
|
|
||||||
|
document.delete()
|
||||||
|
|
||||||
@override_settings(MEDIA_ROOT="/tmp/paperless-tests-{}".
|
@override_settings(MEDIA_ROOT="/tmp/paperless-tests-{}".
|
||||||
format(str(uuid4())[:8]))
|
format(str(uuid4())[:8]))
|
||||||
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{correspondent}/{correspondent}")
|
@override_settings(PAPERLESS_FILENAME_FORMAT="{tag[type]}")
|
||||||
|
def test_tags_with_dash(self):
|
||||||
|
document = Document()
|
||||||
|
document.file_type = "pdf"
|
||||||
|
document.storage_type = Document.STORAGE_TYPE_UNENCRYPTED
|
||||||
|
document.save()
|
||||||
|
|
||||||
|
# Add tag to document
|
||||||
|
document.tags.create(name="type-demo")
|
||||||
|
document.tags.create(name="foo-bar")
|
||||||
|
document.save()
|
||||||
|
|
||||||
|
# Ensure that filename is properly generated
|
||||||
|
tmp = document.source_filename
|
||||||
|
self.assertEqual(document.generate_source_filename(),
|
||||||
|
"demo-0000001.pdf")
|
||||||
|
document.create_source_directory()
|
||||||
|
Path(document.source_path).touch()
|
||||||
|
|
||||||
|
document.delete()
|
||||||
|
|
||||||
|
@override_settings(MEDIA_ROOT="/tmp/paperless-tests-{}".
|
||||||
|
format(str(uuid4())[:8]))
|
||||||
|
@override_settings(PAPERLESS_FILENAME_FORMAT="{tag[type]}")
|
||||||
|
def test_tags_malformed(self):
|
||||||
|
document = Document()
|
||||||
|
document.file_type = "pdf"
|
||||||
|
document.storage_type = Document.STORAGE_TYPE_UNENCRYPTED
|
||||||
|
document.save()
|
||||||
|
|
||||||
|
# Add tag to document
|
||||||
|
document.tags.create(name="type:demo")
|
||||||
|
document.tags.create(name="foo:bar")
|
||||||
|
document.save()
|
||||||
|
|
||||||
|
# Ensure that filename is properly generated
|
||||||
|
tmp = document.source_filename
|
||||||
|
self.assertEqual(document.generate_source_filename(),
|
||||||
|
"0000001.pdf")
|
||||||
|
document.create_source_directory()
|
||||||
|
Path(document.source_path).touch()
|
||||||
|
|
||||||
|
document.delete()
|
||||||
|
|
||||||
|
@override_settings(MEDIA_ROOT="/tmp/paperless-tests-{}".
|
||||||
|
format(str(uuid4())[:8]))
|
||||||
|
@override_settings(PAPERLESS_FILENAME_FORMAT="{tags[0]}")
|
||||||
|
def test_tags_all(self):
|
||||||
|
document = Document()
|
||||||
|
document.file_type = "pdf"
|
||||||
|
document.storage_type = Document.STORAGE_TYPE_UNENCRYPTED
|
||||||
|
document.save()
|
||||||
|
|
||||||
|
# Add tag to document
|
||||||
|
document.tags.create(name="demo")
|
||||||
|
document.save()
|
||||||
|
|
||||||
|
# Ensure that filename is properly generated
|
||||||
|
tmp = document.source_filename
|
||||||
|
self.assertEqual(document.generate_source_filename(),
|
||||||
|
"demo-0000001.pdf")
|
||||||
|
document.create_source_directory()
|
||||||
|
Path(document.source_path).touch()
|
||||||
|
|
||||||
|
document.delete()
|
||||||
|
|
||||||
|
@override_settings(MEDIA_ROOT="/tmp/paperless-tests-{}".
|
||||||
|
format(str(uuid4())[:8]))
|
||||||
|
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/" +
|
||||||
|
"{correspondent}/{correspondent}")
|
||||||
def test_nested_directory_cleanup(self):
|
def test_nested_directory_cleanup(self):
|
||||||
document = Document()
|
document = Document()
|
||||||
document.file_type = "pdf"
|
document.file_type = "pdf"
|
||||||
@ -164,7 +258,8 @@ class TestDate(TestCase):
|
|||||||
document.delete()
|
document.delete()
|
||||||
|
|
||||||
self.assertEqual(os.path.isfile(settings.MEDIA_ROOT +
|
self.assertEqual(os.path.isfile(settings.MEDIA_ROOT +
|
||||||
"/documents/originals/none/none/none-0000001.pdf"), False)
|
"/documents/originals/none/none/none-0000001.pdf"),
|
||||||
|
False)
|
||||||
self.assertEqual(os.path.isdir(settings.MEDIA_ROOT +
|
self.assertEqual(os.path.isdir(settings.MEDIA_ROOT +
|
||||||
"/documents/originals/none/none"), False)
|
"/documents/originals/none/none"), False)
|
||||||
self.assertEqual(os.path.isdir(settings.MEDIA_ROOT +
|
self.assertEqual(os.path.isdir(settings.MEDIA_ROOT +
|
||||||
|
Loading…
x
Reference in New Issue
Block a user