admin.py 4.43 KB
Newer Older
lhark's avatar
lhark committed
1
from django.contrib import admin
2 3
from django.contrib.auth.models import Group
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
4
from reversion.admin import VersionAdmin
lhark's avatar
lhark committed
5

chirac's avatar
chirac committed
6 7
from .models import User, ServiceUser, School, Right, ListRight, ListShell, Ban, Whitelist, Request, LdapUser, LdapServiceUser, LdapUserGroup
from .forms import UserChangeForm, UserCreationForm, ServiceUserChangeForm, ServiceUserCreationForm
8

9 10

class UserAdmin(admin.ModelAdmin):
11 12 13 14 15 16 17
    list_display = (
        'name',
        'surname',
        'pseudo',
        'room',
        'email',
        'school',
18
        'shell',
19 20
        'state'
    )
21
    search_fields = ('name','surname','pseudo','room')
22

23

24
class LdapUserAdmin(admin.ModelAdmin):
chirac's avatar
chirac committed
25
    list_display = ('name','uidNumber','login_shell')
26
    exclude = ('user_password','sambat_nt_password')
27 28
    search_fields = ('name',)

chirac's avatar
chirac committed
29 30 31 32 33
class LdapServiceUserAdmin(admin.ModelAdmin):
    list_display = ('name',)
    exclude = ('user_password',)
    search_fields = ('name',)

34 35 36 37
class LdapUserGroupAdmin(admin.ModelAdmin):
    list_display = ('name','members','gid')
    search_fields = ('name',)

38
class SchoolAdmin(VersionAdmin):
39 40
    list_display = ('name',)

41
class ListRightAdmin(VersionAdmin):
42 43
    list_display = ('listright',)

44 45
class ListShellAdmin(VersionAdmin):
    list_display = ('shell',)
46

chirac's avatar
chirac committed
47
class RightAdmin(VersionAdmin):
48 49
    list_display = ('user', 'right')

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

53
class BanAdmin(VersionAdmin):
chirac's avatar
chirac committed
54 55
    list_display = ('user', 'raison', 'date_start', 'date_end')

56

57
class WhitelistAdmin(VersionAdmin):
chirac's avatar
chirac committed
58 59
    list_display = ('user', 'raison', 'date_start', 'date_end')

60

61
class UserAdmin(VersionAdmin, BaseUserAdmin):
62 63 64 65 66 67 68
    # 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.
69
    list_display = ('pseudo', 'name', 'surname', 'email', 'school', 'is_admin', 'shell')
chirac's avatar
chirac committed
70
    list_display = ('pseudo',)
71 72 73
    list_filter = ()
    fieldsets = (
        (None, {'fields': ('pseudo', 'password')}),
chirac's avatar
chirac committed
74
        ('Personal info', {'fields': ('name', 'surname', 'email', 'school','shell', 'uid_number')}),
75 76 77 78 79 80 81 82 83 84 85 86 87 88
        ('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
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
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 = ()

114
admin.site.register(User, UserAdmin)
chirac's avatar
chirac committed
115
admin.site.register(ServiceUser, ServiceUserAdmin)
116 117
admin.site.register(LdapUser, LdapUserAdmin)
admin.site.register(LdapUserGroup, LdapUserGroupAdmin)
chirac's avatar
chirac committed
118
admin.site.register(LdapServiceUser, LdapServiceUserAdmin)
119
admin.site.register(School, SchoolAdmin)
120 121
admin.site.register(Right, RightAdmin)
admin.site.register(ListRight, ListRightAdmin)
122
admin.site.register(ListShell, ListShellAdmin)
chirac's avatar
chirac committed
123
admin.site.register(Ban, BanAdmin)
chirac's avatar
chirac committed
124
admin.site.register(Whitelist, WhitelistAdmin)
125
admin.site.register(Request, RequestAdmin)
126 127
# Now register the new UserAdmin...
admin.site.unregister(User)
chirac's avatar
chirac committed
128
admin.site.unregister(ServiceUser)
129
admin.site.register(User, UserAdmin)
chirac's avatar
chirac committed
130
admin.site.register(ServiceUser, ServiceUserAdmin)
131 132 133
# ... and, since we're not using Django's built-in permissions,
# unregister the Group model from admin.
admin.site.unregister(Group)