def manage_community_groups(sender, instance, created, **kwargs): """Manage user groups and user permissions for a particular Community""" name = instance.name if created: groups = create_groups(name, groups_templates) assign_permissions( instance, groups, groups_templates, group_permissions) community_admin_group = next( g for g in groups if g.name == COMMUNITY_ADMIN.format(name)) instance.admin.join_group(community_admin_group) instance.add_member(instance.admin) instance.save() else: if name != instance.original_name and instance.original_name: rename_groups(instance.original_name, instance.name) if instance.admin != instance.original_admin and \ instance.original_admin is not None: community_admin_group = \ get_object_or_404(Group, name=COMMUNITY_ADMIN.format(name)) instance.original_admin.leave_group( community_admin_group) instance.admin.join_group(community_admin_group) if instance.admin not in instance.members.all(): instance.add_member(instance.admin) instance.save()
def manage_community_groups(sender, instance, created, **kwargs): """Manage user groups and user permissions for a particular Community""" name = instance.name if created: groups = create_groups(name) assign_permissions(instance, groups) community_admin_group = next(g for g in groups if g.name == COMMUNITY_ADMIN.format(name)) instance.admin.join_group(community_admin_group) instance.add_member(instance.admin) instance.save() else: if name != instance.original_name and instance.original_name: rename_groups(instance.original_name, instance.name) if instance.admin != instance.original_admin and \ instance.original_admin is not None: community_admin_group = \ get_object_or_404(Group, name=COMMUNITY_ADMIN.format(name)) instance.original_admin.leave_group(community_admin_group) instance.admin.join_group(community_admin_group) if instance.admin not in instance.members.all(): instance.add_member(instance.admin) instance.save()
def test_rename_groups(self): """Test the renaming of groups according to a new name""" old_name = "Foo" new_name = "Bar" create_groups(old_name) groups = rename_groups(old_name, new_name) expected_group_names = [] for key, group_name in groups_templates.items(): expected_group_names.append(group_name.format(new_name)) group_names = [] for group in groups: group_names.append(group.name) self.assertCountEqual(expected_group_names, group_names) community_groups = Group.objects.filter(name__startswith=new_name) self.assertCountEqual(community_groups, groups) old_community_groups = Group.objects.filter(name__startswith=old_name) self.assertSequenceEqual(old_community_groups, [])