Commits

Anonymous committed 49bc613

cleanup

Comments (0)

Files changed (4)

djangotoolbox/auth/backends.py

 from django.contrib.auth.backends import ModelBackend
 from django.contrib.auth.models import User
-
-from djangotoolbox.auth.models import UserPermissionList, GroupPermissionList, GroupList
+from djangotoolbox.auth.models import UserPermissionList
 
 
 class NonrelPermissionBackend(ModelBackend):
                 pl = None
                 user_obj._perm_cache = set()
                 
-            user_obj._perm_cache.update(self.get_group_permissions(user_obj, pl))
+            user_obj._perm_cache.update(self.get_group_permissions(user_obj,
+                                                                   pl))
         return user_obj._perm_cache
 
     def has_perm(self, user_obj, perm):

djangotoolbox/auth/models.py

+from django.contrib.auth.models import User, Group
 from django.db import models
-from django.contrib.auth.models import User, Group, Permission
-
 from djangotoolbox.fields import ListField
 
 
 def get_objs(obj_cls, obj_ids):
     objs = set()
     if len(obj_ids) > 0:
-        # order_by() has to be used to override invalid default Permission filter
         objs.update(obj_cls .objects.filter(id__in=obj_ids).order_by('name'))
     return objs
 

djangotoolbox/auth/tests.py

 from django.contrib.auth.models import User, Group, Permission, AnonymousUser
 from django.contrib.contenttypes.models import ContentType
 from django.test import TestCase
-
 from djangotoolbox.auth.models import UserPermissionList, \
      GroupPermissionList, GroupList
 from djangotoolbox.auth.utils import add_permission_to_user, \
         self.assertEqual(user.has_perm('auth.test'), True)
         self.assertEqual(user.has_perm('auth.test1'), False)
 
-        update_user_groups(user, [])
+        update_user_groups(user, [group1])
         user = User.objects.get(username='test')
         self.assertEqual(user.has_perm('auth.test'), False)
         self.assertEqual(user.has_perm('auth.test1'), False)

djangotoolbox/auth/utils.py

-from djangotoolbox.auth.models import UserPermissionList, GroupPermissionList, GroupList
+from copy import copy
 
-def update_user_group_permissions(obj_list):
-    group_ids = obj_list.fk_list
+from djangotoolbox.auth.models import UserPermissionList, GroupPermissionList,\
+     GroupList
+
+
+def update_user_group_permissions(group_list):
+    """
+    updates UserPermissionList.group_permission_list everytime
+    permissions of a group are modified and everytime a user joins
+    or leaves a group
+    """
+    
     perms = set()
-    if len(group_ids) > 0:
+    if len(group_list.fk_list) > 0:
         group_permissions = set()
-        group_permissions.update(GroupPermissionList.objects.filter(group__id__in=group_ids))
+        group_permissions.update(GroupPermissionList.objects.filter(group__id__in=group_list.fk_list))
         for group_perm in group_permissions:
             perms.update(group_perm.permission_list)
         
-    
-    user_perm, created = UserPermissionList.objects.get_or_create(user=obj_list.user)
+    user_perm, created = UserPermissionList.objects.get_or_create(user=group_list.user)
     user_perm.group_permission_list = list(perms)
     user_perm.save()
 
 def add_perm_to(obj, list_cls, filter):
     obj_list, created = list_cls.objects.get_or_create(**filter)
-    obj_list.permission_list.append('%s.%s' % (obj.content_type.app_label, obj.codename))
+    obj_list.permission_list.append('%s.%s' % (obj.content_type.app_label,\
+                                               obj.codename))
     obj_list.save()
 
 def add_permission_to_user(perm, user):
 
 def add_user_to_group(user, group):
     obj_list, created = GroupList.objects.get_or_create(user=user)
-
     obj_list.fk_list.append(group.id)
-    
     obj_list.save()
     update_user_group_permissions(obj_list)
     
 def add_permission_to_group(perm, group):
     add_perm_to(perm, GroupPermissionList, {'group': group})
+    
     group_list = GroupList.objects.filter(fk_list=group.id)
-
     for gl in group_list:
         update_user_group_permissions(gl)
 
 def update_list(perm_objs, list_cls, filter):
-    list_obj, created = list_cls.objects.get_or_create(**filter)
-
-    from copy import copy
+    """
+    updates a list of permissions
+    list_cls can be GroupPermissionList or UserPermissionList
+    """
+    
+    list_obj, created = list_cls.objects.get_or_create(**filter)    
     old_perms = copy(list_obj.permission_list)
 
-    perm_strs = ['%s.%s' % (perm.content_type.app_label, perm.codename) for perm in perm_objs]
+    perm_strs = ['%s.%s' % (perm.content_type.app_label, perm.codename) \
+                 for perm in perm_objs]
     perm_ids = [perm.id for perm in perm_objs]
     
     for perm in old_perms:
             list_obj.permission_list.append(perm)
             list_obj.fk_list.append(perm_ids[i])
         i += 1
-    
-    if len(perm_strs) == 0:
-        list_obj.permission_list = []
-        list_obj.fk_list = []
 
     list_obj.save()
     
     update_list(perms, GroupPermissionList, {'group': group})
 
     group_list = GroupList.objects.filter(fk_list=group.id)
-    
     for gl in group_list:
         update_user_group_permissions(gl)
 
-def update_user_groups(user, group):
-    objs = group
+def update_user_groups(user, groups):
+    objs = groups
     obj_list, created = GroupList.objects.get_or_create(user=user)
-
     old_objs = list(obj_list._get_objs())
     
     for obj in old_objs: