def create_new_place(city_id=None): """ create new resource by my list of places """ city_object = storage.get(City, city_id) if city_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("name") is None: abort(400, 'Missing name') # If the city_id is not linked to any City object, raise a 404 error # this will be never raise a 404 error place_object_json["city_id"] = city_id place_object = Place(**place_object_json) place_object.save() return jsonify(place_object.to_dict()), 201
def create_place(city_id): '''This is the 'create_place' method. Creates a Place object. ''' city = storage.get("City", city_id) if city is None: abort(404) try: r = request.get_json() except: r = None if r is None: return "Not a JSON", 400 if 'user_id' not in r.keys(): return "Missing user_id", 400 user = storage.get("User", r.get("user_id")) if user is None: abort(404) if 'name' not in r.keys(): return "Missing name", 400 r["city_id"] = city_id place = Place(**r) place.save() return jsonify(place.to_json()), 201
def create_new_place(city_id=None): """ create new resource by my list of place """ 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 = storage.get(City, city_id) if city is None: return jsonify({'Error': 'Not found'}), 404 place_object_json = request.get_json() if place_object_json is None: return jsonify({'Error': 'Not a JSON'}), 400 if 'user_id' not in place_object_json.keys(): return jsonify({'Error': 'Missing user_id'}), 400 user = storage.get(User, place_object_json.get("user_id")) if user is None: return jsonify({'Error': 'Not found'}), 404 if 'name' not in place_object_json.keys(): return jsonify({'Error': 'Missing name'}), 400 place_object_json["city_id"] = city_id object_place = Place(**place_object_json) object_place.save() return jsonify(object_place.to_dict()), 201
def post_place(city_id=None): """ Post a place in a city 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 'name' not in body: return make_response(jsonify({'error': 'Missing name'}), 400) city = storage.get(City, city_id) user = storage.get(User, body['user_id']) if city and user: new_place = Place(**body) new_place.city_id = city.id storage.new(new_place) storage.save() return make_response(jsonify(new_place.to_dict()), 201) return abort(404)
def create_place(id): """Creates a new place""" city_exist = storage.get(City, id) if city_exist is None: 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_exist = storage.get(User, body['user_id']) if user_exist is None: return abort(404) if 'name' not in body: return jsonify({'error': 'Missing name'}), 400 new_place = Place(**body) setattr(new_place, 'city_id', id) storage.new(new_place) storage.save() return jsonify(new_place.to_dict()), 201
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 create_place(city_id): """ creates a single place """ city = storage.get("City", city_id) if city is None: abort(404) try: r = request.get_json() except: r = None if r is None: return "Not a JSON", 400 if 'user_id' not in r.keys(): return "Missing user_id", 400 user = storage.get("User", r.get("user_id")) if user is None: abort(404) if 'name' not in r.keys(): return "Missing name", 400 r["city_id"] = city_id s = Place(**r) s.save() return jsonify(s.to_json()), 201
def create_place(city_id): """Example endpoint creates a single place Create a single place based on the 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: 201: description: A list of a dictionary of a place obj 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" }] """ city = storage.get("City", city_id) if city is None: abort(404) try: r = request.get_json() except: r = None if r is None: return "Not a JSON", 400 if 'user_id' not in r.keys(): return "Missing user_id", 400 user = storage.get("User", r.get("user_id")) if user is None: abort(404) if 'name' not in r.keys(): return "Missing name", 400 r["city_id"] = city_id s = Place(**r) s.save() return jsonify(s.to_json()), 201