Sith/core/views/user.py

128 lines
4.0 KiB
Python
Raw Normal View History

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-25 16:03:18 +00:00
from django.contrib.auth import logout as auth_logout, views
from django.core.urlresolvers import reverse
2015-11-26 15:32:56 +00:00
from django.views.generic.edit import UpdateView
from django.views.generic import ListView, DetailView, TemplateView
2015-11-24 15:09:46 +00:00
import logging
2015-12-03 15:47:03 +00:00
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin
2015-12-09 10:01:11 +00:00
from core.views.forms import RegisteringForm, UserPropForm
2015-11-24 19:09:44 +00:00
from core.models import User
2015-11-24 15:09:46 +00:00
2015-11-25 16:03:18 +00:00
def login(request):
"""
The login view
Needs to be improve with correct handling of form exceptions
"""
return views.login(request, template_name="core/login.html")
def logout(request):
"""
The logout view
"""
return views.logout_then_login(request)
def password_change(request):
"""
Allows a user to change its password
"""
2015-11-26 09:57:26 +00:00
return views.password_change(request, template_name="core/password_change.html", post_change_redirect=reverse("core:password_change_done"))
2015-11-25 16:03:18 +00:00
def password_change_done(request):
"""
Allows a user to change its password
"""
return views.password_change_done(request, template_name="core/password_change_done.html")
2015-11-26 09:57:26 +00:00
def password_reset(request):
2015-11-26 10:27:52 +00:00
"""
Allows someone to enter an email adresse for resetting password
"""
2015-11-26 09:57:26 +00:00
return views.password_reset(request,
template_name="core/password_reset.html",
email_template_name="core/password_reset_email.html",
2015-11-26 10:27:52 +00:00
post_reset_redirect="core:password_reset_done",
2015-11-26 09:57:26 +00:00
)
2015-11-25 16:03:18 +00:00
def password_reset_done(request):
2015-11-26 10:27:52 +00:00
"""
Confirm that the reset email has been sent
"""
return views.password_reset_done(request, template_name="core/password_reset_done.html")
2015-11-25 16:03:18 +00:00
2015-11-26 09:57:26 +00:00
def password_reset_confirm(request, uidb64=None, token=None):
2015-11-26 10:27:52 +00:00
"""
Provide a reset password formular
"""
2015-11-26 09:57:26 +00:00
return views.password_reset_confirm(request, uidb64=uidb64, token=token,
post_reset_redirect="core:password_reset_complete",
template_name="core/password_reset_confirm.html",
)
def password_reset_complete(request):
2015-11-26 10:27:52 +00:00
"""
Confirm the password has sucessfully been reset
"""
2015-11-26 09:57:26 +00:00
return views.password_reset_complete(request,
template_name="core/password_reset_complete.html",
)
2015-11-25 16:03:18 +00:00
2015-11-26 15:32:56 +00:00
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)
2015-12-03 15:47:03 +00:00
class UserView(CanViewMixin, DetailView):
2015-11-24 15:09:46 +00:00
"""
Display a user's profile
"""
2015-11-26 15:32:56 +00:00
model = User
pk_url_kwarg = "user_id"
context_object_name = "profile"
class UserListView(ListView):
"""
Displays the user list
"""
model = User
2015-12-03 15:47:03 +00:00
class UserUpdateProfileView(CanEditMixin, UpdateView):
2015-11-26 15:32:56 +00:00
"""
Edit a user's profile
"""
model = User
pk_url_kwarg = "user_id"
template_name = "core/user_edit.html"
fields = ('first_name', 'last_name', 'nick_name', 'email', 'date_of_birth', )
2015-12-09 10:01:11 +00:00
class UserUpdatePropView(CanEditPropMixin, UpdateView):
2015-11-26 15:32:56 +00:00
"""
Edit a user's groups
"""
model = User
pk_url_kwarg = "user_id"
2015-12-09 10:01:11 +00:00
template_name = "core/user_prop.html"
form_class = UserPropForm
2015-11-26 15:32:56 +00:00
class UserToolsView(TemplateView):
"""
Displays the logged user's tools
"""
template_name = "core/user_tools.html"