From e1429f0316a9965387a4513cebd38c7ffbfa1475 Mon Sep 17 00:00:00 2001 From: robinvandermolen Date: Thu, 12 Dec 2024 17:13:01 +0100 Subject: [PATCH] :boom: [#2177] Handling geojson in plugins --- src/openforms/formio/constants.py | 7 +++++++ .../contrib/objects_api/submission_registration.py | 9 +++++++-- .../registrations/contrib/stuf_zds/plugin.py | 11 +++++++++-- .../registrations/contrib/zgw_apis/plugin.py | 9 +++++++-- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/openforms/formio/constants.py b/src/openforms/formio/constants.py index 69e21071ce..6b3d6196e9 100644 --- a/src/openforms/formio/constants.py +++ b/src/openforms/formio/constants.py @@ -1,3 +1,5 @@ +from django.db import models + COMPONENT_DATATYPES = { "date": "date", "time": "time", @@ -11,3 +13,8 @@ "editgrid": "array", "datetime": "datetime", } + +class GeoJsonGeometryTypes(models.TextChoices): + point = "Point", "Point" + polygon = "Polygon", "Polygon" + line_string = "LineString", "LineString" diff --git a/src/openforms/registrations/contrib/objects_api/submission_registration.py b/src/openforms/registrations/contrib/objects_api/submission_registration.py index 6c3a31d895..5734afe765 100644 --- a/src/openforms/registrations/contrib/objects_api/submission_registration.py +++ b/src/openforms/registrations/contrib/objects_api/submission_registration.py @@ -33,6 +33,7 @@ create_csv_document, create_report_document, ) +from openforms.formio.constants import GeoJsonGeometryTypes from openforms.formio.service import FormioData from openforms.formio.typing import Component from openforms.registrations.exceptions import RegistrationFailed @@ -70,9 +71,13 @@ def _point_coordinate(value: Any) -> dict[str, Any] | object: - if not isinstance(value, list) or len(value) != 2: + if not value or not (geometry := value.get("geometry")): return SKIP - return {"type": "Point", "coordinates": [value[0], value[1]]} + + if geometry.get("type", None) not in GeoJsonGeometryTypes or len(geometry.get("coordinates", [])) != 2: + return SKIP + + return geometry def _resolve_documenttype( diff --git a/src/openforms/registrations/contrib/stuf_zds/plugin.py b/src/openforms/registrations/contrib/stuf_zds/plugin.py index 11077daf60..4362e8fffd 100644 --- a/src/openforms/registrations/contrib/stuf_zds/plugin.py +++ b/src/openforms/registrations/contrib/stuf_zds/plugin.py @@ -11,6 +11,7 @@ from json_logic.typing import Primitive +from openforms.formio.constants import GeoJsonGeometryTypes from openforms.plugins.exceptions import InvalidPluginConfiguration from openforms.registrations.base import BasePlugin, PreRegistrationResult from openforms.registrations.constants import ( @@ -112,9 +113,15 @@ def _safe_int(num): def _point_coordinate(value): - if not value or not isinstance(value, list) or len(value) != 2: + if not value or not (geometry := value.get("geometry")): return SKIP - return {"lat": value[0], "lng": value[1]} + + if geometry.get("type", None) not in GeoJsonGeometryTypes or len(geometry.get("coordinates", [])) != 2: + return SKIP + + # GeoJson geometry uses [lng, lat] format for the coordinates + coordinates = geometry.get("coordinates") + return {"lat": coordinates[1], "lng": coordinates[0]} def _gender_choices(value): diff --git a/src/openforms/registrations/contrib/zgw_apis/plugin.py b/src/openforms/registrations/contrib/zgw_apis/plugin.py index 27f02b905a..f42db13ec6 100644 --- a/src/openforms/registrations/contrib/zgw_apis/plugin.py +++ b/src/openforms/registrations/contrib/zgw_apis/plugin.py @@ -26,6 +26,7 @@ create_attachment_document, create_report_document, ) +from openforms.formio.constants import GeoJsonGeometryTypes from openforms.submissions.mapping import SKIP, FieldConf, apply_data_mapping from openforms.submissions.models import Submission, SubmissionReport from openforms.utils.date import datetime_in_amsterdam @@ -76,9 +77,13 @@ def get_property_mappings_from_submission( def _point_coordinate(value): - if not value or not isinstance(value, list) or len(value) != 2: + if not value or not (geometry := value.get("geometry")): return SKIP - return {"type": "Point", "coordinates": [value[0], value[1]]} + + if geometry.get("type", None) not in GeoJsonGeometryTypes or len(geometry.get("coordinates", [])) != 2: + return SKIP + + return geometry def _gender_choices(value):