From 7b7b2fdbd789e41c08d233f89de7475d5db6e528 Mon Sep 17 00:00:00 2001 From: Andrea Cecchi Date: Wed, 11 Oct 2023 14:49:16 +0200 Subject: [PATCH] Sort gate slots in get_free_slots method to better handle also pauses --- CHANGES.rst | 3 +- src/redturtle/prenotazioni/adapters/slot.py | 1 - .../browser/prenotazioni_context_state.py | 2 +- .../tests/test_prenotazioni_context_state.py | 61 +++++++++++++++++++ 4 files changed, 64 insertions(+), 3 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 39e5242a..ef1d9f4a 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -5,7 +5,8 @@ Changelog 2.1.1 (unreleased) ------------------ -- Nothing changed yet. +- Sort gate slots in get_free_slots method to better handle also pauses. + [cekk] 2.1.0 (2023-10-11) diff --git a/src/redturtle/prenotazioni/adapters/slot.py b/src/redturtle/prenotazioni/adapters/slot.py index 9483c84d..c6e53b82 100644 --- a/src/redturtle/prenotazioni/adapters/slot.py +++ b/src/redturtle/prenotazioni/adapters/slot.py @@ -145,7 +145,6 @@ def __sub__(self, value): """Subtract something from this""" if isinstance(value, Interval): value = [value] - # We filter not overlapping intervals good_intervals = [x for x in value if x.overlaps(self)] points = slots_to_points(good_intervals) diff --git a/src/redturtle/prenotazioni/browser/prenotazioni_context_state.py b/src/redturtle/prenotazioni/browser/prenotazioni_context_state.py index 8e0e4d61..9e66c132 100644 --- a/src/redturtle/prenotazioni/browser/prenotazioni_context_state.py +++ b/src/redturtle/prenotazioni/browser/prenotazioni_context_state.py @@ -766,7 +766,7 @@ def get_free_slots(self, booking_date, period="day"): gate_slots.append(slot) for interval in intervals: if interval: - availability[gate].extend(interval - gate_slots) + availability[gate].extend(interval - sorted(gate_slots)) return availability def get_freebusy_slots(self, booking_date, period="day"): diff --git a/src/redturtle/prenotazioni/tests/test_prenotazioni_context_state.py b/src/redturtle/prenotazioni/tests/test_prenotazioni_context_state.py index 9b52602c..bceedc33 100644 --- a/src/redturtle/prenotazioni/tests/test_prenotazioni_context_state.py +++ b/src/redturtle/prenotazioni/tests/test_prenotazioni_context_state.py @@ -83,3 +83,64 @@ def test_get_free_slots_skip_bookigs_inside_pause_range(self): self.assertEqual(view.get_free_slots(today)["Gate A"][0].stop(), "08:00") self.assertEqual(view.get_free_slots(today)["Gate A"][1].start(), "11:00") self.assertEqual(view.get_free_slots(today)["Gate A"][1].stop(), "13:00") + + def test_get_free_slots_handle_pauses_correctly(self): + booker = IBooker(self.folder_prenotazioni) + + today = date.today() + # need this just to have the day container + aq_parent( + booker.create( + { + "booking_date": datetime( + today.year, today.month, today.day, 10, 30 + ), + "booking_type": "Type A", + "title": "foo", + } + ) + ) + for hour in [7, 8, 9, 11, 12]: + aq_parent( + booker.create( + { + "booking_date": datetime( + today.year, today.month, today.day, hour, 00 + ), + "booking_type": "Type A", + "title": "foo", + } + ) + ) + + aq_parent( + booker.create( + { + "booking_date": datetime( + today.year, today.month, today.day, hour, 30 + ), + "booking_type": "Type A", + "title": "foo", + } + ) + ) + + self.folder_prenotazioni.pause_table = [ + {"day": "0", "pause_end": "1030", "pause_start": "1000"}, + {"day": "1", "pause_end": "1030", "pause_start": "1000"}, + {"day": "2", "pause_end": "1030", "pause_start": "1000"}, + {"day": "3", "pause_end": "1030", "pause_start": "1000"}, + {"day": "4", "pause_end": "1030", "pause_start": "1000"}, + {"day": "5", "pause_end": "1030", "pause_start": "1000"}, + {"day": "6", "pause_end": "1030", "pause_start": "1000"}, + {"day": "7", "pause_end": "1030", "pause_start": "1000"}, + ] + + view = api.content.get_view( + context=self.folder_prenotazioni, + request=self.request, + name="prenotazioni_context_state", + ) + res = view.get_free_slots(today) + # there are no free slots because are all taken by bookings or pause + self.assertEqual(len(res["Gate A"]), 0)