2012-08-30 06:51:07 +00:00
|
|
|
import itertools
|
|
|
|
|
2012-09-20 02:48:54 +00:00
|
|
|
from django.db.models import Count, Min
|
2012-08-30 17:52:11 +00:00
|
|
|
|
|
|
|
from symposion.schedule.models import Room, Slot, SlotRoom
|
2012-08-30 06:52:50 +00:00
|
|
|
|
2012-08-30 06:51:07 +00:00
|
|
|
|
2012-08-30 06:53:31 +00:00
|
|
|
class TimeTable(object):
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-08-30 06:51:07 +00:00
|
|
|
def __init__(self, day):
|
|
|
|
self.day = day
|
2018-12-08 06:25:14 +00:00
|
|
|
self.slots_qs = Slot.objects.filter(day=day)\
|
|
|
|
.select_related('kind', 'content_ptr__speaker__user')\
|
|
|
|
.prefetch_related('content_ptr__additional_speakers')
|
|
|
|
self._times = sorted(
|
|
|
|
set(itertools.chain(*self.slots_qs.values_list("start", "end")))
|
|
|
|
)
|
|
|
|
|
|
|
|
def slots(self):
|
|
|
|
if not hasattr(self, '_slots'):
|
|
|
|
filters = {
|
|
|
|
"room_count": Count("slotroom"),
|
|
|
|
"order": Min("slotroom__room__order")
|
|
|
|
}
|
|
|
|
self._slots = self.slots_qs.annotate(**filters).order_by("order")
|
|
|
|
return self._slots
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-08-30 06:51:07 +00:00
|
|
|
def rooms(self):
|
2018-12-08 06:25:14 +00:00
|
|
|
if not hasattr(self, '_rooms'):
|
|
|
|
self._rooms = Room.objects\
|
|
|
|
.filter(slotroom__slot__in=self.slots_qs)\
|
|
|
|
.distinct()\
|
|
|
|
.order_by("order")
|
|
|
|
return self._rooms
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-08-30 06:51:07 +00:00
|
|
|
def __iter__(self):
|
2018-12-08 06:25:14 +00:00
|
|
|
slots = self.slots()
|
|
|
|
|
2012-08-30 06:51:07 +00:00
|
|
|
row = []
|
2016-12-09 21:30:44 +00:00
|
|
|
total_room_count = self.rooms().count()
|
2018-12-08 06:25:14 +00:00
|
|
|
for time, next_time in pairwise(self._times):
|
|
|
|
row = {"time": time, "end": next_time, "slots": []}
|
2017-12-24 03:52:09 +00:00
|
|
|
row_slots = [ slot for slot in slots if slot.start == time]
|
|
|
|
for slot in row_slots:
|
2018-12-08 06:25:14 +00:00
|
|
|
slot.rowspan = TimeTable.rowspan(self._times, slot.start, slot.end)
|
2017-12-24 03:52:09 +00:00
|
|
|
slot.colspan = slot.room_count if not slot.exclusive else total_room_count
|
|
|
|
row["slots"].append(slot)
|
2012-08-30 06:51:07 +00:00
|
|
|
if row["slots"] or next_time is None:
|
|
|
|
yield row
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-08-30 06:51:07 +00:00
|
|
|
@staticmethod
|
|
|
|
def rowspan(times, start, end):
|
2017-12-25 10:44:31 +00:00
|
|
|
return times.index(end) - times.index(start)
|
2012-08-30 06:51:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
def pairwise(iterable):
|
|
|
|
a, b = itertools.tee(iterable)
|
2017-09-17 12:08:39 +00:00
|
|
|
next(b)
|
|
|
|
return itertools.zip_longest(a, b)
|