1
0
mirror of https://gitlab.crans.org/mediatek/med.git synced 2025-07-06 06:03:54 +02:00

Buttons in user admin

This commit is contained in:
Alexandre Iooss
2019-08-08 21:51:37 +02:00
parent 0c504e85fc
commit c225f5045e
6 changed files with 113 additions and 32 deletions

View File

@ -4,6 +4,8 @@
from django.contrib import admin
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 reversion.admin import VersionAdmin
@ -54,7 +56,7 @@ class UserAdmin(VersionAdmin, BaseUserAdmin):
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
)
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',
'groups')
@ -64,15 +66,33 @@ class UserAdmin(VersionAdmin, BaseUserAdmin):
"""
last_adh_year = Adhesion.objects.all().order_by('annee_debut') \
.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.allow_tags = True
# TODO {% if not user.is_adherent and perms.users.add_user %}
# {% url 'users:adherer' user.id %}
# TODO {% if perms.media.add_emprunt %}
# {% url 'media:add-emprunt' user.id %}">
def actions_btn(self, obj):
# TODO permit adhere only if perms.media.add_emprunt
return format_html(
'<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)