Exemplo n.º 1
0
def update_trip_handler(request, trip_id):
    db_trip = get_trip_by_id(request.user, trip_id)
    if not db_trip:
        return error_response(status=404)

    parsed_body = safe_parse_json(request.body)
    if parsed_body is None:
        return error_response(message="Could not parse JSON body")
    try:
        updated_trip = update_trip(request.user, db_trip, **parsed_body)
    except UpdateNotAllowed:
        return validation_error_response(
            ["Cannot update one or more requested fields"])
    return success_response(data=Trip.from_db_model(updated_trip))
Exemplo n.º 2
0
def test_create_trip(call_create_trip_endpoint, data):
    get_or_create_user(external_id="foo")

    assert 0 == count_trips_in_db()
    response = call_create_trip_endpoint(data=data)
    assert response.status_code == 201
    assert 1 == count_trips_in_db()

    response_json = safe_parse_json(response.content)
    expected_response_data = {
        "trip_id": response_json.get("trip_id"),
        "created_on": response_json.get("created_on"),
        "title": data["title"],
    }
    if data.get("description", "") != "":
        expected_response_data["description"] = data["description"]
    if "tags" in data:
        expected_response_data["tags"] = data["tags"]

    assert response_json == expected_response_data
Exemplo n.º 3
0
def create_move_handler(request):
    request_data = safe_parse_json(request.body)
    if request_data is None:
        return error_response(message="Could not parse request body as JSON")
    move_model = Move.from_dict(request_data)
    if not move_model.is_valid:
        return validation_error_response(move_model.validation_errors)

    start_location, end_location = get_locations_from_location_ids(
        request.user, move_model.start_location_id, move_model.end_location_id)
    if not start_location or not end_location:
        validation_errors = get_location_not_found_validation_errors(
            start_location_id=move_model.start_location_id,
            end_location_id=move_model.end_location_id,
            start_location=start_location,
            end_location=end_location,
        )
        return validation_error_response(validation_errors)

    db_move = create_move(start_location, end_location)

    response_dict = Move.from_db_model(db_move)
    return success_response(status=201, data=response_dict)
def get_request_data(request_body: bytes) -> Optional[RequestData]:
    try:
        return RequestData.from_dict(safe_parse_json(request_body))
    except Exception:  # noqa
        return None
Exemplo n.º 5
0
def get_trip_from_request_body(request_body):
    parsed_body = safe_parse_json(request_body) or {}
    return Trip.from_dict(parsed_body)
Exemplo n.º 6
0
def parse_location_from_request_body(request_body):
    data = safe_parse_json(request_body)
    return Location.from_dict(data)