2012-08-30 06:51:07 +00:00
|
|
|
import itertools
|
2012-08-30 17:52:11 +00:00
|
|
|
import operator
|
2012-08-30 06:51:07 +00:00
|
|
|
|
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):
|
2012-08-30 06:51:07 +00:00
|
|
|
|
|
|
|
def __init__(self, day):
|
|
|
|
self.day = day
|
|
|
|
|
|
|
|
def slots_qs(self):
|
2012-08-30 17:52:11 +00:00
|
|
|
qs = Slot.objects.all()
|
|
|
|
qs = qs.filter(day=self.day)
|
|
|
|
return qs
|
2012-08-30 06:51:07 +00:00
|
|
|
|
|
|
|
def rooms(self):
|
2012-08-30 17:52:11 +00:00
|
|
|
qs = Room.objects.all()
|
|
|
|
qs = qs.filter(schedule=self.day.schedule)
|
|
|
|
qs = qs.filter(pk__in=SlotRoom.objects.filter(slot__in=self.slots_qs().values("pk")).values("room"))
|
|
|
|
qs = qs.order_by("order")
|
|
|
|
return qs
|
2012-08-30 06:51:07 +00:00
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
times = sorted(set(itertools.chain(*self.slots_qs().values_list("start", "end"))))
|
2012-09-20 02:48:54 +00:00
|
|
|
slots = Slot.objects.filter(pk__in=self.slots_qs().values("pk"))
|
|
|
|
slots = slots.annotate(room_count=Count("slotroom"), order=Min("slotroom__room__order"))
|
|
|
|
slots = slots.order_by("start", "order")
|
2012-08-30 06:51:07 +00:00
|
|
|
row = []
|
|
|
|
for time, next_time in pairwise(times):
|
|
|
|
row = {"time": time, "slots": []}
|
|
|
|
for slot in slots:
|
|
|
|
if slot.start == time:
|
2012-08-30 06:53:31 +00:00
|
|
|
slot.rowspan = TimeTable.rowspan(times, slot.start, slot.end)
|
2012-08-30 17:52:11 +00:00
|
|
|
slot.colspan = slot.room_count
|
2012-08-30 06:51:07 +00:00
|
|
|
row["slots"].append(slot)
|
|
|
|
if row["slots"] or next_time is None:
|
|
|
|
yield row
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def rowspan(times, start, end):
|
|
|
|
return times.index(end) - times.index(start)
|
|
|
|
|
|
|
|
|
|
|
|
def pairwise(iterable):
|
|
|
|
a, b = itertools.tee(iterable)
|
|
|
|
b.next()
|
|
|
|
return itertools.izip_longest(a, b)
|