From 1fe7f29504957f5cf842d4a358723c1d4d1b9cd9 Mon Sep 17 00:00:00 2001 From: Andrea Cecchi Date: Mon, 12 Feb 2024 16:19:09 +0100 Subject: [PATCH] Fix in @scadenziario endpoint: return future events if afterToday criteria is set --- CHANGES.rst | 3 +- .../restapi/services/scadenziario/post.py | 8 +- .../tests/test_service_scadenziario.py | 89 +++++++++++++++++++ 3 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 src/design/plone/contenttypes/tests/test_service_scadenziario.py diff --git a/CHANGES.rst b/CHANGES.rst index c0d5ecc2..65f15808 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,7 +4,8 @@ Changelog 6.1.14 (unreleased) ------------------- -- Nothing changed yet. +- Fix in @scadenziario endpoint: return future events if afterToday criteria is set. + [cekk] 6.1.13 (2024-02-08) diff --git a/src/design/plone/contenttypes/restapi/services/scadenziario/post.py b/src/design/plone/contenttypes/restapi/services/scadenziario/post.py index 07ba6299..e19720c1 100644 --- a/src/design/plone/contenttypes/restapi/services/scadenziario/post.py +++ b/src/design/plone/contenttypes/restapi/services/scadenziario/post.py @@ -158,7 +158,13 @@ def reply(self): if "start" in query_for_catalog: start = query_for_catalog["start"]["query"] if "end" in query_for_catalog: - end = query_for_catalog["end"]["query"] + if query_for_catalog["end"].get("range", "") != "min": + # per esempio, è impostato il filtro "con fine evento da domani". + # se impostiamo un'end (la data di domani), poi nella generazione delle ricorrenze, + # vengono scartati tutti gli eventi che hanno una data di inizio nel futuro + # (https://github.com/plone/plone.event/blob/master/plone/event/recurrence.py#L141) + # perché la data della ricorrenza è maggiore di "until", che è quello che qui inviamo come end. + end = query_for_catalog["end"]["query"] expanded_events = self.expand_events(events, 3, start, end) all_results = not_events + expanded_events diff --git a/src/design/plone/contenttypes/tests/test_service_scadenziario.py b/src/design/plone/contenttypes/tests/test_service_scadenziario.py new file mode 100644 index 00000000..bdc56425 --- /dev/null +++ b/src/design/plone/contenttypes/tests/test_service_scadenziario.py @@ -0,0 +1,89 @@ +# -*- coding: utf-8 -*- +from datetime import datetime +from datetime import timedelta +from design.plone.contenttypes.testing import ( + DESIGN_PLONE_CONTENTTYPES_API_FUNCTIONAL_TESTING, +) +from plone import api +from plone.app.testing import setRoles +from plone.app.testing import SITE_OWNER_NAME +from plone.app.testing import SITE_OWNER_PASSWORD +from plone.app.testing import TEST_USER_ID +from plone.restapi.testing import RelativeSession +from transaction import commit + +import unittest + + +class ScadenziarioTest(unittest.TestCase): + layer = DESIGN_PLONE_CONTENTTYPES_API_FUNCTIONAL_TESTING + + def setUp(self): + self.app = self.layer["app"] + self.portal = self.layer["portal"] + self.request = self.layer["request"] + self.portal_url = self.portal.absolute_url() + + self.api_session = RelativeSession(self.portal_url) + self.api_session.headers.update({"Accept": "application/json"}) + self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD) + + setRoles(self.portal, TEST_USER_ID, ["Manager"]) + + commit() + + def tearDown(self): + self.api_session.close() + + def test_return_future_events_if_query_is_end_after_today(self): + now = datetime.now() + + # past event + api.content.create( + container=self.portal, + type="Event", + title="Past event", + start=now.replace(hour=8) + timedelta(days=-2), + end=now.replace(hour=18) + timedelta(days=-2), + ) + + future_event_1 = api.content.create( + container=self.portal, + type="Event", + title="Future event", + start=now.replace(hour=8) + timedelta(days=2), + end=now.replace(hour=18) + timedelta(days=4), + ) + future_event_2 = api.content.create( + container=self.portal, + type="Event", + title="Future event that starts in the past", + start=now.replace(hour=8) + timedelta(days=-4), + end=now.replace(hour=18) + timedelta(days=4), + ) + + commit() + + response = self.api_session.post( + f"{self.portal_url}/@scadenziario", + json={ + "query": [ + { + "i": "end", + "o": "plone.app.querystring.operation.date.afterToday", + "v": "", + } + ] + }, + ).json() + self.assertEqual(len(response["items"]), 2) + + # results are in asc order + self.assertEqual( + response["items"][0], + future_event_2.start.strftime("%Y/%m/%d"), + ) + self.assertEqual( + response["items"][1], + future_event_1.start.strftime("%Y/%m/%d"), + )