Chore: give OCR templates their own matching fields

Templates are never assigned to documents, so the classifier-based Automatic
matching inherited from MatchingModel could never work. Declare the matching
fields with the workflow trigger choices instead. Also keep zones when their
custom field is deleted, allow longer OCR language lists, fix help texts,
and regenerate the unreleased migration.
This commit is contained in:
stumpylog committed 2026-11-19 10:04:31 -08:00
1 parent 8e07452604
commit 4c2df48795
6 files changed
+91 -36

No files matched your search

+16 -21
View File
@@ -171,14 +171,10 @@ class DocumentUpdateFieldsModelSerializer(DynamicFieldsModelSerializer):
return instance
class MatchingModelSerializer(serializers.ModelSerializer[Any]):
document_count = serializers.IntegerField(read_only=True)
def get_slug(self, obj) -> str:
return slugify(obj.name)
slug = SerializerMethodField()
# Name and owner uniqueness and match regex validation for models with a name,
# an owner and match rules. A comment, not a docstring, so it stays out of the
# API schema descriptions of every serializer built on it.
class MatchRuleSerializer(serializers.ModelSerializer[Any]):
def validate(self, data):
# TODO: remove pending https://github.com/encode/django-rest-framework/issues/7173
name = data.get(
@@ -217,6 +213,15 @@ class MatchingModelSerializer(serializers.ModelSerializer[Any]):
return match
class MatchingModelSerializer(MatchRuleSerializer):
document_count = serializers.IntegerField(read_only=True)
def get_slug(self, obj) -> str:
return slugify(obj.name)
slug = SerializerMethodField()
class SetPermissionsMixin:
def _validate_user_ids(self, user_ids):
users = User.objects.none()
@@ -3827,7 +3832,8 @@ class OcrTemplateZoneSerializer(serializers.ModelSerializer):
{name: field.error_messages["required"]},
)
# A custom field target may be left unassigned, as the editor creates new
# zones; built-in targets never use a custom field.
# zones, and a zone is left unassigned when its custom field is deleted;
# built-in targets never use a custom field.
if (
data.get("target", OcrTemplateZone.TargetType.CUSTOM_FIELD)
!= OcrTemplateZone.TargetType.CUSTOM_FIELD
@@ -3897,17 +3903,13 @@ def _zone_field_key(zone: dict[str, Any]) -> str:
return target
class OcrTemplateSerializer(MatchingModelSerializer, OwnedObjectSerializer):
class OcrTemplateSerializer(MatchRuleSerializer, OwnedObjectSerializer):
zones = OcrTemplateZoneSerializer(many=True, required=False)
combine_formats = serializers.DictField(
child=serializers.CharField(allow_blank=True, trim_whitespace=False),
required=False,
)
# Templates are never assigned to documents
slug = None
document_count = None
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
# Writes accept documents the user can view, plus the sample document
@@ -3947,13 +3949,6 @@ class OcrTemplateSerializer(MatchingModelSerializer, OwnedObjectSerializer):
]
read_only_fields = ["created", "updated"]
def validate_matching_algorithm(self, value):
if value == MatchingModel.MATCH_AUTO:
raise serializers.ValidationError(
"Automatic matching is not supported for OCR templates.",
)
return value
def validate(self, data):
data = super().validate(data)
if "zones" in data: