aboutsummaryrefslogtreecommitdiff
path: root/views.py
blob: 7bb39166edc32de43f24d02ad65871ae87022c23 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#    Copyright (C) 2017-2018  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/>.

import datetime

from django.db import connection
from django.db.models import Count, Max
from django.db.models.functions import ExtractWeek, ExtractYear, Length, \
    TruncWeek
from django.http import Http404
from django.shortcuts import get_object_or_404, render
from django.utils import timezone
from django.views.decorators.csrf import csrf_exempt

from .forms import QSJPSForm
from .models import Course, Group, Room, Timetable, Year
from .utils import get_current_week, get_current_or_next_week, get_week, \
    group_courses

import edt

if connection.vendor == "postgresql":
    from django.contrib.postgres.aggregates import ArrayAgg


def index(request):
    years = Year.objects.order_by("name")
    return render(request, "year_list.html", {"elements": 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, "elements": timetables})


def group_list(request, year_slug, timetable_slug):
    timetable = get_object_or_404(Timetable, year__slug=year_slug,
                                  slug=timetable_slug)

    start, _ = get_week(*get_current_week())
    end = start + datetime.timedelta(weeks=4)

    groups = Group.objects.get_relevant_groups(start, source=timetable.source,
                                               hidden=False)
    groups_weeks = Course.objects.get_weeks(begin__gte=start, begin__lt=end,
                                            groups__in=groups)

    for group in groups:
        for group_week in groups_weeks:
            if group.corresponds_to(group_week["groups__mention"],
                                    group_week["groups__semester"],
                                    group_week["groups__subgroup"]):
                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, "elements": groups})


def groups_all(request, year_slug, timetable_slug):
    # Récupération de l’emploi du temps et du groupe
    timetable = get_object_or_404(Timetable, year__slug=year_slug,
                                  slug=timetable_slug)
    groups = Group.objects.filter(source=timetable.source).order_by("name")

    # Rendu de la page
    return render(request, "groups_all_list.html",
                  {"timetable": timetable, "elements": groups})


def group_weeks(request, year_slug, timetable_slug, group_slug):
    # Récupération de l’emploi du temps et des groupes
    timetable = get_object_or_404(Timetable, year__slug=year_slug,
                                  slug=timetable_slug)
    group = get_object_or_404(Group, slug=group_slug, source=timetable.source)

    # Groupes parents
    groups = Group.objects.get_parents(group)

    # Récupération de toutes les semaines avec des cours, sans doublons
    courses = Course.objects.filter(groups__in=groups) \
                            .order_by("year", "week") \
                            .annotate(year=ExtractYear("begin"),
                                      week=ExtractWeek("begin")) \
                            .values("year", "week") \
                            .annotate(c=Count("*"))

    # Conversion des semaines de cours en dates
    weeks = [get_week(course["year"], course["week"])[0] for course in courses]

    # Rendu
    return render(request, "group_weeks_list.html",
                  {"timetable": timetable, "group": group,
                   "elements": weeks})


def timetable_common(request, obj, year=None, week=None, timetable=None):
    current_year, current_week = get_current_or_next_week()
    is_old_timetable, provided_week = False, True

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

    try:
        start, end = get_week(year, week)
    except ValueError:
        raise Http404

    courses = Course.objects.get_courses(obj, begin__gte=start, begin__lt=end)
    if not courses.exists() and provided_week:
        raise Http404

    # Récupération des semaines suivantes et précédentes pour les
    # afficher proprement dans l’emploi du temps
    last_course = Course.objects.get_courses(obj, begin__lt=start).last()
    last_week = getattr(last_course, "begin", None)

    next_course = Course.objects.get_courses(obj, begin__gte=end).first()
    next_week = getattr(next_course, "begin", None)

    last_update = courses.aggregate(Max("last_update"))["last_update__max"]
    grouped_courses = group_courses(courses)

    return render(request, "timetable.html",
                  {"group": obj, "courses": grouped_courses,
                   "last_update": last_update,
                   "year": year, "week": int(week),
                   "last_week": last_week,
                   "next_week": next_week,
                   "is_old_timetable": is_old_timetable,
                   "group_mode": isinstance(obj, Group),
                   "timetable": timetable})


def timetable(request, year_slug, timetable_slug, group_slug,
              year=None, week=None):
    timetable = get_object_or_404(Timetable, year__slug=year_slug,
                                  slug=timetable_slug)
    group = get_object_or_404(Group, slug=group_slug, source=timetable.source)

    return timetable_common(request, group, year, week, timetable)


def calendars(request, year_slug, timetable_slug, group_slug):
    timetable = get_object_or_404(Timetable, year__slug=year_slug,
                                  slug=timetable_slug)
    group = get_object_or_404(Group, source=timetable.source, slug=group_slug)
    groups = Group.objects.get_parents(group) \
                          .annotate(length=Length("subgroup")) \
                          .order_by("length")

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


def rooms(request):
    # On récupère les dates allant de cette semaine à dans un mois
    start, _ = get_week(*get_current_week())
    end = start + datetime.timedelta(weeks=4)

    if connection.vendor == "postgresql":
        # Si le SGBD est PostgreSQL, on utilise une requête à base de
        # ArrayAgg. Elle présente l’avantage d’être plus rapide que la
        # requête « généraliste » et de ne pas nécessiter de
        # traitement après.  On récupère chaque salle ayant un cours
        # dans le mois à venir. Pour chacun de ses cours, on ne
        # récupère que le premier jour de la semaine, et si jamais ce
        # jour n’est pas déjà dans la liste des semaines de cours
        # (« weeks »), on l’y rajoute.
        rooms = Room.objects.filter(course__begin__gte=start,
                                    course__begin__lt=end) \
                            .order_by("name") \
                            .annotate(weeks=ArrayAgg(TruncWeek("week"),
                                                     distinct=True))

        return render(request, "room_list.html", {"elements": rooms})

    # Récupération des salles et de toutes les semaines où elles sont
    # concernées.
    # Cette requête associe chaque salle à toutes les semaines où un
    # cours s’y déroule. Le résultat est trié par le nom de la salle
    # et par semaine.
    # TODO optimiser cette requête, elle me semble un peu lente
    rooms = Room.objects.filter(course__begin__gte=start,
                                course__begin__lt=end) \
                        .order_by("name") \
                        .annotate(week=TruncWeek("course__begin"),
                                  c=Count("*"))

    # Regroupement des semaines dans une liste de chaque objet salle
    rooms_weeks = []
    for room in rooms:
        # Si on a pas traité de salle ou que la salle courante
        # dans le résultat de la requête est différente de la dernière
        # dans la liste des salles traitées
        if len(rooms_weeks) == 0 or rooms_weeks[-1].id != room.id:
            # On lui affecte un tableau et on l’ajoute dans
            # la liste des salles à traiter
            room.weeks = []
            rooms_weeks.append(room)

        # Et on le rajoute dans la liste des semaines de la salle.
        rooms_weeks[-1].weeks.append(room.week)

    # Rendu de la page.
    return render(request, "room_list.html", {"elements": rooms_weeks})


def room_weeks(request, room_slug):
    room = get_object_or_404(Room, slug=room_slug)

    # Récupération des semaines de cours
    courses = Course.objects.filter(rooms=room) \
                            .order_by("year", "week") \
                            .annotate(year=ExtractYear("begin"),
                                      week=ExtractWeek("begin")) \
                            .values("year", "week") \
                            .annotate(c=Count("*"))

    weeks = [get_week(course["year"], course["week"])[0] for course in courses]

    return render(request, "room_weeks_list.html",
                  {"room": room, "elements": weeks})


def room_timetable(request, room_slug, year=None, week=None):
    room = get_object_or_404(Room, slug=room_slug)
    return timetable_common(request, room, year, week)


@csrf_exempt
def qsjps(request):
    status = 200

    if request.method == "POST":
        # Si on traite un formulaire, on le valide
        form = QSJPSForm(request.POST)
        if form.is_valid():
            # Formulaire validé
            day = form.cleaned_data["day"]
            begin_hour = form.cleaned_data["begin"]
            end_hour = form.cleaned_data["end"]

            begin = timezone.make_aware(datetime.datetime.combine(day,
                                                                  begin_hour))
            end = timezone.make_aware(datetime.datetime.combine(day, end_hour))

            rooms = Room.objects.qsjps(begin, end)
            return render(request, "qsjps.html",
                          {"elements": rooms, "form": form})

        # Si le formulaire est invalide, on ré-affiche le formulaire
        # avec les erreurs
        status = 400
    else:
        # Si le formulaire n’a pas été soumis, on en instancie un
        # nouveau
        form = QSJPSForm()

    return render(request, "qsjps_form.html", {"form": form}, status=status)


def ctx_processor(request):
    return {"celcatsanitizer_version": edt.VERSION}