A community-supported supercharged document management system: scan, index and archive all your documents
Python aiangulararchivingdjangodmsdocument-managementdocument-management-systemllmmachine-learningocroptical-character-recognitionpdf
Refactored source_filename_new into generate_source_filename and create_source_directory
Wolf-Bastian Poettnerbcff75433c1e
diff --git a/src/documents/models.py b/src/documents/models.pyindex 1c7905643..e37a4fbb5 100644--- a/src/documents/models.py+++ b/src/documents/models.py@@ -279,7 +279,7 @@ class Document(models.Model):@propertydef 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 formatif 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)+@propertydef 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 doneif 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" pathpath_current = instance.filename_to_path(instance.filename)diff --git a/src/documents/tests/test_file_handling.py b/src/documents/tests/test_file_handling.pyindex 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_UNENCRYPTEDdocument.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 generatedtmp = 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 againdocument.storage_type = Document.STORAGE_TYPE_GPGtmp = 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 generatedtmp = 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 generatedtmp = 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_UNENCRYPTEDdocument.save()- self.assertEqual(document.source_filename_new(), "0000001.pdf")+ self.assertEqual(document.generate_source_filename(), "0000001.pdf")