admin.py 2.73 KB
Newer Older
erdnaxe's avatar
erdnaxe committed
1 2 3
# -*- mode: python; coding: utf-8 -*-
# Copyright (C) 2017-2019 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
4 5 6

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
7
from django.utils.translation import ugettext_lazy as _
8 9
from reversion.admin import VersionAdmin

erdnaxe's avatar
erdnaxe committed
10
from .models import User, Right, Adhesion, ListRight, Clef, Request
11 12 13 14 15


class RequestAdmin(admin.ModelAdmin):
    list_display = ('user', 'type', 'created_at', 'expires_at')

erdnaxe's avatar
erdnaxe committed
16

Med's avatar
Med committed
17 18 19
class RightAdmin(VersionAdmin):
    list_display = ('user', 'right')

erdnaxe's avatar
erdnaxe committed
20

Med's avatar
Med committed
21
class ClefAdmin(VersionAdmin):
erdnaxe's avatar
erdnaxe committed
22
    list_display = ('nom', 'proprio')
Med's avatar
Med committed
23

erdnaxe's avatar
erdnaxe committed
24

25 26 27
class AdhesionAdmin(VersionAdmin):
    list_display = ('annee_debut', 'annee_fin')

erdnaxe's avatar
erdnaxe committed
28

Med's avatar
Med committed
29 30
class ListRightAdmin(VersionAdmin):
    list_display = ('listright',)
31

erdnaxe's avatar
erdnaxe committed
32

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
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


51
class UserAdmin(VersionAdmin, BaseUserAdmin):
52
    # Customize admin to add more fields
53
    fieldsets = (
erdnaxe's avatar
erdnaxe committed
54
        (None, {'fields': ('username', 'password')}),
55 56 57 58 59 60
        (_('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')}),
61
    )
62 63 64 65 66 67 68 69 70 71 72 73 74 75
    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')
76

erdnaxe's avatar
erdnaxe committed
77

78 79
admin.site.register(User, UserAdmin)
admin.site.register(Request, RequestAdmin)
Med's avatar
Med committed
80 81
admin.site.register(ListRight, ListRightAdmin)
admin.site.register(Right, RightAdmin)
82
admin.site.register(Adhesion, AdhesionAdmin)
Med's avatar
Med committed
83
admin.site.register(Clef, ClefAdmin)