2019-08-02 12:57:53 +00:00
|
|
|
# -*- mode: python; coding: utf-8 -*-
|
|
|
|
# Copyright (C) 2017-2019 by BDE ENS Paris-Saclay
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2017-06-11 23:34:13 +00:00
|
|
|
|
|
|
|
from django.contrib import admin
|
|
|
|
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
|
2019-08-08 10:16:40 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2017-06-11 23:34:13 +00:00
|
|
|
from reversion.admin import VersionAdmin
|
|
|
|
|
2019-08-02 12:57:53 +00:00
|
|
|
from .models import User, Right, Adhesion, ListRight, Clef, Request
|
2017-06-11 23:34:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RequestAdmin(admin.ModelAdmin):
|
|
|
|
list_display = ('user', 'type', 'created_at', 'expires_at')
|
|
|
|
|
2019-08-02 12:57:53 +00:00
|
|
|
|
2017-06-30 01:25:07 +00:00
|
|
|
class RightAdmin(VersionAdmin):
|
|
|
|
list_display = ('user', 'right')
|
|
|
|
|
2019-08-02 12:57:53 +00:00
|
|
|
|
2017-07-03 18:06:21 +00:00
|
|
|
class ClefAdmin(VersionAdmin):
|
2019-08-08 12:55:20 +00:00
|
|
|
list_display = ('nom', 'proprio', 'commentaire')
|
|
|
|
# TODO order by nom
|
2017-07-03 18:06:21 +00:00
|
|
|
|
2019-08-02 12:57:53 +00:00
|
|
|
|
2017-07-04 22:47:05 +00:00
|
|
|
class AdhesionAdmin(VersionAdmin):
|
|
|
|
list_display = ('annee_debut', 'annee_fin')
|
|
|
|
|
2019-08-02 12:57:53 +00:00
|
|
|
|
2017-06-30 01:25:07 +00:00
|
|
|
class ListRightAdmin(VersionAdmin):
|
|
|
|
list_display = ('listright',)
|
2017-06-11 23:34:13 +00:00
|
|
|
|
2019-08-02 12:57:53 +00:00
|
|
|
|
2019-08-08 11:04:40 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2017-06-11 23:34:13 +00:00
|
|
|
class UserAdmin(VersionAdmin, BaseUserAdmin):
|
2019-08-08 10:16:40 +00:00
|
|
|
# Customize admin to add more fields
|
2017-06-11 23:34:13 +00:00
|
|
|
fieldsets = (
|
2019-08-02 19:35:30 +00:00
|
|
|
(None, {'fields': ('username', 'password')}),
|
2019-08-08 10:16:40 +00:00
|
|
|
(_('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')}),
|
2017-06-11 23:34:13 +00:00
|
|
|
)
|
2019-08-08 11:04:40 +00:00
|
|
|
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')
|
2017-06-11 23:34:13 +00:00
|
|
|
|
2019-08-02 12:57:53 +00:00
|
|
|
|
2017-06-11 23:34:13 +00:00
|
|
|
admin.site.register(User, UserAdmin)
|
|
|
|
admin.site.register(Request, RequestAdmin)
|
2017-06-30 01:25:07 +00:00
|
|
|
admin.site.register(ListRight, ListRightAdmin)
|
|
|
|
admin.site.register(Right, RightAdmin)
|
2017-07-04 22:47:05 +00:00
|
|
|
admin.site.register(Adhesion, AdhesionAdmin)
|
2017-07-03 18:06:21 +00:00
|
|
|
admin.site.register(Clef, ClefAdmin)
|