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

provide an IContentListingObject adapter #411

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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/410.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Provide an IContentListingObject adapter
[erral]
22 changes: 22 additions & 0 deletions plone/app/event/adapters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from plone.app.contentlisting.interfaces import IContentListingObject
from plone.app.contentlisting.realobject import RealContentListingObject
from zope.interface import implementer


@implementer(IContentListingObject)
class OccurrenceContentListingObject(RealContentListingObject):

def __getattr__(self, name):
"""We'll override getattr so that we can defer name lookups to
the real underlying objects without knowing the names of all
attributes.
"""
if name.startswith("_"):
raise AttributeError(name)
obj = self.getObject()
# we need to override the behavior of RealContentListingObject
# because Occurrence objects rely on acquisition to show their title
# and other attributes
if hasattr(obj, name):
return getattr(obj, name)
raise AttributeError(name)
Comment on lines +20 to +22
Copy link
Member

Choose a reason for hiding this comment

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

I'm pretty sure this can be simplified to

Suggested change
if hasattr(obj, name):
return getattr(obj, name)
raise AttributeError(name)
return getattr(obj, name)

There's no need to check for the attribute before getting it. getattr will already raise AttributeError if it's not there.

7 changes: 7 additions & 0 deletions plone/app/event/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,11 @@
name="plone.app.event"
/>


<adapter
factory=".adapters.OccurrenceContentListingObject"
for="plone.event.interfaces.IOccurrence"
zcml:condition="installed plone.app.contentlisting"
/>

</configure>
54 changes: 54 additions & 0 deletions plone/app/event/tests/test_recurrence.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from OFS.SimpleItem import SimpleItem
from plone.app.contentlisting.interfaces import IContentListingObject
from plone.app.event.base import get_events
from plone.app.event.base import RET_MODE_ACCESSORS
from plone.app.event.base import RET_MODE_OBJECTS
from plone.app.event.dx.traverser import OccurrenceTraverser
from plone.app.event.recurrence import Occurrence
from plone.app.event.testing import PAEvent_INTEGRATION_TESTING
Expand Down Expand Up @@ -352,3 +354,55 @@ def test_recurrence_occurrences_with_range_start_and_end(self):

# Subsequent ones are IOccurrence objects
self.assertTrue(IOccurrence.providedBy(result[1]))


class TestRecurrenceContentListingSupport(unittest.TestCase):
"""test that content listing objects are correctly serialized in plone.app.contentlisting objects"""

layer = PAEventDX_INTEGRATION_TESTING

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

set_browserlayer(self.request)
set_env_timezone(TZNAME)
set_timezone(TZNAME)

now = patched_now()

setRoles(self.portal, TEST_USER_ID, ["Manager"])
self.daily = createContentInContainer(
self.portal,
"plone.app.event.dx.event",
id="daily",
title="Daily Event",
start=now,
end=now + datetime.timedelta(hours=1),
location="Vienna",
recurrence="RRULE:FREQ=DAILY;COUNT=4",
)

def test_recurrence_objects_adapted_to_content_listing_objects(self):
res = get_events(self.portal, ret_mode=RET_MODE_OBJECTS, expand=True)

self.assertEqual(len(res), 4)

first_item = res[0]
second_item = res[1]

first_content_listing_object = IContentListingObject(first_item)
second_content_listing_object = IContentListingObject(second_item)

self.assertEqual(first_content_listing_object.Title(), first_item.Title())
self.assertEqual(second_content_listing_object.Title(), second_item.Title())

self.assertEqual(first_content_listing_object.start, first_item.start)
self.assertEqual(second_content_listing_object.start, second_item.start)

self.assertEqual(first_content_listing_object.end, first_item.end)
self.assertEqual(second_content_listing_object.end, second_item.end)

self.assertTrue(
first_content_listing_object.start < second_content_listing_object.start
)