Buttons in user admin

This commit is contained in:
Alexandre Iooss 2019-08-08 21:51:37 +02:00
parent 0c504e85fc
commit c225f5045e
No known key found for this signature in database
GPG Key ID: 6C79278F3FCDCC02
6 changed files with 113 additions and 32 deletions

View File

@ -30,7 +30,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
{% block content %} {% block content %}
{% if users_list %} {% if users_list %}
<h2>Résultats dans les utilisateurs</h2> <h2>Résultats dans les utilisateurs</h2>
{% include "users/aff_users.html" with users_list=users_list %} {% include "search/aff_users.html" with users_list=users_list %}
{% endif%} {% endif%}
{% if emprunts_list %} {% if emprunts_list %}
<h2>Résultats dans les emprunt : </h2> <h2>Résultats dans les emprunt : </h2>

View File

@ -4,6 +4,8 @@
from django.contrib import admin from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.core.urlresolvers import reverse
from django.utils.html import format_html
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from reversion.admin import VersionAdmin from reversion.admin import VersionAdmin
@ -54,7 +56,7 @@ class UserAdmin(VersionAdmin, BaseUserAdmin):
(_('Important dates'), {'fields': ('last_login', 'date_joined')}), (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
) )
list_display = ('username', 'email', 'first_name', 'last_name', list_display = ('username', 'email', 'first_name', 'last_name',
'maxemprunt', 'is_adherent', 'is_staff') 'maxemprunt', 'is_adherent', 'is_staff', 'actions_btn')
list_filter = (IsAdherentFilter, 'is_staff', 'is_superuser', 'is_active', list_filter = (IsAdherentFilter, 'is_staff', 'is_superuser', 'is_active',
'groups') 'groups')
@ -64,15 +66,33 @@ class UserAdmin(VersionAdmin, BaseUserAdmin):
""" """
last_adh_year = Adhesion.objects.all().order_by('annee_debut') \ last_adh_year = Adhesion.objects.all().order_by('annee_debut') \
.reverse().first() .reverse().first()
return last_adh_year and obj in last_adh_year.adherent.all() is_member = last_adh_year and obj in last_adh_year.adherent.all()
if is_member:
return format_html(
'<img src="/static/admin/img/icon-yes.svg" alt="True">'
)
else:
# TODO permit adhere only if perms.users.add_user
return format_html(
'<img src="/static/admin/img/icon-no.svg" alt="False"> '
'<a class="button" href="{}">{}</a>',
reverse('users:adherer', args=[obj.pk]),
_('Adhere')
)
is_adherent.boolean = True
is_adherent.short_description = _('is adherent') is_adherent.short_description = _('is adherent')
is_adherent.allow_tags = True
# TODO {% if not user.is_adherent and perms.users.add_user %} def actions_btn(self, obj):
# {% url 'users:adherer' user.id %} # TODO permit adhere only if perms.media.add_emprunt
# TODO {% if perms.media.add_emprunt %} return format_html(
# {% url 'media:add-emprunt' user.id %}"> '<a class="button" href="{}">{}</a>',
reverse('media:add-emprunt', args=[obj.pk]),
_('Register borrowed item')
)
actions_btn.short_description = _('actions')
actions_btn.allow_tags = True
admin.site.register(User, UserAdmin) admin.site.register(User, UserAdmin)

View File

@ -28,10 +28,10 @@ class BaseInfoForm(ModelForm):
class Meta: class Meta:
model = User model = User
fields = [ fields = [
'first_name',
'username', 'username',
'last_name',
'email', 'email',
'telephone', 'first_name',
'last_name',
'address', 'address',
'telephone',
] ]

View File

@ -1,21 +1,72 @@
{% extends "base.html" %} {% extends "admin/base_site.html" %}
{% comment %} {% comment %}
SPDX-License-Identifier: GPL-3.0-or-later SPDX-License-Identifier: GPL-3.0-or-later
{% endcomment %} {% endcomment %}
{% load bootstrap3 %} {% load i18n static %}
{% block title %}Création et modification d'utilisateur{% endblock %} {% block breadcrumbs %}
<div class="breadcrumbs">
<a href="{% url 'index' %}">{% trans 'Home' %}</a>
{% if title %} &rsaquo; {{ title }}{% endif %}
</div>
{% endblock %}
{% block extrahead %}{{ block.super }}
{# Load Django Admin widgets #}
<script type="text/javascript" src="{% url 'admin:jsi18n' %}"></script>
<script type="text/javascript" src="{% static 'admin/js/core.js' %}"></script>
{{ form.media }}
{% endblock %}
{% block extrastyle %}{{ block.super }}
<link rel="stylesheet" type="text/css" href="{% static "admin/css/forms.css" %}">
{% endblock %}
{% block content %} {% block content %}
{% bootstrap_form_errors userform %} <form method="post" onsubmit="window.onbeforeunload=null">{% csrf_token %}
{% if form.non_field_errors %}
<a href="{% url 'users:password' %}"> <p class="errornote">
{% for error in form.non_field_errors %}
{{ error }}
{% endfor %}
</p>
{% endif %}
<fieldset class="module aligned">
{% for field in form %}
<div class="form-row{% if field.errors %} errors{% endif %}">
{{ field.errors }}
<div>
{{ field.label_tag }}
{% if field.is_readonly %}
<div class="readonly">{{ field.contents }}</div>
{% else %}
{{ field }}
{% endif %}
{% if field.field.help_text %}
<div class="help">{{ field.field.help_text|safe }}</div>
{% endif %}
</div>
</div>
{% endfor %}
{% if password_change %}
<div class="form-row">
<div>
<label>Mot de passe</label>
<div class="readonly">
<a class="button" href="{% url 'users:password' %}">
Changer le mot de passe Changer le mot de passe
</a> </a>
<form class="form" method="post"> </div>
{% csrf_token %} </div>
{% bootstrap_form userform %} </div>
{% bootstrap_button "Créer ou modifier" button_type="submit" icon="star" %} {% endif %}
</fieldset>
<div class="submit-row">
<input class="default" type="submit" value="{% trans 'Create or edit' %}">
</div>
</form> </form>
{% endblock %} {% endblock %}

View File

@ -11,14 +11,14 @@ from django.shortcuts import get_object_or_404, redirect, render
from django.template import loader from django.template import loader
from django.template.context_processors import csrf from django.template.context_processors import csrf
from django.utils import timezone from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from reversion import revisions as reversion from reversion import revisions as reversion
from med.settings import ASSO_EMAIL, ASSO_NAME, EMAIL_FROM, \ from med.settings import ASSO_EMAIL, ASSO_NAME, EMAIL_FROM, \
REQ_EXPIRE_STR, SITE_NAME REQ_EXPIRE_STR, SITE_NAME
from media.models import Emprunt
from users.forms import BaseInfoForm from users.forms import BaseInfoForm
from users.forms import PassForm from users.forms import PassForm
from users.models import Adhesion, Request, Right, User from users.models import Adhesion, Request, User
def form(ctx, template, request): def form(ctx, template, request):
@ -40,7 +40,7 @@ def password_change_action(u_form, user, request, req=False):
if req: if req:
req.delete() req.delete()
return redirect("/") return redirect("/")
return redirect("/users/profil/" + str(user.id)) return redirect("/")
def reset_passwd_mail(req, request): def reset_passwd_mail(req, request):
@ -83,8 +83,11 @@ def new_user(request):
"L'utilisateur %s a été crée, un mail pour " "L'utilisateur %s a été crée, un mail pour "
"l'initialisation du mot de passe a été " "l'initialisation du mot de passe a été "
"envoyé" % user.username) "envoyé" % user.username)
return redirect("/users/profil/" + str(user.id)) return redirect("/")
return form({'userform': user}, 'users/user.html', request) return form({
'form': user,
'title': _('New user'),
}, 'users/user.html', request)
@login_required @login_required
@ -100,8 +103,12 @@ def edit_info(request):
reversion.set_comment("Champs modifié(s) : %s" % ', '.join( reversion.set_comment("Champs modifié(s) : %s" % ', '.join(
field for field in user.changed_data)) field for field in user.changed_data))
messages.success(request, "L'user a bien été modifié") messages.success(request, "L'user a bien été modifié")
return redirect("/users/profil/") return redirect("/")
return form({'userform': user}, 'users/user.html', request) return form({
'form': user,
'password_change': True,
'title': _('Edit user profile'),
}, 'users/user.html', request)
@login_required @login_required
@ -112,7 +119,10 @@ def password(request):
u_form = PassForm(request.POST or None) u_form = PassForm(request.POST or None)
if u_form.is_valid(): if u_form.is_valid():
return password_change_action(u_form, request.user, request) return password_change_action(u_form, request.user, request)
return form({'userform': u_form}, 'users/user.html', request) return form({
'form': u_form,
'title': _('Change password'),
}, 'users/user.html', request)
@login_required @login_required
@ -122,7 +132,7 @@ def adherer(request, userid):
users = User.objects.get(pk=userid) users = User.objects.get(pk=userid)
except User.DoesNotExist: except User.DoesNotExist:
messages.error(request, "Utilisateur inexistant") messages.error(request, "Utilisateur inexistant")
return redirect("/users/") return redirect("/")
adh_year = Adhesion.objects.all().order_by('annee_debut').reverse().first() adh_year = Adhesion.objects.all().order_by('annee_debut').reverse().first()
with transaction.atomic(), reversion.create_revision(): with transaction.atomic(), reversion.create_revision():
reversion.set_user(request.user) reversion.set_user(request.user)
@ -130,7 +140,7 @@ def adherer(request, userid):
adh_year.save() adh_year.save()
reversion.set_comment("Adhesion de %s" % users) reversion.set_comment("Adhesion de %s" % users)
messages.success(request, "Adhesion effectuee") messages.success(request, "Adhesion effectuee")
return redirect("/users/profil/" + userid) return redirect("/")
def process(request, token): def process(request, token):