def delete(self, request, *args, **kwargs): """ Delete a group for a user in a repo """ # Get the user username = self.kwargs.get('username') user = User.objects.get(username=username) # Get the repo group type group_type = self.kwargs.get('group_type') repo_group_type = GroupTypes.get_repo_groupname_by_base(group_type) # Get the repo object repo = Repository.objects.get(slug=self.kwargs['repo_slug']) # if the group is administrators and this user is the last one # forbid to delete if (group_type == BaseGroupTypes.ADMINISTRATORS and is_last_admin_in_repo(user, repo)): return Response( data={ "detail": ("This is the last " "administrator of the repository") }, status=status.HTTP_400_BAD_REQUEST ) remove_user_from_repo_group(user, repo, repo_group_type) return Response(status=status.HTTP_204_NO_CONTENT)
def test_is_last_admin_in_repo(self): """ Test for is_last_admin_in_repo """ # By default the repo creator is also administrator self.assertTrue( api.is_last_admin_in_repo(self.user, self.repo) ) # Add another user to the administrators. api.assign_user_to_repo_group( self.user_norepo, self.repo, GroupTypes.REPO_ADMINISTRATOR ) self.assertFalse( api.is_last_admin_in_repo(self.user, self.repo) ) # Remove the first user from the administrators. api.remove_user_from_repo_group( self.user, self.repo, GroupTypes.REPO_ADMINISTRATOR ) # Add user to the curators. api.assign_user_to_repo_group( self.user, self.repo, GroupTypes.REPO_CURATOR ) # The user is not the last of the admins because he is not admin self.assertFalse( api.is_last_admin_in_repo(self.user, self.repo) ) # The other user is indeed the last admin self.assertTrue( api.is_last_admin_in_repo(self.user_norepo, self.repo) )