mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-11-20 04:26:53 +00:00
Chore: share one combine format key between OCR runs and validation
The run and the serializer each built the key and could drift. Both now use field_key from zone_ocr.
This commit is contained in:
1 parent
ff6b5f3820
commit
a9ad9d9d65
2 files changed
+45
-25
No files matched your search
@@ -99,6 +99,7 @@ from documents.validators import url_validator
|
||||
from documents.versioning import has_prefetched_effective_content
|
||||
from documents.versioning import sort_versions_newest_first
|
||||
from documents.zone_ocr import ZoneOcrReason
|
||||
from documents.zone_ocr import field_key
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from collections.abc import Iterable
|
||||
@@ -3915,17 +3916,6 @@ class OcrTemplateTestSerializer(serializers.Serializer):
|
||||
)
|
||||
|
||||
|
||||
def _zone_field_key(zone: dict[str, Any]) -> str:
|
||||
"""The combine_formats key of a zone: its custom field id or its target."""
|
||||
custom_field = zone.get("custom_field")
|
||||
if isinstance(custom_field, CustomField):
|
||||
custom_field = custom_field.pk
|
||||
target = zone.get("target", OcrTemplateZone.TargetType.CUSTOM_FIELD)
|
||||
if target == OcrTemplateZone.TargetType.CUSTOM_FIELD and custom_field:
|
||||
return str(custom_field)
|
||||
return target
|
||||
|
||||
|
||||
class OcrTemplateSerializer(MatchRuleSerializer, OwnedObjectSerializer):
|
||||
zones = OcrTemplateZoneSerializer(many=True, required=False)
|
||||
combine_formats = serializers.DictField(
|
||||
@@ -3984,12 +3974,25 @@ class OcrTemplateSerializer(MatchRuleSerializer, OwnedObjectSerializer):
|
||||
)
|
||||
if "combine_formats" in data:
|
||||
if "zones" in data:
|
||||
zones = data["zones"]
|
||||
keys = {
|
||||
field_key(
|
||||
OcrTemplateZone.TargetType(
|
||||
zone.get("target", OcrTemplateZone.TargetType.CUSTOM_FIELD),
|
||||
),
|
||||
zone["custom_field"].pk if zone.get("custom_field") else None,
|
||||
)
|
||||
for zone in data["zones"]
|
||||
}
|
||||
elif self.instance:
|
||||
zones = list(self.instance.zones.values("target", "custom_field"))
|
||||
keys = {
|
||||
field_key(OcrTemplateZone.TargetType(target), custom_field_id)
|
||||
for target, custom_field_id in self.instance.zones.values_list(
|
||||
"target",
|
||||
"custom_field",
|
||||
)
|
||||
}
|
||||
else:
|
||||
zones = []
|
||||
keys = {_zone_field_key(zone) for zone in zones}
|
||||
keys = set()
|
||||
unknown = sorted(set(data["combine_formats"]) - keys)
|
||||
if unknown:
|
||||
raise serializers.ValidationError(
|
||||
|
||||
+27
-10
@@ -17,6 +17,7 @@ from functools import lru_cache
|
||||
from functools import partial
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING
|
||||
from typing import NewType
|
||||
from typing import TypeAlias
|
||||
from typing import assert_never
|
||||
|
||||
@@ -92,6 +93,9 @@ MAX_PARSED_TEXT_LENGTH = 256
|
||||
# A converted zone value, as stored on a document or custom field instance.
|
||||
ZoneValue: TypeAlias = str | int | float | bool | date
|
||||
|
||||
# A key of OcrTemplate.combine_formats, naming the field a zone writes to.
|
||||
FieldKey = NewType("FieldKey", str)
|
||||
|
||||
|
||||
@dataclass(frozen=True, kw_only=True, slots=True)
|
||||
class ZoneExtraction:
|
||||
@@ -302,7 +306,10 @@ def _process_template(
|
||||
) -> list[ZoneOcrResult]:
|
||||
"""Extract and combine zone values before applying a template's updates."""
|
||||
# Each zone's result and its accepted text, by target field.
|
||||
grouped: dict[str, list[tuple[OcrTemplateZone, ZoneOcrResult, str | None]]] = {}
|
||||
grouped: dict[
|
||||
FieldKey,
|
||||
list[tuple[OcrTemplateZone, ZoneOcrResult, str | None]],
|
||||
] = {}
|
||||
results = []
|
||||
for zone in zones:
|
||||
page_idx = _resolve_page_idx(zone.page, document.page_count)
|
||||
@@ -332,13 +339,17 @@ def _process_template(
|
||||
reason = ZoneOcrReason.REGEX_MISMATCH
|
||||
result = _zone_result(template, zone, reason=reason)
|
||||
results.append(result)
|
||||
grouped.setdefault(_field_key(zone), []).append(
|
||||
key = field_key(
|
||||
OcrTemplateZone.TargetType(zone.target),
|
||||
zone.custom_field_id,
|
||||
)
|
||||
grouped.setdefault(key, []).append(
|
||||
(zone, result, extracted if reason is None else None),
|
||||
)
|
||||
|
||||
updates = []
|
||||
root_document = get_root_document(document)
|
||||
combine_formats = template.combine_formats or {}
|
||||
combine_formats: dict[FieldKey, str] = template.combine_formats or {}
|
||||
for key, field_zones in grouped.items():
|
||||
accepted = [result for _, result, _ in field_zones if result.reason is None]
|
||||
if not accepted:
|
||||
@@ -372,7 +383,7 @@ def _process_template(
|
||||
if update is None:
|
||||
result.reason = (
|
||||
ZoneOcrReason.UNASSIGNED
|
||||
if key == "custom_field"
|
||||
if key == UNASSIGNED_FIELD_KEY
|
||||
else ZoneOcrReason.INVALID_VALUE
|
||||
)
|
||||
_apply_field_updates(root_document, updates)
|
||||
@@ -393,12 +404,18 @@ def _asn_in_use(update: FieldUpdate, root_document: Document) -> bool:
|
||||
)
|
||||
|
||||
|
||||
def _field_key(zone: OcrTemplateZone) -> str:
|
||||
"""Use the same target keys as the editor's combination formats."""
|
||||
target = zone.target
|
||||
if target == OcrTemplateZone.TargetType.CUSTOM_FIELD and zone.custom_field_id:
|
||||
return str(zone.custom_field_id)
|
||||
return target
|
||||
def field_key(
|
||||
target: OcrTemplateZone.TargetType,
|
||||
custom_field_id: int | None,
|
||||
) -> FieldKey:
|
||||
"""The combine_formats key of a zone: its custom field id or its target."""
|
||||
if target == OcrTemplateZone.TargetType.CUSTOM_FIELD and custom_field_id:
|
||||
return FieldKey(str(custom_field_id))
|
||||
return FieldKey(target)
|
||||
|
||||
|
||||
# The key shared by custom field zones without a custom field.
|
||||
UNASSIGNED_FIELD_KEY = field_key(OcrTemplateZone.TargetType.CUSTOM_FIELD, None)
|
||||
|
||||
|
||||
def _combine_field_value(
|
||||
|
||||
Reference in new issue
Block a user