Created
March 11, 2021 11:34
-
-
Save tiberiuichim/aa21bb3952e2011ba538212d56dc531c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SubformsTransformer(object): | |
order = -100 # this should to be executed as first as possible | |
block_type = None | |
iface = None | |
def __init__(self, context, request): | |
self.context = context | |
self.request = request | |
def _transform(self, blocks): | |
for id, block_value in blocks.items(): | |
block_type = block_value.get("@type", "") | |
handlers = [] | |
for h in subscribers( | |
(self.context, self.request), | |
self.iface | |
): | |
if h.block_type == block_type or h.block_type is None: | |
h.blockid = id | |
handlers.append(h) | |
for handler in sorted(handlers, key=lambda h: h.order): | |
block_value = handler(block_value) | |
blocks[id] = block_value | |
return blocks | |
def __call__(self, block_value): | |
if not isinstance(block_value, dict): | |
return block_value | |
if 'data' in block_value: | |
if isinstance(block_value['data'], dict): | |
if 'blocks' in block_value['data']: | |
block_value['data']['blocks'] = self._transform( | |
block_value['data']['blocks'] | |
) | |
if 'blocks' in block_value: | |
block_value['blocks'] = self._transform(block_value['blocks']) | |
return block_value | |
@implementer(IBlockFieldSerializationTransformer) | |
@adapter(Interface, IBrowserRequest) | |
class SubformsSerializationTransformer(SubformsTransformer): | |
iface = IBlockFieldSerializationTransformer | |
@implementer(IBlockFieldSerializationTransformer) | |
@adapter(Interface, IBrowserRequest) | |
class SubformsDeserializationTransformer(SubformsTransformer): | |
iface = IBlockFieldDeserializationTransformer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment