diff options
author | Alban Gruin | 2017-10-03 09:28:49 +0200 |
---|---|---|
committer | Alban Gruin | 2017-10-03 09:28:49 +0200 |
commit | ae3358c1296a02352409910a9ffcc2307d5ea87a (patch) | |
tree | b47df3b99f639acc24d751cc0a6a73937c10069b /views.py | |
parent | 9641fc9bb6ef2897c0d70e6d5d5ed3f8e295dbec (diff) | |
parent | 175fcd46f56d8c1c5d10a50b401c09c25cfcdf82 (diff) |
Merge branch 'stable/0.10.z' into prod/pa1ch/0.y.zv0.10.0-pa1ch
Diffstat (limited to 'views.py')
-rw-r--r-- | views.py | 55 |
1 files changed, 40 insertions, 15 deletions
@@ -13,33 +13,39 @@ # 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.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, LastUpdate, Group, Course, Year -from .utils import get_current_week, get_week, group_courses +from .models import Timetable, Group, Course, Year +from .utils import get_current_week, get_current_or_next_week, get_week, group_courses def index(request): years = Year.objects.order_by("name") - return render(request, "index.html", {"years": years}) + return render(request, "index.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, "timetables": timetables}) + return render(request, "index.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) - groups = Group.objects.get_relevant_groups(timetable=timetable).order_by("name") +def group_list_common(request, timetable, groups): + start, _ = get_week(*get_current_week()) + end = start + datetime.timedelta(weeks=4) - 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") + groups_weeks = Course.objects.get_weeks(begin__gte=start, begin__lt=end, 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 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 = [] @@ -52,20 +58,39 @@ def group_list(request, year_slug, timetable_slug): return render(request, "group_list.html", {"timetable": timetable, "groups": groups}) +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") + return group_list_common(request, timetable, groups) + def timetable(request, year_slug, timetable_slug, group_slug, year=None, week=None): + current_year, current_week = get_current_or_next_week() + is_old_timetable = False + if year is None or week is None: - year, week = get_current_week() + 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) - last_update = get_object_or_404(LastUpdate, timetable=timetable, week=week, year=year) - courses = Course.objects.get_courses_for_group(group, begin__gte=start, begin__lt=end) + + if group.children.count(): + return group_list_common(request, timetable, Group.objects.get_relevant_children(group)) + + 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": last_update.date, "year": year, "week": int(week)}) + 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]}) |