admin.py 5.67 KB
Newer Older
1
# -*- mode: python; coding: utf-8 -*-
lhark's avatar
lhark committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 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
24
from django.contrib import admin
25 26
from django.contrib.auth.models import Group
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
27
from reversion.admin import VersionAdmin
lhark's avatar
lhark committed
28

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

32 33

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

46

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

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

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

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

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

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

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

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

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

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

83

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

87

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

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