2015-11-24 15:09:46 +00:00
|
|
|
# This file contains all the views that concern the user model
|
|
|
|
from django.shortcuts import render, redirect, get_object_or_404
|
2015-11-24 19:09:44 +00:00
|
|
|
from django.contrib.auth import logout as auth_logout
|
2015-11-25 15:20:28 +00:00
|
|
|
from django.contrib.auth import views
|
2015-11-24 19:09:44 +00:00
|
|
|
from django.contrib.auth.forms import PasswordChangeForm
|
2015-11-25 15:20:28 +00:00
|
|
|
from django.core.urlresolvers import reverse
|
2015-11-24 15:09:46 +00:00
|
|
|
import logging
|
|
|
|
|
2015-11-24 19:09:44 +00:00
|
|
|
from core.views.forms import RegisteringForm, LoginForm, UserEditForm
|
|
|
|
from core.models import User
|
2015-11-24 15:09:46 +00:00
|
|
|
|
|
|
|
def register(request):
|
|
|
|
context = {'title': 'Register a user'}
|
|
|
|
if request.method == 'POST':
|
|
|
|
form = RegisteringForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
logging.debug("Registering "+form.cleaned_data['first_name']+form.cleaned_data['last_name'])
|
|
|
|
u = form.save()
|
|
|
|
context['user_registered'] = u
|
|
|
|
context['tests'] = 'TEST_REGISTER_USER_FORM_OK'
|
|
|
|
form = RegisteringForm()
|
|
|
|
else:
|
|
|
|
context['error'] = 'Erreur'
|
|
|
|
context['tests'] = 'TEST_REGISTER_USER_FORM_FAIL'
|
|
|
|
else:
|
|
|
|
form = RegisteringForm()
|
|
|
|
context['form'] = form.as_p()
|
|
|
|
return render(request, "core/register.html", context)
|
|
|
|
|
|
|
|
def login(request):
|
|
|
|
"""
|
|
|
|
The login view
|
|
|
|
|
|
|
|
Needs to be improve with correct handling of form exceptions
|
|
|
|
"""
|
2015-11-25 15:20:28 +00:00
|
|
|
return views.login(request, template_name="core/login.html")
|
2015-11-24 15:09:46 +00:00
|
|
|
|
|
|
|
def logout(request):
|
|
|
|
"""
|
|
|
|
The logout view
|
|
|
|
"""
|
2015-11-25 15:20:28 +00:00
|
|
|
return views.logout_then_login(request)
|
2015-11-24 15:09:46 +00:00
|
|
|
|
|
|
|
def user(request, user_id=None):
|
|
|
|
"""
|
|
|
|
Display a user's profile
|
|
|
|
"""
|
|
|
|
context = {'title': 'View a user'}
|
|
|
|
if user_id == None:
|
|
|
|
context['user_list'] = User.objects.all
|
|
|
|
return render(request, "core/user.html", context)
|
|
|
|
context['profile'] = get_object_or_404(User, pk=user_id)
|
|
|
|
return render(request, "core/user.html", context)
|
|
|
|
|
|
|
|
def user_edit(request, user_id=None):
|
|
|
|
"""
|
|
|
|
This view allows a user, or the allowed users to modify a profile
|
|
|
|
"""
|
|
|
|
user_id = int(user_id)
|
|
|
|
context = {'title': 'Edit a user'}
|
|
|
|
if user_id is not None:
|
|
|
|
user_id = int(user_id)
|
|
|
|
if request.user.is_authenticated() and (request.user.pk == user_id or request.user.is_superuser):
|
|
|
|
p = get_object_or_404(User, pk=user_id)
|
|
|
|
if request.method == 'POST':
|
|
|
|
f = UserEditForm(request.POST, instance=p)
|
|
|
|
# Saving user
|
|
|
|
if f.is_valid():
|
|
|
|
f.save()
|
|
|
|
context['tests'] = "USER_SAVED"
|
|
|
|
else:
|
|
|
|
context['tests'] = "USER_NOT_SAVED"
|
|
|
|
else:
|
|
|
|
f = UserEditForm(instance=p)
|
|
|
|
context['profile'] = p
|
|
|
|
context['user_form'] = f.as_p()
|
|
|
|
return render(request, "core/edit_user.html", context)
|
|
|
|
return user(request, user_id)
|