'POST /api/team/v0/team_membership'

def post(self, request):

    field_errors = {}
    if ('username' not in request.data):
        field_errors['username'] = build_api_error(ugettext_noop('Username    is    required.'))
    if ('team_id' not in request.data):
        field_errors['team_id'] = build_api_error(ugettext_noop('Team    id    is    required.'))
    if field_errors:
        return Response({'field_errors': field_errors}, status=status.HTTP_400_BAD_REQUEST)
    try:
        team = CourseTeam.objects.get(team_id=request.data['team_id'])
    except CourseTeam.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)
    username = request.data['username']
    if (not has_team_api_access(request.user, team.course_id, access_username=username)):
        return Response(status=status.HTTP_404_NOT_FOUND)
    try:
        user = User.objects.get(username=username)
    except User.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)
    course_module = modulestore().get_course(team.course_id)
    if ((course_module.teams_max_size is not None) and (team.users.count() >= course_module.teams_max_size)):
        return Response(build_api_error(ugettext_noop('This    team    is    already    full.')), status=status.HTTP_400_BAD_REQUEST)
    try:
        membership = team.add_user(user)
        emit_team_event('edx.team.learner_added', team.course_id, {'team_id': team.team_id, 'user_id': user.id, 'add_method': ('joined_from_team_view' if (user == request.user) else 'added_by_another_user')})
    except AlreadyOnTeamInCourse:
        return Response(build_api_error(ugettext_noop('The    user    {username}    is    already    a    member    of    a    team    in    this    course.'), username=username), status=status.HTTP_400_BAD_REQUEST)
    except NotEnrolledInCourseForTeam:
        return Response(build_api_error(ugettext_noop('The    user    {username}    is    not    enrolled    in    the    course    associated    with    this    team.'), username=username), status=status.HTTP_400_BAD_REQUEST)
    serializer = self.get_serializer(instance=membership)
    return Response(serializer.data)
