Commits

Anonymous committed 698c887

fix update_user_groups

Comments (0)

Files changed (2)

djangotoolbox/auth/tests.py

 from djangotoolbox.auth.models import UserPermissionList, \
      GroupPermissionList, GroupList
 from djangotoolbox.auth.utils import add_permission_to_user, \
-     add_user_to_group, add_permission_to_group, update_permissions_user
+     add_user_to_group, add_permission_to_group, update_permissions_user, update_user_groups
 
 
 class BackendTest(TestCase):
         self.assertEqual(user.has_perm('auth.test1'), False)
         self.assertEqual(user.has_perm('auth.test2'), False)
         
-    """
+ 
     def test_add_user_to_group(self):
         user = User.objects.get(username='test')
         group = Group.objects.create(name='test_group')
-        add_user_to_group(user, group)
+        update_user_groups(user, [group])
         self.assertEqual(GroupList.objects.count(), 1)
         self.assertNotEqual(GroupList.objects.all()[0] , None)
-
+    """
     def test_add_permission_to_group(self):
         content_type = ContentType.objects.get_for_model(Group)
         perm = Permission.objects.create(name='test',

djangotoolbox/auth/utils.py

 
     perm_strs = ['%s.%s' % (perm.content_type.app_label, perm.codename) for perm in perm_objs]
 
-    print old_perms
     for perm in old_perms:
         try: 
             perm_strs.index(perm)
         except ValueError:
             list_obj.permission_list.remove(perm)
 
-    print perm_strs
     for perm in perm_strs:
         try:
             old_perms.index(perm)
     update_list(perms, GroupPermissionList, {'group': group})
 
 def update_user_groups(user, group):
-    update_list(group, GroupList, {'user': user})
+    objs = group
+    obj_list, created = GroupList.objects.get_or_create(user=user)
+
+    old_objs = list(obj_list._get_objs())
+    
+    for obj in old_objs:
+        try:
+            objs.index(obj)
+        except ValueError:
+            obj_list.fk_list.remove(obj.id)
+    
+    for obj in objs:
+        try:
+            old_objs.index(obj)
+        except ValueError:
+            obj_list.fk_list.append(obj.id)
+    
+    obj_list.save()
+
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.