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

chirac's avatar
chirac committed
5
from .models import User, School, Right, ListRight, Ban, Whitelist
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

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

36

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

40 41 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

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 = ()

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