from django.http import HttpResponse from django.db.models import Count from django.shortcuts import get_object_or_404, render, render_to_response from edt.models import Timetable, Group, Course from edt.utils import get_week import datetime def index(request): timetables = Timetable.objects.all() groups = Group.objects.filter(tp__isnull=False).order_by("name") for group in groups: courses = Course.objects.get_courses_for_group(group).values("begin").annotate(count=Count("begin")) weeks = [] for course in courses: year, week, _ = course["begin"].isocalendar() date, _ = get_week(year, week) if date not in weeks: weeks.append(date) group.weeks = weeks return render_to_response("index.html", {"timetables": timetables, "groups": groups}) def timetable(request, timetable_slug, year, week, group_slug): start, end = get_week(int(year), int(week)) timetable = get_object_or_404(Timetable, slug=timetable_slug) group = get_object_or_404(Group, slug=group_slug, timetable=timetable) courses = Course.objects.get_courses_for_group(group, begin__gte=start, begin__lt=end) grouped_courses = [] for i, course in enumerate(courses): if i == 0 or courses[i - 1].begin.day != course.begin.day: grouped_courses.append([course]) else: grouped_courses[-1].append(course) return render_to_response("timetable.html", {"timetable": timetable, "group": group, "courses": grouped_courses, "week": week})