Пример #1
0
def create_a_new_review(place_id):
    """ create new resource by my list of place """

    place = storage.get(Place, place_id)
    if place is None:
        return jsonify({'Error': 'Not found'}), 404

    review_object_json = request.get_json()
    if review_object_json is None:
        return jsonify({'Error': 'Not a JSON'}), 400

    if 'user_id' not in review_object_json:
        return jsonify({'Error': 'Missing user_id'}), 400

    user_id = review_object_json.get('user_id')

    user = storage.get(User, user_id)
    if user is None:
        return jsonify({'Error': 'Not found'}), 404

    if 'text' not in review_object_json:
        return jsonify({'Error': 'Missing text'}), 400

    review_object_json['place_id'] = place_id
    instance = Review(**review_object_json)
    instance.save()

    return jsonify(instance.to_dict()), 201
Пример #2
0
def create_new_review(place_id=None):
    """ create new resource by my list of reviews """

    place_object = storage.get(Place, place_id)
    if place_object is None:
        abort(404, "Not found")

    place_object_json = request.get_json()
    if place_object_json is None:
        abort(400, "Not a JSON")

    # looking for a "user_id" value for creating a object place
    user_id = place_object_json.get("user_id")
    if user_id is None:
        abort(400, 'Missing user_id')

    # creating a new user object as attribute of place object
    user_object = storage.get(User, user_id)
    if user_object is None:
        abort(404, "Not found")

    # verify if exist "name" value into POST request
    if place_object_json.get("text") is None:
        abort(400, 'Missing text')

    # If the place_id is not linked to any City object, raise a 404 error
    # this will be never raise a 404 error
    place_object_json["place_id"] = place_id

    review_object = Review(**place_object_json)
    review_object.save()

    return jsonify(review_object.to_dict()), 201
Пример #3
0
def post_review(place_id=None):
    """ Post a review in a place with an id """
    body = request.get_json(silent=True)
    if not body:
        return make_response(jsonify({'error': 'Not a JSON'}), 400)
    if 'user_id' not in body:
        return make_response(jsonify({'error': 'Missing user_id'}), 400)
    if 'text' not in body:
        return make_response(jsonify({'error': 'Missing text'}), 400)
    place = storage.get(Place, place_id)
    user = storage.get(User, body['user_id'])
    if place and user:
        new_review = Review(**body)
        new_review.place_id = place.id
        storage.new(new_review)
        storage.save()
        return make_response(jsonify(new_review.to_dict()), 201)
    return abort(404)
Пример #4
0
def create_review(places_id):
    """Creates a new place"""
    place = storage.get(Place, places_id)
    if not bool(place):
        return abort(404)

    body = request.get_json(silent=True)
    if body is None:
        return jsonify({'error': 'Not a JSON'}), 400

    if 'user_id' not in body:
        return jsonify({'error': 'Missing user_id'}), 400
    user = storage.get(User, body.get('user_id'))
    if not bool(user):
        return abort(404)

    if 'text' in body:
        new_review = Review(**body)
        setattr(new_review, 'place_id', place.id)
        storage.new(new_review)
        storage.save()
        return jsonify(new_review.to_dict()), 201
    else:
        return jsonify({'error': 'Missing text'}), 400