ernestophone.ens.fr/gestion/views.py
2021-04-29 00:27:33 +02:00

187 lines
7 KiB
Python

import random
import string
from django.conf import settings
from django.contrib.auth.forms import PasswordChangeForm
from django.contrib.auth.views import LoginView
from django.shortcuts import redirect, render
from calendrier.forms import ChangeDoodleName
from gestion.forms import (ChangeFormUser, ChangeMembreForm,
InscriptionMembreForm, RegistrationFormUser)
from gestion.models import ErnestoUser, Photo, VideoGallery
from partitions.models import Category
from django.views.generic import TemplateView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.utils.translation import gettext_lazy as _
def generer(*args):
caracteres = string.ascii_letters + string.digits
aleatoire = [random.choice(caracteres) for i in range(6)]
return "".join(aleatoire)
class Home(TemplateView):
template_name = "gestion/index.html"
def dispatch(self, request, *args, **kwargs):
if request.user.is_authenticated:
return redirect('calendrier:home')
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(
name="Partitions actives").prefetch_related(
"partitionset_set"
)
context['videos'] = VideoGallery.objects.all()
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()
return context
class MyLoginView(LoginView):
template_name = "gestion/login.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["photo"] = Photo.objects.filter(
cat="login").order_by("?").first()
return context
class Thanks(TemplateView):
template_name = "gestion/thanks.html"
class ChangeName(LoginRequiredMixin, TemplateView):
form_class = ChangeDoodleName
template_name = "gestion/changename.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['form'] = self.form_class(instance=self.request.user)
return context
def post(self, request, *args, **kwargs):
requbis = request.POST.copy()
form = self.form_class(requbis,
instance=request.user)
if form.is_valid():
form.save()
return redirect("change_membre")
context = self.get_context_data()
context['form'] = form
return render(request, self.template_name, context)
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
context["photo"] = Photo.objects.filter(
cat="change_membre").order_by("?").first()
return context
def post(self, request, *args, **kwargs):
success = False
comp_form = self.comp_form_class(request.POST,
instance=request.user.profile)
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
context = self.get_context_data()
context['success'] = success
context['comp_form'] = comp_form
context['user_form'] = user_form
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)
context['form'] = self.form_class(self.request.user)
return context
def post(self, request, *args, **kwargs):
requbis = request.POST.copy()
success = False
form = self.form_class(request.user, data=requbis)
if form.is_valid():
form.save()
success = True
context = self.get_context_data()
context['success'] = success
context['form'] = form
return render(request, self.template_name, context)
class Inscription(TemplateView):
user_form_class = RegistrationFormUser
comp_form_class = InscriptionMembreForm
template_name = "gestion/registration.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
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()
return context
def post(self, request, *args, **kwargs):
user_form = self.user_form_class(request.POST)
comp_form = self.comp_form_class(request.POST)
if user_form.is_valid() and comp_form.is_valid():
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()
context['user_form'] = user_form
context['comp_form'] = comp_form
context['error'] = error
return render(request, self.template_name, context)
member = user_form.save(commit=False)
temp = True
while temp:
code = generer()
try:
ErnestoUser.objects.get(slug=code)
except ErnestoUser.DoesNotExist:
temp = False
member.save()
(profile, k) = ErnestoUser.objects.get_or_create(user=member,
slug=code)
comp_form = self.comp_form_class(request.POST, instance=profile)
obj = comp_form.save(commit=False)
obj.slug = code
obj.save()
return redirect("thanks")
else:
context = self.get_context_data()
context['user_form'] = user_form
context['comp_form'] = comp_form
return render(request, self.template_name, context)