gestioCOF/gestion/views.py
Martin Pépin 1aed36330f Write modern-style urls
- Proper use of include
- Defining namespaces (I do not use them for now because many urls are
  going to change)
- Do not try to reverse with old-style references: 'cof.views.XXX'
2017-02-23 01:52:55 +01:00

73 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("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
user = request.user
if request.method == "POST":
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
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})