from django.http import HttpResponse, Http404 from django.db.models import Count from django.shortcuts import get_object_or_404, redirect, render from django.utils import timezone from django.core.mail import send_mail from django.conf import settings 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 import datetime def index(request): timetables = Timetable.objects.all() groups = Group.objects.filter(tp__isnull=False).order_by("name") year, week = get_current_or_next_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") 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 not hasattr(group, "weeks"): group.weeks = [] date, _ = get_week(group_week["year"], group_week["week"]) if date not in group.weeks: group.weeks.append(date) if hasattr(group, "weeks"): group.weeks.sort() return render(request, "index.html", {"timetables": timetables, "groups": groups}) 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() 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.get_courses_for_group(group, begin__gte=start, begin__lt=end) last_update = LastUpdate.objects.get(timetable=timetable, week=week, year=year) 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(): 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}) send_mail("Confirmation de l'abonnemenent", template.render(context), settings.DEFAULT_FROM_EMAIL, [request.POST["email"]]) return redirect("timetable", timetable_slug=itmetable_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 = get_object_or_404(Subscription, token=token) if subscription.active: raise Http404() subscription.active = True subscription.save() year, week = get_current_or_next_week() return redirect("timetable", group_slug=subscription.group.slug, year=year, week=week) def cancel_subscription(request, token): subscription = get_object_or_404(Subscription, token=token) 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")