Skip to content

Commit

Permalink
Merge pull request #404 from plone/eventlisting_date_filter
Browse files Browse the repository at this point in the history
Fix date conversion when collection context has start/end date queries
  • Loading branch information
petschki authored Aug 22, 2024
2 parents 5e3ba1f + 22ce428 commit 8a991b1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 2 additions & 0 deletions news/404.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix date conversion between collection query and ``expand_events`` filtering.
[petschki]
9 changes: 8 additions & 1 deletion plone/app/event/browser/event_listing.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ def _expand_events_start_end(self, start, end):
# XXX: This actually fits most needs, but not all. Maybe someone
# wants to come up with some edgecases!
se = dict(start=None, end=None)

def safe_dt(val):
# convert value from DateTime to datetime
if hasattr(val, "asdatetime"):
return val.asdatetime()
return val

if start:
q = start.get("query")
r = start.get("range")
Expand All @@ -238,7 +245,7 @@ def _expand_events_start_end(self, start, end):
r = end.get("range")
if r == "min":
se["start"] = q
return se["start"], se["end"]
return safe_dt(se["start"]), safe_dt(se["end"])

def formatted_date(self, occ):
provider = getMultiAdapter(
Expand Down
28 changes: 28 additions & 0 deletions plone/app/event/tests/test_event_listing.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from DateTime import DateTime
from plone.app.event.base import localized_today
from plone.app.event.testing import make_fake_response
from plone.app.event.testing import PAEventDX_INTEGRATION_TESTING
Expand Down Expand Up @@ -151,3 +152,30 @@ def test_no_date_filtering(self):
"mode_day",
]:
self.assertTrue(_class not in out)

@mock.patch("plone.app.event.browser.event_listing.localized_now", new=PN)
@mock.patch("plone.app.event.base.localized_now", new=PN)
def test_end_date_filtering(self):
"""Test date conversion between collection query and 'expand_events'
filtering.
"""
# plone.app.contenttypes ICollection type
self.portal.invokeFactory("Collection", "col_with_date_criterion", title="Col")
collection = self.portal.col_with_date_criterion
collection.query = [
{
"i": "portal_type",
"o": "plone.app.querystring.operation.selection.any",
"v": ["Event", "plone.app.event.dx.event"],
},
{
"i": "end",
"o": "plone.app.querystring.operation.date.largerThan",
"v": DateTime(PN()),
},
]

view = self.portal.col_with_date_criterion.restrictedTraverse("@@event_listing")
view()

self.assertIs(len(self.portal.col_with_date_criterion.results()), 3)

0 comments on commit 8a991b1

Please sign in to comment.