mirror of
https://github.com/ae-utbm/sith.git
synced 2024-11-25 02:24:26 +00:00
Improve user views
This commit is contained in:
parent
385f91a9db
commit
48e3f88b87
@ -63,6 +63,12 @@ class User(AbstractBaseUser, PermissionsMixin):
|
||||
verbose_name = _('user')
|
||||
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):
|
||||
return self.username
|
||||
|
||||
|
@ -10,7 +10,7 @@
|
||||
{% block header %}
|
||||
{% if user.is_authenticated %}Hello, {{ user.username }}!{% endif %}
|
||||
<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:logout' %}">Logout</a></li>
|
||||
<li><a href="{% url 'core:user_list' %}">Users</a></li>
|
||||
|
@ -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 %}
|
||||
|
26
core/templates/core/user_detail.html
Normal file
26
core/templates/core/user_detail.html
Normal 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 %}
|
||||
|
||||
|
14
core/templates/core/user_edit.html
Normal file
14
core/templates/core/user_edit.html
Normal 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 %}
|
||||
|
||||
|
||||
|
||||
|
14
core/templates/core/user_groups.html
Normal file
14
core/templates/core/user_groups.html
Normal 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 %}
|
||||
|
||||
|
||||
|
||||
|
15
core/templates/core/user_list.html
Normal file
15
core/templates/core/user_list.html
Normal 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 %}
|
||||
|
@ -17,9 +17,10 @@ urlpatterns = [
|
||||
url(r'^reset/done/$', password_reset_complete, name='password_reset_complete'),
|
||||
url(r'^register$', register, name='register'),
|
||||
|
||||
url(r'^user/$', user, name='user_list'),
|
||||
url(r'^user/(?P<user_id>[0-9]+)/$', user, name='user_profile'),
|
||||
url(r'^user/(?P<user_id>[0-9]+)/edit$', user_edit, name='user_edit'),
|
||||
url(r'^user/$', UserListView.as_view(), name='user_list'),
|
||||
url(r'^user/(?P<user_id>[0-9]+)/$', UserView.as_view(), name='user_profile'),
|
||||
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/(?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'),
|
||||
|
@ -1,6 +1,7 @@
|
||||
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm, UserChangeForm
|
||||
from django import forms
|
||||
from django.contrib.auth import logout, login, authenticate
|
||||
from django.forms import CheckboxSelectMultiple
|
||||
import logging
|
||||
|
||||
from core.models import User, Page
|
||||
@ -20,39 +21,15 @@ class RegisteringForm(UserCreationForm):
|
||||
user.save()
|
||||
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'
|
||||
required_css_class = 'required'
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ('first_name', 'last_name', 'nick_name', 'email', 'date_of_birth', 'groups', 'user_permissions',)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(UserEditForm, self).__init__(*args, **kwargs)
|
||||
|
||||
def clean_password(self):
|
||||
"""We never handle password in this form"""
|
||||
return
|
||||
fields = ['groups', 'user_permissions',]
|
||||
widgets = {
|
||||
'groups': CheckboxSelectMultiple,
|
||||
'user_permissions': CheckboxSelectMultiple,
|
||||
}
|
||||
|
||||
|
@ -1,12 +1,12 @@
|
||||
# This file contains all the views that concern the user model
|
||||
from django.shortcuts import render, redirect, get_object_or_404
|
||||
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.mail import send_mail
|
||||
from django.views.generic.edit import UpdateView
|
||||
from django.views.generic import ListView, DetailView
|
||||
import logging
|
||||
|
||||
from core.views.forms import RegisteringForm, LoginForm, UserEditForm
|
||||
from core.views.forms import RegisteringForm, UserGroupsForm
|
||||
from core.models import User
|
||||
|
||||
def login(request):
|
||||
@ -68,6 +68,7 @@ def password_reset_complete(request):
|
||||
template_name="core/password_reset_complete.html",
|
||||
)
|
||||
|
||||
|
||||
def register(request):
|
||||
context = {'title': 'Register a user'}
|
||||
if request.method == 'POST':
|
||||
@ -86,38 +87,35 @@ def register(request):
|
||||
context['form'] = form.as_p()
|
||||
return render(request, "core/register.html", context)
|
||||
|
||||
def user(request, user_id=None):
|
||||
class UserView(DetailView):
|
||||
"""
|
||||
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)
|
||||
model = User
|
||||
pk_url_kwarg = "user_id"
|
||||
context_object_name = "profile"
|
||||
|
||||
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)
|
||||
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)
|
||||
model = User
|
||||
|
||||
class UserUpdateProfileView(UpdateView):
|
||||
"""
|
||||
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', )
|
||||
|
||||
class UserUpdateGroupsView(UpdateView):
|
||||
"""
|
||||
Edit a user's groups
|
||||
"""
|
||||
model = User
|
||||
pk_url_kwarg = "user_id"
|
||||
template_name = "core/user_groups.html"
|
||||
form_class = UserGroupsForm
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user