1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
from django.http import HttpResponse
from django.db.models import Count
from django.shortcuts import get_object_or_404, render, render_to_response
from django.utils import timezone
from edt.models import Timetable, Group, Course
import datetime
def get_week(year, week):
start = timezone.make_aware(datetime.datetime.strptime("{0}-W{1:02d}-1".format(year, week), "%Y-W%W-%w"))
end = start + datetime.timedelta(weeks=1)
return start, end
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)
return render_to_response("timetable.html", {"timetable": timetable, "group": group, "courses": courses})
|