Source

django-object-permission-backend-nonrel / object_permission_backend_nonrel / admin.py

Full commit
import settings
from django import forms
from django.template import RequestContext
from django.shortcuts import render_to_response
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User, Group, Permission
from django.contrib.contenttypes.models import ContentType

from models import PermissionType, ObjectPermission, GroupObjectPermission


def create_user_permission_obj(modeladmin, request, queryset):
    for obj in queryset:
         ct = ContentType.objects.get_for_model(obj)
         perm_obj = ObjectPermission.objects.create(content_type=ct,
                                                    object_id=obj.id)

def create_group_permission_obj(modeladmin, request, queryset):
    for obj in queryset:
         ct = ContentType.objects.get_for_model(obj)
         perm_obj = GroupObjectPermission.objects.create(content_type=ct,
                                                         object_id=obj.id)

admin.site.add_action(create_user_permission_obj)
admin.site.add_action(create_group_permission_obj)


class ObjectPermissionForm(forms.ModelForm):

    permission_list = forms.MultipleChoiceField(required=False)
    
    def __init__(self, *args, **kwargs):
        super(ObjectPermissionForm, self).__init__(*args, **kwargs)

        self.fields['permission_list'] = forms.MultipleChoiceField(required=False)
        
        permission_types = PermissionType.objects.filter(content_type=kwargs['instance'].content_type)

        choices = []
        cls_list = set()
        for pt in permission_types:
            choices.append([pt.permission_name,  '%s:%s' %( pt.content_type, pt.permission_name)])
           
        self.fields['permission_list'].choices = choices
        
    class Meta:
        model = ObjectPermission
   
    
class ObjectPermissionAdmin(admin.ModelAdmin):
    form = ObjectPermissionForm

class GroupObjectPermissionForm(forms.ModelForm):

     permission_list = forms.MultipleChoiceField(required=False)

     def __init__(self, *args, **kwargs):
         super(GroupObjectPermissionForm, self).__init__(*args, **kwargs)

         self.fields['permission_list'] = forms.MultipleChoiceField(required=False)

         permission_types = PermissionType.objects.filter(content_type=kwargs['instance'].content_type)

         choices = []
         cls_list = set()
         for pt in permission_types:
             choices.append([pt.permission_name,  '%s:%s' %( pt.content_type, pt.permission_name)])

         self.fields['permission_list'].choices = choices


     class Meta:
         model = GroupObjectPermission

class GroupObjectPermissionAdmin(admin.ModelAdmin):
    form = GroupObjectPermissionForm


class GroupObjectPermissionAdmin(admin.ModelAdmin):
     form = GroupObjectPermissionForm

class PermissionTypeAdmin(admin.ModelAdmin):
    pass

admin.site.register(ObjectPermission, ObjectPermissionAdmin)
admin.site.register(GroupObjectPermission, GroupObjectPermissionAdmin)
admin.site.register(PermissionType, PermissionTypeAdmin)