From bcff75433c1e8274a98bc6dafde7ec3381c68ea9 Mon Sep 17 00:00:00 2001 From: Wolf-Bastian Poettner Date: Sat, 1 Feb 2020 13:52:27 +0000 Subject: [PATCH] Refactored source_filename_new into generate_source_filename and create_source_directory --- src/documents/models.py | 22 +++++++++++++++------- src/documents/tests/test_file_handling.py | 22 +++++++++++++--------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/documents/models.py b/src/documents/models.py index 1c7905643..e37a4fbb5 100644 --- a/src/documents/models.py +++ b/src/documents/models.py @@ -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) diff --git a/src/documents/tests/test_file_handling.py b/src/documents/tests/test_file_handling.py index edeb1ab5e..a4d842236 100644 --- a/src/documents/tests/test_file_handling.py +++ b/src/documents/tests/test_file_handling.py @@ -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")