From 6ae8a2fb8f767c815199781157506f986a96a5c6 Mon Sep 17 00:00:00 2001 From: Andrea Cecchi Date: Fri, 26 Jul 2024 10:24:11 +0200 Subject: [PATCH] add missing deserializer --- .../contenttypes/restapi/configure.zcml | 1 + .../restapi/deserializers/__init__.py | 0 .../restapi/deserializers/configure.zcml | 5 ++++ .../restapi/deserializers/dxfields.py | 29 +++++++++++++++++++ 4 files changed, 35 insertions(+) create mode 100644 src/iosanita/contenttypes/restapi/deserializers/__init__.py create mode 100644 src/iosanita/contenttypes/restapi/deserializers/configure.zcml create mode 100644 src/iosanita/contenttypes/restapi/deserializers/dxfields.py diff --git a/src/iosanita/contenttypes/restapi/configure.zcml b/src/iosanita/contenttypes/restapi/configure.zcml index bfe84b9..a9558d0 100644 --- a/src/iosanita/contenttypes/restapi/configure.zcml +++ b/src/iosanita/contenttypes/restapi/configure.zcml @@ -7,6 +7,7 @@ package="plone.restapi" file="configure.zcml" /> + diff --git a/src/iosanita/contenttypes/restapi/deserializers/__init__.py b/src/iosanita/contenttypes/restapi/deserializers/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/iosanita/contenttypes/restapi/deserializers/configure.zcml b/src/iosanita/contenttypes/restapi/deserializers/configure.zcml new file mode 100644 index 0000000..659642d --- /dev/null +++ b/src/iosanita/contenttypes/restapi/deserializers/configure.zcml @@ -0,0 +1,5 @@ + + + + diff --git a/src/iosanita/contenttypes/restapi/deserializers/dxfields.py b/src/iosanita/contenttypes/restapi/deserializers/dxfields.py new file mode 100644 index 0000000..3046b2f --- /dev/null +++ b/src/iosanita/contenttypes/restapi/deserializers/dxfields.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +from iosanita.contenttypes import _ +from iosanita.contenttypes.interfaces import IIosanitaContenttypesLayer +from plone.dexterity.interfaces import IDexterityContent +from plone.formwidget.geolocation.geolocation import Geolocation +from plone.formwidget.geolocation.interfaces import IGeolocationField +from plone.restapi.deserializer.dxfields import DefaultFieldDeserializer +from plone.restapi.interfaces import IFieldDeserializer +from zope.component import adapter +from zope.i18n import translate +from zope.interface import implementer + + +@implementer(IFieldDeserializer) +@adapter(IGeolocationField, IDexterityContent, IIosanitaContenttypesLayer) +class GeolocationFieldDeserializer(DefaultFieldDeserializer): + def __call__(self, value): + if "latitude" not in value or "longitude" not in value: + raise ValueError( + translate( + _( + "geolocation_field_validator_label", + default="Invalid geolocation data: ${value}. Provide latitude and longitude coordinates.", # noqa + mapping={"value": value}, + ), + context=self.request, + ) + ) + return Geolocation(latitude=value["latitude"], longitude=value["longitude"])