Skip to content

Commit

Permalink
customize relationfield serializer to bypass empty relations
Browse files Browse the repository at this point in the history
  • Loading branch information
cekk committed Aug 27, 2024
1 parent d6d3938 commit 62c9787
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/iosanita/contenttypes/restapi/serializers/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@
factory=".summary.JSONSummarySerializerMetadata"
name="iosanita.contenttypes.summary_serializer_metadata"
/>

<adapter factory=".relationfield.RelationListFieldSerializer" />

</configure>
21 changes: 21 additions & 0 deletions src/iosanita/contenttypes/restapi/serializers/relationfield.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from plone.dexterity.interfaces import IDexterityContent
from plone.restapi.interfaces import IFieldSerializer
from z3c.relationfield.interfaces import IRelationList
from zope.component import adapter
from zope.interface import implementer
from plone.restapi.serializer.relationfield import (
RelationListFieldSerializer as BaseSerializer,
)
from iosanita.contenttypes.interfaces import IIosanitaContenttypesLayer


@adapter(IRelationList, IDexterityContent, IIosanitaContenttypesLayer)
@implementer(IFieldSerializer)
class RelationListFieldSerializer(BaseSerializer):

def __call__(self):
"""
Do not return broken relations
"""
data = super().__call__()
return [x for x in data if x is not None]

0 comments on commit 62c9787

Please sign in to comment.