Source

django-acl / acl / admin.py

Full commit
# this module's not really good.
from django.contrib import admin
from django.contrib.auth.models import User, Group
from django.contrib.auth.admin import UserAdmin, GroupAdmin
from acl.models import ACL, Role, RoleData, SETTING_CHOICES

from django.forms.models import ModelChoiceField
from django.contrib.contenttypes.models import ContentType
from django import forms

class ContentTypeField(ModelChoiceField):

    def label_from_instance(self, obj):
        return "%s.%s" % (obj.app_label, obj.model)

class ACLInlineUser(admin.TabularInline):
    model = ACL
    extra = 3
    radio_fields = {'setting': admin.HORIZONTAL}
    exclude = ('group',)

class ACLInlineGroup(ACLInlineUser):
    exclude = ('user',)


class UserAdmin(UserAdmin):
    inlines = (ACLInlineUser,)
    filter_horizontal = ()

class GroupAdmin(GroupAdmin):
    inlines = (ACLInlineGroup,)
    filter_horizontal = ()

class RoleDataInline(admin.TabularInline):
    model = RoleData
    #form = RoleDataForm
    extra = 3

class RoleAdmin(admin.ModelAdmin):
    inlines = (RoleDataInline,)

class ACLForm(forms.ModelForm):
    content_type = ContentTypeField(ContentType.objects.all(), required=False)
    class Meta:
        model = ACL
#ACLForm.base_fields['setting'].choices = SETTING_CHOICES


class ACLAdmin(admin.ModelAdmin):
    form = ACLForm
    list_display = ('user', 'group', 'content_type', 'object_id', 'role',
                    'permission', 'setting')
    radio_fields = {'setting': admin.HORIZONTAL}

admin.site.register(ACL, ACLAdmin)
admin.site.register(Role, RoleAdmin)
admin.site.unregister(User)
admin.site.unregister(Group)
admin.site.register(User, UserAdmin)
admin.site.register(Group, GroupAdmin)