Commits

Florian Hahn committed 8067aef

use values_list('permission_name') instead of manual list creation

Comments (0)

Files changed (1)

object_permission_backend_nonrel/utils.py

         except ObjectPermission.DoesNotExist:
             perm_obj = ObjectPermission(user_id=user.pk,
                                         content_type=ct, object_id=model.pk)
-        current_permission_list = perm_obj.get_permission_list()
-        registered_permissions = PermissionType.objects.filter(content_type=
-                                                               ct)
+        current_permissions = perm_obj.get_permission_list() or set()
+        registered_permission_list = list(PermissionType.objects.filter(content_type=
+                                                               ct).values_list('permission_name', flat=True))
 
-        registerd_permission_list = []
-        new_permission_list = current_permission_list
-        for perm in registered_permissions:
-            registerd_permission_list.append(perm.permission_name)
+        new_permission_list = current_permissions                
+
+        current_permissions.update(permission_list)        
         
-        # remove already existing entries from permission_list
-        for perm in current_permission_list:
+        for perm in current_permissions:
             try:
-                permission_list.remove(permission_list.index(perm))
-            except ValueError:
-                pass
-        
-        for perm in permission_list:
-            try:
-                new_permission_list.add(registerd_permission_list.pop(registerd_permission_list.index(perm)))
+                new_permission_list.add(registered_permission_list.pop(registered_permission_list.index(perm)))
             except ValueError:
                 return False
 
     perm_obj, created = GroupObjectPermission.objects.get_or_create(group_id=group.pk,
                                                                     content_type=ct,
                                                                     object_id=obj.pk)
-    permission_list = perm_obj.get_permission_list()
-    permission_list.add(perm)
-    perm_obj.set_permission_list(permission_list)
+    permissions = perm_obj.get_permission_list()
+    permissions.add(perm)
+    perm_obj.set_permission_list(permissions)
     perm_obj.save()