Skip to content

Commit

Permalink
add serializer/deserializer for controlpanel
Browse files Browse the repository at this point in the history
  • Loading branch information
cekk committed Sep 20, 2024
1 parent ffad43c commit 1d8f1a6
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/iosanita/policy/restapi/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
>

<include package="plone.restapi" />
<include package=".deserializer" />
<include package=".serializer" />
<include package=".services" />



<!-- enable controlpanel on plone.restapi -->
<adapter
factory=".controlpanel.IoSanitaSettingsControlpanel"
Expand Down
Empty file.
10 changes: 10 additions & 0 deletions src/iosanita/policy/restapi/deserializer/configure.zcml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:cache="http://namespaces.zope.org/cache"
xmlns:plone="http://namespaces.plone.org/plone"
xmlns:zcml="http://namespaces.zope.org/zcml"
>

<adapter factory=".controlpanel.IoSanitaControlpanelDeserializeFromJson" />

</configure>
32 changes: 32 additions & 0 deletions src/iosanita/policy/restapi/deserializer/controlpanel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from plone.restapi.deserializer import json_body
from plone.restapi.deserializer.controlpanels import ControlpanelDeserializeFromJson
from plone.restapi.interfaces import IDeserializeFromJson
from zope.component import adapter
from zope.interface import implementer
from iosanita.policy.interfaces import IIoSanitaSettingsControlpanel


@implementer(IDeserializeFromJson)
@adapter(IIoSanitaSettingsControlpanel)
class IoSanitaControlpanelDeserializeFromJson(ControlpanelDeserializeFromJson):
def __call__(self):
"""
Convert json data into a string
"""
super().__call__()

req = json_body(self.controlpanel.request)
proxy = self.registry.forInterface(self.schema, prefix=self.schema_prefix)

search_sections = req.get("search_sections", [])
for section in search_sections:
# simplify stored data
for item in section.get("items", []):
fixed_urls = [
{"UID": x.get("UID", "")}
for x in item.get("linkUrl", [])
if x.get("UID", "")
]
item["linkUrl"] = fixed_urls

setattr(proxy, "search_sections", search_sections)
Empty file.
10 changes: 10 additions & 0 deletions src/iosanita/policy/restapi/serializer/configure.zcml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:cache="http://namespaces.zope.org/cache"
xmlns:plone="http://namespaces.plone.org/plone"
xmlns:zcml="http://namespaces.zope.org/zcml"
>

<adapter factory=".controlpanel.IoSanitaControlpanelSerializeToJson" />

</configure>
39 changes: 39 additions & 0 deletions src/iosanita/policy/restapi/serializer/controlpanel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from plone.restapi.serializer.controlpanels import ControlpanelSerializeToJson
from plone.restapi.interfaces import ISerializeToJson
from zope.component import adapter
from zope.interface import implementer
from iosanita.policy.interfaces import IIoSanitaSettingsControlpanel
from plone import api
from plone.restapi.interfaces import ISerializeToJsonSummary
from zope.component import getMultiAdapter

import json


@implementer(ISerializeToJson)
@adapter(IIoSanitaSettingsControlpanel)
class IoSanitaControlpanelSerializeToJson(ControlpanelSerializeToJson):
def __call__(self):
"""
Convert json data into a string
"""
json_data = super().__call__()
search_sections = json.loads(json_data["data"].get("search_sections", "{}"))

for section in search_sections:
# expand stored data
for item in section.get("items", []):
expanded_links = []
for link_url in item.get("linkUrl", []):
section = api.content.get(UID=link_url.get("UID", ""))
if not section:
continue
item_infos = getMultiAdapter(
(section, self.request),
ISerializeToJsonSummary,
)()
expanded_links.append(item_infos)
item["linkUrl"] = expanded_links

json_data["data"]["search_sections"] = json.dumps(search_sections)
return json_data
4 changes: 4 additions & 0 deletions src/iosanita/policy/restapi/services/search_filters/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def get_section_data(self, field_id):
items = []
for section_settings in setting.get("items") or []:
for uid in section_settings.get("linkUrl") or []:
if isinstance(uid, dict):
uid = uid.get("UID", "")
if not uid:
continue
try:
section = api.content.get(UID=uid)
except Unauthorized:
Expand Down

0 comments on commit 1d8f1a6

Please sign in to comment.