Commit 48e3f88b authored by Skia's avatar Skia

Improve user views

parent 385f91a9
...@@ -63,6 +63,12 @@ class User(AbstractBaseUser, PermissionsMixin): ...@@ -63,6 +63,12 @@ class User(AbstractBaseUser, PermissionsMixin):
verbose_name = _('user') verbose_name = _('user')
verbose_name_plural = _('users') verbose_name_plural = _('users')
def get_absolute_url(self):
"""
This is needed for black magic powered UpdateView's children
"""
return reverse('core:user_profile', kwargs={'user_id': self.pk})
def __str__(self): def __str__(self):
return self.username return self.username
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
{% block header %} {% block header %}
{% if user.is_authenticated %}Hello, {{ user.username }}!{% endif %} {% if user.is_authenticated %}Hello, {{ user.username }}!{% endif %}
<ul> <ul>
<li><a href="">Register</a></li> <li><a href="{% url 'core:register' %}">Register</a></li>
<li><a href="{% url 'core:login' %}">Login</a></li> <li><a href="{% url 'core:login' %}">Login</a></li>
<li><a href="{% url 'core:logout' %}">Logout</a></li> <li><a href="{% url 'core:logout' %}">Logout</a></li>
<li><a href="{% url 'core:user_list' %}">Users</a></li> <li><a href="{% url 'core:user_list' %}">Users</a></li>
......
{% extends "core/base.html" %} {% extends "core/base.html" %}
{% block title %} {% block title %}
{% if profile %}
{{ profile.get_display_name }}'s profile {{ profile.get_display_name }}'s profile
{% endif %}
{% if user_list %}
User list
{% endif %}
{% endblock %} {% endblock %}
{% block content %} {% block content %}
{% if profile %}
<h3>User Profile</h3> <h3>User Profile</h3>
<p><a href="{% url 'core:user_list' %}">Back to list</a></p> <p><a href="{% url 'core:user_list' %}">Back to list</a></p>
{% if user.is_superuser or user.id == profile.id %}
<p><a href="{% url 'core:user_edit' profile.id %}">Edit</a></p>
{% endif %}
<p>You're seeing the profile of <strong>{{ profile.get_display_name }}</strong></p>
<p>{{ profile.nick_name }}</p>
{% endif %}
{% if user_list %} {% if user.is_superuser or user.id == profile.id %}
<h3>User list</h3>
<ul> <ul>
{% for u in user_list %} <li><a href="{% url 'core:user_edit' profile.id %}">Edit</a></li>
<li><a href="{% url 'core:user_profile' u.id %}">{{ u.get_display_name }}</a></li> <li><a href="{% url 'core:user_groups' profile.id %}">Groups</a></li>
{% endfor %}
</ul> </ul>
{% endif %} {% endif %}
<p>
You're seeing the profile of <strong>{{ profile.get_full_name }}</strong><br/>
<strong>{{ profile.nick_name }}</strong><br/>
<em>{{ profile.date_of_birth|date:"d/m/Y" }}</em>
</p>
{% endblock %} {% endblock %}
{% extends "core/base.html" %}
{% block content %}
<h2>Edit user profile</h2>
<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<p><input type="submit" value="Update" /></p>
</form>
{% endblock %}
{% extends "core/base.html" %}
{% block content %}
<h2>Edit user groups</h2>
<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<p><input type="submit" value="Update" /></p>
</form>
{% endblock %}
{% extends "core/base.html" %}
{% block title %}
User list
{% endblock %}
{% block content %}
<h3>User list</h3>
<ul>
{% for u in user_list %}
<li><a href="{% url 'core:user_profile' u.id %}">{{ u.get_display_name }}</a></li>
{% endfor %}
</ul>
{% endblock %}
...@@ -17,9 +17,10 @@ urlpatterns = [ ...@@ -17,9 +17,10 @@ urlpatterns = [
url(r'^reset/done/$', password_reset_complete, name='password_reset_complete'), url(r'^reset/done/$', password_reset_complete, name='password_reset_complete'),
url(r'^register$', register, name='register'), url(r'^register$', register, name='register'),
url(r'^user/$', user, name='user_list'), url(r'^user/$', UserListView.as_view(), name='user_list'),
url(r'^user/(?P<user_id>[0-9]+)/$', user, name='user_profile'), url(r'^user/(?P<user_id>[0-9]+)/$', UserView.as_view(), name='user_profile'),
url(r'^user/(?P<user_id>[0-9]+)/edit$', user_edit, name='user_edit'), url(r'^user/(?P<user_id>[0-9]+)/edit$', UserUpdateProfileView.as_view(), name='user_edit'),
url(r'^user/(?P<user_id>[0-9]+)/groups$', UserUpdateGroupsView.as_view(), name='user_groups'),
url(r'^page/$', PageListView.as_view(), name='page_list'), url(r'^page/$', PageListView.as_view(), name='page_list'),
url(r'^page/(?P<page_name>[a-z0-9/]*)/$', PageView.as_view(), name='page'), url(r'^page/(?P<page_name>[a-z0-9/]*)/$', PageView.as_view(), name='page'),
url(r'^page/(?P<page_name>[a-z0-9/]*)/edit$', PageEditView.as_view(), name='page_edit'), url(r'^page/(?P<page_name>[a-z0-9/]*)/edit$', PageEditView.as_view(), name='page_edit'),
......
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm, UserChangeForm from django.contrib.auth.forms import UserCreationForm, AuthenticationForm, UserChangeForm
from django import forms from django import forms
from django.contrib.auth import logout, login, authenticate from django.contrib.auth import logout, login, authenticate
from django.forms import CheckboxSelectMultiple
import logging import logging
from core.models import User, Page from core.models import User, Page
...@@ -20,39 +21,15 @@ class RegisteringForm(UserCreationForm): ...@@ -20,39 +21,15 @@ class RegisteringForm(UserCreationForm):
user.save() user.save()
return user return user
class LoginForm(AuthenticationForm):
def login(self):
u = authenticate(username=self.request.POST['username'],
password=self.request.POST['password'])
if u is not None:
if u.is_active:
login(self.request, u)
logging.debug("Logging in "+str(u))
else:
raise forms.ValidationError(
self.error_messages['invalid_login'],
code='inactive',
params={'username': self.username_field.verbose_name},
)
else:
logging.debug("Login failed")
raise forms.ValidationError(
self.error_messages['invalid_login'],
code='invalid_login',
params={'username': self.username_field.verbose_name},
)
class UserEditForm(UserChangeForm): class UserGroupsForm(forms.ModelForm):
error_css_class = 'error' error_css_class = 'error'
required_css_class = 'required' required_css_class = 'required'
class Meta: class Meta:
model = User model = User
fields = ('first_name', 'last_name', 'nick_name', 'email', 'date_of_birth', 'groups', 'user_permissions',) fields = ['groups', 'user_permissions',]
widgets = {
def __init__(self, *args, **kwargs): 'groups': CheckboxSelectMultiple,
super(UserEditForm, self).__init__(*args, **kwargs) 'user_permissions': CheckboxSelectMultiple,
}
def clean_password(self):
"""We never handle password in this form"""
return
# This file contains all the views that concern the user model # This file contains all the views that concern the user model
from django.shortcuts import render, redirect, get_object_or_404 from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth import logout as auth_logout, views from django.contrib.auth import logout as auth_logout, views
from django.contrib.auth.forms import PasswordChangeForm
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.core.mail import send_mail from django.views.generic.edit import UpdateView
from django.views.generic import ListView, DetailView
import logging import logging
from core.views.forms import RegisteringForm, LoginForm, UserEditForm from core.views.forms import RegisteringForm, UserGroupsForm
from core.models import User from core.models import User
def login(request): def login(request):
...@@ -68,6 +68,7 @@ def password_reset_complete(request): ...@@ -68,6 +68,7 @@ def password_reset_complete(request):
template_name="core/password_reset_complete.html", template_name="core/password_reset_complete.html",
) )
def register(request): def register(request):
context = {'title': 'Register a user'} context = {'title': 'Register a user'}
if request.method == 'POST': if request.method == 'POST':
...@@ -86,38 +87,35 @@ def register(request): ...@@ -86,38 +87,35 @@ def register(request):
context['form'] = form.as_p() context['form'] = form.as_p()
return render(request, "core/register.html", context) return render(request, "core/register.html", context)
def user(request, user_id=None): class UserView(DetailView):
""" """
Display a user's profile Display a user's profile
""" """
context = {'title': 'View a user'} model = User
if user_id == None: pk_url_kwarg = "user_id"
context['user_list'] = User.objects.all context_object_name = "profile"
return render(request, "core/user.html", context)
context['profile'] = get_object_or_404(User, pk=user_id) class UserListView(ListView):
return render(request, "core/user.html", context) """
Displays the user list
def user_edit(request, user_id=None): """
""" model = User
This view allows a user, or the allowed users to modify a profile
""" class UserUpdateProfileView(UpdateView):
user_id = int(user_id) """
context = {'title': 'Edit a user'} Edit a user's profile
if user_id is not None: """
user_id = int(user_id) model = User
if request.user.is_authenticated() and (request.user.pk == user_id or request.user.is_superuser): pk_url_kwarg = "user_id"
p = get_object_or_404(User, pk=user_id) template_name = "core/user_edit.html"
if request.method == 'POST': fields = ('first_name', 'last_name', 'nick_name', 'email', 'date_of_birth', )
f = UserEditForm(request.POST, instance=p)
# Saving user class UserUpdateGroupsView(UpdateView):
if f.is_valid(): """
f.save() Edit a user's groups
context['tests'] = "USER_SAVED" """
else: model = User
context['tests'] = "USER_NOT_SAVED" pk_url_kwarg = "user_id"
else: template_name = "core/user_groups.html"
f = UserEditForm(instance=p) form_class = UserGroupsForm
context['profile'] = p
context['user_form'] = f.as_p()
return render(request, "core/edit_user.html", context)
return user(request, user_id)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment