Commits

Florian Hahn committed e083d0f

add working admin interface

Comments (0)

Files changed (1)

object_permission_backend_nonrel/admin.py

 from django.contrib.auth.models import User, Group, Permission
 from django.contrib.contenttypes.models import ContentType
 
+from .utils import grant_permissions
 from models import PermissionType, ObjectPermission, GroupObjectPermission
 
 
 admin.site.add_action(create_user_permission_obj)
 admin.site.add_action(create_group_permission_obj)
 
-class ObjectPermissionForm(forms.ModelForm):
 
-    obj_permission_list = forms.MultipleChoiceField(required=False)
-    
+class BaseObjectPermissionForm(forms.ModelForm):
+
     def __init__(self, *args, **kwargs):
-        super(ObjectPermissionForm, self).__init__(*args, **kwargs)
+        super(BaseObjectPermissionForm, self).__init__(*args, **kwargs)
 
         self.fields['obj_permission_list'] = forms.MultipleChoiceField(required=False)
         
            
         self.fields['obj_permission_list'].choices = choices
         
+        selected = self.Meta.model.objects.get(id=kwargs['instance'].id)
+        initial = []
+        for permission in kwargs['instance'].permissions:
+            initial.append(permission)
+        
+        self.fields['obj_permission_list'].initial = initial
+
+
+class ObjectPermissionForm(BaseObjectPermissionForm):
+    obj_permission_list = forms.MultipleChoiceField(required=False)
+
+            
     class Meta:
         model = ObjectPermission
-        exclude = ('permission_list')
+        exclude = ('permissions')
+
+
+class BaseObjectPermissionAdmin(admin.ModelAdmin):
     
-class ObjectPermissionAdmin(admin.ModelAdmin):
+    def save_model(self, request, obj, form, change):
+        if form.is_valid():
+            obj.permissions = form.cleaned_data['obj_permission_list']
+        super(BaseObjectPermissionAdmin, self).save_model(request, obj, form, change)
+
+
+class ObjectPermissionAdmin(BaseObjectPermissionAdmin):
     form = ObjectPermissionForm
 
-class GroupObjectPermissionForm(forms.ModelForm):
 
+class GroupObjectPermissionForm(BaseObjectPermissionForm):
      obj_permission_list = forms.MultipleChoiceField(required=False)
-
-     def __init__(self, *args, **kwargs):
-         super(GroupObjectPermissionForm, self).__init__(*args, **kwargs)
-
-         self.fields['obj_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['obj_permission_list'].choices = choices
-
+     
 
      class Meta:
          model = GroupObjectPermission
-         exclude = ('permission_list')
+         exclude = ('permissions')
 
-class GroupObjectPermissionAdmin(admin.ModelAdmin):
+
+class GroupObjectPermissionAdmin(BaseObjectPermissionAdmin):
     form = GroupObjectPermissionForm
 
+
 class PermissionTypeAdmin(admin.ModelAdmin):
     pass
 
 admin.site.register(ObjectPermission, ObjectPermissionAdmin)
 admin.site.register(GroupObjectPermission, GroupObjectPermissionAdmin)
 admin.site.register(PermissionType, PermissionTypeAdmin)
-
-
-
-
-
-