diff --git a/src/documents/serialisers.py b/src/documents/serialisers.py index 4e914e1da0..337cb164f8 100644 --- a/src/documents/serialisers.py +++ b/src/documents/serialisers.py @@ -3855,6 +3855,17 @@ 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(MatchingModelSerializer, OwnedObjectSerializer): zones = OcrTemplateZoneSerializer(many=True, required=False) combine_formats = serializers.DictField( @@ -3914,6 +3925,31 @@ class OcrTemplateSerializer(MatchingModelSerializer, OwnedObjectSerializer): def validate(self, data): data = super().validate(data) + if "zones" in data: + names = [zone["name"] for zone in data["zones"]] + duplicates = sorted({name for name in names if names.count(name) > 1}) + if duplicates: + # Combine format tokens refer to zones by name. + raise serializers.ValidationError( + {"zones": f"Zone names must be unique: {', '.join(duplicates)}"}, + ) + if "combine_formats" in data: + if "zones" in data: + zones = data["zones"] + elif self.instance: + zones = list(self.instance.zones.values("target", "custom_field")) + else: + zones = [] + keys = {_zone_field_key(zone) for zone in zones} + unknown = sorted(set(data["combine_formats"]) - keys) + if unknown: + raise serializers.ValidationError( + { + "combine_formats": ( + "Keys must match the field of a zone: " + ", ".join(unknown) + ), + }, + ) zones = data.get("zones", []) if ( self.instance diff --git a/src/documents/tests/test_api_ocr_templates.py b/src/documents/tests/test_api_ocr_templates.py index 89d9046e7d..54f6848cd4 100644 --- a/src/documents/tests/test_api_ocr_templates.py +++ b/src/documents/tests/test_api_ocr_templates.py @@ -1214,12 +1214,121 @@ class TestOcrTemplateValidation: """ response = admin_client.post( ENDPOINT, - template_payload(combine_formats={"title": " {A} - {B} "}), + template_payload( + zones=[ + zone_payload(None, name="A", target="title"), + zone_payload(None, name="B", target="title"), + ], + combine_formats={"title": " {A} - {B} "}, + ), format="json", ) assert response.status_code == status.HTTP_201_CREATED assert response.json()["combine_formats"] == {"title": " {A} - {B} "} + def test_zone_names_must_be_unique( + self, + admin_client: APIClient, + text_zone_payload: Callable[..., dict[str, Any]], + ) -> None: + """ + GIVEN: + - Two zones with the same name + WHEN: + - Create is attempted + THEN: + - 400 is returned naming the duplicate + """ + response = admin_client.post( + ENDPOINT, + template_payload( + zones=[ + text_zone_payload(name="Total"), + text_zone_payload(name="Total"), + ], + ), + format="json", + ) + + assert response.status_code == status.HTTP_400_BAD_REQUEST + assert "Total" in str(response.json()["zones"]) + + @pytest.mark.parametrize( + ("keys", "expected"), + [ + pytest.param(["title"], status.HTTP_201_CREATED, id="title-zone"), + pytest.param(["field"], status.HTTP_201_CREATED, id="custom-field-zone"), + pytest.param(["asn"], status.HTTP_400_BAD_REQUEST, id="no-asn-zone"), + pytest.param(["999"], status.HTTP_400_BAD_REQUEST, id="unknown-field"), + ], + ) + def test_combine_format_keys_must_be_zone_targets( + self, + admin_client: APIClient, + text_field: CustomField, + keys: list[str], + expected: int, + ) -> None: + """ + GIVEN: + - A template with a title zone and a custom field zone, and a + combine format keyed by a target that may or may not be used + WHEN: + - Create is attempted + THEN: + - Keys of the zones' targets are accepted and others are rejected + on combine_formats + """ + formats = { + (str(text_field.pk) if key == "field" else key): "{Title}" for key in keys + } + + response = admin_client.post( + ENDPOINT, + template_payload( + zones=[ + zone_payload(None, name="Title", target="title"), + zone_payload(text_field.pk, name="Number"), + ], + combine_formats=formats, + ), + format="json", + ) + + assert response.status_code == expected + if expected == status.HTTP_400_BAD_REQUEST: + assert "combine_formats" in response.json() + + def test_combine_formats_patch_checks_saved_zones( + self, + admin_client: APIClient, + ) -> None: + """ + GIVEN: + - A saved template with only a title zone + WHEN: + - combine_formats is patched without zones, keyed by title and then + by asn + THEN: + - The title key is accepted and the asn key is rejected + """ + template = OcrTemplateFactory(match="Northwind") + OcrTemplateZoneFactory(template=template, target="title") + + response = admin_client.patch( + detail_url(template.pk), + {"combine_formats": {"title": "{A}"}}, + format="json", + ) + assert response.status_code == status.HTTP_200_OK + + response = admin_client.patch( + detail_url(template.pk), + {"combine_formats": {"asn": "{A}"}}, + format="json", + ) + assert response.status_code == status.HTTP_400_BAD_REQUEST + @pytest.mark.django_db @pytest.mark.usefixtures("paperless_dirs")