ernestophone.ens.fr/gestion/views.py

275 lines
9.1 KiB
Python
Raw Permalink Normal View History

2022-01-06 13:11:16 +01:00
import os
2021-04-29 00:27:33 +02:00
import random
import string
from django.conf import settings
from django.contrib.auth.forms import PasswordChangeForm
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.contrib.auth.views import LoginView
from django.http import HttpResponseRedirect
2021-04-29 00:27:33 +02:00
from django.shortcuts import redirect, render
from django.urls import reverse_lazy
2021-04-29 00:33:58 +02:00
from django.utils.translation import gettext_lazy as _
from django.views.generic import (CreateView, DeleteView, ListView,
TemplateView, UpdateView)
2021-04-29 00:27:33 +02:00
from calendrier.forms import ChangeDoodleName
2021-04-29 00:27:33 +02:00
from gestion.forms import (ChangeFormUser, ChangeMembreForm,
InscriptionMembreForm, RegistrationFormUser)
2022-01-06 13:11:16 +01:00
from gestion.mixins import (AllChefRequiredMixin, ChefComRequiredMixin,
ChefRequiredMixin)
2021-04-29 00:27:33 +02:00
from gestion.models import ErnestoUser, Photo, VideoGallery
from partitions.models import Category
2015-03-17 19:03:51 +01:00
def generer(*args):
2021-10-23 11:16:07 +02:00
caracteres = string.ascii_letters + string.digits
2021-04-29 00:27:33 +02:00
aleatoire = [random.choice(caracteres) for i in range(6)]
return "".join(aleatoire)
class Home(TemplateView):
template_name = "gestion/index.html"
2021-04-29 00:27:33 +02:00
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(Home, self).dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["categories"] = Category.objects.filter(
2021-04-29 00:33:58 +02:00
name="Partitions actives"
).prefetch_related("partitionset_set")
context["videos"] = VideoGallery.objects.all().order_by("order")
2021-04-29 00:33:58 +02:00
context["photo_rep"] = (
Photo.objects.filter(cat="home_rep").order_by("?").first()
)
context["photo_join"] = (
Photo.objects.filter(cat="home_join").order_by("?").first()
)
context["photo_contact"] = (
Photo.objects.filter(cat="home_contact").order_by("?").first()
)
2021-04-29 00:27:33 +02:00
return context
2015-03-17 19:03:51 +01:00
2021-03-31 16:05:26 +02:00
class MyLoginView(LoginView):
2021-04-29 00:27:33 +02:00
template_name = "gestion/login.html"
2021-03-31 16:05:26 +02:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
2021-04-29 00:33:58 +02:00
context["photo"] = Photo.objects.filter(cat="login").order_by("?").first()
2021-03-31 16:05:26 +02:00
return context
2021-04-29 00:27:33 +02:00
class Thanks(TemplateView):
template_name = "gestion/thanks.html"
2021-05-27 14:27:30 +02:00
class Social(LoginRequiredMixin, TemplateView):
template_name = "gestion/social.html"
class Profil(LoginRequiredMixin, TemplateView):
template_name = "gestion/profile.html"
2021-10-23 11:16:07 +02:00
class Chef(AllChefRequiredMixin, TemplateView):
2021-05-27 14:27:30 +02:00
template_name = "gestion/chef.html"
2022-01-06 13:11:16 +01:00
2021-12-16 11:43:25 +01:00
class YearBook2021(TemplateView):
2021-11-25 10:00:01 +01:00
template_name = "gestion/yearbook2021.html"
2021-05-27 14:27:30 +02:00
2021-04-29 00:27:33 +02:00
class ChangeName(LoginRequiredMixin, TemplateView):
form_class = ChangeDoodleName
template_name = "gestion/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)
2021-04-29 00:27:33 +02:00
return context
def post(self, request, *args, **kwargs):
2015-03-17 19:03:51 +01:00
requbis = request.POST.copy()
2021-04-29 00:33:58 +02:00
form = self.form_class(requbis, instance=request.user)
if form.is_valid():
form.save()
2021-04-29 00:27:33 +02:00
return redirect("change_membre")
2021-04-29 00:27:33 +02:00
context = self.get_context_data()
2021-04-29 00:33:58 +02:00
context["form"] = form
2021-04-29 00:27:33 +02:00
return render(request, self.template_name, context)
2021-04-29 00:27:33 +02:00
class ChangeMembre(LoginRequiredMixin, TemplateView):
comp_form_class = ChangeMembreForm
user_form_class = ChangeFormUser
template_name = "gestion/change.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
comp_form = self.comp_form_class(instance=self.request.user.profile)
user_form = self.user_form_class(instance=self.request.user)
context["comp_form"] = comp_form
context["user_form"] = user_form
2021-04-29 00:33:58 +02:00
context["photo"] = (
Photo.objects.filter(cat="change_membre").order_by("?").first()
)
2021-04-29 00:27:33 +02:00
return context
def post(self, request, *args, **kwargs):
success = False
2021-04-29 00:33:58 +02:00
comp_form = self.comp_form_class(request.POST, instance=request.user.profile)
2021-04-29 00:27:33 +02:00
user_form = self.user_form_class(request.POST, instance=request.user)
if user_form.is_valid() and comp_form.is_valid():
user_form.save()
comp_form.save()
success = True
2021-04-29 00:27:33 +02:00
context = self.get_context_data()
2021-04-29 00:33:58 +02:00
context["success"] = success
context["comp_form"] = comp_form
context["user_form"] = user_form
2021-04-29 00:27:33 +02:00
return render(request, self.template_name, context)
class ChangePassword(LoginRequiredMixin, TemplateView):
form_class = PasswordChangeForm
template_name = "gestion/changepasswd.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(self.request.user)
2021-04-29 00:27:33 +02:00
return context
def post(self, request, *args, **kwargs):
requbis = request.POST.copy()
2021-04-29 00:27:33 +02:00
success = False
form = self.form_class(request.user, data=requbis)
if form.is_valid():
form.save()
success = True
2021-04-29 00:27:33 +02:00
context = self.get_context_data()
2021-04-29 00:33:58 +02:00
context["success"] = success
context["form"] = form
2021-04-29 00:27:33 +02:00
return render(request, self.template_name, context)
2021-04-29 00:27:33 +02:00
class Inscription(TemplateView):
user_form_class = RegistrationFormUser
comp_form_class = InscriptionMembreForm
template_name = "gestion/registration.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["comp_form"] = self.comp_form_class()
context["user_form"] = self.user_form_class()
context["photo"] = (
Photo.objects.filter(cat="inscription_membre").order_by("?").first()
)
2021-04-29 00:27:33 +02:00
return context
2021-04-29 00:27:33 +02:00
def post(self, request, *args, **kwargs):
user_form = self.user_form_class(request.POST)
comp_form = self.comp_form_class(request.POST)
2015-03-17 19:03:51 +01:00
if user_form.is_valid() and comp_form.is_valid():
2021-04-29 00:27:33 +02:00
if not (
comp_form.cleaned_data["validation"] == settings.ACCOUNT_CREATION_PASS
):
error = _("Le champ Validation ne correspond pas à celui attendu")
context = self.get_context_data()
2021-04-29 00:33:58 +02:00
context["user_form"] = user_form
context["comp_form"] = comp_form
context["error"] = error
2021-04-29 00:27:33 +02:00
return render(request, self.template_name, context)
2015-07-22 22:08:59 +02:00
member = user_form.save(commit=False)
temp = True
while temp:
code = generer()
try:
ErnestoUser.objects.get(slug=code)
2021-04-29 00:27:33 +02:00
except ErnestoUser.DoesNotExist:
temp = False
member.save()
2021-04-29 00:33:58 +02:00
(profile, k) = ErnestoUser.objects.get_or_create(user=member, slug=code)
2021-04-29 00:27:33 +02:00
comp_form = self.comp_form_class(request.POST, instance=profile)
2015-07-22 22:08:59 +02:00
obj = comp_form.save(commit=False)
obj.slug = code
2015-07-22 22:08:59 +02:00
obj.save()
2021-04-29 00:27:33 +02:00
return redirect("thanks")
else:
context = self.get_context_data()
2021-04-29 00:33:58 +02:00
context["user_form"] = user_form
context["comp_form"] = comp_form
2021-04-29 00:27:33 +02:00
return render(request, self.template_name, context)
2021-10-23 11:16:07 +02:00
class PhotoList(ChefComRequiredMixin, ListView):
model = Photo
context_object_name = "photos"
ordering = "cat"
template_name = "gestion/photo.html"
2021-10-23 11:16:07 +02:00
class PhotoCreate(ChefComRequiredMixin, CreateView):
model = Photo
fields = ["name", "cat", "auteur", "url", "color", "image"]
template_name = "gestion/create_photo.html"
success_url = reverse_lazy("liste_photo")
def form_valid(self, form):
photo = form.save(commit=False)
photo.save()
return HttpResponseRedirect(self.success_url)
2021-10-23 11:16:07 +02:00
class PhotoUpdate(ChefComRequiredMixin, UpdateView):
model = Photo
fields = ["name", "cat", "auteur", "url", "color", "image"]
template_name = "gestion/update_photo.html"
success_url = reverse_lazy("liste_photo")
2021-10-23 11:16:07 +02:00
class PhotoDelete(ChefComRequiredMixin, DeleteView):
model = Photo
template_name = "gestion/delete_photo.html"
success_url = reverse_lazy("liste_photo")
2021-10-23 11:16:07 +02:00
class VideoList(ChefComRequiredMixin, ListView):
model = VideoGallery
ordering = "order"
context_object_name = "videos"
template_name = "gestion/video.html"
2021-10-23 11:16:07 +02:00
class VideoCreate(ChefComRequiredMixin, CreateView):
model = VideoGallery
fields = ["name", "url", "order"]
template_name = "gestion/create_video.html"
success_url = reverse_lazy("liste_video")
def form_valid(self, form):
video = form.save(commit=False)
video.save()
return HttpResponseRedirect(self.success_url)
2021-10-23 11:16:07 +02:00
class VideoUpdate(ChefComRequiredMixin, UpdateView):
model = VideoGallery
fields = ["name", "url", "order"]
template_name = "gestion/update_video.html"
success_url = reverse_lazy("liste_video")
2021-10-23 11:16:07 +02:00
class VideoDelete(ChefComRequiredMixin, DeleteView):
model = VideoGallery
template_name = "gestion/delete_video.html"
success_url = reverse_lazy("liste_video")