1. Florian Hahn
  2. django-object-permission-backend-nonrel

Commits

Florian Hahn  committed 600d255

add group field to GroupObjectPermissionAdmin

  • Participants
  • Parent commits 56f7bd4
  • Branches default

Comments (0)

Files changed (1)

File object_permission_backend_nonrel/admin.py

View file
  • Ignore whitespace
     def save_model(self, request, obj, form, change):
         if form.is_valid():
             obj.permissions = form.cleaned_data['obj_permission_list']
-            obj.user_id = form.cleaned_data['user']
         super(BaseObjectPermissionAdmin, self).save_model(request, obj, form, change)
 
 
 class ObjectPermissionAdmin(BaseObjectPermissionAdmin):
     form = ObjectPermissionForm
 
+    def save_model(self, request, obj, form, change):
+        if form.is_valid():
+            obj.user_id = form.cleaned_data['user']
+        super(ObjectPermissionAdmin, self).save_model(request, obj, form, change)
 
+        
 class GroupObjectPermissionForm(BaseObjectPermissionForm):
      obj_permission_list = forms.MultipleChoiceField(required=False)
-     
+     group = forms.ChoiceField(required=True)
 
+     def __init__(self, *args, **kwargs):
+        super(GroupObjectPermissionForm, self).__init__(*args, **kwargs)        
+        groups = Group.objects.all()
+        choices = [[group.id, group.name] for group in groups]
+        self.fields['group'].choices = choices
+        try:
+            self.fields['group'].initial = kwargs['instance'].group_id
+        except KeyError:
+            pass
+        
      class Meta:
          model = GroupObjectPermission
-         exclude = ('permissions')
+         exclude = ('permissions', 'group_id')
 
 
 class GroupObjectPermissionAdmin(BaseObjectPermissionAdmin):
     form = GroupObjectPermissionForm
 
+    def save_model(self, request, obj, form, change):
+        if form.is_valid():
+            obj.group_id = form.cleaned_data['group']
+        super(GroupObjectPermissionAdmin, self).save_model(request, obj, form, change)
 
+        
 class PermissionTypeAdmin(admin.ModelAdmin):
     pass