Anonymous committed 698c887

fix update_user_groups

Comments (0)

Files changed (2)


 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',


     perm_strs = ['%s.%s' % (perm.content_type.app_label, perm.codename) for perm in perm_objs]
-    print old_perms
     for perm in old_perms:
         except ValueError:
-    print perm_strs
     for perm in perm_strs:
     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(
+    for obj in objs:
+        try:
+            old_objs.index(obj)
+        except ValueError:
+            obj_list.fk_list.append(
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
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.