Open zopyx opened 6 years ago
Best approach/workaround so far is this:
from zope.interface import implementer
import datetime
import dateutil.parser
from zope.component import adapter
from plone.restapi.interfaces import IFieldDeserializer
from plone.dexterity.interfaces import IDexterityContent
from zope.publisher.interfaces.browser import IBrowserRequest
from plone.restapi.deserializer.dxfields import DefaultFieldDeserializer
from collective.z3cform.datagridfield.interfaces import IDataGridField
from zope.schema.interfaces import IList
@implementer(IFieldDeserializer)
@adapter(IList, IDexterityContent, IBrowserRequest)
class GridDeserializer(DefaultFieldDeserializer):
def __call__(self, value):
result = list()
for d in value:
d = d.copy()
if 'date_from' in d:
d['date_from'] = dateutil.parser.parse(d['date_from']).date()
result.append(d)
return result
This is indeed a missing piece. Might be related: https://github.com/plone/plone.restapi/pull/177
I would love to see this in plone.restapi core.
Related discussion:
https://community.plone.org/t/plone-restapi-serialization-deserialization-of-collective-z3cform-datagridfield-fields/5984/4
I have a z3cform.datagridfield with the following field
and a related row definition
It is obvious that we can not send python date instances as JSON over Plone REST api.
The first draft of a deserializer looks like this
Note that this serializer is for
IList
and very generic and called for basically all IList fields in dependent of the content type. The serializer also has access to thevalue
and no other context information in order to proceed with a more content-type specific deserialization.