Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport della fix #118 su 2.1.* #120

Merged
merged 2 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ Changelog
------------------

- Nothing changed yet.
- Fix sub BaseSlots whene some slots overlap
[mamico]

- Compatibility with old code that use booking_types field in PrenotazioniFolder
[mamico]


2.1.5 (2023-11-10)
Expand Down
24 changes: 21 additions & 3 deletions src/redturtle/prenotazioni/adapters/slot.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@ def slots_to_points(slots):
return sorted(points)


def merge_intervals(slots):
if not slots:
return slots
slots = sorted(slots)
stack = []
# insert first interval into stack
stack.append(slots[0])
for s in slots[1:]:
# Check for overlapping interval,
# if interval overlap
if stack[-1].lower_value <= s.lower_value <= stack[-1].upper_value:
stack[-1] = BaseSlot(
stack[-1].lower_value, max(stack[-1].upper_value, s.upper_value)
)
else:
stack.append(s)
return stack


class ISlot(Interface):

"""
Expand All @@ -85,7 +104,6 @@ class UpperEndpoint(int):

@implementer(ISlot)
class BaseSlot(Interval):

"""Overrides and simplifies pyinter.Interval"""

_lower = Interval.CLOSED
Expand Down Expand Up @@ -145,8 +163,8 @@ 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)]
# We filter not overlapping with self, and merge the overlapping ones
good_intervals = merge_intervals([x for x in value if x.overlaps(self)])
points = slots_to_points(good_intervals)

start = self.lower_value
Expand Down
52 changes: 52 additions & 0 deletions src/redturtle/prenotazioni/tests/test_base_slot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-
import unittest
from redturtle.prenotazioni.adapters.slot import BaseSlot
from datetime import datetime, timedelta
from redturtle.prenotazioni.testing import REDTURTLE_PRENOTAZIONI_FUNCTIONAL_TESTING


class TestBaseSlot(unittest.TestCase):
layer = REDTURTLE_PRENOTAZIONI_FUNCTIONAL_TESTING
maxDiff = None

def test_sub(self):
now = datetime.now().replace(hour=8, minute=0, second=0, microsecond=0)
a = [
BaseSlot(now, now + timedelta(minutes=30)),
BaseSlot(now + timedelta(minutes=30), now + timedelta(hours=1)),
]
b = BaseSlot(now, now + timedelta(hours=2))

self.assertEqual(
b - a, [BaseSlot(now + timedelta(hours=1), now + timedelta(hours=2))]
)

def test_sub_with_holes(self):
now = datetime.now().replace(hour=8, minute=0, second=0, microsecond=0)
a = [
BaseSlot(now, now + timedelta(minutes=30)),
BaseSlot(now + timedelta(minutes=75), now + timedelta(minutes=90)),
BaseSlot(now + timedelta(minutes=105), now + timedelta(minutes=110)),
]
b = BaseSlot(now + timedelta(minutes=60), now + timedelta(minutes=120))

self.assertEqual(
b - a,
[
BaseSlot(now + timedelta(minutes=60), now + timedelta(minutes=75)),
BaseSlot(now + timedelta(minutes=90), now + timedelta(minutes=105)),
BaseSlot(now + timedelta(minutes=110), now + timedelta(minutes=120)),
],
)

def test_sub_overlapped(self):
now = datetime.now().replace(hour=8, minute=0, second=0, microsecond=0)
a = [
BaseSlot(now, now + timedelta(hours=1)),
BaseSlot(now + timedelta(minutes=30), now + timedelta(hours=1)),
]
b = BaseSlot(now, now + timedelta(hours=2))

self.assertEqual(
b - a, [BaseSlot(now + timedelta(hours=1), now + timedelta(hours=2))]
)
Loading