aboutsummaryrefslogtreecommitdiff
path: root/views.py
diff options
context:
space:
mode:
Diffstat (limited to 'views.py')
-rw-r--r--views.py45
1 files changed, 30 insertions, 15 deletions
diff --git a/views.py b/views.py
index a7898c2..273720a 100644
--- a/views.py
+++ b/views.py
@@ -1,3 +1,19 @@
+# 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
+# (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.
+#
+# 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.
+
from django.http import HttpResponse, Http404
from django.db.models import Count
from django.shortcuts import get_object_or_404, redirect, render
@@ -8,7 +24,7 @@ from django.template import Context, loader
from edt.forms import SubscribeForm
from edt.models import Timetable, LastUpdate, Group, Subscription, Course
-from edt.utils import get_current_or_next_week, get_week, group_courses
+from edt.utils import get_current_week, get_week, group_courses
import datetime
@@ -16,7 +32,7 @@ def index(request):
timetables = Timetable.objects.all()
groups = Group.objects.filter(tp__isnull=False).order_by("name")
- year, week = get_current_or_next_week()
+ 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")
@@ -37,7 +53,7 @@ def index(request):
def timetable(request, timetable_slug, group_slug, year=None, week=None):
if year is None or week is None:
- year, week = get_current_or_next_week()
+ year, week = get_current_week()
start, end = get_week(int(year), int(week))
@@ -57,12 +73,13 @@ def subscribe(request, timetable_slug, group_slug, year, week):
if request.method == "POST":
form = SubscribeForm(request.POST)
if form.is_valid():
- subscription = Subscription(email=request.POST["email"], group=group)
- subscription.save()
+ 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"]])
+ 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:
@@ -71,14 +88,12 @@ def subscribe(request, timetable_slug, group_slug, year, week):
return render(request, "subscribe.html", {"year": year, "week": week, "group": group, "form": form})
def confirm_subscription(request, token):
- subscription = get_object_or_404(Subscription, token=token)
- if subscription.active:
- raise Http404()
-
- subscription.active = True
- subscription.save()
+ subscription = Subscription.objects.filter(token=token).first()
+ year, week = get_current_week()
- year, week = get_current_or_next_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)