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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
from django.http import HttpResponse, Http404
from django.db.models import Count
from django.shortcuts import get_object_or_404, redirect, render
from django.utils import timezone
from edt.forms import SubscribeForm
from edt.models import Timetable, LastUpdate, Group, Subscription, Course
from edt.utils import get_week, group_courses
import datetime
def index(request):
timetables = Timetable.objects.all()
groups = Group.objects.filter(tp__isnull=False).order_by("name")
current_year, current_week, _ = timezone.now().isocalendar()
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()
if year >= current_year and week >= current_week:
date, _ = get_week(year, week)
if date not in weeks:
weeks.append(date)
group.weeks = weeks
return render(request, "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)
last_update = LastUpdate.objects.get(timetable=timetable, week=week, year=year)
grouped_courses = group_courses(courses)
return render(request, "timetable.html", {"timetable": timetable, "group": group, "courses": grouped_courses, "last_update": last_update.date, "year": year, "week": week})
def subscribe(request, timetable_slug, year, week, group_slug):
group = get_object_or_404(Group, slug=group_slug)
if request.method == "POST":
form = SubscribeForm(request.POST)
if form.is_valid():
subscription = Subscription(email=request.POST["email"], group=group)
subscription.save()
return redirect("timetable", timetable_slug=timetable_slug, year=year, week=week, group_slug=group_slug)
else:
form = SubscribeForm()
timetable = get_object_or_404(Timetable, slug=timetable_slug)
return render(request, "subscribe.html", {"timetable": timetable, "year": year, "week": week, "group": group, "form": form})
def confirm_subscription(request, token):
subscription = get_object_or_404(Subscription, token=token)
if subscription.active:
raise Http404()
subscription.active = True
subscription.save()
year, week, day = timezone.now().isocalendar()
if day >= 6:
year, week, _ = (timezone.now() + datetime.timedelta(weeks=1)).isocalendar()
return redirect("timetable", timetable_slug=subscription.group.timetable.slug, year=year, week=week, group_slug=subscription.group.slug)
def cancel_subscription(request, token):
subscription = get_object_or_404(Subscription, token=token)
subscription.delete()
return redirect("index")
|