admin.py 5.64 KB
Newer Older
lhark's avatar
lhark committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
# se veut agnostique au réseau considéré, de manière à être installable en
# quelques clics.
#
# Copyright © 2017  Gabriel Détraz
# Copyright © 2017  Goulven Kermarec
# Copyright © 2017  Augustin Lemesle
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

lhark's avatar
lhark committed
23
from django.contrib import admin
24 25
from django.contrib.auth.models import Group
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
26
from reversion.admin import VersionAdmin
lhark's avatar
lhark committed
27

28
from .models import User, ServiceUser, School, Right, ListRight, ListShell, Ban, Whitelist, Request, LdapUser, LdapServiceUser, LdapServiceUserGroup, LdapUserGroup
chirac's avatar
chirac committed
29
from .forms import UserChangeForm, UserCreationForm, ServiceUserChangeForm, ServiceUserCreationForm
30

31 32

class UserAdmin(admin.ModelAdmin):
33 34 35 36 37 38 39
    list_display = (
        'name',
        'surname',
        'pseudo',
        'room',
        'email',
        'school',
40
        'shell',
41 42
        'state'
    )
43
    search_fields = ('name','surname','pseudo','room')
44

45

46
class LdapUserAdmin(admin.ModelAdmin):
chirac's avatar
chirac committed
47
    list_display = ('name','uidNumber','login_shell')
48
    exclude = ('user_password','sambat_nt_password')
49 50
    search_fields = ('name',)

chirac's avatar
chirac committed
51 52 53 54 55
class LdapServiceUserAdmin(admin.ModelAdmin):
    list_display = ('name',)
    exclude = ('user_password',)
    search_fields = ('name',)

56 57 58 59
class LdapUserGroupAdmin(admin.ModelAdmin):
    list_display = ('name','members','gid')
    search_fields = ('name',)

60 61 62 63
class LdapServiceUserGroupAdmin(admin.ModelAdmin):
    list_display = ('name',)
    search_fields = ('name',)

64
class SchoolAdmin(VersionAdmin):
65 66
    list_display = ('name',)

67
class ListRightAdmin(VersionAdmin):
68 69
    list_display = ('listright',)

70 71
class ListShellAdmin(VersionAdmin):
    list_display = ('shell',)
72

chirac's avatar
chirac committed
73
class RightAdmin(VersionAdmin):
74 75
    list_display = ('user', 'right')

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

79
class BanAdmin(VersionAdmin):
chirac's avatar
chirac committed
80 81
    list_display = ('user', 'raison', 'date_start', 'date_end')

82

83
class WhitelistAdmin(VersionAdmin):
chirac's avatar
chirac committed
84 85
    list_display = ('user', 'raison', 'date_start', 'date_end')

86

87
class UserAdmin(VersionAdmin, BaseUserAdmin):
88 89 90 91 92 93 94
    # The forms to add and change user instances
    form = UserChangeForm
    add_form = UserCreationForm

    # The fields to be used in displaying the User model.
    # These override the definitions on the base UserAdmin
    # that reference specific fields on auth.User.
95
    list_display = ('pseudo', 'name', 'surname', 'email', 'school', 'is_admin', 'shell')
chirac's avatar
chirac committed
96
    list_display = ('pseudo',)
97 98 99
    list_filter = ()
    fieldsets = (
        (None, {'fields': ('pseudo', 'password')}),
chirac's avatar
chirac committed
100
        ('Personal info', {'fields': ('name', 'surname', 'email', 'school','shell', 'uid_number')}),
101 102 103 104 105 106 107 108 109 110 111 112 113 114
        ('Permissions', {'fields': ('is_admin', )}),
    )
    # add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
    # overrides get_fieldsets to use this attribute when creating a user.
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('pseudo', 'name', 'surname', 'email', 'school', 'is_admin', 'password1', 'password2')}
        ),
    )
    search_fields = ('pseudo',)
    ordering = ('pseudo',)
    filter_horizontal = ()

chirac's avatar
chirac committed
115 116 117 118 119 120 121 122
class ServiceUserAdmin(VersionAdmin, BaseUserAdmin):
    # The forms to add and change user instances
    form = ServiceUserChangeForm
    add_form = ServiceUserCreationForm

    # The fields to be used in displaying the User model.
    # These override the definitions on the base UserAdmin
    # that reference specific fields on auth.User.
123
    list_display = ('pseudo', 'access_group')
chirac's avatar
chirac committed
124 125
    list_filter = ()
    fieldsets = (
126
        (None, {'fields': ('pseudo', 'password', 'access_group')}),
chirac's avatar
chirac committed
127 128 129 130 131 132 133 134 135 136 137 138 139
    )
    # add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
    # overrides get_fieldsets to use this attribute when creating a user.
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('pseudo', 'password1', 'password2')}
        ),
    )
    search_fields = ('pseudo',)
    ordering = ('pseudo',)
    filter_horizontal = ()

140
admin.site.register(User, UserAdmin)
chirac's avatar
chirac committed
141
admin.site.register(ServiceUser, ServiceUserAdmin)
142 143
admin.site.register(LdapUser, LdapUserAdmin)
admin.site.register(LdapUserGroup, LdapUserGroupAdmin)
chirac's avatar
chirac committed
144
admin.site.register(LdapServiceUser, LdapServiceUserAdmin)
145
admin.site.register(LdapServiceUserGroup, LdapServiceUserGroupAdmin)
146
admin.site.register(School, SchoolAdmin)
147 148
admin.site.register(Right, RightAdmin)
admin.site.register(ListRight, ListRightAdmin)
149
admin.site.register(ListShell, ListShellAdmin)
chirac's avatar
chirac committed
150
admin.site.register(Ban, BanAdmin)
chirac's avatar
chirac committed
151
admin.site.register(Whitelist, WhitelistAdmin)
152
admin.site.register(Request, RequestAdmin)
153 154
# Now register the new UserAdmin...
admin.site.unregister(User)
chirac's avatar
chirac committed
155
admin.site.unregister(ServiceUser)
156
admin.site.register(User, UserAdmin)
chirac's avatar
chirac committed
157
admin.site.register(ServiceUser, ServiceUserAdmin)
158 159 160
# ... and, since we're not using Django's built-in permissions,
# unregister the Group model from admin.
admin.site.unregister(Group)