mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-04-02 13:45:10 -05:00
Refactored source_filename_new into generate_source_filename and
create_source_directory
This commit is contained in:
parent
e79f5394fc
commit
bcff75433c
@ -279,7 +279,7 @@ class Document(models.Model):
|
||||
@property
|
||||
def source_filename(self):
|
||||
if self.filename is None:
|
||||
self.filename = self.source_filename_new()
|
||||
self.filename = self.generate_source_filename()
|
||||
|
||||
return self.filename
|
||||
|
||||
@ -298,7 +298,7 @@ class Document(models.Model):
|
||||
|
||||
return mydictionary
|
||||
|
||||
def source_filename_new(self):
|
||||
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(
|
||||
@ -335,12 +335,17 @@ class Document(models.Model):
|
||||
if self.storage_type == self.STORAGE_TYPE_GPG:
|
||||
filename += ".gpg"
|
||||
|
||||
# Create directory for target (and ignore, if directory exists already)
|
||||
create_dir = self.filename_to_path(slugify(directory))
|
||||
os.makedirs(create_dir, exist_ok=True)
|
||||
|
||||
return filename
|
||||
|
||||
def create_source_directory(self):
|
||||
new_filename = self.generate_source_filename()
|
||||
|
||||
# Determine the full "target" path
|
||||
dir_new = self.filename_to_path(os.path.dirname(new_filename))
|
||||
|
||||
# Create new path
|
||||
os.makedirs(dir_new, exist_ok=True)
|
||||
|
||||
@property
|
||||
def source_path(self):
|
||||
return self.filename_to_path(self.source_filename)
|
||||
@ -408,7 +413,7 @@ def update_filename(sender, instance, **kwargs):
|
||||
return
|
||||
|
||||
# Build the new filename
|
||||
new_filename = instance.source_filename_new()
|
||||
new_filename = instance.generate_source_filename()
|
||||
|
||||
# If the filename is the same, then nothing needs to be done
|
||||
if instance.filename is None or \
|
||||
@ -421,6 +426,9 @@ def update_filename(sender, instance, **kwargs):
|
||||
path_new = instance.filename_to_path(new_filename)
|
||||
dir_new = instance.filename_to_path(os.path.dirname(new_filename))
|
||||
|
||||
# Create new path
|
||||
instance.create_source_directory()
|
||||
|
||||
# Determine the full "current" path
|
||||
path_current = instance.filename_to_path(instance.filename)
|
||||
|
||||
|
@ -29,16 +29,17 @@ class TestDate(TestCase):
|
||||
|
||||
@override_settings(PAPERLESS_DIRECTORY_FORMAT="")
|
||||
@override_settings(PAPERLESS_FILENAME_FORMAT="")
|
||||
def test_source_filename_new(self):
|
||||
def test_generate_source_filename(self):
|
||||
document = Document()
|
||||
document.file_type = "pdf"
|
||||
document.storage_type = Document.STORAGE_TYPE_UNENCRYPTED
|
||||
document.save()
|
||||
|
||||
self.assertEqual(document.source_filename_new(), "0000001.pdf")
|
||||
self.assertEqual(document.generate_source_filename(), "0000001.pdf")
|
||||
|
||||
document.storage_type = Document.STORAGE_TYPE_GPG
|
||||
self.assertEqual(document.source_filename_new(), "0000001.pdf.gpg")
|
||||
self.assertEqual(document.generate_source_filename(),
|
||||
"0000001.pdf.gpg")
|
||||
|
||||
@override_settings(MEDIA_ROOT="/tmp/paperless-tests-{}".
|
||||
format(str(uuid4())[:8]))
|
||||
@ -52,8 +53,9 @@ class TestDate(TestCase):
|
||||
|
||||
# Ensure that filename is properly generated
|
||||
tmp = document.source_filename
|
||||
self.assertEqual(document.source_filename_new(),
|
||||
self.assertEqual(document.generate_source_filename(),
|
||||
"none/none-0000001.pdf")
|
||||
document.create_source_directory()
|
||||
Path(document.source_path).touch()
|
||||
|
||||
# Test source_path
|
||||
@ -63,7 +65,7 @@ class TestDate(TestCase):
|
||||
# Enable encryption and check again
|
||||
document.storage_type = Document.STORAGE_TYPE_GPG
|
||||
tmp = document.source_filename
|
||||
self.assertEqual(document.source_filename_new(),
|
||||
self.assertEqual(document.generate_source_filename(),
|
||||
"none/none-0000001.pdf.gpg")
|
||||
document.save()
|
||||
|
||||
@ -82,7 +84,7 @@ class TestDate(TestCase):
|
||||
"/documents/originals/none"), False)
|
||||
self.assertEqual(os.path.isfile(settings.MEDIA_ROOT + "/documents/" +
|
||||
"originals/test/test-0000001.pdf.gpg"), True)
|
||||
self.assertEqual(document.source_filename_new(),
|
||||
self.assertEqual(document.generate_source_filename(),
|
||||
"test/test-0000001.pdf.gpg")
|
||||
|
||||
@override_settings(MEDIA_ROOT="/tmp/paperless-tests-{}".
|
||||
@ -97,8 +99,9 @@ class TestDate(TestCase):
|
||||
|
||||
# Ensure that filename is properly generated
|
||||
tmp = document.source_filename
|
||||
self.assertEqual(document.source_filename_new(),
|
||||
self.assertEqual(document.generate_source_filename(),
|
||||
"none/none-0000001.pdf")
|
||||
document.create_source_directory()
|
||||
Path(document.source_path).touch()
|
||||
|
||||
# Ensure file deletion after delete
|
||||
@ -120,8 +123,9 @@ class TestDate(TestCase):
|
||||
|
||||
# Ensure that filename is properly generated
|
||||
tmp = document.source_filename
|
||||
self.assertEqual(document.source_filename_new(),
|
||||
self.assertEqual(document.generate_source_filename(),
|
||||
"none/none-0000001.pdf")
|
||||
document.create_source_directory()
|
||||
Path(document.source_path).touch()
|
||||
Path(document.source_path + "test").touch()
|
||||
|
||||
@ -151,4 +155,4 @@ class TestDate(TestCase):
|
||||
document.storage_type = Document.STORAGE_TYPE_UNENCRYPTED
|
||||
document.save()
|
||||
|
||||
self.assertEqual(document.source_filename_new(), "0000001.pdf")
|
||||
self.assertEqual(document.generate_source_filename(), "0000001.pdf")
|
||||
|
Loading…
x
Reference in New Issue
Block a user