Пример #1
0
def amenity_add():
    data = request.get_json()
    if data is None:
        return 'Not a JSON', 400
    if 'name' not in data.keys():
        return 'Missing name', 400
    amenity = Amenity(**data)
    amenity.save()
    return jsonify(amenity.to_json()), 201
Пример #2
0
def post_amenity():
    """ Adds a new state to State
    """
    if not request.json:
        return jsonify({'error': 'Not a JSON'}), 400
    if 'name' not in request.json:
        return jsonify({'error': 'Missing name'}), 400
    news = request.get_json().get('name')
    newo = Amenity(name=news)
    newo.save()
    return jsonify(newo.to_dict()), 201
Пример #3
0
def create_amenity():
    """Creates a new amenity"""
    req = request.get_json()
    if req is None:
        return (jsonify("Not a JSON"), 400)
    try:
        req["name"]
    except:
        return (jsonify("Missing name"), 400)
    data = Amenity(**req)
    data.save()
    return (jsonify(data.to_json()), 201)
Пример #4
0
def post_amenities():
    """
    creates an amenity
    """
    content = request.get_json()
    if not content:
        return (jsonify({"error": "Not a JSON"}), 400)
    if "name" not in content:
        return (jsonify({"error": "Missing name"}), 400)
    post = Amenity(**content)
    post.save()
    return (jsonify(post.to_dict()), 201)
Пример #5
0
def post_amenity(amenity_id):
    """
    Post to amenity
    """
    try:
        r = request.get_json()
        if "name" not in r:
            return ("Missing name", 400)
        add_amenity = Amenity(r)
        add_amenity.save()
        return (jsonify(add_amenity.to_json()), 201)
    except:
        return ("Not a json", 400)
Пример #6
0
def post_amenities():
    """use the post method to create"""
    try:
        if not request.is_json:
            return make_response(jsonify({'error': 'Not a JSON'}), 400)
        req = request.get_json()
    except:
        return make_response(jsonify({'error': 'Not a JSON'}), 400)
    dict_name = req.get("name")
    if dict_name is None:
        return make_response(jsonify({'error': 'Missing name'}), 400)
    new_amenity = Amenity(**req)
    storage.new(new_amenity)
    storage.save()
    return(jsonify(new_amenity.to_dict()), 201)
Пример #7
0
def create_amenity():
    '''
    Creates an Amenity object.
    '''
    if not request.is_json:
        abort(400, "Not a JSON")
    update = request.get_json()
    if 'name' not in update.keys():
        abort(400, "Missing name")
    new_amenity = Amenity()
    storage.new(new_amenity)
    for key, value in update.items():
        new_amenity.__dict__[key] = value
    storage.save()
    return jsonify(new_amenity.to_dict()), 201
Пример #8
0
def post_amenities():
    """
    Creat amenity
    """
    content = request.get_json()
    if content is None:
        return jsonify({"error": "Not a JSON"}), 400
    if "name" not in content:
        return jsonify({"error": "Missing name"}), 400
    exclude = ['id', 'created_at', 'updated_at']
    for e in exclude:
        content.pop(e, None)
    new_amenity = Amenity(**content)
    storage.new(new_amenity)
    storage.save()
    return jsonify(new_amenity.to_dict()), 201
Пример #9
0
def all_amenities():
    '''
        GET: list all amenities in storage
        POST: add a new amenity
    '''
    if request.method == 'POST':
        amenity_dict = request.get_json()
        if amenity_dict is None:
            return 'Not a JSON', 400
        if 'name' not in amenity_dict.keys():
            return 'Missing name', 400
        my_amenity = Amenity(**amenity_dict)
        my_amenity.save()
        return jsonify(my_amenity.to_dict()), 201
    my_amn = [amenity.to_dict() for amenity in storage.all('Amenity').values()]
    return jsonify(my_amn)
Пример #10
0
def create_amenity():
    """
    Creates new amenity. If request body not valid JSON, raises 400
    If dict does not contain 'name' key, raise 400
    Returns amenity object with status 201
    """
    if not request.get_json() or 'name' not in request.get_json():
        abort(400, "Missing name")

    kwargs = request.get_json()
    my_amenity = Amenity(**kwargs)

    storage.new(my_amenity)
    storage.save()

    return jsonify(my_amenity.to_dict()), 201
Пример #11
0
def get_post_amenities():
    """
    Retrieves list of all Amenity objects
    """
    if request.method == 'GET':
        return jsonify(
            [obj.to_dict() for obj in storage.all("Amenity").values()])
    if request.method == 'POST':
        if not request.json:
            abort(400, 'Not a JSON')
        if 'name' not in request.json:
            abort(400, 'Missing name')
        data = request.get_json().get('name')
        new_amenity = Amenity(name=data)
        new_amenity.save()
        return make_response(jsonify(new_amenity.to_dict()), 201)
Пример #12
0
def create_amenity():
    '''
       Creates a new Amenity object and saves it to storage
    '''
    if not request.json:
        return jsonify({"error": "Not a JSON"}), 400
    else:
        amenity_dict = request.get_json()
        if "name" in amenity_dict:
            amenity_name = amenity_dict["name"]
            amenity = Amenity(name=amenity_name)
            for k, v in amenity_dict.items():
                setattr(amenity, k, v)
            amenity.save()
            return jsonify(amenity.to_dict()), 201
        else:
            return jsonify({"error": "Missing name"}), 400
Пример #13
0
def make_amenity():
    """
    create an amenity
    """
    try:
        r = request.get_json()
    except:
        r = None
    if r is None:
        abort(400, "Not a JSON")
    if name not in r.keys():
        abort(400, "Missing name")
    new = Amenity(r)
    storage.new(new)
    storage.save()
    return (jsonify(storage.get("Amenity", new.id).to_json()), 201)
Пример #14
0
def create_amenity():
    ''' Creates an Amenity '''
    try:
        content = request.get_json()
    except:
        return (jsonify({"error": "Not a JSON"}), 400)
    name = content.get("name")
    if name is None:
        return (jsonify({"error": "Missing name"}), 400)

    new_amenity = Amenity()
    new_amenity.name = name
    new_amenity.save()
    return (jsonify(new_amenity.to_dict()), 201)