diff options
author | Alban Gruin | 2017-09-09 16:39:30 +0200 |
---|---|---|
committer | Alban Gruin | 2017-09-09 16:39:30 +0200 |
commit | 5c96538dfe708c4429d13397caedeb0b84a65b5a (patch) | |
tree | 855a9f31cb40a35805129d4f9edd3eac274cc11f /views.py | |
parent | ab9df7a174e2debbf9c644c8ec3e2127eeeaa3cf (diff) | |
parent | 60ea5a0e2a5f61f7f764bcf01aafb362781c16da (diff) |
Merge branch 'stable/0.9.z' into prod/pa1ch/0.y.z
Diffstat (limited to 'views.py')
-rw-r--r-- | views.py | 89 |
1 files changed, 25 insertions, 64 deletions
@@ -1,39 +1,44 @@ # Copyright (C) 2017 Alban Gruin # -# celcatsanitizer is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or +# 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 General Public License for more details. +# GNU Affero General Public License for more details. # -# You should have received a copy of the GNU General Public License along -# with celcatsanitizer; if not, write to the Free Software Foundation, Inc., -# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# You should have received a copy of the GNU Affero General Public License +# along with celcatsanitizer. If not, see <http://www.gnu.org/licenses/>. -from django.shortcuts import get_object_or_404, redirect, render -from django.core.mail import send_mail -from django.conf import settings -from django.template import Context, loader +from django.shortcuts import get_object_or_404, render -from .forms import SubscribeForm -from .models import Timetable, LastUpdate, Group, Subscription, Course +from .models import Timetable, LastUpdate, Group, Course, Year from .utils import get_current_week, get_week, group_courses def index(request): - timetables = Timetable.objects.order_by("name") - groups = Group.objects.get_relevant_groups().order_by("name") + years = Year.objects.order_by("name") + return render(request, "index.html", {"years": 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}) + +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") year, week = get_current_week() start, _ = get_week(year, week) - groups_weeks = Course.objects.get_weeks(begin__gte=start).values("groups__timetable", "groups__mention", "groups__subgroup", "groups__td", "groups__tp", "year", "week") + groups_weeks = Course.objects.get_weeks(begin__gte=start, 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(group_week["groups__timetable"], 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 = [] @@ -44,15 +49,15 @@ def index(request): if hasattr(group, "weeks"): group.weeks.sort() - return render(request, "index.html", {"timetables": timetables, "groups": groups}) + return render(request, "group_list.html", {"timetable": timetable, "groups": groups}) -def timetable(request, timetable_slug, group_slug, year=None, week=None): +def timetable(request, year_slug, timetable_slug, group_slug, year=None, week=None): if year is None or week is None: year, week = get_current_week() start, end = get_week(int(year), int(week)) - timetable = get_object_or_404(Timetable, slug=timetable_slug) + 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) @@ -60,47 +65,3 @@ def timetable(request, timetable_slug, group_slug, year=None, week=None): 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)}) - -def subscribe(request, timetable_slug, group_slug, year, week): - timetable = get_object_or_404(Timetable, slug=timetable_slug) - group = get_object_or_404(Group, slug=group_slug, timetable=timetable) - - if request.method == "POST": - form = SubscribeForm(request.POST) - if form.is_valid(): - if not Subscription.objects.filter(email=request.POST["email"], group=group).exists(): - subscription = Subscription(email=request.POST["email"], group=group) - subscription.save() - - template = loader.get_template("mail/mail_confirm.txt") - context = Context({"group": group, "admins": settings.ADMINS, "token": subscription.token, "domain": settings.DEFAULT_DOMAIN}) - send_mail("Confirmation de l’abonnemenent", template.render(context), settings.DEFAULT_FROM_EMAIL, [request.POST["email"]]) - - return redirect("timetable", timetable_slug=timetable_slug, group_slug=group_slug, year=year, week=int(week)) - else: - form = SubscribeForm() - - return render(request, "subscribe.html", {"year": year, "week": week, "group": group, "form": form}) - -def confirm_subscription(request, token): - subscription = Subscription.objects.filter(token=token).first() - year, week = get_current_week() - - if subscription is not None and not subscription.active: - subscription.active = True - subscription.save() - - return redirect("timetable", timetable_slug=subscription.group.timetable.slug, group_slug=subscription.group.slug, year=year, week=week) - -def cancel_subscription(request, token): - subscription = Subscription.objects.filter(token=token).first() - - if subscription is not None: - group = subscription.group - subscription.delete() - - template = loader.get_template("mail/mail_unsubscribed.txt") - context = Context({"group": group}) - send_mail("Confirmation de la désinscription", template.render(context), settings.DEFAULT_FROM_EMAIL, [subscription.email]) - - return redirect("index") |