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

6
from .models import User, School, Right, ListRight, Ban, Whitelist, Request
7 8
from .forms import UserChangeForm, UserCreationForm

9 10

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

21

22
class SchoolAdmin(VersionAdmin):
23 24
    list_display = ('name',)

25

26
class ListRightAdmin(VersionAdmin):
27 28
    list_display = ('listright',)

29

30 31 32
class RightAdmin(admin.ModelAdmin):
    list_display = ('user', 'right')

33 34
class RequestAdmin(admin.ModelAdmin):
    list_display = ('user', 'type', 'created_at', 'expires_at')
35

36
class BanAdmin(VersionAdmin):
chirac's avatar
chirac committed
37 38
    list_display = ('user', 'raison', 'date_start', 'date_end')

39

40
class WhitelistAdmin(VersionAdmin):
chirac's avatar
chirac committed
41 42
    list_display = ('user', 'raison', 'date_start', 'date_end')

43

44
class UserAdmin(VersionAdmin, BaseUserAdmin):
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    # 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.
    list_display = ('pseudo', 'name', 'surname', 'email', 'school', 'is_admin')
    list_filter = ()
    fieldsets = (
        (None, {'fields': ('pseudo', 'password')}),
        ('Personal info', {'fields': ('name', 'surname', 'email', 'school')}),
        ('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 = ()

71 72
admin.site.register(User, UserAdmin)
admin.site.register(School, SchoolAdmin)
73 74
admin.site.register(Right, RightAdmin)
admin.site.register(ListRight, ListRightAdmin)
chirac's avatar
chirac committed
75
admin.site.register(Ban, BanAdmin)
chirac's avatar
chirac committed
76
admin.site.register(Whitelist, WhitelistAdmin)
77
admin.site.register(Request, RequestAdmin)
78 79 80 81 82 83
# Now register the new UserAdmin...
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
# ... and, since we're not using Django's built-in permissions,
# unregister the Group model from admin.
admin.site.unregister(Group)