-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
5 changes: 5 additions & 0 deletions
5
src/iosanita/contenttypes/restapi/deserializers/configure.zcml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<configure xmlns="http://namespaces.zope.org/zope" | ||
xmlns:zcml="http://namespaces.zope.org/zcml"> | ||
|
||
<adapter factory=".dxfields.GeolocationFieldDeserializer" /> | ||
</configure> |
29 changes: 29 additions & 0 deletions
29
src/iosanita/contenttypes/restapi/deserializers/dxfields.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"]) |