Beispiel #1
0
def single_invite(request, invite_id):
    # Early exit on missing authorization / invalid token
    if 'Authorization' not in request.headers:
        error_message = generate_error_json(
            status.HTTP_401_UNAUTHORIZED,
            "Authorization was not included in headers", None, None)
        return Response(data=error_message,
                        status=status.HTTP_401_UNAUTHORIZED)
    else:
        if not token_status(request.headers['Authorization']):
            error_message = generate_error_json(status.HTTP_401_UNAUTHORIZED,
                                                "Token was invalid", None,
                                                None)
            return Response(data=error_message,
                            status=status.HTTP_401_UNAUTHORIZED)

    if request.method == 'GET':
        return single_invite_get(request, invite_id)
    elif request.method == 'PUT':
        response = single_invite_put(request, invite_id)
        return response
    elif request.method == 'DELETE':
        return single_invite_delete(request, invite_id)
    else:
        print("/invites/invite_id does not have a \'" + request.method +
              "\' handling")
def players_post(request):
    decode_error_message = generate_error_json(status.HTTP_400_BAD_REQUEST, 'Json decode error',
                                               'Your body should probably look like the value in correct_data',
                                               CORRECT_PLAYER_OBJ)

    json_request = get_json_data_object(request, decode_error_message)
    if type(json_request) is Response:
        return json_request

    try:
        player_req = json_request['player']
        json_body = {
            "player": {
                "username": player_req['username'],
                "email": player_req['email'],
                "first_name": player_req['first_name'],
                "last_name": player_req['last_name'],
                "study_programme": player_req['study_programme'],
                "high_score": player_req['high_score']
            }
        }
        response = connection_service("/players/", json_body, None, "POST")
        return Response(data=json.loads(response.content), status=response.status_code)
    except KeyError:
        return Response(data=decode_error_message, status=status.HTTP_400_BAD_REQUEST)
Beispiel #3
0
def invites_post(request):
    decode_error_message = generate_error_json(
        status.HTTP_400_BAD_REQUEST, 'Json decode error',
        'Your body should probably look like the value in correct_data',
        CORRECT_INVITE_OBJ)

    json_request = get_json_data_object(request, decode_error_message)
    if type(json_request) is Response:
        return json_request

    invite = json_request['invite']
    # if invite['accepted']==

    json_body = {
        "invite": {
            "sender_player_id": invite['sender_player_id'],
            "receiver_player_id": invite['receiver_player_id'],
            "match_name": invite['match_name'],
            "question_duration": invite['question_duration'],
            "accepted": invite['accepted']
        }
    }
    response = connection_service("/invites/", json_body, None, "POST")
    return Response(data=json.loads(response.content),
                    status=response.status_code)
def players(request):
    # Early exit on missing authorization / invalid token
    if 'Authorization' not in request.headers:
        error_message = generate_error_json(status.HTTP_401_UNAUTHORIZED, "Authorization was not included in headers",
                                            None, None)
        return Response(data=error_message, status=status.HTTP_401_UNAUTHORIZED)
    else:
        if not token_status(request.headers['Authorization']):
            error_message = generate_error_json(status.HTTP_401_UNAUTHORIZED, "Token was invalid", None, None)
            return Response(data=error_message, status=status.HTTP_401_UNAUTHORIZED)

    # Goes to the different method calls
    if request.method == 'GET':
        return players_get(request)
    elif request.method == 'POST':
        return players_post(request)
    else:
        print("/players/ does not have a \'" + request.method + "\' handling")
Beispiel #5
0
def create_game(invite_data):
    invite = invite_data['invite']
    sender_player_id = invite['sender_player_id']
    receiver_player_id = invite['receiver_player_id']
    player1 = {}
    player2 = {}
    player1['game_player'] = {
        "player_id": sender_player_id,
        "game_progress": 0,
        "score": 0
    }
    player2['game_player'] = {
        "player_id": receiver_player_id,
        "game_progress": 0,
        "score": 0
    }

    player1['game_round'] = []
    player2['game_round'] = []

    # Get the invite to get a hold of
    # match_name: string
    # question_duration: double

    player_status = []
    questions_list = []
    player_status.append(player1)
    player_status.append(player2)

    try:
        questions_list = get_questions_list()
        game_object = {
            "game": {
                "match_name": invite['match_name'],
                "question_duration": invite['question_duration'],
                "questions": questions_list,
                "player_status": player_status
            }
        }
        connection_service("/games/", game_object, None, "POST")
        return game_object
    except ConnectionError as e:
        error_message = generate_error_json(status.HTTP_404_NOT_FOUND,
                                            "No database connection found",
                                            None, None)
        response = JsonResponse(data=error_message,
                                status=status.HTTP_404_NOT_FOUND)
        return response
Beispiel #6
0
def single_invite_put(request, invite_id):
    decode_error_message = generate_error_json(
        status.HTTP_400_BAD_REQUEST, 'Json decode error',
        'Your body should probably look like the value in correct_data',
        CORRECT_INVITE_OBJ)

    json_request = get_json_data_object(request, decode_error_message)
    # If the type of the return from get_json_data_object is a Response,
    # and not a json, there was an error
    if type(json_request) is Response:
        return json_request

    response = connection_service(f"/invites/{invite_id}/", json_request, None,
                                  "PUT")
    if type(response) != JsonResponse:
        response = create_game(json_request)
        return Response(data=response, status=status.HTTP_202_ACCEPTED)
    elif type(response) == JsonResponse:
        return response