Skip to content

Commit

Permalink
add adapter for datagrid fields
Browse files Browse the repository at this point in the history
  • Loading branch information
cekk committed Aug 28, 2024
1 parent 2d7e5da commit 6d669ed
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/iosanita/contenttypes/restapi/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<include package=".deserializers" />
<include package=".services" />
<include package=".serializers" />
<include package=".types" />

<adapter factory=".converters.geolocation_converter" />

Expand Down
Empty file.
56 changes: 56 additions & 0 deletions src/iosanita/contenttypes/restapi/types/adapters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# -*- coding: utf-8 -*-
from collective.z3cform.datagridfield.interfaces import IRow
from plone.restapi.types.adapters import ObjectJsonSchemaProvider
from plone.restapi.types.interfaces import IJsonSchemaProvider
from plone.restapi.types.utils import get_fieldsets
from plone.restapi.types.utils import get_jsonschema_properties
from plone.restapi.types.utils import iter_fields
from zope.component import adapter
from zope.interface import implementer
from zope.interface import Interface


@adapter(IRow, Interface, Interface)
@implementer(IJsonSchemaProvider)
class DataGridRowJsonSchemaProvider(ObjectJsonSchemaProvider):
def __init__(self, field, context, request):
super().__init__(field, context, request)
self.fieldsets = get_fieldsets(context, request, self.field.schema)

def get_factory(self):
return "DataGridField Row"

def get_properties(self):
if self.prefix:
prefix = ".".join([self.prefix, self.field.__name__])
else:
prefix = self.field.__name__
return get_jsonschema_properties(
self.context, self.request, self.fieldsets, prefix
)

def additional(self):
info = super().additional()
properties = self.get_properties()
required = []
for field in iter_fields(self.fieldsets):
name = field.field.getName()

# Determine required fields
if field.field.required:
required.append(name)

# Include field modes
if field.mode:
properties[name]["mode"] = field.mode

info["fieldsets"] = [
{
"id": "default",
"title": "Default",
"fields": [x for x in properties.keys()],
},
]
info["required"] = required
info["properties"] = properties
return info
6 changes: 6 additions & 0 deletions src/iosanita/contenttypes/restapi/types/configure.zcml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<configure xmlns="http://namespaces.zope.org/zope"
xmlns:zcml="http://namespaces.zope.org/zcml" i18n_domain="plone.restapi">

<adapter factory=".adapters.DataGridRowJsonSchemaProvider" />

</configure>

0 comments on commit 6d669ed

Please sign in to comment.