med/users/admin.py

87 lines
2.8 KiB
Python

# -*- mode: python; coding: utf-8 -*-
# Copyright (C) 2017-2019 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.utils.translation import ugettext_lazy as _
from reversion.admin import VersionAdmin
from .models import Adhesion, Clef, ListRight, Request, Right, User
class RequestAdmin(admin.ModelAdmin):
list_display = ('user', 'type', 'created_at', 'expires_at')
class RightAdmin(VersionAdmin):
list_display = ('user', 'right')
class ClefAdmin(VersionAdmin):
list_display = ('nom', 'proprio', 'commentaire')
# TODO order by nom
class AdhesionAdmin(VersionAdmin):
list_display = ('annee_debut', 'annee_fin')
class ListRightAdmin(VersionAdmin):
list_display = ('listright',)
class IsAdherentFilter(admin.SimpleListFilter):
title = _('adherent status')
parameter_name = 'is_adherent'
def lookups(self, request, model_admin):
return (
('Yes', _('Yes')),
)
def queryset(self, request, queryset):
value = self.value()
if value == 'Yes':
# Get current membership year and list all members
last_adh_year = Adhesion.objects.all().order_by('annee_debut')\
.reverse().first()
return last_adh_year.adherent
return queryset
class UserAdmin(VersionAdmin, BaseUserAdmin):
# Customize admin to add more fields
fieldsets = (
(None, {'fields': ('username', 'password')}),
(_('Personal info'), {'fields': ('first_name', 'last_name', 'email',
'telephone', 'address', 'comment')}),
(_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
'groups', 'user_permissions',
'maxemprunt')}),
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
)
list_display = ('username', 'email', 'first_name', 'last_name',
'maxemprunt', 'is_adherent', 'is_staff')
list_filter = (IsAdherentFilter, 'is_staff', 'is_superuser', 'is_active',
'groups')
def is_adherent(self, obj):
"""
Get current membership year and check if user is there
"""
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_adherent.boolean = True
is_adherent.short_description = _('is adherent')
admin.site.register(User, UserAdmin)
admin.site.register(Request, RequestAdmin)
admin.site.register(ListRight, ListRightAdmin)
admin.site.register(Right, RightAdmin)
admin.site.register(Adhesion, AdhesionAdmin)
admin.site.register(Clef, ClefAdmin)