|
| 1 | +from plone.restapi.behaviors import IBlocks |
| 2 | +from plone.restapi.interfaces import IBlockFieldSerializationTransformer |
| 3 | +from zope.component import adapter |
| 4 | +from zope.interface import implementer |
| 5 | +from zope.publisher.interfaces.browser import IBrowserRequest |
| 6 | +from plone.restapi.types.utils import get_info_for_type |
| 7 | +from zope.component import queryMultiAdapter |
| 8 | +from plone import api |
| 9 | + |
| 10 | + |
| 11 | +@implementer(IBlockFieldSerializationTransformer) |
| 12 | +@adapter(IBlocks, IBrowserRequest) |
| 13 | +class SearchTableVariationBlockSerialize: |
| 14 | + order = 100 |
| 15 | + block_type = "search" |
| 16 | + |
| 17 | + def __init__(self, context, request): |
| 18 | + self.context = context |
| 19 | + self.request = request |
| 20 | + |
| 21 | + def _get_schema(self, portal_type): |
| 22 | + dtool = queryMultiAdapter( |
| 23 | + (api.portal.get(), self.request), name="dexterity-types" |
| 24 | + ) |
| 25 | + try: |
| 26 | + dtype = dtool.publishTraverse(self.request, portal_type) |
| 27 | + except KeyError: |
| 28 | + # TODO: log missing portal_type |
| 29 | + return None |
| 30 | + schema = get_info_for_type(dtype, self.request, portal_type) |
| 31 | + return schema |
| 32 | + |
| 33 | + def __call__(self, value): |
| 34 | + schemas = {} |
| 35 | + if value.get("listingBodyTemplate") == "table": |
| 36 | + for col in value.get("columns") or []: |
| 37 | + if not col.get("ct") or not col.get("field"): |
| 38 | + continue |
| 39 | + if col["ct"] not in schemas: |
| 40 | + # get schema as plone.restapi /@types/Schema |
| 41 | + schemas[col["ct"]] = self._get_schema(col["ct"]) |
| 42 | + if not schemas[col["ct"]]: |
| 43 | + continue |
| 44 | + schema = schemas[col["ct"]] |
| 45 | + if "properties" in schema and col["field"] in schema["properties"]: |
| 46 | + # TODO: ma servono veramente tutte le info o solo una parte ? |
| 47 | + col["field_properties"] = schema["properties"][col["field"]] |
| 48 | + return value |
| 49 | + |
0 commit comments