admin.py 5.71 KB
Newer Older
1
# -*- mode: python; coding: utf-8 -*-
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.

24 25
from __future__ import unicode_literals

lhark's avatar
lhark committed
26
from django.contrib import admin
27 28
from django.contrib.auth.models import Group
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
29
from reversion.admin import VersionAdmin
lhark's avatar
lhark committed
30

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

34 35

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

48

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

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

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

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

67
class SchoolAdmin(VersionAdmin):
68 69
    list_display = ('name',)

70
class ListRightAdmin(VersionAdmin):
71 72
    list_display = ('listright',)

73 74
class ListShellAdmin(VersionAdmin):
    list_display = ('shell',)
75

76
class RightAdmin(VersionAdmin):
77 78
    list_display = ('user', 'right')

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

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

85

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

89

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

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