A community-supported supercharged document management system: scan, index and archive all your documents
Python aiangulararchivingdjangodmsdocument-managementdocument-management-systemllmmachine-learningocroptical-character-recognitionpdf
delete option for exporter, short options, tests
jonaswinklerb9725437d935
diff --git a/src/documents/management/commands/document_exporter.py b/src/documents/management/commands/document_exporter.pyindex 250ae1adf..37fcf2024 100644--- a/src/documents/management/commands/document_exporter.py+++ b/src/documents/management/commands/document_exporter.py@@ -31,7 +31,7 @@ class Command(Renderable, BaseCommand):parser.add_argument("target")parser.add_argument(- "--compare-checksums",+ "-c", "--compare-checksums",default=False,action="store_true",help="Compare file checksums when determining whether to export "@@ -40,13 +40,22 @@ class Command(Renderable, BaseCommand):)parser.add_argument(- "--use-filename-format",+ "-f", "--use-filename-format",default=False,action="store_true",help="Use PAPERLESS_FILENAME_FORMAT for storing files in the ""export directory, if configured.")+ parser.add_argument(+ "-d", "--delete",+ default=False,+ action="store_true",+ help="After exporting, delete files in the export directory that "+ "do not belong to the current export, such as files from "+ "deleted documents."+ )+def __init__(self, *args, **kwargs):BaseCommand.__init__(self, *args, **kwargs)self.target = None@@ -54,12 +63,14 @@ class Command(Renderable, BaseCommand):self.exported_files = []self.compare_checksums = Falseself.use_filename_format = False+ self.delete = Falsedef handle(self, *args, **options):self.target = options["target"]self.compare_checksums = options['compare_checksums']self.use_filename_format = options['use_filename_format']+ self.delete = options['delete']if not os.path.exists(self.target):raise CommandError("That path doesn't exist")@@ -176,15 +187,17 @@ class Command(Renderable, BaseCommand):with open(manifest_path, "w") as f:json.dump(manifest, f, indent=2)- if manifest_path in self.files_in_export_dir:- self.files_in_export_dir.remove(manifest_path)+ if self.delete:+ # 5. Remove files which we did not explicitly export in this run++ if manifest_path in self.files_in_export_dir:+ self.files_in_export_dir.remove(manifest_path)- # 5. Remove files which we did not explicitly export in this run- for f in self.files_in_export_dir:- os.remove(f)+ for f in self.files_in_export_dir:+ os.remove(f)- delete_empty_directories(os.path.abspath(os.path.dirname(f)),- os.path.abspath(self.target))+ delete_empty_directories(os.path.abspath(os.path.dirname(f)),+ os.path.abspath(self.target))def check_and_copy(self, source, source_checksum, target):if os.path.abspath(target) in self.files_in_export_dir:diff --git a/src/documents/tests/test_management_exporter.py b/src/documents/tests/test_management_exporter.pyindex f859649ed..212b87ec1 100644--- a/src/documents/tests/test_management_exporter.py+++ b/src/documents/tests/test_management_exporter.py@@ -47,12 +47,14 @@ class TestExportImport(DirectoriesMixin, TestCase):@override_settings(PASSPHRASE="test")- def _do_export(self, use_filename_format=False, compare_checksums=False):+ def _do_export(self, use_filename_format=False, compare_checksums=False, delete=False):args = ['document_exporter', self.target]if use_filename_format:args += ["--use-filename-format"]if compare_checksums:args += ["--compare-checksums"]+ if delete:+ args += ["--delete"]call_command(*args)@@ -182,8 +184,11 @@ class TestExportImport(DirectoriesMixin, TestCase):self.assertTrue(os.path.isfile(os.path.join(self.target, doc_from_manifest[EXPORTER_FILE_NAME])))self.d3.delete()- manifest2 = self._do_export()+ manifest = self._do_export()self.assertRaises(ValueError, self._get_document_from_manifest, manifest, self.d3.id)+ self.assertTrue(os.path.isfile(os.path.join(self.target, doc_from_manifest[EXPORTER_FILE_NAME])))++ manifest = self._do_export(delete=True)self.assertFalse(os.path.isfile(os.path.join(self.target, doc_from_manifest[EXPORTER_FILE_NAME])))self.assertTrue(len(manifest), 6)@@ -200,11 +205,13 @@ class TestExportImport(DirectoriesMixin, TestCase):self.d1.title = "new_title"self.d1.save()- self._do_export(use_filename_format=True)+ self._do_export(use_filename_format=True, delete=True)self.assertFalse(os.path.isfile(os.path.join(self.target, "wow1", "c.pdf")))self.assertFalse(os.path.isdir(os.path.join(self.target, "wow1")))self.assertTrue(os.path.isfile(os.path.join(self.target, "new_title", "c.pdf")))self.assertTrue(os.path.exists(os.path.join(self.target, "manifest.json")))+ self.assertTrue(os.path.isfile(os.path.join(self.target, "wow2", "none.pdf")))+ self.assertTrue(os.path.isfile(os.path.join(self.target, "wow2", "none_01.pdf")))def test_export_missing_files(self):