From 6d669ed68e2622287affaebbc7ee7f75bf581531 Mon Sep 17 00:00:00 2001 From: Andrea Cecchi Date: Wed, 28 Aug 2024 14:37:53 +0200 Subject: [PATCH] add adapter for datagrid fields --- .../contenttypes/restapi/configure.zcml | 1 + .../contenttypes/restapi/types/__init__.py | 0 .../contenttypes/restapi/types/adapters.py | 56 +++++++++++++++++++ .../contenttypes/restapi/types/configure.zcml | 6 ++ 4 files changed, 63 insertions(+) create mode 100644 src/iosanita/contenttypes/restapi/types/__init__.py create mode 100644 src/iosanita/contenttypes/restapi/types/adapters.py create mode 100644 src/iosanita/contenttypes/restapi/types/configure.zcml diff --git a/src/iosanita/contenttypes/restapi/configure.zcml b/src/iosanita/contenttypes/restapi/configure.zcml index a9558d0..70fa028 100644 --- a/src/iosanita/contenttypes/restapi/configure.zcml +++ b/src/iosanita/contenttypes/restapi/configure.zcml @@ -10,6 +10,7 @@ + diff --git a/src/iosanita/contenttypes/restapi/types/__init__.py b/src/iosanita/contenttypes/restapi/types/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/iosanita/contenttypes/restapi/types/adapters.py b/src/iosanita/contenttypes/restapi/types/adapters.py new file mode 100644 index 0000000..809ebf6 --- /dev/null +++ b/src/iosanita/contenttypes/restapi/types/adapters.py @@ -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 diff --git a/src/iosanita/contenttypes/restapi/types/configure.zcml b/src/iosanita/contenttypes/restapi/types/configure.zcml new file mode 100644 index 0000000..7e10ad6 --- /dev/null +++ b/src/iosanita/contenttypes/restapi/types/configure.zcml @@ -0,0 +1,6 @@ + + + + +