symposion_app/symposion/schedule/timetable.py

38 lines
1.1 KiB
Python
Raw Normal View History

2012-08-30 06:51:07 +00:00
import itertools
class Timetable(object):
def __init__(self, day):
self.day = day
def slots_qs(self):
return Slot.objects.filter(day=self.day)
def rooms(self):
return Room.objects.filter(day=self.day.schedule)
def __iter__(self):
times = sorted(set(itertools.chain(*self.slots_qs().values_list("start", "end"))))
slots = list(self.slots_qs().order_by("start"))
row = []
for time, next_time in pairwise(times):
row = {"time": time, "slots": []}
for slot in slots:
if slot.start == time:
slot.rowspan = Timetable.rowspan(times, slot.start, slot.end)
row["slots"].append(slot)
row["colspan"] = self.rooms.consecutive_count()
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)