Skip to content
Snippets Groups Projects

Avoid setting the slot duration at import

Merged Felix Blanke requested to merge feature/avoid-duration-setting-at-import into main
1 file
+ 16
5
Compare changes
  • Side-by-side
  • Inline
+ 53
7
@@ -6,7 +6,7 @@ from dataclasses import dataclass
@@ -6,7 +6,7 @@ from dataclasses import dataclass
from datetime import datetime, timedelta
from datetime import datetime, timedelta
from typing import Iterable, Generator
from typing import Iterable, Generator
from django.db import models
from django.db import models, transaction
from django.apps import apps
from django.apps import apps
from django.db.models import Count
from django.db.models import Count
from django.urls import reverse_lazy
from django.urls import reverse_lazy
@@ -401,7 +401,8 @@ class Event(models.Model):
@@ -401,7 +401,8 @@ class Event(models.Model):
else:
else:
yield from self.uniform_time_slots(slots_in_an_hour=slots_in_an_hour)
yield from self.uniform_time_slots(slots_in_an_hour=slots_in_an_hour)
def schedule_from_json(self, schedule: str) -> None:
@transaction.atomic
 
def schedule_from_json(self, schedule: str) -> int:
"""Load AK schedule from a json string.
"""Load AK schedule from a json string.
:param schedule: A string that can be decoded to json, describing
:param schedule: A string that can be decoded to json, describing
@@ -419,18 +420,63 @@ class Event(models.Model):
@@ -419,18 +420,63 @@ class Event(models.Model):
for timeslot in block
for timeslot in block
}
}
 
slots_updated = 0
for scheduled_slot in schedule["scheduled_aks"]:
for scheduled_slot in schedule["scheduled_aks"]:
 
scheduled_slot["timeslot_ids"] = list(map(int, scheduled_slot["timeslot_ids"]))
slot = AKSlot.objects.get(id=int(scheduled_slot["ak_id"]))
slot = AKSlot.objects.get(id=int(scheduled_slot["ak_id"]))
slot.room = Room.objects.get(id=int(scheduled_slot["room_id"]))
scheduled_slot["timeslot_ids"] = list(map(int, scheduled_slot["timeslot_ids"]))
if not scheduled_slot["timeslot_ids"]:
 
raise ValueError(
 
_("AK {ak_name} is not assigned any timeslot by the solver").format(ak_name=slot.ak.name)
 
)
start_timeslot = timeslot_dict[min(scheduled_slot["timeslot_ids"])].avail
start_timeslot = timeslot_dict[min(scheduled_slot["timeslot_ids"])].avail
end_timeslot = timeslot_dict[max(scheduled_slot["timeslot_ids"])].avail
end_timeslot = timeslot_dict[max(scheduled_slot["timeslot_ids"])].avail
 
solver_duration = (end_timeslot.end - start_timeslot.start).total_seconds() / 3600.0
 
 
if solver_duration + 2e-4 < slot.duration:
 
raise ValueError(
 
_(
 
"Duration of AK {ak_name} assigned by solver ({solver_duration} hours) "
 
"is less than the duration required by the slot ({slot_duration} hours)"
 
).format(
 
ak_name=slot.ak.name,
 
solver_duration=solver_duration,
 
slot_duration=slot.duration,
 
)
 
)
 
 
if slot.fixed:
 
solver_room = Room.objects.get(id=int(scheduled_slot["room_id"]))
 
if slot.room != solver_room:
 
raise ValueError(
 
_(
 
"Fixed AK {ak_name} assigned by solver to room {solver_room} "
 
"is fixed to room {slot_room}"
 
).format(
 
ak_name=slot.ak.name,
 
solver_room=solver_room.name,
 
slot_room=slot.room.name,
 
)
 
)
 
if slot.start != start_timeslot.start:
 
raise ValueError(
 
_(
 
"Fixed AK {ak_name} assigned by solver to start at {solver_start} "
 
"is fixed to start at {slot_start}"
 
).format(
 
ak_name=slot.ak.name,
 
solver_start=start_timeslot.start,
 
slot_start=slot.start,
 
)
 
)
 
else:
 
slot.room = Room.objects.get(id=int(scheduled_slot["room_id"]))
 
slot.start = start_timeslot.start
 
slot.save()
 
slots_updated += 1
slot.start = start_timeslot.start
return slots_updated
slot.duration = (end_timeslot.end - start_timeslot.start).total_seconds() / 3600.0
slot.save()
class AKOwner(models.Model):
class AKOwner(models.Model):
""" An AKOwner describes the person organizing/holding an AK.
""" An AKOwner describes the person organizing/holding an AK.
Loading