Skip to content

Commit

Permalink
update serializer for bando children
Browse files Browse the repository at this point in the history
  • Loading branch information
luca-bellenghi committed Oct 31, 2024
1 parent 22797d5 commit 38fa23f
Showing 1 changed file with 105 additions and 0 deletions.
105 changes: 105 additions & 0 deletions src/design/plone/contenttypes/browser/bando.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
"""
This is a customization for original view. in design.plone.contenttypes
we allow folder deepening to contain also Modulo CT and we need to
handle it properly
I know it would be better to refactor redturtle.bandi to make
retrieveContentsOfFolderDeepening smaller and have "mini" methods
to retrieve information. This will be done maybe in future
"""

from design.plone.contenttypes.behaviors.multi_file import IMultiFileSchema
from plone import api
from plone.restapi.interfaces import IFieldSerializer
from redturtle.bandi.browser.bando import BandoView as BaseBandoView
from redturtle.bandi.browser.bando import IBandoView
from zope.component import queryMultiAdapter
from zope.interface import implementer

try:
from plone.restapi.serializer.utils import uid_to_url
from plone.restapi.serializer.converters import json_compatible

HAS_PLONERESTAPI = True
except ImportError:
HAS_PLONERESTAPI = False


fields = [
"file_principale",
"formato_alternativo_1",
"formato_alternativo_2",
]


@implementer(IBandoView)
class BandoView(BaseBandoView):

def retrieveContentsOfFolderDeepening(self, path_dfolder):
"""Retrieves all objects contained in Folder Deppening"""

values = []
brains = self.context.portal_catalog(
path={"query": path_dfolder, "depth": 1},
sort_on="getObjPositionInParent",
)
siteid = api.portal.get().getId()
for brain in brains:
if not brain.getPath() == path_dfolder and not brain.exclude_from_nav:
effective = brain.effective
if effective.year() == 1969:
# content not yet published
effective = None
dictfields = dict(
title=brain.Title,
description=brain.Description,
url=brain.getURL(),
path=brain.getPath(),
effective=effective,
modified=brain.modified,
)
if brain.Type == "Link":
dictfields["url"] = brain.getRemoteUrl
# resolve /resolveuid/... to url
# XXX: ma qui non funziona perchè il path è /Plone/resolveuid/...
# mentre la regex di uid_to_url si aspetta /resolveuid/... o
# ../resolveuid/...
# dictfields["url"] = uid_to_url(dictfields["url"])
# XXX: bug di Link ? in remoteUrl per i link interni nei brain
# c'è il path completo (con /Plone) invece che una url
# probabilmente legato al fatto che i link ora sono creati via
# api e non da interfaccia Plone (?)
if dictfields["url"].startswith(f"/{siteid}"):
dictfields["url"] = dictfields["url"][len(siteid) + 1 :]
if HAS_PLONERESTAPI:
dictfields["url"] = uid_to_url(dictfields["url"])
elif brain.Type == "File":
obj_file = brain.getObject().file
if obj_file:
dictfields["url"] = (
f"{brain.getURL()}/@@download/file/{obj_file.filename}" # noqa E501
)
obj_size = obj_file.size
dictfields["filesize"] = self.getSizeString(obj_size)
elif brain.Type == "Modulo":
obj = brain.getObject()
for field in fields:
field_obj = IMultiFileSchema[field]
serializer = queryMultiAdapter(
(field_obj, obj, self.request), IFieldSerializer
)
value = serializer()
dictfields[field] = value

# else:
# dictfields["url"] = brain.getURL() + "/view"
dictfields["content-type"] = brain.mime_type
# icon = getMultiAdapter((self.context, self.request, obj), IContentIcon)
# dictfields['icon'] = icon.html_tag()
dictfields["type"] = brain.Type

if HAS_PLONERESTAPI:
dictfields = json_compatible(dictfields)
values.append(dictfields)

return values

0 comments on commit 38fa23f

Please sign in to comment.