Improve user views

This commit is contained in:
Skia 2015-11-26 16:32:56 +01:00
parent 385f91a9db
commit 48e3f88b87
10 changed files with 117 additions and 98 deletions

View File

@ -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

View File

@ -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>

View File

@ -1,32 +0,0 @@
{% extends "core/base.html" %}
{% block title %}
{% if profile %}
{{ profile.get_display_name }}'s profile
{% endif %}
{% if user_list %}
User list
{% endif %}
{% endblock %}
{% block content %}
{% if profile %}
<h3>User Profile</h3>
<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 %}
<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>
{% endif %}
{% endblock %}

View File

@ -0,0 +1,26 @@
{% extends "core/base.html" %}
{% block title %}
{{ profile.get_display_name }}'s profile
{% endblock %}
{% block content %}
<h3>User Profile</h3>
<p><a href="{% url 'core:user_list' %}">Back to list</a></p>
{% if user.is_superuser or user.id == profile.id %}
<ul>
<li><a href="{% url 'core:user_edit' profile.id %}">Edit</a></li>
<li><a href="{% url 'core:user_groups' profile.id %}">Groups</a></li>
</ul>
{% 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 %}

View File

@ -0,0 +1,14 @@
{% 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 %}

View File

@ -0,0 +1,14 @@
{% 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 %}

View File

@ -0,0 +1,15 @@
{% 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 %}

View File

@ -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'),

View File

@ -1,6 +1,7 @@
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):
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

View File

@ -1,12 +1,12 @@
# 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):
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):
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)
return render(request, "core/user.html", context)
def user_edit(request, user_id=None): class UserListView(ListView):
""" """
This view allows a user, or the allowed users to modify a profile Displays the user list
""" """
user_id = int(user_id) model = User
context = {'title': 'Edit a user'}
if user_id is not None: class UserUpdateProfileView(UpdateView):
user_id = int(user_id) """
if request.user.is_authenticated() and (request.user.pk == user_id or request.user.is_superuser): Edit a user's profile
p = get_object_or_404(User, pk=user_id) """
if request.method == 'POST': model = User
f = UserEditForm(request.POST, instance=p) pk_url_kwarg = "user_id"
# Saving user template_name = "core/user_edit.html"
if f.is_valid(): fields = ('first_name', 'last_name', 'nick_name', 'email', 'date_of_birth', )
f.save()
context['tests'] = "USER_SAVED" class UserUpdateGroupsView(UpdateView):
else: """
context['tests'] = "USER_NOT_SAVED" Edit a user's groups
else: """
f = UserEditForm(instance=p) model = User
context['profile'] = p pk_url_kwarg = "user_id"
context['user_form'] = f.as_p() template_name = "core/user_groups.html"
return render(request, "core/edit_user.html", context) form_class = UserGroupsForm
return user(request, user_id)