diff options
author | Alban Gruin | 2019-08-30 14:01:21 +0200 |
---|---|---|
committer | Alban Gruin | 2019-09-01 13:28:32 +0200 |
commit | 947632bbb0fb1a721bde549f0ca9bf8b8f281ca4 (patch) | |
tree | 4374644f8091829e9596bac9686d1c567b343973 /views.py | |
parent | d1369ea3654b56e6a91335bd108035cd5eecbc76 (diff) |
views: utilisation de TruncWeek
Signed-off-by: Alban Gruin <alban at pa1ch dot fr>
Diffstat (limited to 'views.py')
-rw-r--r-- | views.py | 16 |
1 files changed, 6 insertions, 10 deletions
@@ -17,7 +17,8 @@ import datetime from django.db import connection from django.db.models import Count, Max -from django.db.models.functions import ExtractWeek, ExtractYear, Length +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 @@ -32,7 +33,6 @@ import edt if connection.vendor == "postgresql": from django.contrib.postgres.aggregates import ArrayAgg - from django.db.models.expressions import RawSQL def index(request): @@ -195,9 +195,8 @@ def rooms(request): rooms = Room.objects.filter(course__begin__gte=start, course__begin__lt=end) \ .order_by("name") \ - .annotate(weeks=ArrayAgg( - RawSQL("date_trunc('week', edt_course.begin)", - []), distinct=True)) + .annotate(weeks=ArrayAgg(TruncWeek("week"), + distinct=True)) return render(request, "room_list.html", {"elements": rooms}) @@ -210,8 +209,7 @@ def rooms(request): rooms = Room.objects.filter(course__begin__gte=start, course__begin__lt=end) \ .order_by("name") \ - .annotate(year=ExtractYear("course__begin"), - week=ExtractWeek("course__begin"), + .annotate(week=TruncWeek("course__begin"), c=Count("*")) # Regroupement des semaines dans une liste de chaque objet salle @@ -226,10 +224,8 @@ def rooms(request): room.weeks = [] rooms_weeks.append(room) - # On récupère le premier jour de la semaine - date, _ = get_week(room.year, room.week) # Et on le rajoute dans la liste des semaines de la salle. - rooms_weeks[-1].weeks.append(date) + rooms_weeks[-1].weeks.append(room.week) # Rendu de la page. return render(request, "room_list.html", {"elements": rooms_weeks}) |