ernestophone.ens.fr/gestion/views.py
2016-09-23 23:03:08 +02:00

102 lines
3.6 KiB
Python

from django.shortcuts import render
from django.contrib.auth.views import login as django_login_view
from django.contrib.auth.models import User
from django.contrib.auth.forms import PasswordChangeForm
from django.contrib.auth.decorators import login_required
from django.conf import settings
from datetime import datetime
from gestion.forms import InscriptionMembreForm, RegistrationFormUser, \
ChangeMembreForm
from gestion.models import ErnestoUser
from propositions.utils import generer
from calendrier.views import calendar
def inscription_membre(request):
if request.method == 'POST':
requbis = request.POST.copy()
user_form = RegistrationFormUser(requbis)
comp_form = InscriptionMembreForm(requbis)
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(requbis, instance=profile)
obj = comp_form.save(commit=False)
obj.slug = code
obj.save()
envoi = True
return render(request, 'gestion/thanks.html', locals())
else:
comp_form = InscriptionMembreForm()
user_form = RegistrationFormUser()
return render(request, 'gestion/registration.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():
form.save()
success = True
else:
form = PasswordChangeForm(request.user)
return render(request, 'gestion/changepasswd.html', locals())
@login_required
def change_membre(request):
if request.method == 'POST':
requbis = request.POST.copy()
form = ChangeMembreForm(requbis, instance=request.user)
if form.is_valid():
form.save()
success = True
else:
form = ChangeMembreForm(instance=request.user)
return render(request, 'gestion/change.html', locals())
def home(request):
lToday = datetime.now()
return calendar(request, lToday.year, lToday.month)
def login(request):
if request.method == "POST" and "username" in request.POST:
try:
user = User.objects.get(username=request.POST["username"])
if not user.is_active:
error = "Votre compte n'est pas actif"
return render(request, "gestion/login.html", locals())
if not user.has_usable_password() or user.password in ("", "!"):
return render(request, "error.html",
{"error_type": "no_password"})
except User.DoesNotExist:
pass
return django_login_view(request, template_name='gestion/login.html')
@login_required
def divers(request):
return render(request, "gestion/divers.html", locals())
# Create your views here.