From a4c9f9027c443a32ff3475dc292c7495f685b651 Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Fri, 20 Jan 2017 14:37:49 +0100 Subject: Affichage des emplois du temps par semaine --- templates/index.html | 4 +++- templatetags/dt_week.py | 13 +++++++++++++ urls.py | 2 +- views.py | 28 ++++++++++++++++++++++++---- 4 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 templatetags/dt_week.py diff --git a/templates/index.html b/templates/index.html index 262d454..7b43411 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,3 +1,4 @@ +{% load dt_week %} @@ -11,7 +12,8 @@ diff --git a/templatetags/dt_week.py b/templatetags/dt_week.py new file mode 100644 index 0000000..714bc0d --- /dev/null +++ b/templatetags/dt_week.py @@ -0,0 +1,13 @@ +from django import template + +import datetime + +register = template.Library() + +@register.filter +def dt_week(dt): + return dt.isocalendar()[1] + +@register.filter +def dt_prettyprint(dt): + return "{0}/{1:02d}/{2:02d}".format(dt.year, dt.month, dt.day) diff --git a/urls.py b/urls.py index 41b61a9..46a15cc 100644 --- a/urls.py +++ b/urls.py @@ -4,5 +4,5 @@ from . import views urlpatterns = [ url(r"^$", views.index, name="index"), - url(r"^(?P[-\w]+)/(?P[-\w]+)/$", views.timetable, name="timetable"), + url(r"^(?P[-\w]+)/(?P[0-9]{4})/(?P[0-53]{1,2})/(?P[-\w]+)/$", views.timetable, name="timetable"), ] diff --git a/views.py b/views.py index 07572e9..d11c026 100644 --- a/views.py +++ b/views.py @@ -1,19 +1,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.db.models import Q +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, group_slug): +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.filter(Q(groups__td__isnull=True) | Q(groups__td=group.td), Q(groups__tp__isnull=True) | Q(groups__tp=group.tp), groups__mention=group.mention, groups__subgroup=group.subgroup).order_by("begin").distinct() + 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}) -- cgit v1.2.1