A community-supported supercharged document management system: scan, index and archive all your documents
Python aiangulararchivingdjangodmsdocument-managementdocument-management-systemllmmachine-learningocroptical-character-recognitionpdf
fixes #165
jonaswinklere75534c0f222
diff --git a/src/documents/management/commands/document_importer.py b/src/documents/management/commands/document_importer.pyindex 70d05d98b..8e9a79219 100644--- a/src/documents/management/commands/document_importer.py+++ b/src/documents/management/commands/document_importer.py@@ -1,18 +1,29 @@import jsonimport osimport shutil+from contextlib import contextmanagerfrom django.conf import settingsfrom django.core.management import call_commandfrom django.core.management.base import BaseCommand, CommandError+from django.db.models.signals import post_save, m2m_changedfrom filelock import FileLockfrom documents.models import Documentfrom documents.settings import EXPORTER_FILE_NAME, EXPORTER_THUMBNAIL_NAME, \EXPORTER_ARCHIVE_NAME-from ...file_handling import create_source_path_directory, \- generate_unique_filename+from ...file_handling import create_source_path_directoryfrom ...mixins import Renderable+from ...signals.handlers import update_filename_and_move_files+++@contextmanager+def disable_signal(sig, receiver, sender):+ try:+ sig.disconnect(receiver=receiver, sender=sender)+ yield+ finally:+ sig.connect(receiver=receiver, sender=sender)class Command(Renderable, BaseCommand):@@ -47,11 +58,16 @@ class Command(Renderable, BaseCommand):self.manifest = json.load(f)self._check_manifest()+ with disable_signal(post_save,+ receiver=update_filename_and_move_files,+ sender=Document):+ with disable_signal(m2m_changed,+ receiver=update_filename_and_move_files,+ sender=Document.tags.through):+ # Fill up the database with whatever is in the manifest+ call_command("loaddata", manifest_path)- # Fill up the database with whatever is in the manifest- call_command("loaddata", manifest_path)-- self._import_files_from_manifest()+ self._import_files_from_manifest()@staticmethoddef _check_manifest_exists(path):@@ -117,9 +133,6 @@ class Command(Renderable, BaseCommand):document.storage_type = Document.STORAGE_TYPE_UNENCRYPTEDwith FileLock(settings.MEDIA_LOCK):- document.filename = generate_unique_filename(- document, settings.ORIGINALS_DIR)-if os.path.isfile(document.source_path):raise FileExistsError(document.source_path)diff --git a/src/documents/tests/test_management_exporter.py b/src/documents/tests/test_management_exporter.pyindex 22d6fc7f6..d6ab7eadd 100644--- a/src/documents/tests/test_management_exporter.py+++ b/src/documents/tests/test_management_exporter.py@@ -24,11 +24,17 @@ class TestExportImport(DirectoriesMixin, TestCase):file = os.path.join(self.dirs.originals_dir, "0000001.pdf")- Document.objects.create(content="Content", checksum="42995833e01aea9b3edee44bbfdd7ce1", archive_checksum="62acb0bcbfbcaa62ca6ad3668e4e404b", title="wow", filename="0000001.pdf", mime_type="application/pdf")- Document.objects.create(content="Content", checksum="9c9691e51741c1f4f41a20896af31770", title="wow", filename="0000002.pdf.gpg", mime_type="application/pdf", storage_type=Document.STORAGE_TYPE_GPG)- Tag.objects.create(name="t")- DocumentType.objects.create(name="dt")- Correspondent.objects.create(name="c")+ d1 = Document.objects.create(content="Content", checksum="42995833e01aea9b3edee44bbfdd7ce1", archive_checksum="62acb0bcbfbcaa62ca6ad3668e4e404b", title="wow", filename="0000001.pdf", mime_type="application/pdf")+ d2 = Document.objects.create(content="Content", checksum="9c9691e51741c1f4f41a20896af31770", title="wow", filename="0000002.pdf.gpg", mime_type="application/pdf", storage_type=Document.STORAGE_TYPE_GPG)+ t1 = Tag.objects.create(name="t")+ dt1 = DocumentType.objects.create(name="dt")+ c1 = Correspondent.objects.create(name="c")++ d1.tags.add(t1)+ d1.correspondents = c1+ d1.document_type = dt1+ d1.save()+ d2.save()target = tempfile.mkdtemp()self.addCleanup(shutil.rmtree, target)@@ -59,11 +65,25 @@ class TestExportImport(DirectoriesMixin, TestCase):self.assertEqual(checksum, element['fields']['archive_checksum'])with paperless_environment() as dirs:+ self.assertEqual(Document.objects.count(), 2)+ Document.objects.all().delete()+ Correspondent.objects.all().delete()+ DocumentType.objects.all().delete()+ Tag.objects.all().delete()+ self.assertEqual(Document.objects.count(), 0)+call_command('document_importer', target)+ self.assertEqual(Document.objects.count(), 2)messages = check_sanity()# everything is alright after the testself.assertEqual(len(messages), 0, str([str(m) for m in messages]))+ @override_settings(+ PAPERLESS_FILENAME_FORMAT="{title}"+ )+ def test_exporter_with_filename_format(self):+ self.test_exporter()+def test_export_missing_files(self):target = tempfile.mkdtemp()