From b58cddfcd38a17df8ee905c0456704c8f27e8d92 Mon Sep 17 00:00:00 2001 From: robinvandermolen Date: Tue, 17 Dec 2024 14:13:30 +0100 Subject: [PATCH] :label: [#2173] Add MapComponent type definition --- src/openforms/formio/components/custom.py | 16 +++++++++++----- src/openforms/formio/formatters/custom.py | 4 ++-- src/openforms/formio/typing/__init__.py | 3 ++- src/openforms/formio/typing/custom.py | 10 ++++++++++ src/openforms/formio/typing/map.py | 6 ++++++ 5 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 src/openforms/formio/typing/map.py diff --git a/src/openforms/formio/components/custom.py b/src/openforms/formio/components/custom.py index 7f78b6fc19..e36f585677 100644 --- a/src/openforms/formio/components/custom.py +++ b/src/openforms/formio/components/custom.py @@ -31,7 +31,13 @@ ) from ..formatters.formio import DefaultFormatter, TextFieldFormatter from ..registry import BasePlugin, register -from ..typing import AddressNLComponent, Component, DateComponent, DatetimeComponent +from ..typing import ( + AddressNLComponent, + Component, + DateComponent, + DatetimeComponent, + MapComponent, +) from ..utils import conform_to_mask from .np_family_members.constants import FamilyMembersDataAPIChoices from .np_family_members.haal_centraal import get_np_family_members_haal_centraal @@ -186,11 +192,11 @@ def build_serializer_field( @register("map") -class Map(BasePlugin[Component]): +class Map(BasePlugin[MapComponent]): formatter = MapFormatter def mutate_config_dynamically( - self, component, submission: Submission, data: DataMapping + self, component: MapComponent, submission: Submission, data: DataMapping ) -> None: if (identifier := component.get("tileLayerIdentifier")) is not None: tile_layer = MapTileLayer.objects.filter(identifier=identifier).first() @@ -199,7 +205,7 @@ def mutate_config_dynamically( component["tileLayerUrl"] = tile_layer.url @staticmethod - def rewrite_for_request(component, request: Request): + def rewrite_for_request(component: MapComponent, request: Request): if component.get("useConfigDefaultMapSettings", False): config = GlobalConfiguration.get_solo() component["defaultZoom"] = config.form_map_default_zoom_level @@ -207,7 +213,7 @@ def rewrite_for_request(component, request: Request): component["initialCenter"]["lat"] = config.form_map_default_latitude component["initialCenter"]["lng"] = config.form_map_default_longitude - def build_serializer_field(self, component: Component) -> serializers.ListField: + def build_serializer_field(self, component: MapComponent) -> serializers.ListField: validate = component.get("validate", {}) required = validate.get("required", False) base = serializers.FloatField( diff --git a/src/openforms/formio/formatters/custom.py b/src/openforms/formio/formatters/custom.py index a76a4cec6d..6c87744fc9 100644 --- a/src/openforms/formio/formatters/custom.py +++ b/src/openforms/formio/formatters/custom.py @@ -6,7 +6,7 @@ from django.utils.html import format_html from django.utils.safestring import mark_safe -from ..typing import AddressNLComponent, Component +from ..typing import AddressNLComponent, Component, MapComponent from .base import FormatterBase @@ -22,7 +22,7 @@ def format(self, component: Component, value: str) -> str: class MapFormatter(FormatterBase): - def format(self, component: Component, value: list[float]) -> str: + def format(self, component: MapComponent, value: list[float]) -> str: # use a comma here since its a single data element return ", ".join((str(x) for x in value)) diff --git a/src/openforms/formio/typing/__init__.py b/src/openforms/formio/typing/__init__.py index a086a4a252..8283e31bee 100644 --- a/src/openforms/formio/typing/__init__.py +++ b/src/openforms/formio/typing/__init__.py @@ -7,7 +7,7 @@ """ from .base import Component, FormioConfiguration, OptionDict -from .custom import AddressNLComponent, DateComponent +from .custom import AddressNLComponent, DateComponent, MapComponent from .vanilla import ( Column, ColumnsComponent, @@ -43,5 +43,6 @@ # special "EditGridComponent", "AddressNLComponent", + "MapComponent", # deprecated ] diff --git a/src/openforms/formio/typing/custom.py b/src/openforms/formio/typing/custom.py index c1db889011..76792272da 100644 --- a/src/openforms/formio/typing/custom.py +++ b/src/openforms/formio/typing/custom.py @@ -2,6 +2,7 @@ from .base import Component from .dates import DatePickerConfig, DatePickerCustomOptions +from .map import MapInitialCenter class DateComponent(Component): @@ -11,3 +12,12 @@ class DateComponent(Component): class AddressNLComponent(Component): deriveAddress: bool + + +class MapComponent(Component): + useConfigDefaultMapSettings: bool + defaultZoom: NotRequired[int] + initialCenter: NotRequired[MapInitialCenter] + tileLayerIdentifier: NotRequired[str] + # The tileLayerUrl will be dynamically generated from the tileLayerIdentifier + tileLayerUrl: NotRequired[str] diff --git a/src/openforms/formio/typing/map.py b/src/openforms/formio/typing/map.py new file mode 100644 index 0000000000..cfc712f65d --- /dev/null +++ b/src/openforms/formio/typing/map.py @@ -0,0 +1,6 @@ +from typing import NotRequired, TypedDict + + +class MapInitialCenter(TypedDict): + lat: NotRequired[float] + lng: NotRequired[float]