Skip to content

Commit

Permalink
Fix in @scadenziario endpoint: return future events if afterToday cri…
Browse files Browse the repository at this point in the history
…teria is set
  • Loading branch information
cekk committed Feb 12, 2024
1 parent 903161a commit 1fe7f29
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
89 changes: 89 additions & 0 deletions src/design/plone/contenttypes/tests/test_service_scadenziario.py
Original file line number Diff line number Diff line change
@@ -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"),
)

0 comments on commit 1fe7f29

Please sign in to comment.