Commits

Anonymous committed a9ff2c2

add generic update_list function

Comments (0)

Files changed (1)

djangotoolbox/auth/utils.py

 def add_permission_to_group(perm, group):
     add_to(perm, GroupPermissionList, {'group': group})
 
-def update_permissions_user(perms, user):
-    obj_list, created = UserPermissionList.objects.get_or_create(user=user)
+def update_list(objs, list_cls, filter):
+    obj_list, created = list_cls.objects.get_or_create(**filter)
 
-    old_permissions = list(obj_list.permissions)
+    old_objs = list(obj_list._get_objs())
     
-    for perm in old_permissions:
+    for obj in old_objs:
         try:
-            perms.index(perm)
+            objs.index(obj)
         except ValueError:
-            obj_list.fk_list.remove(perm.id)
+            obj_list.fk_list.remove(obj.id)
     
-    for perm in perms      :
+    for obj in objs:
         try:
-            old_permissions.index(perm)
+            old_objs.index(obj)
         except ValueError:
-            obj_list.fk_list.append(perm.id)
+            obj_list.fk_list.append(obj.id)
     
     obj_list.save()
+    
+def update_permissions_user(perms, user):
+    update_list(perms, UserPermissionList, {'user': user})
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.