admin.py 2.5 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
lhark's avatar
lhark committed
4

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

8 9

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

20 21 22 23

class SchoolAdmin(admin.ModelAdmin):
    list_display = ('name',)

24

25 26 27
class ListRightAdmin(admin.ModelAdmin):
    list_display = ('listright',)

28

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

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

chirac's avatar
chirac committed
35 36 37
class BanAdmin(admin.ModelAdmin):
    list_display = ('user', 'raison', 'date_start', 'date_end')

38

chirac's avatar
chirac committed
39 40 41
class WhitelistAdmin(admin.ModelAdmin):
    list_display = ('user', 'raison', 'date_start', 'date_end')

42 43 44 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

class UserAdmin(BaseUserAdmin):
    # 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 = ()

70 71
admin.site.register(User, UserAdmin)
admin.site.register(School, SchoolAdmin)
72 73
admin.site.register(Right, RightAdmin)
admin.site.register(ListRight, ListRightAdmin)
chirac's avatar
chirac committed
74
admin.site.register(Ban, BanAdmin)
chirac's avatar
chirac committed
75
admin.site.register(Whitelist, WhitelistAdmin)
76
admin.site.register(Request, RequestAdmin)
77 78 79 80 81 82
# 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)