def _save_classrooms(cls, version, schedules): # [ clsrm -> [week_type -> [day -> day_schedule]] ] classrooms = AutoaddDict( lambda _: AutoaddDict( lambda _: AutoaddDict( lambda _: DaySchedule()))) for data in schedules: def process_week(wtype): week_data = data.week(wtype) for (day_id, day) in week_data: for lesson in day.list(): clsdata = \ classrooms[lesson.classroom_id][wtype][day_id] clsdata.set_lesson(lesson.number, lesson) process_week(UPPER_WEEK) process_week(LOWER_WEEK) def conv(lst): p = lst.items() p.sort(lambda t1, t2: cmp(t1[0], t2[0])) return [ i for i in p if len(i[1])>0 ] records = [] for (room_id, wdata) in classrooms.items(): data = ClassroomDataContainer(room_id, conv(wdata[UPPER_WEEK]), conv(wdata[LOWER_WEEK])) classroom_data = GsqlClassroomData.from_data(data) classroom_data.version = version classroom_data.put() records.append(classroom_data) crm = GsqlClassroomDescriptor(version=version, building=room_id.building, number=room_id.number) crm.put() cls._save_free_classrooms(version, records)
def _save_free_classrooms(cls, version, classrooms): # key is [week][building][day][number] free_schedule = AutoaddDict( lambda _: AutoaddDict( lambda _: AutoaddDict( lambda _: AutoaddDict( lambda _: list() ) ) ) ) def process_week(classroom, week): data = classroom.week(week) for (dayid, day) in data: for (number, lesson) in zip(xrange(len(day.lessons())), day.lessons()): if lesson is None: week_type = week.name building = classroom.classroom_id().building classroom_num = classroom.classroom_id().number repr = (unicode(classroom_num), unicode(building)) free_schedule[week_type][building][dayid][number] \ .append(repr) for classroom in classrooms: process_week(classroom, UPPER_WEEK) process_week(classroom, LOWER_WEEK) for (wt, wt_data) in free_schedule.iteritems(): for (b, b_data) in wt_data.iteritems(): for (day, day_data) in b_data.iteritems(): record = GsqlFreeClassroomsView.from_data(day_data, building=b, week=wt, day=day, version=version) record.put()
def free_classrooms(request): conv = lambda x: x != '*' and x or None curr = current_lesson() or 1 frm = int(request.GET.get('lesson', curr)) len = int(request.GET.get('length', 1)) timestamp = { 'week': request.GET.get('week', current_week().name), 'building': conv(request.GET.get('building', '*')), 'day': int(request.GET.get('day', current_weekday())), 'lessons': range(frm, frm + len) } buildings = AutoaddDict(lambda _: list()) for classroom in SOURCE.free_classrooms(**timestamp): buildings[classroom.building].append(classroom) buildings = sorted(buildings.iteritems(), cmp=lambda a, b: cmp(a[0], b[0])) fbuildings = map(lambda x: {'name': x[0], 'classrooms': sorted(x[1], cmp=cid_cmp)}, buildings) inits = dict(request.GET.items()) inits.update(timestamp) inits['lesson'] = frm data = {'form': FindFreeClassroomsForm(initial=inits), 'buildings': fbuildings, 'week_txt': timestamp['week']} return render_response(request, 'free_classrooms.html', data)