From 0d2bdd813d97614283827315b2c2bdcb0d8ee111 Mon Sep 17 00:00:00 2001 From: Roman Date: Tue, 3 Oct 2023 14:39:27 +0200 Subject: [PATCH] Check bookings limit also in according to booking_type --- src/redturtle/prenotazioni/adapters/booker.py | 25 +++++++++++++------ .../tests/test_bookings_num_limit.py | 24 ++++++++++++++++++ 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/redturtle/prenotazioni/adapters/booker.py b/src/redturtle/prenotazioni/adapters/booker.py index 4eaea103..9afadc5e 100644 --- a/src/redturtle/prenotazioni/adapters/booker.py +++ b/src/redturtle/prenotazioni/adapters/booker.py @@ -41,11 +41,11 @@ def prenotazioni(self): """The prenotazioni context state view""" return self.context.unrestrictedTraverse("@@prenotazioni_context_state") # noqa - def _validate_user_limit(self, fiscalcode): + def _validate_user_limit(self, data: dict): """Control if user did not exceed the limit yet Args: - fiscalcode (str): User's fiscal code + data (dict): booking data Returns: None @@ -57,15 +57,17 @@ def _validate_user_limit(self, fiscalcode): if not self.context.max_bookings_allowed: return - if len(self.search_future_bookings_by_fiscalcode(fiscalcode)) >= ( - self.context.max_bookings_allowed - ): + if len( + self.search_future_bookings_by_fiscalcode( + data["fiscalcode"], data["booking_type"] + ) + ) >= (self.context.max_bookings_allowed): raise BookingsLimitExceded(self.context) - def search_future_bookings_by_fiscalcode(self, fiscalcode): + def search_future_bookings_by_fiscalcode(self, fiscalcode, booking_type=None): """Find all the future bookings registered for the same fiscalcode""" result = [] - for booking in api.portal.get_tool("portal_catalog").unrestrictedSearchResults( + query = dict( portal_type="Prenotazione", fiscalcode=fiscalcode, path={"query": "/".join(self.context.getPhysicalPath())}, @@ -73,6 +75,13 @@ def search_future_bookings_by_fiscalcode(self, fiscalcode): review_state={ "query": ("confirmed", "pending", "private"), }, + ) + + if booking_type: + query["booking_type"] = booking_type + + for booking in api.portal.get_tool("portal_catalog").unrestrictedSearchResults( + **query ): result.append(booking) @@ -145,7 +154,7 @@ def _create(self, data, duration=-1, force_gate=""): if fiscalcode: params["fiscalcode"] = fiscalcode - self._validate_user_limit(fiscalcode) + self._validate_user_limit(params) obj = api.content.create( type="Prenotazione", diff --git a/src/redturtle/prenotazioni/tests/test_bookings_num_limit.py b/src/redturtle/prenotazioni/tests/test_bookings_num_limit.py index ad1f1599..8488d4b9 100644 --- a/src/redturtle/prenotazioni/tests/test_bookings_num_limit.py +++ b/src/redturtle/prenotazioni/tests/test_bookings_num_limit.py @@ -42,6 +42,7 @@ def setUp(self): daData=date.today(), booking_types=[ {"name": "Type A", "duration": "30"}, + {"name": "Type B", "duration": "30"}, ], gates=["Gate A"], ) @@ -191,3 +192,26 @@ def test_limit_exceeded_is_not_raised_if_have_refused_bookings(self): } ) ) + + def test_limit_exceeded_is_not_raised_if_different_booking_type(self): + self.create_booking( + data={ + "booking_date": self.tomorrow_8_0, + "booking_type": "Type A", + "title": "foo", + "email": "jdoe@redturtle.it", + "fiscalcode": self.testing_fiscalcode, + } + ) + + self.assertTrue( + self.create_booking( + data={ + "booking_date": self.tomorrow_8_0 + timedelta(days=1), + "booking_type": "Type B", + "title": "foo", + "email": "jdoe@redturtle.it", + "fiscalcode": self.testing_fiscalcode, + } + ) + )