Skip to content

Commit

Permalink
Checkpoint - With BlocksObjectWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
sneridagh committed Nov 14, 2024
1 parent ea29a41 commit 85259c0
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 0 deletions.
48 changes: 48 additions & 0 deletions backend/src/kitconcept/intranet/behaviors/theming.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
from plone.autoform import directives
from plone.autoform.interfaces import IFormFieldProvider
from plone.namedfile.field import NamedBlobImage
from plone.schema import JSONField
from plone.supermodel import model
from zope.interface import provider
from zope.schema import TextLine
from zope.schema.vocabulary import SimpleTerm
from zope.schema.vocabulary import SimpleVocabulary

import json


messages = {
Expand All @@ -28,6 +33,29 @@
},
}

BLOCKS_SCHEMA_DEFAULT_VALUE = {
"blocks": {},
"blocks_layout": {},
}

BLOCKS_SCHEMA = json.dumps(
{
"type": "object",
"properties": {
"blocks": {"type": "object"},
"blocks_layout": {"type": "object"},
},
}
)

FONT_VOCABULARY = SimpleVocabulary(
[
SimpleTerm(value="default", title=_("Default FZJ font")),
SimpleTerm(value="impact-arialNarrow", title=_("Impact / Arial Narrow")),
SimpleTerm(value="georgia-lucidaSans", title=_("Georgia / Lucida Sans")),
]
)


@provider(IFormFieldProvider)
class ITheming(model.Schema):
Expand All @@ -46,6 +74,8 @@ class ITheming(model.Schema):
# "primary_color", # Not used in PiK
"secondary_foreground_color",
"secondary_color",
"footer_logos",
"footer_links",
],
)

Expand Down Expand Up @@ -107,3 +137,21 @@ class ITheming(model.Schema):
),
required=False,
)

directives.widget("footer_logos", frontendOptions={"widget": "footerLogos"})
footer_logos = JSONField(
title=_("Footer logos"),
schema=BLOCKS_SCHEMA,
default=BLOCKS_SCHEMA_DEFAULT_VALUE,
required=False,
widget="",
)

directives.widget("footer_links", frontendOptions={"widget": "footerLinks"})
footer_links = JSONField(
title=_("Footer links"),
schema=BLOCKS_SCHEMA,
default=BLOCKS_SCHEMA_DEFAULT_VALUE,
required=False,
widget="",
)
2 changes: 2 additions & 0 deletions backend/src/kitconcept/intranet/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

<include package=".behaviors" />
<include package=".services" />
<include package=".serializers" />
<include package=".deserializers" />

<browser:page
name="images"
Expand Down
33 changes: 33 additions & 0 deletions backend/src/kitconcept/intranet/deserializers/blocks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from kitconcept.intranet.interfaces import IBrowserLayer
from plone.restapi.behaviors import IBlocks
from plone.restapi.blocks import iter_block_transform_handlers
from plone.restapi.blocks import visit_blocks
from plone.restapi.deserializer.dxfields import DefaultFieldDeserializer
from plone.restapi.interfaces import IBlockFieldDeserializationTransformer
from plone.restapi.interfaces import IFieldDeserializer
from plone.schema import IJSONField
from zope.component import adapter
from zope.interface import implementer


@implementer(IFieldDeserializer)
@adapter(IJSONField, IBlocks, IBrowserLayer)
class BlocksJSONFieldDeserializer(DefaultFieldDeserializer):
def __call__(self, value):
value = super().__call__(value)

if "blocks" in value:
blocksValue = value["blocks"]
else:
blocksValue = value

if self.field.getName() == "blocks" or "blocks" in value:
for block in visit_blocks(self.context, blocksValue):
new_block = block.copy()
for handler in iter_block_transform_handlers(
self.context, block, IBlockFieldDeserializationTransformer
):
new_block = handler(new_block)
block.clear()
block.update(new_block)
return value
10 changes: 10 additions & 0 deletions backend/src/kitconcept/intranet/deserializers/configure.zcml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:i18n="http://namespaces.zope.org/i18n"
xmlns:plone="http://namespaces.plone.org/plone"
i18n_domain="kitconcept.intranet"
>

<adapter factory=".blocks.BlocksJSONFieldDeserializer" />

</configure>
36 changes: 36 additions & 0 deletions backend/src/kitconcept/intranet/serializers/blocks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from kitconcept.intranet.interfaces import IBrowserLayer
from plone.restapi.behaviors import IBlocks
from plone.restapi.blocks import iter_block_transform_handlers
from plone.restapi.blocks import visit_blocks
from plone.restapi.interfaces import IBlockFieldSerializationTransformer
from plone.restapi.interfaces import IFieldSerializer
from plone.restapi.serializer.converters import json_compatible
from plone.restapi.serializer.dxfields import DefaultFieldSerializer
from plone.schema import IJSONField
from zope.component import adapter
from zope.interface import implementer

import copy


@adapter(IJSONField, IBlocks, IBrowserLayer)
@implementer(IFieldSerializer)
class BlocksJSONFieldSerializer(DefaultFieldSerializer):
def __call__(self):
value = copy.deepcopy(self.get_value())

if "blocks" in value:
blocksValue = value["blocks"]
else:
blocksValue = value

if self.field.getName() == "blocks" or "blocks" in value:
for block in visit_blocks(self.context, blocksValue):
new_block = block.copy()
for handler in iter_block_transform_handlers(
self.context, block, IBlockFieldSerializationTransformer
):
new_block = handler(new_block)
block.clear()
block.update(new_block)
return json_compatible(value)
10 changes: 10 additions & 0 deletions backend/src/kitconcept/intranet/serializers/configure.zcml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:i18n="http://namespaces.zope.org/i18n"
xmlns:plone="http://namespaces.plone.org/plone"
i18n_domain="kitconcept.intranet"
>

<adapter factory=".blocks.BlocksJSONFieldSerializer" />

</configure>
2 changes: 2 additions & 0 deletions backend/src/kitconcept/intranet/services/navroot.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"primary_foreground_color",
"secondary_color",
"secondary_foreground_color",
"footer_logos",
"footer_links",
]


Expand Down

0 comments on commit 85259c0

Please sign in to comment.