Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Safely convert items to IContentListingObject #1810

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions news/1810.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Safely convert objects to IContentListingObject
[erral]
5 changes: 4 additions & 1 deletion src/plone/restapi/serializer/summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ def __init__(self, context, request):
self.blocklisted_attributes = metadata["blocklisted_attributes"]

def __call__(self):
obj = IContentListingObject(self.context)
try:
obj = IContentListingObject(self.context)
except TypeError:
return {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this just make it less obvious what the problem is? As a consumer of the API, I would find it surprising to see an empty object in a list of items, and I would wonder what happened. Better to throw the error that happens now, I think.


summary = {}
for field in self.metadata_fields():
Expand Down
50 changes: 50 additions & 0 deletions src/plone/restapi/tests/test_serializer_summary.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
from datetime import datetime
from datetime import timedelta
from DateTime import DateTime
from plone.app.contentlisting.interfaces import IContentListingObject
from plone.app.event.dx.traverser import OccurrenceTraverser
from plone.app.testing import popGlobalRegistry
from plone.app.testing import pushGlobalRegistry
from plone.dexterity.utils import createContentInContainer
from plone.event.interfaces import IEvent
from plone.event.interfaces import IEventRecurrence
from plone.restapi.interfaces import ISerializeToJsonSummary
from plone.restapi.testing import PLONE_RESTAPI_DX_INTEGRATION_TESTING
from plone.restapi.testing import register_static_uuid_utility
from Products.CMFCore.utils import getToolByName
from zope.component import getMultiAdapter
from zope.component.hooks import getSite
from zope.interface import alsoProvides

import Missing
import unittest
Expand Down Expand Up @@ -203,3 +209,47 @@ def test_dx_type_summary(self):
},
summary,
)


class TestSummarySerializerswithRecurrenceObjects(unittest.TestCase):
layer = PLONE_RESTAPI_DX_INTEGRATION_TESTING

def setUp(self):
self.portal = self.layer["portal"]
self.request = self.layer["request"]

pushGlobalRegistry(getSite())
register_static_uuid_utility(prefix="c6dcbd55ab2746e199cd4ed458")

behaviors = self.portal.portal_types.DXTestDocument.behaviors
behaviors = behaviors + (
"plone.eventbasic",
"plone.eventrecurrence",
)
self.portal.portal_types.DXTestDocument.behaviors = behaviors

self.event = createContentInContainer(
self.portal,
"DXTestDocument",
id="doc1",
title="Lorem Ipsum event",
description="Description event",
start=datetime.now(),
end=datetime.now() + timedelta(hours=1),
recurrence="RRULE:FREQ=DAILY;COUNT=3", # see https://github.com/plone/plone.app.event/blob/master/plone/app/event/tests/base_setup.py
)

alsoProvides(self.event, IEvent)
alsoProvides(self.event, IEventRecurrence)

def tearDown(self):
popGlobalRegistry(getSite())

def test_dx_event_with_recurrence(self):
tomorrow = datetime.now() + timedelta(days=1)
tomorrow_str = tomorrow.strftime("%Y-%m-%d")
ot = OccurrenceTraverser(self.event, self.request)
ocurrence = ot.publishTraverse(self.request, tomorrow_str)
summary = getMultiAdapter((ocurrence, self.request), ISerializeToJsonSummary)()

self.assertEqual(summary, {})
Loading