admin.py 5.41 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

chirac's avatar
chirac committed
28 29
from .models import User, ServiceUser, School, Right, ListRight, ListShell, Ban, Whitelist, Request, LdapUser, LdapServiceUser, LdapUserGroup
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
class SchoolAdmin(VersionAdmin):
61 62
    list_display = ('name',)

63
class ListRightAdmin(VersionAdmin):
64 65
    list_display = ('listright',)

66 67
class ListShellAdmin(VersionAdmin):
    list_display = ('shell',)
68

chirac's avatar
chirac committed
69
class RightAdmin(VersionAdmin):
70 71
    list_display = ('user', 'right')

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

75
class BanAdmin(VersionAdmin):
chirac's avatar
chirac committed
76 77
    list_display = ('user', 'raison', 'date_start', 'date_end')

78

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

82

83
class UserAdmin(VersionAdmin, BaseUserAdmin):
84 85 86 87 88 89 90
    # 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.
91
    list_display = ('pseudo', 'name', 'surname', 'email', 'school', 'is_admin', 'shell')
chirac's avatar
chirac committed
92
    list_display = ('pseudo',)
93 94 95
    list_filter = ()
    fieldsets = (
        (None, {'fields': ('pseudo', 'password')}),
chirac's avatar
chirac committed
96
        ('Personal info', {'fields': ('name', 'surname', 'email', 'school','shell', 'uid_number')}),
97 98 99 100 101 102 103 104 105 106 107 108 109 110
        ('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
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
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.
    list_display = ('pseudo',)
    list_filter = ()
    fieldsets = (
        (None, {'fields': ('pseudo', 'password')}),
    )
    # 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 = ()

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