diff --git a/CHANGES.rst b/CHANGES.rst index d7a91a44..6b35675b 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -5,6 +5,8 @@ Changelog 2.0.0rc2 (unreleased) --------------------- +- Show default gates as unavailable in get_gates method, if they are overrided. + [cekk] - Skip required field validation when add out of office bookings in @booking endpoint. [cekk] - Only users with permission can add out of office bookings in @booking endpoint. diff --git a/src/redturtle/prenotazioni/browser/prenotazioni_context_state.py b/src/redturtle/prenotazioni/browser/prenotazioni_context_state.py index c69cc8f1..07925fcd 100644 --- a/src/redturtle/prenotazioni/browser/prenotazioni_context_state.py +++ b/src/redturtle/prenotazioni/browser/prenotazioni_context_state.py @@ -401,19 +401,42 @@ def get_gates(self, booking_date=None): # sometimes booking_date is passed as date and sometimes as datetime booking_date = booking_date.date() - gates = self.context.getGates() + gates = [ + { + "name": gate, + "available": True, + } + for gate in self.context.getGates() or [""] + ] + overrides = self.get_week_overrides(day=booking_date) - if overrides: - gates = overrides.get("gates", []) or gates + if not overrides: + return gates - return [ + overrided_gates = overrides.get("gates", []) + if not overrided_gates: + return gates + + overrided_gates = [ { "name": gate, "available": True, } - for gate in gates or [""] + for gate in overrided_gates or [""] + ] + + # set default gates as unavailable + gates = [ + { + "name": gate["name"], + "available": False, + } + for gate in gates + if gate["name"] not in [i["name"] for i in overrided_gates] ] + return gates + overrided_gates + def get_busy_gates_in_slot(self, booking_date, booking_end_date=None): """ The gates already associated to a Prenotazione object for booking_date diff --git a/src/redturtle/prenotazioni/restapi/services/available_slots/get.py b/src/redturtle/prenotazioni/restapi/services/available_slots/get.py index b5870156..e1bbff49 100644 --- a/src/redturtle/prenotazioni/restapi/services/available_slots/get.py +++ b/src/redturtle/prenotazioni/restapi/services/available_slots/get.py @@ -20,7 +20,9 @@ def reply(self): If not, the search will start from current date until the end of current month. """ prenotazioni_week_view = api.content.get_view( - "prenotazioni_week_view", context=self.context, request=self.request + "prenotazioni_week_view", + context=self.context, + request=self.request, ) start = self.request.form.get("start", "") end = self.request.form.get("end", "") diff --git a/src/redturtle/prenotazioni/tests/test_available_slots.py b/src/redturtle/prenotazioni/tests/test_available_slots.py index ae13c103..e197bca2 100644 --- a/src/redturtle/prenotazioni/tests/test_available_slots.py +++ b/src/redturtle/prenotazioni/tests/test_available_slots.py @@ -10,7 +10,9 @@ from plone.restapi.serializer.converters import json_compatible from plone.restapi.testing import RelativeSession from redturtle.prenotazioni.adapters.booker import IBooker -from redturtle.prenotazioni.testing import REDTURTLE_PRENOTAZIONI_API_FUNCTIONAL_TESTING +from redturtle.prenotazioni.testing import ( + REDTURTLE_PRENOTAZIONI_API_FUNCTIONAL_TESTING, +) import calendar import pytz @@ -57,7 +59,9 @@ def setUp(self): self.folder_prenotazioni.week_table = week_table year = api.content.create( - container=self.folder_prenotazioni, type="PrenotazioniYear", title="Year" + container=self.folder_prenotazioni, + type="PrenotazioniYear", + title="Year", ) week = api.content.create(container=year, type="PrenotazioniWeek", title="Week") self.day_folder = api.content.create( diff --git a/src/redturtle/prenotazioni/tests/test_gates_overrides.py b/src/redturtle/prenotazioni/tests/test_gates_overrides.py index bafbf093..1d81cf16 100644 --- a/src/redturtle/prenotazioni/tests/test_gates_overrides.py +++ b/src/redturtle/prenotazioni/tests/test_gates_overrides.py @@ -2,7 +2,9 @@ from datetime import date from plone import api from plone.app.testing import TEST_USER_ID, setRoles -from redturtle.prenotazioni.testing import REDTURTLE_PRENOTAZIONI_FUNCTIONAL_TESTING +from redturtle.prenotazioni.testing import ( + REDTURTLE_PRENOTAZIONI_FUNCTIONAL_TESTING, +) from redturtle.prenotazioni.tests.helpers import WEEK_TABLE_SCHEMA import json @@ -27,7 +29,7 @@ def setUp(self): booking_types=[ {"name": "Type A", "duration": "30"}, ], - gates=["Gate A"], + gates=["Gate A", "Gate B"], week_table=WEEK_TABLE_SCHEMA, week_table_overrides=json.dumps( [ @@ -35,7 +37,7 @@ def setUp(self): "from_day": "1", "from_month": "1", "to_month": "2", - "gates": ["foo", "bar"], + "gates": ["Gate B", "foo", "bar"], "to_day": "18", "week_table": [], "pause_table": [], @@ -52,18 +54,31 @@ def setUp(self): request=self.request, ) - def test_day_in_override_gates(self): + def test_day_in_override_gates_return_overrided_gates_available_and_default_unavailable( + self, + ): now = date.today() gates = self.view.get_gates(date(now.year, 1, 10)) self.assertEqual( gates, - [{"name": "foo", "available": True}, {"name": "bar", "available": True}], + [ + {"name": "Gate A", "available": False}, + {"name": "Gate B", "available": True}, + {"name": "foo", "available": True}, + {"name": "bar", "available": True}, + ], ) def test_day_not_in_override_gates(self): now = date.today() gates = self.view.get_gates(date(now.year, 6, 10)) - self.assertEqual(gates, [{"name": "Gate A", "available": True}]) + self.assertEqual( + gates, + [ + {"name": "Gate A", "available": True}, + {"name": "Gate B", "available": True}, + ], + ) def test_if_gates_override_not_set_use_default(self): folder = api.content.create(