gestioCOF/gestion/views.py

75 lines
2.5 KiB
Python
Raw Normal View History

"""
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
from django.contrib.auth.models import User
from django.contrib.auth.views import (
login as django_login, logout as django_logout
)
2017-02-10 23:12:12 +01:00
from .forms import ProfileForm, UserForm
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
if request.method == "POST":
user_form = UserForm(request.POST, instance=request.user)
if user_form.is_valid():
user = user_form.save()
profile_form = ProfileForm(request.POST, instance=user.profile)
if profile_form.is_valid():
profile_form.save()
success = True
2017-02-10 23:12:12 +01:00
else:
user = request.user
2017-02-10 23:12:12 +01:00
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})