kpsul/gestion/views.py
Martin Pépin a28c00e474 Move the auth stuff to gestion/
- The login views are in `gestion/`
- The templates are under `gestion/templates/gestion/`
- `cof/shared.py` moves to `gestion/` and is splitted into 3 files:
    - The auth backends are in `backends.py`.
    - The context_processor is in `context_processor.py`
    - The LOCK/UNLOCK functions remain in `shared.py`
2017-02-12 15:38:14 +01:00

75 lines
2.5 KiB
Python

"""
The common views of the different organisations.
- Authentication
- Profile edition
"""
from django.shortcuts import render, redirect
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
)
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)
@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
else:
user = request.user
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})