ernestophone.ens.fr/gestion/views.py

133 lines
5 KiB
Python
Raw Normal View History

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
2021-03-31 16:05:26 +02:00
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
2021-03-31 16:05:26 +02:00
from django.contrib.auth.views import LoginView
from django.urls import reverse_lazy
from django.shortcuts import render_to_response
import string
import random
2015-03-17 19:03:51 +01:00
def generer(*args):
caracteres = string.ascii_letters + string.digits
aleatoire = [random.choice(caracteres) for _ in range(6)]
return ''.join(aleatoire)
2015-03-17 19:03:51 +01:00
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()
2021-03-31 16:05:26 +02:00
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):
2015-03-17 19:03:51 +01:00
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):
2021-03-31 16:05:26 +02:00
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):
2021-03-31 16:05:26 +02:00
photo = Photo.objects.filter(cat='inscription_membre').order_by('?').first()
if request.method == 'POST':
user_form = RegistrationFormUser(request.POST)
comp_form = InscriptionMembreForm(request.POST)
2015-03-17 19:03:51 +01:00
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):
2015-03-28 19:18:14 +01:00
error = "Le champ Validation ne correspond pas à celui attendu"
return render(request, "gestion/registration.html", locals())
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)
except:
temp = False
member.save()
(profile, _) = ErnestoUser.objects.get_or_create(user=member,
slug=code)
comp_form = InscriptionMembreForm(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()
2015-03-17 19:03:51 +01:00
envoi = True
return redirect('thanks')
2015-03-17 19:03:51 +01:00
else:
comp_form = InscriptionMembreForm()
user_form = RegistrationFormUser()
return render(request, 'gestion/registration.html', locals())