A community-supported supercharged document management system: scan, index and archive all your documents
Python aiangulararchivingdjangodmsdocument-managementdocument-management-systemllmmachine-learningocroptical-character-recognitionpdf
Includes a version.json file with the current version in export. On import, catch certain errors and check the version if possible
Trenton Holmesa823b8f70ca7
diff --git a/src/documents/management/commands/document_exporter.py b/src/documents/management/commands/document_exporter.pyindex b110475a5..27f1fd8a9 100644--- a/src/documents/management/commands/document_exporter.py+++ b/src/documents/management/commands/document_exporter.py@@ -22,6 +22,7 @@ from documents.settings import EXPORTER_ARCHIVE_NAMEfrom documents.settings import EXPORTER_FILE_NAMEfrom documents.settings import EXPORTER_THUMBNAIL_NAMEfrom filelock import FileLock+from paperless import versionfrom paperless.db import GnuPGfrom paperless_mail.models import MailAccountfrom paperless_mail.models import MailRule@@ -232,12 +233,18 @@ class Command(BaseCommand):archive_target,)- # 4. write manifest to target forlder+ # 4.1 write manifest to target foldermanifest_path = os.path.abspath(os.path.join(self.target, "manifest.json"))with open(manifest_path, "w") as f:json.dump(manifest, f, indent=2)+ # 4.2 write version information to target folder+ version_path = os.path.abspath(os.path.join(self.target, "version.json"))++ with open(version_path, "w") as f:+ json.dump({"version": version.__full_version_str__}, f, indent=2)+if self.delete:# 5. Remove files which we did not explicitly export in this rundiff --git a/src/documents/management/commands/document_importer.py b/src/documents/management/commands/document_importer.pyindex d1ae33afb..398bc05b4 100644--- a/src/documents/management/commands/document_importer.py+++ b/src/documents/management/commands/document_importer.py@@ -6,9 +6,11 @@ from contextlib import contextmanagerimport tqdmfrom django.conf import settings+from django.core.exceptions import FieldDoesNotExistfrom django.core.management import call_commandfrom django.core.management.base import BaseCommandfrom django.core.management.base import CommandError+from django.core.serializers.base import DeserializationErrorfrom django.db.models.signals import m2m_changedfrom django.db.models.signals import post_savefrom documents.models import Document@@ -16,6 +18,7 @@ from documents.settings import EXPORTER_ARCHIVE_NAMEfrom documents.settings import EXPORTER_FILE_NAMEfrom documents.settings import EXPORTER_THUMBNAIL_NAMEfrom filelock import FileLock+from paperless import versionfrom ...file_handling import create_source_path_directoryfrom ...signals.handlers import update_filename_and_move_files@@ -53,6 +56,7 @@ class Command(BaseCommand):BaseCommand.__init__(self, *args, **kwargs)self.source = Noneself.manifest = None+ self.version = Nonedef handle(self, *args, **options):@@ -72,6 +76,11 @@ class Command(BaseCommand):with open(manifest_path) as f:self.manifest = json.load(f)+ version_path = os.path.join(self.source, "version.json")+ if os.path.exists(version_path):+ with open(version_path) as f:+ self.version = json.load(f)["version"]+self._check_manifest()with disable_signal(post_save,@@ -84,7 +93,20 @@ class Command(BaseCommand):sender=Document.tags.through,):# Fill up the database with whatever is in the manifest- call_command("loaddata", manifest_path)+ try:+ call_command("loaddata", manifest_path)+ except (FieldDoesNotExist, DeserializationError) as e:+ if (+ self.version is not None+ and self.version != version.__full_version_str__+ ):+ raise CommandError(+ "Error loading database, version mismatch. "+ f"Currently {version.__full_version_str__},"+ f" importing {self.version}",+ ) from e+ else:+ raise CommandError("Error loading database") from eself._import_files_from_manifest(options["no_progress_bar"])