ernestophone.ens.fr/gestion/views.py
Lucie Galland 5d1d3f8eff v1.1
2021-03-31 16:05:26 +02:00

132 lines
5 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, Photo
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.contrib.auth.views import LoginView
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()
photo_rep = Photo.objects.filter(cat='home_rep').order_by('?').first()
photo_join = Photo.objects.filter(cat='home_join').order_by('?').first()
photo_contact = Photo.objects.filter(cat='home_contact').order_by('?').first()
return render_to_response( 'gestion/index.html', {"request":request,"categories": categories,'videos':videos,'photo_rep':photo_rep,'photo_join':photo_join,'photo_contact':photo_contact})
class MyLoginView(LoginView):
template_name = 'gestion/login.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
photo = Photo.objects.filter(cat='login').order_by('?').first()
context['photo'] = photo
return context
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):
photo = Photo.objects.filter(cat='change_membre').order_by('?').first()
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):
if request.method == 'POST':
requbis = request.POST.copy()
form = PasswordChangeForm(request.user, data=requbis)
if form.is_valid():
request.user.profile.photo.delete()
form.save()
success = True
else:
form = PasswordChangeForm(request.user)
return render(request, 'gestion/changepasswd.html', locals())
def inscription_membre(request):
photo = Photo.objects.filter(cat='inscription_membre').order_by('?').first()
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())