def all_reviews(review_id=None): """ Access the api call with on a place object to get its reviews returns a 404 if not found. - GET: Default, gets a review at <review_id>, status 200 - DELETE: Deletes the review at id. Returns '{}', status 200 - PUT: """ if review_id not in storage.all('Review'): abort(404) if request.method == 'DELETE': storage.delete(storage.get('Review', review_id)) storage.save() return (jsonify({})) if request.method == 'PUT': put_obj = request.get_json() if put_obj is None: return ("Not a JSON", 400) instance = storage.get('Review', review_id) ignore_keys = ['id', 'user_id', 'place_id', 'updated_at'] for attrib in put_obj: if attrib not in ignore_keys: setattr(instance, attrib, put_obj[attrib]) instance.save() return (jsonify(instance.to_json())) """ Default: GET """ instance_get = storage.get('Review', review_id) return (jsonify(instance_get.to_json()))
def user_by_id(user_id=None): """ Access the api call with on a specific user object returns a 404 if not found. Delete method removes the object Defaults is to return the user object. """ if user_id not in storage.all('User'): abort(404) if request.method == 'DELETE': storage.delete(storage.get('User', user_id)) storage.save() return (jsonify({})) if request.method == 'PUT': put_obj = request.get_json() if put_obj is None: return ("Not a JSON", 400) instance = storage.get('User', user_id) ignore_keys = ['id', 'email', 'created_at', 'updated_at'] for attrib in put_obj: if attrib not in ignore_keys: setattr(instance, attrib, put_obj[attrib]) instance.save() return (jsonify(instance.to_json())) """Default: GET request returns the object in json form""" instance = storage.get('User', user_id) return (jsonify(instance.to_json()))
def get_city_obj(city_id=None): """ API call to interact with a specific city object returns a 404 if city_id is not found. - DELETE method: Deletes the resource and returns {}, status 200 - PUT method: Updates the resource with the supplied json, status 201 - GET method: Default, returns the city object """ if city_id not in storage.all('City'): abort(404) if request.method == 'DELETE': storage.delete(storage.get('City', city_id)) storage.save() return (jsonify({})) if request.method == 'PUT': put_obj = request.get_json() if put_obj is None: return ("Not a JSON", 400) instance = storage.get('City', city_id) ignore_keys = ['id', 'state_id', 'created_at', 'updated_at'] for attrib in put_obj: if attrib not in ignore_keys: setattr(instance, attrib, put_obj[attrib]) instance.save() return (jsonify(instance.to_json())) """ Default: GET """ city_get = storage.get('City', city_id) return (jsonify(city_get.to_json()))
def manipulate_amenties_place(place_id=None): """ Access the api call with on a place object to get its amenities returns a 404 if not found. - DELETE: Deletes the link between Amenity objects and Place objects If the Amenity is not linked to the Place before the request, raise a 404 error Returns an empty dictionary with the status code 200 - POST: Link a Amenity object to a Place If the Amenity is already linked to the Place, return the Amenity with the status code 200 Returns the Amenity with the status code 201 """ if place_id not in storage.all('Place'): abort(404) if amenity_id not in storage.all('Amenity'): abort(404) if request.method == 'DELETE': storage.delete(storage.get('Place', place_id)) storage.save() return (jsonify({})) if request.method == 'POST': post_obj = request.get_json() if post_obj is None: return ("Not a JSON", 400) if 'name' not in post_obj: return ("Missing name", 400) new_obj = City(**post_obj) new_obj.state_id = state_id new_obj.save() return (jsonify(new_obj.to_json()), 201)
def review_by_place(place_id=None): """ Access the api call with on a place object to get its reviews returns a 404 if not found. - POST: Creates a new review object with the place_object linked - GET: Default, returns all review objects linked to the place. """ if place_id not in storage.all('Place'): abort(404) if request.method == 'POST': posted_obj = request.get_json() if posted_obj is None: return ("Not a JSON", 400) if 'name' not in posted_obj: return ("Missing name", 400) new_obj = State(**posted_obj) storage.save() return (jsonify(new_obj.to_json()), 201) # all_reviews = storage.get('Place', place_id).review # rtn_json = [] # for review in all_reviews: # rtn_json.append(review.to_json()) return (get_linked('Place', place_id, review))
def delete_amenity_by_place_id(place_id, amenity_id): """ deletes a amenity object by place_id """ place = storage.get(Place, place_id) if place is None: return jsonify({'Error': 'Not found'}), 404 amenity = storage.get(Amenity, amenity_id) if amenity is None: return jsonify({'Error': 'Not found'}), 404 if getenv('HBNB_TYPE_STORAGE') == "db": if amenity not in place.amenities: return jsonify({'Error': 'Not found'}), 404 place.amenities.remove(amenity) else: if amenity_id not in place.amenity_ids: return jsonify({'Error': 'Not found'}), 404 place.amenity_ids.remove(amenity_id) storage.save() return jsonify({}), 200
def link_amenity_to_place(place_id=None, amenity_id=None): """ Link a Amenity object to a Place """ place = storage.get(Place, place_id) if place is None: return jsonify({'Error': 'Not found'}), 404 amenity = storage.get(Amenity, amenity_id) if amenity is None: return jsonify({'Error': 'Not found'}), 404 if getenv('HBNB_TYPE_STORAGE') == "db": if amenity in place.amenities: return jsonify(amenity.to_dict()), 200 else: place.amenities.append(amenity) else: if amenity_id in place.amenity_ids: return jsonify(amenity.to_dict()), 200 else: place.amenity_ids.append(amenity_id) storage.save() return jsonify(amenity.to_dict()), 201
def state_by_id(state_id=None): """ Access the api call with on a specific state object returns a 404 if not found. - DELETE: Removes the state object - PUT: Updates the state object - GET: Default, return the state object. """ if state_id not in storage.all('State'): abort(404) if request.method == 'DELETE': storage.delete(storage.get('State', state_id)) storage.save() return (jsonify({})) if request.method == 'PUT': put_obj = request.get_json() if put_obj is None: return ("Not a JSON", 400) instance = storage.get('State', state_id) for attrib in put_obj: setattr(instance, attrib, put_obj[attrib]) instance.save() return (jsonify(instance.to_json())) """ Default: GET""" instance = storage.get('State', state_id) return (jsonify(instance.to_json()))
def place_by_id(place_id=None): """ Access the api call with on a specific state object returns a 404 if not found. - DELETE: Removes the state object - PUT: Updates the state object - GET: Default, return the state object. """ if place_id not in storage.all('Place'): abort(404) if request.method == 'DELETE': storage.delete(storage.get('Place', place_id)) storage.save() return (jsonify({})) if request.method == 'PUT': put_obj = request.get_json() if put_obj is None: return ("Not a JSON", 400) instance = storage.get('Place', place_id) ignore_keys = ['id', 'user_id', 'city_id', 'created_at', 'updated_at'] for attrib in put_obj: if attrib not in ignore_keys: setattr(instance, attrib, put_obj[attrib]) instance.save() return (jsonify(instance.to_json())) """ Default: GET""" instance = storage.get('Place', place_id) return (jsonify(instance.to_json()))
def delete_city(city_id): """Delete a City object by it's ID.""" city = storage.get('City', city_id) if city is None: abort(404) storage.delete(city) storage.save() return jsonify({}), 200
def delete_amenity(amenity_id): """Delete an Amenity object by it's ID.""" amenity = storage.get("Amenity", amenity_id) if amenity is None: abort(404) storage.delete(amenity) storage.save() return jsonify({}), 200
def delete_place(place_id): """Delete a Place object by it's ID.""" try: storage.delete(storage.get('Place', place_id)) storage.save() return jsonify({}), 200 except: abort(404)
def delete_user(user_id): """Delete a User object by it's ID.""" user = storage.get("User", user_id) if user is None: abort(404) storage.delete(user) storage.save() return jsonify({}), 200
def delete_review(review_id): """Delete a Review object by it's ID.""" try: storage.delete(storage.get('Review', review_id)) storage.save() return jsonify({}), 200 except: abort(404)
def delete_review_by_id(review_id=None): """ delete a resource of my list of review """ review_object = storage.get(Review, review_id) if review_object is None: abort(404, "Not found") storage.delete(review_object) storage.save() return jsonify("{}"), 200
def delete_city(city_id=None): """ delete a resource of my list of states """ city_object = storage.get(City, city_id) if city_object is None: abort(404, "Not found") storage.delete(city_object) storage.save() return jsonify("{}"), 200
def delete_user(user_id=None): """ delete a resource of my list of cities """ user_object = storage.get(User, user_id) if user_object is None: abort(404, "Not found") storage.delete(user_object) storage.save() return jsonify("{}"), 200
def delete_place(place_id=None): """ delete a place object by id """ place_object = storage.get(Place, place_id) if place_object is None: return jsonify({'Error': 'Not found'}), 404 storage.delete(place_object) storage.save() return jsonify({}), 200
def delete_amenity(amenity_id=None): """ delete a resource of my list of states """ amenity_object = storage.get(Amenity, amenity_id) if amenity_object is None: return jsonify({'Error': 'Not found'}), 404 storage.delete(amenity_object) storage.save() return jsonify({}), 200
def delete_city(city_id=None): """ delete a city object by id """ city_object = storage.get(City, city_id) if city_object is None: return jsonify({'Error': 'Not found'}), 404 storage.delete(city_object) storage.save() return jsonify({}), 200
def delete_user(user_id=None): """ delete a resource of my list of states """ user_object = storage.get(User, user_id) if user_object is None: return jsonify({'Error': 'Not found'}), 404 storage.delete(user_object) storage.save() return jsonify({}), 200
def delete_place_by_id(place_id=None): """ delete a resource of my list of states """ place_object = storage.get(Place, place_id) if place_object is None: abort(404, "Not found") storage.delete(place_object) storage.save() return jsonify("{}"), 200
def delete_review(review_id): """delete review by id """ review = storage.get(Review, review_id) if review is None: return jsonify({'Error': 'Not found'}), 404 storage.delete(review) storage.save() return jsonify({}), 200
def update_a_user(user_id=None): """ update a resource of my objects """ user_object_json = request.get_json() if user_object_json is None: return jsonify({'Error': 'Not a JSON'}), 400 user = storage.get(User, user_id) if user is None: return jsonify({'Error': 'Not found'}), 404 ignore = ['id', 'email', 'created_at', 'updated_at'] for key, value in user_object_json.items(): if key not in ignore: setattr(user, key, value) storage.save() return jsonify(user.to_dict()), 200
def put_review(review_id): """ Updates a Review object resouce by id """ review = storage.get(Review, review_id) if review 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 ignore = ['id', 'user_id', 'place_id', 'created_at', 'updated_at'] for key, value in review_object_json.items(): if key not in ignore: setattr(review, key, value) storage.save() return jsonify(review.to_dict()), 200
def update_a_amenity(amenity_id=None): """ update a resource of my objects """ amenity_object_json = request.get_json() if amenity_object_json is None: return jsonify({'Error': 'Not a JSON'}), 400 amenity = storage.get(Amenity, amenity_id) if amenity is None: return jsonify({'Error': 'Not found'}), 404 ignore = ['id', 'created_at', 'updated_at'] for key, value in amenity_object_json.items(): if key not in ignore: setattr(amenity, key, value) storage.save() return jsonify(amenity.to_dict()), 200
def create_place_amenity(place_id=None, amenity_id=None): '''This is the 'create_place_amenity' method. Creates a link between an Amenity object and a given place. ''' place = storage.get("Place", place_id) if place is None: return "Bad place", 404 amenity = storage.get("Amenity", amenity_id) if amenity is None: return "Bad amenity", 404 if amenity in place.amenities: return jsonify(amenity.to_json()), 200 place.amenities.append(amenity) place.save() storage.save() return jsonify(amenity.to_json()), 201
def update_a_city(city_id=None): """ update a object city by id """ print("New request:") # print("\t-path: {}".format(path)) print("\t-verb: {}".format(request.method)) print("\t-headers: ") for k, v in request.headers: print("\t\t{} = {}".format(k, v)) print("\t-query parameters: ") for qp in request.args: print("\t\t{} = {}".format(qp, request.args.get(qp))) print("\t-raw body: ") print("\t\t{}".format(request.data)) print("\t-form body: ") for fb in request.form: print("\t\t{} = {}".format(fb, request.form.get(fb))) print("\t-json body: ") print("\t\t{}".format(request.json)) city_object_json = request.get_json() if city_object_json is None: return jsonify({'Error': 'Not a JSON'}), 400 city = storage.get(City, city_id) if city is None: return jsonify({'Error': 'Not found'}), 404 ignore = ['id', 'state_id', 'created_at', 'updated_at'] for key, value in request.get_json().items(): if key not in ignore: setattr(city, key, value) storage.save() return jsonify(city.to_dict()), 200
def all_states(): """ Access the api call on all state objects - POST: Adds a new State object. Requires name parameter. - GET: Default, returns a list of all states """ if request.method == 'POST': posted_obj = request.get_json() if posted_obj is None: return ("Not a JSON", 400) if 'name' not in posted_obj: return ("Missing name", 400) new_obj = State(**posted_obj) storage.save() return (jsonify(new_obj.to_json()), 201) """ Default: GET""" rtn_json = [] all_obj = storage.all('State') for instance in all_obj: rtn_json.append(all_obj[instance].to_json()) return (jsonify(rtn_json))
def amenity_by_id(amenity_id=None): if amenity_id not in storage.all('Amenity'): abort(404) if request.method == 'DELETE': storage.delete(storage.get('Amenity', amenity_id)) storage.save() return (jsonify({})) if request.method == 'PUT': put_obj = request.get_json() if put_obj is None: return ("Not a JSON", 400) instance = storage.get('Amenity', amenity_id) ignore_keys = ['id', 'created_at', 'updated_at'] for attrib in put_obj: if attrib not in ignore_keys: setattr(instance, attrib, put_obj[attrib]) instance.save() return (jsonify(instance.to_json())) instance = storage.get('Amenity', amenity_id) return (jsonify(instance.to_json()))