Skip to content

Commit

Permalink
improve signature of is_busy
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdelatorre committed Jun 21, 2024
1 parent cf169b5 commit fc6f5f4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/pycamp_bot/commands/wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def define_wizards_schedule(pycamp):
for slot in slots_list:
wizard = wizards_list[idx%n_wizards]
n_iter = 0 # railguard
while wizard.is_busy(slot):
while wizard.is_busy(*slot):
logger.info('Magx {} con conflicto en el slot {}. Pruebo otro.'.format(wizard.username, slot))
if n_iter == n_wizards:
logger.warning('Queda el magx {} con conflicto en el slot {}'.format(wizard, slot))
Expand Down
9 changes: 4 additions & 5 deletions src/pycamp_bot/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,13 @@ def __str__(self):
rv_str += 'Admin' if self.admin else 'Commoner'
return rv_str

def is_busy(self, moment):
"""`moment` is a tuple (start, end) with two datetime objects."""
target_period_start, target_period_end = moment
def is_busy(self, from_time, to_time):
"""`from_time, to_time` are two datetime objects."""
project_presentation_slots = Slot.select().where(Slot.current_wizard == self)
for slot in project_presentation_slots:
# https://stackoverflow.com/a/13403827/1161156
latest_start = max(target_period_start, slot.start)
earliest_end = min(target_period_end, slot.get_end_time())
latest_start = max(from_time, slot.start)
earliest_end = min(to_time, slot.get_end_time())
if latest_start <= earliest_end: # Overlap
return True
return False
Expand Down
14 changes: 7 additions & 7 deletions test/test_pycampista.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ def test_return_true_if_assigned_in_slot_equal_to_target_period(self):
pycamper = Pycampista.create(username="pepe")
slot = Slot.create(code = "A1", start=datetime.now(), current_wizard=pycamper)
period = (slot.start, slot.get_end_time())
assert pycamper.is_busy(period)
assert pycamper.is_busy(*period)

@use_test_database
def test_return_true_if_assigned_in_slot_starting_at_target_period(self):
pycamper = Pycampista.create(username="pepe")
slot_start = datetime.now()
Slot.create(code = "A1", start=slot_start, current_wizard=pycamper)
period = (slot_start, slot_start + timedelta(minutes=5))
assert pycamper.is_busy(period)
assert pycamper.is_busy(*period)

@use_test_database
def test_return_true_if_assigned_in_slot_around_target_period(self):
Expand All @@ -48,7 +48,7 @@ def test_return_true_if_assigned_in_slot_around_target_period(self):
Slot.create(code = "A1", start=slot_start, current_wizard=pycamper)
period_start = slot_start + timedelta(minutes=5)
period = (period_start, period_start + timedelta(minutes=10))
assert pycamper.is_busy(period)
assert pycamper.is_busy(*period)

@use_test_database
def test_return_true_if_assigned_in_slot_ending_after_target_period_starts(self):
Expand All @@ -58,7 +58,7 @@ def test_return_true_if_assigned_in_slot_ending_after_target_period_starts(self)
slot.start + timedelta(minutes=5),
slot.get_end_time() + timedelta(minutes=5),
)
assert pycamper.is_busy(period)
assert pycamper.is_busy(*period)

@use_test_database
def test_return_true_if_assigned_in_slot_starting_before_target_period_ends(self):
Expand All @@ -68,7 +68,7 @@ def test_return_true_if_assigned_in_slot_starting_before_target_period_ends(self
slot.start - timedelta(minutes=5),
slot.start + timedelta(minutes=5),
)
assert pycamper.is_busy(period)
assert pycamper.is_busy(*period)

@use_test_database
def test_return_false_if_assigned_in_slot_ending_before_target_period_starts(self):
Expand All @@ -78,7 +78,7 @@ def test_return_false_if_assigned_in_slot_ending_before_target_period_starts(sel
slot.get_end_time() + timedelta(seconds=1),
slot.get_end_time() + timedelta(seconds=10),
)
assert not pycamper.is_busy(period)
assert not pycamper.is_busy(*period)

@use_test_database
def test_return_false_if_assigned_in_slot_start_after_target_period_ends(self):
Expand All @@ -88,4 +88,4 @@ def test_return_false_if_assigned_in_slot_start_after_target_period_ends(self):
slot.start - timedelta(seconds=10),
slot.start - timedelta(seconds=1),
)
assert not pycamper.is_busy(period)
assert not pycamper.is_busy(*period)

0 comments on commit fc6f5f4

Please sign in to comment.