def places_by_city(city_id): """ Access the api call on all state objects - POST: Adds a new Place object. Requires name parameter. - GET: Default, returns a list of all places within a city """ if city_id not in storage.all('City'): print("ERROR: ID not found") abort(404) if request.method == 'POST': posted_obj = request.get_json() if posted_obj is None: return ("Not a JSON", 400) if 'user_id' not in posted_obj: return ("Missing user_id", 400) if posted_obj['user_id'] not in storage.all('User'): abort(404) if 'name' not in posted_obj: return ("Missing name", 400) new_obj = Place(**posted_obj) new_obj.city_id = city_id new_obj.save() return (jsonify(new_obj.to_json()), 201) """ Default: GET""" all_obj = storage.get('City', city_id).places rtn_json = [] for place in all_obj: rtn_json.append(place.to_json()) return (jsonify(rtn_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 view_amenity(amenity_id=None): """Example endpoint returning a list of all amenities or of one specified Retrieves a list of all amenties or of one specified by amenity_id --- parameters: - name: amenity_id in: path type: string enum: ["all", cf701d1a-3c19-4bac-bd99-15321f1140f2", None] required: true default: None definitions: Amenity: type: object properties: __class__: type: string description: The string of class object created_at: type: string description: The date the object created email: type: string first_name: type: string last_name: type: string id: type: string description: the id of the user updated_at: type: string description: The date the object was updated items: $ref: '#/definitions/Color' Color: type: string responses: 200: description: A list of dicts or dict, each dict is an amenity schema: $ref: '#/definitions/Amenity' examples: [{"__class__": "Amenity", "created_at": "2017-03-25T02:17:06", "id": "cf701d1a-3c19-4bac-bd99-15321f1140f2", "name": "Dog(s)", "updated_at": "2017-03-25T02:17:06"}] """ if amenity_id is None: all_amenities = [ state.to_json() for state in storage.all("Amenity").values() ] return jsonify(all_amenities) s = storage.get("Amenity", amenity_id) if s is None: abort(404) return jsonify(s.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 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 city_by_state(state_id=None): """ Access the api call with on a state object to get its cities returns a 404 if not found. - POST: Creates a new city object with the state_object linked - GET: Default, returns all city objects linked to the state. """ if state_id not in storage.all('State'): abort(404) 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) """Default: GET""" all_cities = storage.get('State', state_id).cities rtn_json = [] for city in all_cities: rtn_json.append(city.to_json()) return (jsonify(rtn_json))
def all_users(): """ Adds new User objects, if provided with a name parameter in a POST request Default is to Returns a list of all users in json format for GET requests """ all_users = storage.all('User') if request.method == 'POST': posted_obj = request.get_json() if posted_obj is None: return ("Not a JSON", 400) if 'email' not in posted_obj.keys(): return ("Missing email", 400) if 'password' not in posted_obj.keys(): return ("Missing password", 400) # linear search through users to see if email is in db already for user in all_users: email_in_db = getattr(all_users.get(user), 'email') if posted_obj['email'] == email_in_db: return ("ERROR: Email is already registered", 400) new_obj = User(**posted_obj) new_obj.save() return (jsonify(new_obj.to_json()), 201) rtn_json = [] for instance in all_users: rtn_json.append(all_users[instance].to_json()) return (jsonify(rtn_json))
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 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 get_amenities(): """ list of all Amenity objects """ all_amenitie = storage.all(Amenity).values() all_amenities = [state.to_dict() for state in all_amenitie] return jsonify(all_amenities)
def view_all_states(): '''This is the 'view_all_states' method. Lists all State objects. ''' states = [state.to_json() for state in storage.all("State").values()] return jsonify(states)
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 get_user(): """ list of all Users objects """ all_user = storage.all(User).values() all_users = [user.to_dict() for user in all_user] return jsonify(all_users)
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 all_users(): """Return a JSON list of all User objects.""" try: users = storage.all('User').values() except: abort(404) return jsonify([user.to_json() for user in users])
def list_places(): """ list all places """ try: r = request.get_json() except: r = None if r is None: return "Not a JSON", 400 if not r: return jsonify([e.to_json() for e in storage.all("Place").values()]) all_cities_id = r.get("cities", []) states = r.get("states") if states: all_states = [storage.get("State", s) for s in states] all_states = [a for a in all_states if a is not None] all_cities_id += [c.id for s in all_states for c in s.cities] all_cities_id = list(set(all_cities_id)) all_amenities = r.get("amenities") all_places = [] if all_cities_id or all_amenities: all_places2 = storage.all("Place").values() if all_cities_id: all_places2 = [ p for p in all_places2 if p.city_id in all_cities_id ] if all_amenities: if os.getenv('HBNB_TYPE_STORAGE', 'fs') != 'db': all_places = [ p for p in all_places2 if set(all_amenities) <= set(p.amenities_id) ] else: for e in all_places2: flag = True for a in all_amenities: if a not in [i.id for i in e.amenities]: flag = False break if flag: all_places.append(e) else: all_places = all_places2 return jsonify([p.to_json() for p in all_places])
def view_amenity(amenity_id=None): """Example endpoint returning a list of all amenities or of one specified Retrieves a list of all amenties or of one specified by amenity_id --- parameters: - name: amenity_id in: path type: string enum: ["all", cf701d1a-3c19-4bac-bd99-15321f1140f2", None] required: true default: None definitions: Amenity: type: object properties: __class__: type: string description: The string of class object created_at: type: string description: The date the object created email: type: string first_name: type: string last_name: type: string id: type: string description: the id of the user updated_at: type: string description: The date the object was updated items: $ref: '#/definitions/Color' Color: type: string responses: 200: description: A list of dicts or dict, each dict is an amenity schema: $ref: '#/definitions/Amenity' examples: [{"__class__": "Amenity", "created_at": "2017-03-25T02:17:06", "id": "cf701d1a-3c19-4bac-bd99-15321f1140f2", "name": "Dog(s)", "updated_at": "2017-03-25T02:17:06"}] """ if amenity_id is None: all_amenities = [state.to_json() for state in storage.all("Amenity").values()] return jsonify(all_amenities) s = storage.get("Amenity", amenity_id) if s is None: abort(404) return jsonify(s.to_json())
def all_amenities(): """ Return a JSON list of all Amenity objects. """ try: amenities = storage.all('Amenity').values() except: abort(404) a = [amenity.to_json() for amenity in amenities] return jsonify(a)
def view_user(user_id=None): """ view all user """ if user_id is None: all_users = [state.to_json() for state in storage.all("User").values()] return jsonify(all_users) s = storage.get("User", user_id) if s is None: abort(404) return jsonify(s.to_json())
def view_user(user_id=None): '''This is the 'user_id' method. Retrieves a User object. ''' if user_id is None: users = [state.to_json() for state in storage.all("User").values()] return jsonify(users) user = storage.get("User", user_id) if user is None: abort(404) return jsonify(user.to_json())
def amenity_by_place(place_id=None): """ Access the api call with on a place object to get its amenities returns a 404 if not found. - POST: Creates a new amenity object with the linked place object - DELETE: Default, returns all amenity objects linked to the place. """ if place_id not in storage.all('Place'): abort(404) all_places = storage.get('Place', place_id).amenities rtn_json = [] for place in all_places: rtn_json.append(place.to_json()) return (jsonify(rtn_json))
def all_amenities(): 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 = Amenity(**posted_obj) new_obj.save() return (jsonify(new_obj.to_json()), 201) rtn_json = [] all_obj = storage.all('Amenity') for instance in all_obj: rtn_json.append(all_obj[instance].to_json()) return (jsonify(rtn_json))
def view_amenity(amenity_id=None): '''This is the 'view_amenity' method. Retrieves a given Amenity object. ''' if amenity_id is None: amenities = [ state.to_json() for state in storage.all("Amenity").values() ] return jsonify(amenities) amenity = storage.get("Amenity", amenity_id) if amenity is None: abort(404) return jsonify(amenity.to_json())
def view_all_states(): """Example endpoint returning a list of all the states Retrieves a list of all the states --- definitions: State: type: object properties: __class__: type: string description: The string of class object created_at: type: string description: The date the object created id: type: string description: the id of the state name: type: string description: name of the state updated_at: type: string description: The date the object was updated items: $ref: '#/definitions/Color' Color: type: string responses: 200: description: A list of dictionarys, each dict is a State schema: $ref: '#/definitions/State' examples: [{'__class__': 'State', 'created_at': '2017-03-25T02:17:06', 'id': '10098698-bace-4bfb-8c0a-6bae0f7f5b8f', 'name': 'Oregon', 'updated_at': '2017-03-25T02:17:06'}] """ all_states = [state.to_json() for state in storage.all("State").values()] return jsonify(all_states)
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()))
def list_places(): """Example endpoint list all places of a JSON body Retrieves a list of all places of a JSON body --- parameters: - name: city_id in: path type: string enum: ['None', "1da255c0-f023-4779-8134-2b1b40f87683"] required: true default: None definitions: Place: type: object properties: __class__: type: string description: The string of class object created_at: type: string description: The date the object created description: type: string description: The description of the place id: type: string description: The id of the place latitude: type: float longitude: type: float max_guest: type: int description: The maximum guest allowed name: type: string description: name of the place number_bathrooms: type: int number_rooms: type: int price_by_night: type: int updated_at: type: string description: The date the object was updated user_id: type: string description: id of the owner of the place items: $ref: '#/definitions/Color' Color: type: string responses: 200: description: A list of a dictionary of the desire objects schema: $ref: '#/definitions/Place' examples: [{"__class__": "Place", "created_at": "2017-03-25T02:17:06", "id": "dacec983-cec4-4f68-bd7f-af9068a305f5", "name": "The Lynn House", "city_id": "1721b75c-e0b2-46ae-8dd2-f86b62fb46e6", "user_id": "3ea61b06-e22a-459b-bb96-d900fb8f843a", "description": "Our place is 2 blocks from Vista Park", "number_rooms": 2, "number_bathrooms": 2, "max_guest": 4, "price_by_night": 82, "latitude": 31.4141, "longitude": -109.879, "updated_at": "2017-03-25T02:17:06" }, "..."] """ try: r = request.get_json() except: r = None if r is None: return "Not a JSON", 400 if not r: return jsonify([e.to_json() for e in storage.all("Place").values()]) all_cities_id = r.get("cities", None) states = r.get("states", None) if states: all_states = [storage.get("State", s) for s in states] all_states = [a for a in all_states if a is not None] if all_cities_id: all_cities_id += [c.id for s in all_states for c in s.cities] else: all_cities_id = [c.id for s in all_states for c in s.cities] if all_cities_id: all_cities_id = list(set(all_cities_id)) all_amenities = r.get("amenities", None) all_places = [] if (all_cities_id is not None) or (all_amenities is not None): all_places2 = storage.all("Place").values() if all_cities_id: all_places2 = [ p for p in all_places2 if p.city_id in all_cities_id ] if all_amenities is not None: if os.getenv('HBNB_TYPE_STORAGE', 'fs') != 'db': all_places = [ p for p in all_places2 if set(all_amenities) <= set(p.amenities_id) ] else: for e in all_places2: flag = True for a in all_amenities: if a not in [i.id for i in e.amenities]: flag = False break if flag: # using amenities make it instance attribute, # not just class check out to_json all_places.append(e) else: all_places = all_places2 return jsonify([p.to_json() for p in all_places])
def view_user(user_id=None): """Example endpoint returning a list of all users or of one specified Retrieves a list of all users or of one specified by user_id --- parameters: - name: user_id in: path type: string enum: ["all", "32c11d3d-99a1-4406-ab41-7b6ccb7dd760"] required: true default: None definitions: User: type: object properties: __class__: type: string description: The string of class object created_at: type: string description: The date the object created email: type: string first_name: type: string last_name: type: string id: type: string description: the id of the user updated_at: type: string description: The date the object was updated items: $ref: '#/definitions/Color' Color: type: string responses: 200: description: A list of dictionarys or dictionary, each dict is a user schema: $ref: '#/definitions/User' examples: [{"__class__": "User", "_password": "******", "created_at": "2017-03-25T02:17:06", "email": "*****@*****.**", "first_name": "Susan", "id": "32c11d3d-99a1-4406-ab41-7b6ccb7dd760", "last_name": "Finney", "updated_at": "2017-03-25T02:17:06"}] """ if user_id is None: all_users = [state.to_json() for state in storage.all("User").values()] return jsonify(all_users) s = storage.get("User", user_id) if s is None: abort(404) return jsonify(s.to_json())