Skip to content

Instantly share code, notes, and snippets.

@ericof
Created June 27, 2023 17:58
Show Gist options
  • Save ericof/8dd79356644788cb3215dfc427bcfa3f to your computer and use it in GitHub Desktop.
Save ericof/8dd79356644788cb3215dfc427bcfa3f to your computer and use it in GitHub Desktop.
from foo import _
from plone import api
from plone.dexterity.content import Container
from plone.supermodel.model import Schema
from zope import schema
from zope.interface import implementer
ICONS = [
"building-columns",
"business-time",
"cart-flatbed-suitcase",
"charging-station",
"cloud-sun-rain",
"graduation-cap",
"hand-holding-heart",
"heart-pulse",
"masks-theater",
"microscope",
"parachute-box",
"scale-balanced",
"store",
"tower-broadcast",
"tractor",
"truck",
]
class ICategory(Schema):
"""A Category of services."""
# Basic info
title = schema.TextLine(title=_("Category"), required=True)
description = schema.Text(title=_("Summary"), required=False)
icon = schema.Choice(title=_("Icon"), required=False, values=ICONS)
@implementer(ICategory)
class Category(Container):
"""A Category of services."""
@property
def services(self):
relations = api.relation.get(target=self, relationship="category")
return [i.from_object for i in relations]
from foo.content.category import ICategory
from foo.utils import breadcrumbs
from plone.restapi.interfaces import ISerializeToJson
from plone.restapi.interfaces import ISerializeToJsonSummary
from plone.restapi.serializer.converters import json_compatible
from plone.restapi.serializer.dxcontent import SerializeFolderToJson
from zope.component import adapter
from zope.component import getMultiAdapter
from zope.interface import implementer
from zope.interface import Interface
@implementer(ISerializeToJson)
@adapter(ICategory, Interface)
class CategoryJSONSerializer(SerializeFolderToJson):
def services(self):
services = []
for obj in self.context.services:
services.append(
getMultiAdapter((obj, self.request), ISerializeToJsonSummary)()
)
services = sorted(services, key=lambda x: x.get("title"))
return services
def __call__(self, version=None, include_items=True):
result = super().__call__(version, include_items)
result.update(
json_compatible(
{
"breadcrumbs": breadcrumbs(self.context),
"services": self.services(),
}
)
)
return result
from foo.content.service import IService
from foo.utils import breadcrumbs
from plone.restapi.interfaces import ISerializeToJson
from plone.restapi.serializer.converters import json_compatible
from plone.restapi.serializer.dxcontent import SerializeFolderToJson
from zope.component import adapter
from zope.interface import implementer
from zope.interface import Interface
@implementer(ISerializeToJson)
@adapter(IService, Interface)
class ServiceJSONSerializer(SerializeFolderToJson):
def __call__(self, version=None, include_items=True):
result = super().__call__(version, include_items)
category_rel = self.context.category
category_breadcrumbs = []
if category_rel:
category = [c for c in category_rel][0].to_object
category_breadcrumbs = breadcrumbs(category)
result.update(
json_compatible(
{
"category_breadcrumbs": category_breadcrumbs,
}
)
)
return result
from foo import _
from plone.app.textfield import RichText
from plone.autoform import directives
from plone.dexterity.content import Container
from plone.supermodel import model
from plone.supermodel.model import Schema
from z3c.relationfield.schema import RelationChoice
from z3c.relationfield.schema import RelationList
from zope import schema
from zope.interface import implementer
class IService(Schema):
"""A Service provided to the citizens."""
model.fieldset(
"description",
_("Service Description"),
fields=[
"basic_data",
"users",
"steps",
"other",
"lgpd",
],
)
# Basic info
title = schema.TextLine(title=_("Service"), required=True)
description = schema.Text(title=_("Summary"), required=False)
category = RelationList(
title=_("Category"),
description=_(""),
value_type=RelationChoice(
vocabulary="govsp.vocabulary.categories",
),
required=False,
default=[],
)
basic_data = RichText(title=_("Basic Data"), required=True)
users = RichText(title=_("Users"), required=True)
steps = RichText(title=_("Steps"), required=True)
other = RichText(title=_("Other"), required=True)
lgpd = RichText(title=_("LGPD"), required=False)
url = schema.URI(title=_("URL"), required=False)
directives.widget(
"url",
placeholder=_("i.e.: https://https://www.detran.sp.gov.br/solicitacao2viaCNH"),
)
@implementer(IService)
class Service(Container):
"""A Service provided to the citizens."""
from Acquisition import aq_parent
from foo.content.category import Category
def _serialize_breadcrumb(context: Category) -> dict:
url = context.absolute_url()
return {
"@id": url,
"id": context.id,
"title": context.title,
"icon": context.icon if context.icon else "",
}
def breadcrumbs(context: Category) -> dict:
"""It should include self."""
breadcrumbs = [_serialize_breadcrumb(context)]
parent = aq_parent(context)
while parent.portal_type == context.portal_type:
breadcrumbs.insert(0, _serialize_breadcrumb(parent))
parent = aq_parent(parent)
return breadcrumbs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment