ernestophone.ens.fr/calendrier/views.py

337 lines
12 KiB
Python
Raw Normal View History

2021-04-29 00:27:33 +02:00
import random
import string
from calendar import monthrange
from collections import defaultdict
from datetime import date, datetime
2021-04-29 00:33:58 +02:00
from django.contrib.auth.mixins import LoginRequiredMixin
2021-04-29 00:27:33 +02:00
from django.db.models import Q
from django.shortcuts import get_object_or_404, redirect, render
2018-01-04 23:33:31 +01:00
from django.urls import reverse, reverse_lazy
2021-04-29 00:27:33 +02:00
from django.utils.safestring import mark_safe
2021-04-29 00:33:58 +02:00
from django.views.generic import DeleteView, TemplateView, UpdateView
2015-07-22 22:08:59 +02:00
from actu.models import Actu
2015-04-13 18:56:43 +02:00
from calendrier.calend import EventCalendar
2021-04-29 00:27:33 +02:00
from calendrier.forms import (ChangeDoodleName, EventForm, ModifEventForm,
ParticipantsForm)
from calendrier.models import Event, Participants
2021-04-29 00:27:33 +02:00
from gestion.mixins import ChefRequiredMixin
2021-04-29 00:33:58 +02:00
from gestion.models import Photo
def generer(*args):
caracteres = string.ascii_letters + string.digits
aleatoire = [random.choice(caracteres) for _ in range(6)]
2021-04-29 00:27:33 +02:00
return "".join(aleatoire)
2015-07-22 22:08:59 +02:00
2015-04-13 18:56:43 +02:00
def named_month(pMonthNumber):
2021-04-29 00:27:33 +02:00
return date(1900, pMonthNumber, 1).strftime("%B")
2015-04-13 18:56:43 +02:00
2021-04-29 00:27:33 +02:00
class Agenda(TemplateView):
template_name = "calendrier/agenda.html"
def dispatch(self, request, *args, **kwargs):
if request.user.is_authenticated:
2021-04-29 00:33:58 +02:00
return redirect("calendrier:home")
2021-04-29 00:27:33 +02:00
return super(Agenda, self).dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
lToday = datetime.today()
2021-04-29 00:33:58 +02:00
context["photo"] = Photo.objects.filter(cat="liste").order_by("?").first()
context["events_a_venir"] = (
2021-04-29 00:27:33 +02:00
Event.objects.filter(date__gte=lToday)
.exclude(calendrier__iexact="F")
.order_by("-date")
)
2021-04-29 00:33:58 +02:00
context["events_passe"] = (
2021-04-29 00:27:33 +02:00
Event.objects.filter(date__lt=lToday)
.filter(calendrier__iexact="H")
.order_by("-date")
)
return context
class Calendar(LoginRequiredMixin, TemplateView):
template_name = "calendrier/home.html"
@property
def pYear(self):
2021-04-29 00:33:58 +02:00
return self.kwargs["pYear"]
2021-04-29 00:27:33 +02:00
@property
def pMonth(self):
2021-04-29 00:33:58 +02:00
return self.kwargs["pMonth"]
2021-04-29 00:27:33 +02:00
def get_context_data(self, **kwargs):
context = super(Calendar, self).get_context_data(**kwargs)
actu = Actu.objects.all()
photo = Photo.objects.filter(cat="home").order_by("?").first()
lToday = datetime.now()
lYear = int(self.pYear)
lMonth = int(self.pMonth)
lCalendarFromMonth = datetime(lYear, lMonth, 1)
2021-04-29 00:33:58 +02:00
lCalendarToMonth = datetime(lYear, lMonth, monthrange(lYear, lMonth)[1])
2021-04-29 00:27:33 +02:00
lEvents = Event.objects.filter(
date__gte=lCalendarFromMonth, date__lte=lCalendarToMonth
)
lCalendar = EventCalendar(lEvents).formatmonth(lYear, lMonth)
lPreviousYear = lYear
lPreviousMonth = lMonth - 1
if lPreviousMonth == 0:
lPreviousMonth = 12
lPreviousYear -= 1
lNextYear = lYear
lNextMonth = lMonth + 1
if lNextMonth == 13:
lNextMonth = 1
lNextYear = lYear + 1
lYearAfterThis = lYear + 1
lYearBeforeThis = lYear - 1
try:
events_a_venir_not_answered = Event.objects.filter(
2021-04-29 00:33:58 +02:00
date__gte=lToday
).exclude(participants__participant=self.request.user.profile)
events_a_venir_answered_yes = Event.objects.filter(date__gte=lToday).filter(
2021-04-29 00:27:33 +02:00
Q(participants__participant=self.request.user.profile)
& Q(participants__reponse="oui")
)
2021-04-29 00:33:58 +02:00
events_a_venir_answered_no = Event.objects.filter(date__gte=lToday).filter(
2021-04-29 00:27:33 +02:00
Q(participants__participant=self.request.user.profile)
& Q(participants__reponse="non")
)
2021-04-29 00:33:58 +02:00
events_a_venir_answered_pe = Event.objects.filter(date__gte=lToday).filter(
2021-04-29 00:27:33 +02:00
Q(participants__participant=self.request.user.profile)
& Q(participants__reponse="pe")
)
except Event.DoesNotExist:
events_a_venir_not_answered = Event.objects.filter(
2021-04-29 00:33:58 +02:00
date__gte=lToday
).exclude(participants__participant__user__username=self.request.user)
events_a_venir_answered_yes = Event.objects.filter(date__gte=lToday).filter(
2021-04-29 00:27:33 +02:00
Q(participants__participant__user__username=self.request.user)
& Q(participants__reponse="oui")
)
2021-04-29 00:33:58 +02:00
events_a_venir_answered_no = Event.objects.filter(date__gte=lToday).filter(
2021-04-29 00:27:33 +02:00
Q(participants__participant__user__username=self.request.user)
& Q(participants__reponse="non")
)
2021-04-29 00:33:58 +02:00
events_a_venir_answered_pe = Event.objects.filter(date__gte=lToday).filter(
2021-04-29 00:27:33 +02:00
Q(participants__participant__user__username=self.request.user)
& Q(participants__reponse="pe")
)
context["Calendar"] = mark_safe(lCalendar)
context["Month"] = lMonth
context["MonthName"] = named_month(lMonth)
context["Year"] = lYear
context["PreviousMonth"] = lPreviousMonth
context["PreviousMonthName"] = named_month(lPreviousMonth)
context["PreviousYear"] = lPreviousYear
context["NextMonth"] = lNextMonth
context["NextMonthName"] = named_month(lNextMonth)
context["NextYear"] = lNextYear
context["YearBeforeThis"] = lYearBeforeThis
context["YearAfterThis"] = lYearAfterThis
context["events_a_venir_answered_yes"] = events_a_venir_answered_yes
context["events_a_venir_answered_no"] = events_a_venir_answered_no
context["events_a_venir_answered_pe"] = events_a_venir_answered_pe
context["events_a_venir_not_answered"] = events_a_venir_not_answered
context["actu"] = actu
context["photo"] = photo
return context
class Home(Calendar):
lToday = datetime.now()
2021-04-29 00:27:33 +02:00
@property
def pYear(self):
return self.lToday.year
2015-04-13 18:56:43 +02:00
2021-04-29 00:27:33 +02:00
@property
def pMonth(self):
return self.lToday.month
2021-04-29 00:27:33 +02:00
class ViewEvent(LoginRequiredMixin, TemplateView):
template_name = "calendrier/view_event.html"
2021-04-29 00:27:33 +02:00
def get_context_data(self, **kwargs):
context = super(ViewEvent, self).get_context_data(**kwargs)
2021-04-29 00:33:58 +02:00
event = get_object_or_404(Event, id=self.kwargs["id"])
2021-04-29 00:27:33 +02:00
participants = event.participants_set.all()
multi_instrumentistes = event.participants_set.filter(
Q(participant__multi_instrumentiste="Oui") & ~Q(reponse="non")
)
# Restricted event, only erneso users can see it
if not self.request.user.is_authenticated and not event.calendrier:
return redirect(reverse("calendrier:home"))
# Count the number of occurences of each instrument
instrument_count = defaultdict(lambda: (0, 0, [], [], []))
for participant in participants:
instru = participant.participant.instru
if instru == "Autre":
instru = participant.participant.instru_autre
sure, maybe, namesoui, namespe, namesnon = instrument_count[instru]
if participant.reponse == "oui":
namesoui += [participant.participant.get_doodlename()]
instrument_count[instru] = (
sure + 1,
maybe,
namesoui,
namespe,
namesnon,
)
elif participant.reponse == "pe":
namespe += [participant.participant.get_doodlename()]
instrument_count[instru] = (
sure,
maybe + 1,
namesoui,
namespe,
namesnon,
)
else:
namesnon += [participant.participant.get_doodlename()]
2021-04-29 00:33:58 +02:00
instrument_count[instru] = (sure, maybe, namesoui, namespe, namesnon)
2021-04-29 00:27:33 +02:00
instrument_count = [
(instrument, sure, maybe, namesoui, namespe, namesnon)
for instrument, (
sure,
maybe,
namesoui,
namespe,
namesnon,
) in instrument_count.items()
]
context["event"] = event
context["instrument_count"] = instrument_count
context["participants"] = participants
context["nboui"] = len(participants.filter(reponse="oui"))
context["nbpe"] = len(participants.filter(reponse="pe"))
context["nbnon"] = len(participants.filter(reponse="non"))
context["multi_instrumentistes"] = multi_instrumentistes
return context
class ChangeName(LoginRequiredMixin, TemplateView):
form_class = ChangeDoodleName
template_name = "calendrier/changename.html"
2021-04-29 00:27:33 +02:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
2021-04-29 00:33:58 +02:00
context["form"] = self.form_class(instance=self.request.user)
context["id"] = self.kwargs["id"]
2021-04-29 00:27:33 +02:00
return context
2021-04-29 00:27:33 +02:00
def post(self, request, *args, **kwargs):
success = False
requbis = request.POST.copy()
2021-04-29 00:27:33 +02:00
form = self.form_class(requbis, instance=request.user)
if form.is_valid():
form.save()
success = True
2021-04-29 00:33:58 +02:00
return redirect("calendrier:view-event", id=self.kwargs["id"])
2021-04-29 00:27:33 +02:00
else:
context = self.get_context_data()
2021-04-29 00:33:58 +02:00
context["success"] = success
2021-04-29 00:27:33 +02:00
return render(request, self.template_name, context)
2015-09-23 08:08:26 +02:00
2021-04-29 00:27:33 +02:00
class CreateEvent(LoginRequiredMixin, TemplateView):
form_class = EventForm
template_name = "calendrier/create.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
2021-04-29 00:33:58 +02:00
context["form"] = self.form_class()
2021-04-29 00:27:33 +02:00
return context
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST)
2015-04-13 18:56:43 +02:00
if form.is_valid():
2015-07-22 22:08:59 +02:00
temp = True
while temp:
code = generer()
try:
Event.objects.get(slug=code)
2021-04-29 00:27:33 +02:00
except Event.DoesNotExist:
temp = False
2015-07-22 22:08:59 +02:00
date = form.cleaned_data["date"]
2021-04-29 00:27:33 +02:00
date = date.strftime("%d/%m/%Y")
2015-07-22 22:08:59 +02:00
obj = form.save(commit=False)
obj.slug = code
obj.save()
id = obj.id
2021-04-29 00:27:33 +02:00
return redirect("calendrier:view-event", id=id)
else:
context = self.get_context_data()
return render(request, self.template_name, context)
2021-04-29 00:27:33 +02:00
class ReponseEvent(LoginRequiredMixin, TemplateView):
form_class = ParticipantsForm
template_name = "calendrier/reponse.html"
2021-04-29 00:27:33 +02:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
2021-04-29 00:33:58 +02:00
context["form"] = self.form_class()
context["ev"] = get_object_or_404(Event, id=self.kwargs["id"])
context["id"] = self.kwargs["id"]
2021-04-29 00:27:33 +02:00
return context
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST)
2021-04-29 00:33:58 +02:00
ev = get_object_or_404(Event, id=self.kwargs["id"])
2021-04-29 00:27:33 +02:00
part = request.user.profile
2015-07-22 22:08:59 +02:00
if form.is_valid():
try:
p = Participants.objects.get(event=ev, participant=part)
p.delete()
except Participants.DoesNotExist:
pass
obj = form.save(commit=False)
obj.event = ev
obj.participant = part
obj.save()
2021-04-29 00:33:58 +02:00
return redirect("calendrier:view-event", id=self.kwargs["id"])
2021-04-29 00:27:33 +02:00
else:
context = self.get_context_data()
return render(request, self.template_name, context)
2015-07-22 22:08:59 +02:00
2021-04-29 00:27:33 +02:00
class EventUpdate(ChefRequiredMixin, UpdateView):
2015-04-17 18:38:44 +02:00
model = Event
template_name = "calendrier/update.html"
2015-09-23 08:08:26 +02:00
form_class = ModifEventForm
def get_context_data(self, **kwargs):
ctx = super(EventUpdate, self).get_context_data(**kwargs)
2021-04-29 00:27:33 +02:00
ctx["id"] = self.get_object().id
return ctx
def get_success_url(self):
2021-04-29 00:33:58 +02:00
return reverse("calendrier:view-event", kwargs={"id": self.get_object().id})
2015-04-17 18:38:44 +02:00
2015-07-22 22:08:59 +02:00
2021-04-29 00:27:33 +02:00
class EventDelete(ChefRequiredMixin, DeleteView):
2015-07-22 22:08:59 +02:00
model = Event
template_name = "calendrier/delete.html"
success_url = reverse_lazy("calendrier:home")
def get_context_data(self, **kwargs):
ctx = super(EventDelete, self).get_context_data(**kwargs)
2021-04-29 00:27:33 +02:00
ctx["id"] = self.get_object().id
return ctx