Skip to content

Commit

Permalink
Add plone.restapi deserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
laulaz committed Oct 18, 2021
1 parent 0544562 commit c497731
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Changelog
2.2.4 (unreleased)
------------------

- Add plone.restapi deserializer (if plone.restapi is installed).
[laulaz]

- Allow to set default geolocation on new contents (via bool field in config).
If not checked, the defaut geolocation is only used to center map.
Also, the geolocation map will not show on an object if no geolocation was defined.
Expand Down
4 changes: 4 additions & 0 deletions plone/formwidget/geolocation/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
zcml:condition="installed plone.restapi"
factory=".serializer.GeolocationSerializer" />

<adapter
zcml:condition="installed plone.restapi"
factory=".deserializer.GeolocationDeserializer" />

<include package="z3c.form" file="meta.zcml" />

<z3c:widgetTemplate
Expand Down
22 changes: 22 additions & 0 deletions plone/formwidget/geolocation/deserializer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
from plone.dexterity.interfaces import IDexterityContent
from plone.formwidget.geolocation.geolocation import Geolocation
from plone.formwidget.geolocation.interfaces import IGeolocationField
from plone.restapi.interfaces import IFieldDeserializer
from plone.restapi.deserializer.dxfields import DefaultFieldDeserializer
from zope.component import adapter
from zope.interface import Interface
from zope.interface import implementer


@adapter(IGeolocationField, IDexterityContent, Interface)
@implementer(IFieldDeserializer)
class GeolocationDeserializer(DefaultFieldDeserializer):
def __call__(self, value):
if not isinstance(value, dict):
raise ValueError(u"Invalid geolocation dict: {}".format(value))

if "latitude" not in value or "longitude" not in value:
raise ValueError(u"Geolocation dict must have latitude & longitude keys")

return Geolocation(value["latitude"], value["longitude"])

0 comments on commit c497731

Please sign in to comment.