2017-02-12 14:14:08 +01:00
|
|
|
"""
|
|
|
|
The common views of the different organisations.
|
|
|
|
- Authentication
|
|
|
|
- Profile edition
|
|
|
|
"""
|
|
|
|
|
|
|
|
from django.shortcuts import render, redirect
|
2017-02-10 23:12:12 +01:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2017-02-12 14:14:08 +01:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.contrib.auth.views import (
|
|
|
|
login as django_login, logout as django_logout
|
|
|
|
)
|
2017-02-09 21:04:32 +01:00
|
|
|
|
2017-02-10 23:12:12 +01:00
|
|
|
from .forms import ProfileForm, UserForm
|
|
|
|
|
|
|
|
|
2017-02-12 14:14:08 +01:00
|
|
|
def login(request):
|
|
|
|
if request.user.is_authenticated():
|
|
|
|
return redirect("cof.views.home")
|
|
|
|
context = {}
|
|
|
|
# Fetch the next page from the request data
|
|
|
|
if request.method == "GET" and 'next' in request.GET:
|
|
|
|
context['next'] = request.GET['next']
|
|
|
|
return render(request, "gestion/login_switch.html", context)
|
|
|
|
|
|
|
|
|
|
|
|
def login_ext(request):
|
|
|
|
if request.method == "POST" and "username" in request.POST:
|
|
|
|
try:
|
|
|
|
user = User.objects.get(username=request.POST["username"])
|
|
|
|
if user.profile.login_clipper:
|
|
|
|
return render(request, "gestion/error.html",
|
|
|
|
{"error_type": "use_clipper_login"})
|
|
|
|
if not user.has_usable_password() or user.password in ("", "!"):
|
|
|
|
return render(request, "gestion/error.html",
|
|
|
|
{"error_type": "no_password"})
|
|
|
|
except User.DoesNotExist:
|
|
|
|
pass
|
|
|
|
context = {}
|
|
|
|
# Fetch the next page from the request data
|
|
|
|
if request.method == "GET" and 'next' in request.GET:
|
|
|
|
context['next'] = request.GET['next']
|
|
|
|
if request.method == "POST" and 'next' in request.POST:
|
|
|
|
context['next'] = request.POST['next']
|
|
|
|
return django_login(request, template_name='gestion/login.html',
|
|
|
|
extra_context=context)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def logout(request):
|
|
|
|
if request.user.profile.login_clipper:
|
|
|
|
return redirect("gestion:cas_logout")
|
|
|
|
else:
|
|
|
|
return django_logout(request)
|
|
|
|
|
|
|
|
|
2017-02-10 23:12:12 +01:00
|
|
|
@login_required
|
|
|
|
def profile(request):
|
|
|
|
success = False
|
2017-02-12 18:46:22 +01:00
|
|
|
user = request.user
|
2017-02-10 23:12:12 +01:00
|
|
|
if request.method == "POST":
|
2017-02-12 18:46:22 +01:00
|
|
|
user_form = UserForm(request.POST, instance=user)
|
|
|
|
profile_form = ProfileForm(request.POST, instance=user.profile)
|
|
|
|
if (user_form.is_valid() and profile_form.is_valid()):
|
|
|
|
user_form.save()
|
|
|
|
profile_form.save()
|
|
|
|
success = True
|
2017-02-10 23:12:12 +01:00
|
|
|
else:
|
|
|
|
user_form = UserForm(instance=user)
|
|
|
|
profile_form = ProfileForm(instance=user.profile)
|
|
|
|
return render(request, "gestion/profile.html",
|
|
|
|
{"user_form": user_form, "profile_form": profile_form,
|
|
|
|
"success": success})
|