Fix: include OCR templates in the document exporter

Templates and their zones were missing from the export manifest, so an
import lost them.
This commit is contained in:
stumpylog committed 2026-11-18 07:55:52 -08:00
1 parent 855b161407
commit 0c90c57e3e
3 files changed
+91

No files matched your search

@@ -49,6 +49,8 @@ from documents.models import Document
from documents.models import DocumentBarcode
from documents.models import DocumentType
from documents.models import Note
from documents.models import OcrTemplate
from documents.models import OcrTemplateZone
from documents.models import SavedView
from documents.models import SavedViewFilterRule
from documents.models import ShareLink
@@ -364,6 +366,8 @@ class Command(CryptMixin, PaperlessCommand):
"workflows": Workflow.objects.all(),
"custom_fields": CustomField.objects.all(),
"custom_field_instances": CustomFieldInstance.global_objects.all(),
"ocr_templates": OcrTemplate.objects.all(),
"ocr_template_zones": OcrTemplateZone.objects.all(),
"document_barcodes": DocumentBarcode.objects.all(),
"app_configs": ApplicationConfiguration.objects.all(),
"notes": Note.global_objects.all(),
@@ -36,6 +36,8 @@ from documents.models import Document
from documents.models import DocumentBarcode
from documents.models import DocumentType
from documents.models import Note
from documents.models import OcrTemplate
from documents.models import OcrTemplateZone
from documents.models import ShareLink
from documents.models import ShareLinkBundle
from documents.models import StoragePath
@@ -58,6 +60,8 @@ from paperless_testing.dirs import PaperlessDirs
from paperless_testing.dirs import paperless_environment
from paperless_testing.factories import DocumentBarcodeFactory
from paperless_testing.factories import DocumentFactory
from paperless_testing.factories import OcrTemplateFactory
from paperless_testing.factories import OcrTemplateZoneFactory
from paperless_testing.permissions import grant_object
from paperless_testing.samples import install_document_samples
@@ -415,6 +419,63 @@ class TestExportImport(
b"share-bundle-contents",
)
def test_exporter_includes_ocr_templates(self) -> None:
"""
GIVEN:
- An OCR template with a sample document and zones targeting a
custom field and a built-in field
WHEN:
- The data is exported, the templates are deleted, and the export is
imported again
THEN:
- The template and its zones are restored with their relations
"""
shutil.rmtree(Path(self.dirs.media_dir) / "documents")
install_document_samples(Path(self.dirs.media_dir) / "documents")
template = OcrTemplateFactory(
name="Invoice",
owner=self.user,
sample_document=self.d1,
combine_formats={str(self.cf1.pk): "{Number}"},
)
cf_zone = OcrTemplateZoneFactory(
template=template,
name="Number",
target=OcrTemplateZone.TargetType.CUSTOM_FIELD,
custom_field=self.cf1,
)
title_zone = OcrTemplateZoneFactory(template=template, name="Title")
manifest = self._do_export()
exported_models = [record["model"] for record in manifest]
self.assertEqual(exported_models.count("documents.ocrtemplate"), 1)
self.assertEqual(exported_models.count("documents.ocrtemplatezone"), 2)
with paperless_environment():
OcrTemplate.objects.all().delete()
call_command(
"document_importer",
"--no-progress-bar",
self.target,
skip_checks=True,
)
imported = OcrTemplate.objects.get(pk=template.pk)
self.assertEqual(imported.owner_id, self.user.pk)
self.assertEqual(imported.sample_document_id, self.d1.pk)
self.assertEqual(imported.combine_formats, {str(self.cf1.pk): "{Number}"})
self.assertEqual(
list(imported.zones.order_by("pk").values_list("pk", flat=True)),
[cf_zone.pk, title_zone.pk],
)
self.assertEqual(
OcrTemplateZone.objects.get(pk=cf_zone.pk).custom_field_id,
self.cf1.pk,
)
def test_update_export_changed_time(self) -> None:
shutil.rmtree(Path(self.dirs.media_dir) / "documents")
install_document_samples(Path(self.dirs.media_dir) / "documents")
+26
View File
@@ -12,6 +12,8 @@ from documents.models import Document
from documents.models import DocumentBarcode
from documents.models import DocumentType
from documents.models import MatchingModel
from documents.models import OcrTemplate
from documents.models import OcrTemplateZone
from documents.models import PaperlessTask
from documents.models import StoragePath
from documents.models import Tag
@@ -108,3 +110,27 @@ class PaperlessTaskFactory(TypedModelFactory[PaperlessTask]):
input_data = factory.LazyFunction(dict)
result_data = None
acknowledged = False
class OcrTemplateFactory(TypedModelFactory[OcrTemplate]):
class Meta:
model = OcrTemplate
name = factory.Sequence(lambda n: f"OCR template {n}")
match = ""
matching_algorithm = MatchingModel.MATCH_NONE
source_width = 1000
source_height = 1000
class OcrTemplateZoneFactory(TypedModelFactory[OcrTemplateZone]):
class Meta:
model = OcrTemplateZone
template = factory.SubFactory(OcrTemplateFactory)
name = factory.Sequence(lambda n: f"Zone {n}")
target = OcrTemplateZone.TargetType.TITLE
x = 0
y = 0
width = 100
height = 50