def test_find_free_slots(self): shift = Interval(9, 14) appointments = [Interval(9, 10), Interval(10, 11), Interval(13, 14)] slots = slots_in_interval(1, shift) free = [s for s in slots if not interval_overlaps(s, appointments)] self.assertEqual(free, [Interval(11, 12), Interval(12, 13)])
def get_free_slots(self, start, end, length=None): """Return slots in shifts which does not overlaps with its appointments. """ length = length or self.minimum_length interval = Interval(start, end) for slot in slots_in_interval(length, interval, length): for shift_key, shift in self.get_shifts_iteritems(): if slot not in shift.interval: continue overlapping_appointment = False for _, appo in self.get_appointments_in_shift_iteritems(shift_key): if slot.overlaps(appo.interval): overlapping_appointment = True break if overlapping_appointment == False: yield slot break
def add_appointment(self, start, end): appo = Appointment(None, start, end) length = appo.interval.end - appo.interval.start for _, shift in self._agenda.iteritems(): if appo.interval not in shift.interval: continue if appo.interval not in slots_in_interval( length, shift.interval, self.minimum_length): continue appos_in_shift = [a.interval for (_, a) in shift.iteritems()] if not interval_overlaps(appo.interval, appos_in_shift): appo.parent_key = shift.key try: appo = ds().put(appo) except (OverlappingIntervalWarning, ConcurrencyWarning): appo.parent_key = None continue return appo raise NotAvailableSlotError
def test_slots_in_interval_with_steps(self): shift = Interval(10, 14) slots = list(slots_in_interval(1, shift, 2)) self.assertEquals(slots, [Interval(10, 11), Interval(12, 13)])