ernestophone.ens.fr/gestion/views.py

96 lines
3.5 KiB
Python
Raw Normal View History

2015-03-17 19:03:51 +01:00
from django.shortcuts import render, redirect
from django.contrib.auth.views import login as django_login_view
from django.contrib.auth.models import User
from django.contrib.auth.forms import AuthenticationForm, PasswordChangeForm
from django.contrib.auth.decorators import login_required
2015-03-17 19:03:51 +01:00
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
2015-04-13 18:56:43 +02:00
from datetime import *
import smtplib
from gestion.forms import InscriptionMembreForm, RegistrationFormUser, ChangeMembreForm
2015-03-17 19:03:51 +01:00
from gestion.models import ErnestoUser
2015-07-22 22:08:59 +02:00
from propositions.utils import generer
2015-03-17 19:03:51 +01:00
2015-04-13 18:56:43 +02:00
from calendrier.views import calendar
2015-03-17 19:03:51 +01:00
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']
2015-03-28 19:18:14 +01:00
if not (comp_form.cleaned_data['validation'] == "Pouet-ta-mere"):
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)
2015-03-17 19:03:51 +01:00
comp_form = InscriptionMembreForm(requbis, instance = profile)
2015-07-22 22:08:59 +02:00
obj = comp_form.save(commit=False)
obj.slug=code
obj.save()
2015-03-17 19:03:51 +01:00
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())
2015-03-17 19:03:51 +01:00
def home(request):
2015-04-13 18:56:43 +02:00
lToday = datetime.now()
return calendar(request, lToday.year, lToday.month)
2015-03-17 19:03:51 +01:00
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())
2015-03-17 19:03:51 +01:00
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 = 'login.html', )
@login_required
2015-07-22 22:08:59 +02:00
def divers(request):
return render(request, "gestion/divers.html", locals())
2015-03-17 19:03:51 +01:00
# Create your views here.