Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

Fixes export of custom field instances during a split manifest export (#4984)

Trenton Hbe2de4f15dd6
diff --git a/src/documents/management/commands/document_exporter.py b/src/documents/management/commands/document_exporter.py
index 4f3cb937a..8913b1b6f 100644
--- a/src/documents/management/commands/document_exporter.py
+++ b/src/documents/management/commands/document_exporter.py
@@ -238,18 +238,6 @@ class Command(BaseCommand):
serializers.serialize("json", StoragePath.objects.all()),
)
- notes = json.loads(
- serializers.serialize("json", Note.objects.all()),
- )
- if not self.split_manifest:
- manifest += notes
-
- documents = Document.objects.order_by("id")
- document_map = {d.pk: d for d in documents}
- document_manifest = json.loads(serializers.serialize("json", documents))
- if not self.split_manifest:
- manifest += document_manifest
-
manifest += json.loads(
serializers.serialize("json", MailAccount.objects.all()),
)
@@ -303,10 +291,24 @@ class Command(BaseCommand):
serializers.serialize("json", CustomField.objects.all()),
)
+ # These are treated specially and included in the per-document manifest
+ # if that setting is enabled. Otherwise, they are just exported to the bulk
+ # manifest
+ documents = Document.objects.order_by("id")
+ document_map: dict[int, Document] = {d.pk: d for d in documents}
+ document_manifest = json.loads(serializers.serialize("json", documents))
+
+ notes = json.loads(
+ serializers.serialize("json", Note.objects.all()),
+ )
+
+ custom_field_instances = json.loads(
+ serializers.serialize("json", CustomFieldInstance.objects.all()),
+ )
if not self.split_manifest:
- manifest += json.loads(
- serializers.serialize("json", CustomFieldInstance.objects.all()),
- )
+ manifest += document_manifest
+ manifest += notes
+ manifest += custom_field_instances
# 3. Export files from each document
for index, document_dict in tqdm.tqdm(
@@ -412,6 +414,12 @@ class Command(BaseCommand):
notes,
),
)
+ content += list(
+ filter(
+ lambda d: d["fields"]["document"] == document_dict["pk"],
+ custom_field_instances,
+ ),
+ )
manifest_name.write_text(
json.dumps(content, indent=2, ensure_ascii=False),
encoding="utf-8",
diff --git a/src/documents/tests/test_management_exporter.py b/src/documents/tests/test_management_exporter.py
index b4dc5720a..54bb6f34c 100644
--- a/src/documents/tests/test_management_exporter.py
+++ b/src/documents/tests/test_management_exporter.py
@@ -646,10 +646,13 @@ class TestExportImport(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
with paperless_environment():
self.assertEqual(Document.objects.count(), 4)
+ self.assertEqual(CustomFieldInstance.objects.count(), 1)
Document.objects.all().delete()
+ CustomFieldInstance.objects.all().delete()
self.assertEqual(Document.objects.count(), 0)
call_command("document_importer", "--no-progress-bar", self.target)
self.assertEqual(Document.objects.count(), 4)
+ self.assertEqual(CustomFieldInstance.objects.count(), 1)
def test_folder_prefix(self):
"""