aboutsummaryrefslogtreecommitdiff
path: root/views.py
blob: 442ada6ab84e0bc433d476ff54a3145814d4fd4e (plain)
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
81
82
83
#    Copyright (C) 2017  Alban Gruin
#
#    celcatsanitizer is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published
#    by the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    celcatsanitizer is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with celcatsanitizer.  If not, see <http://www.gnu.org/licenses/>.

from django.conf import settings
from django.db.models import Max
from django.http import Http404
from django.shortcuts import get_object_or_404, render

from .models import Timetable, Group, Course, Year
from .utils import get_current_week, get_week, group_courses

def index(request):
    years = Year.objects.order_by("name")
    return render(request, "index.html", {"years": years})

def mention_list(request, year_slug):
    year = get_object_or_404(Year, slug=year_slug)
    timetables = Timetable.objects.order_by("name").filter(year=year)

    return render(request, "mention_list.html", {"year": year, "timetables": timetables})

def group_list(request, year_slug, timetable_slug):
    timetable = get_object_or_404(Timetable, year__slug=year_slug, slug=timetable_slug)
    groups = Group.objects.get_relevant_groups(timetable=timetable).order_by("name")

    year, week = get_current_week()
    start, _ = get_week(year, week)
    groups_weeks = Course.objects.get_weeks(begin__gte=start, timetable=timetable).values("groups__mention", "groups__subgroup", "groups__td", "groups__tp", "year", "week")

    for group in groups:
        for group_week in groups_weeks:
            if group.corresponds_to(timetable.id, group_week["groups__mention"], group_week["groups__subgroup"], group_week["groups__td"], group_week["groups__tp"]):
                if not hasattr(group, "weeks"):
                    group.weeks = []

                date, _ = get_week(group_week["year"], group_week["week"])
                if date not in group.weeks:
                    group.weeks.append(date)

        if hasattr(group, "weeks"):
            group.weeks.sort()

    return render(request, "group_list.html", {"timetable": timetable, "groups": groups})

def timetable(request, year_slug, timetable_slug, group_slug, year=None, week=None):
    current_year, current_week = get_current_week()
    is_old_timetable = False

    if year is None or week is None:
        year, week = current_year, current_week
    elif (int(year), int(week)) < (current_year, current_week):
        is_old_timetable = True

    start, end = get_week(int(year), int(week))

    timetable = get_object_or_404(Timetable, year__slug=year_slug, 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).annotate(Max("last_update"))
    if courses.count() == 0:
        raise Http404

    grouped_courses = group_courses(courses)

    return render(request, "timetable.html", {"group": group, "courses": grouped_courses,
                                              "last_update": courses.first().last_update__max,
                                              "year": year, "week": int(week),
                                              "is_old_timetable": is_old_timetable})

def contact(request):
    return render(request, "contact.html", {"email": settings.ADMINS[0][1]})