diff options
author | Alban Gruin | 2017-01-20 14:37:49 +0100 |
---|---|---|
committer | Alban Gruin | 2017-01-20 14:37:49 +0100 |
commit | a4c9f9027c443a32ff3475dc292c7495f685b651 (patch) | |
tree | fa5bb432284ad6964519e05c79e93cad4a45aae3 | |
parent | 5349208724305c9ec1ea4ab1c51af9969dc586e0 (diff) |
Affichage des emplois du temps par semaine
-rw-r--r-- | templates/index.html | 4 | ||||
-rw-r--r-- | templatetags/dt_week.py | 13 | ||||
-rw-r--r-- | urls.py | 2 | ||||
-rw-r--r-- | views.py | 28 |
4 files changed, 41 insertions, 6 deletions
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 %} <!DOCTYPE html> <html lang="fr"> <head> @@ -11,7 +12,8 @@ <ul> {% for group in groups %} {% if group.timetable.id == timetable.id %} - <li><a href="{% url "timetable" timetable.slug group.slug %}">{{ group.name }}</a></li> + <li>{{ group.name }} – {% for week in group.weeks %}<a href="{% url "timetable" timetable.slug week.year week|dt_week group.slug %}">{{ week|dt_prettyprint }}</a> {% endfor %}</li> + {#<li><a href="{% url "timetable" timetable.slug 2017 52 group.slug %}">{{ group.name }}</a></li>#} {% endif %} {% endfor %} </ul> 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) @@ -4,5 +4,5 @@ from . import views urlpatterns = [ url(r"^$", views.index, name="index"), - url(r"^(?P<timetable_slug>[-\w]+)/(?P<group_slug>[-\w]+)/$", views.timetable, name="timetable"), + url(r"^(?P<timetable_slug>[-\w]+)/(?P<year>[0-9]{4})/(?P<week>[0-53]{1,2})/(?P<group_slug>[-\w]+)/$", views.timetable, name="timetable"), ] @@ -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}) |