ernestophone.ens.fr/gestion/views.py
2020-09-12 19:08:32 +02:00

113 lines
4.1 KiB
Python

from django.shortcuts import render, redirect
from partitions.models import Category
from gestion.forms import ChangeMembreForm, ChangeFormUser,RegistrationFormUser, InscriptionMembreForm
from django.conf import settings
from django.http import HttpResponseRedirect
from gestion.models import ErnestoUser, VideoGallery
from django.contrib.auth.decorators import login_required
from django.contrib.auth.forms import PasswordChangeForm
from calendrier.forms import ChangeDoodleName
from partitions.decorators import chef_required
from gestion.mixins import ChefRequiredMixin
from django.views.generic import CreateView, DeleteView, ListView, UpdateView
from django.urls import reverse_lazy
from django.shortcuts import render_to_response
import string
import random
def generer(*args):
caracteres = string.ascii_letters + string.digits
aleatoire = [random.choice(caracteres) for _ in range(6)]
return ''.join(aleatoire)
def home(request):
if request.user.is_authenticated :
return redirect('calendrier:home')
categories = Category.objects.filter(name = "Partitions actives").prefetch_related("partitionset_set")
videos = VideoGallery.objects.all()
return render_to_response( 'gestion/index.html', {"request":request,"categories": categories,'videos':videos})
def thanks(request):
return render(request, 'gestion/thanks.html', locals())
@login_required
def changename(request):
if request.method == 'POST':
requbis = request.POST.copy()
form = ChangeDoodleName(requbis, instance=request.user)
if form.is_valid():
form.save()
success = True
return redirect('change_membre')
else:
form = ChangeDoodleName(instance=request.user)
return render(request, 'gestion/changename.html', locals())
@login_required
def change_membre(request):
if request.method == 'POST':
comp_form = ChangeMembreForm(request.POST, instance=request.user.profile)
user_form = ChangeFormUser(request.POST, instance=request.user)
if user_form.is_valid() and comp_form.is_valid():
user_form.save()
comp_form.save()
success = True
return redirect('home')
else:
comp_form = ChangeMembreForm(instance=request.user.profile)
user_form = ChangeFormUser(instance=request.user)
return render(request, 'gestion/change.html', locals())
@login_required
def change_password(request):
success = False
if request.method == 'POST':
form = PasswordChangeForm(request.user, data=request.POST)
if form.is_valid():
form.save()
success = True
else:
form = PasswordChangeForm(request.user)
return render(
request, 'gestion/changepasswd.html', {"form": form, "success": success}
)
def inscription_membre(request):
if request.method == 'POST':
user_form = RegistrationFormUser(request.POST)
comp_form = InscriptionMembreForm(request.POST)
if user_form.is_valid() and comp_form.is_valid():
pseudo = user_form.cleaned_data['username']
if not (comp_form.cleaned_data['validation']
== settings.ACCOUNT_CREATION_PASS):
error = "Le champ Validation ne correspond pas à celui attendu"
return render(request, "gestion/registration.html", locals())
member = user_form.save(commit=False)
temp = True
while temp:
code = generer()
try:
ErnestoUser.objects.get(slug=code)
except:
temp = False
member.save()
(profile, _) = ErnestoUser.objects.get_or_create(user=member,
slug=code)
comp_form = InscriptionMembreForm(request.POST, instance=profile)
obj = comp_form.save(commit=False)
obj.slug = code
obj.save()
envoi = True
return redirect('thanks')
else:
comp_form = InscriptionMembreForm()
user_form = RegistrationFormUser()
return render(request, 'gestion/registration.html', locals())