def places(): """Handle GET and POST requests to /places route. Return a list of all places in the database in the case of a GET request. Create a new place record in the database in the case of a POST request. """ # handle GET requests: # -------------------------------------------------------------------------- if request.method == 'GET': list = [] for record in Place.select(): hash = record.to_hash() list.append(hash) return jsonify(list) # handle POST requests: # -------------------------------------------------------------------------- elif request.method == 'POST': record = Place(owner=request.form['owner_id'], city=request.form['city_id'], name=request.form['name'], description=request.form['description'], number_rooms=request.form['number_rooms'], number_bathrooms=request.form['number_bathrooms'], max_guest=request.form['max_guest'], price_by_night=request.form['price_by_night'], latitude=request.form['latitude'], longitude=request.form['longitude']) record.save() return jsonify(record.to_hash())
def places(): """Handle GET and POST requests to /places route. Return a list of all places in the database in the case of a GET request. Create a new place record in the database in the case of a POST request. """ # handle GET requests: # -------------------------------------------------------------------------- if request.method == 'GET': list = ListStyle.list(Place.select(), request) return jsonify(list) # handle POST requests: # -------------------------------------------------------------------------- elif request.method == 'POST': record = Place( owner=request.form['owner_id'], city=request.form['city_id'], name=request.form['name'], description=request.form['description'], number_rooms=request.form['number_rooms'], number_bathrooms=request.form['number_bathrooms'], max_guest=request.form['max_guest'], price_by_night=request.form['price_by_night'], latitude=request.form['latitude'], longitude=request.form['longitude'] ) record.save() return jsonify(record.to_hash())
def city_places(state_id, city_id): """Handle GET & POST requests to /states/<state_id>/cities/<city_id>/places. Return a list of all places in the database in given city in the case of a GET request. Create a new place record in the given city in the database in the case of a POST request. """ # handle GET requests: # -------------------------------------------------------------------------- if request.method == 'GET': try: list = [] for record in Place.select().where(Place.city == city_id): hash = record.to_hash() list.append(hash) return jsonify(list) # return 404 not found record does not exist except Place.DoesNotExist: return json_response(add_status_=False, status_=404, code=404, msg="not found") # handle POST requests: # -------------------------------------------------------------------------- elif request.method == 'POST': record = Place(owner=request.form['owner_id'], city=city_id, name=request.form['name'], description=request.form['description'], number_rooms=request.form['number_rooms'], number_bathrooms=request.form['number_bathrooms'], max_guest=request.form['max_guest'], price_by_night=request.form['price_by_night'], latitude=request.form['latitude'], longitude=request.form['longitude']) record.save() return jsonify(record.to_hash())
def city_places(state_id, city_id): """Handle GET & POST requests to /states/<state_id>/cities/<city_id>/places. Return a list of all places in the database in given city in the case of a GET request. Create a new place record in the given city in the database in the case of a POST request. """ # handle GET requests: # -------------------------------------------------------------------------- if request.method == 'GET': try: list = ListStyle.list(Place.select().where(Place.city == city_id), request) return jsonify(list) # return 404 not found record does not exist except Place.DoesNotExist: return json_response( add_status_=False, status_=404, code=404, msg="not found" ) # handle POST requests: # -------------------------------------------------------------------------- elif request.method == 'POST': record = Place( owner=request.form['owner_id'], city=city_id, name=request.form['name'], description=request.form['description'], number_rooms=request.form['number_rooms'], number_bathrooms=request.form['number_bathrooms'], max_guest=request.form['max_guest'], price_by_night=request.form['price_by_night'], latitude=request.form['latitude'], longitude=request.form['longitude'] ) record.save() return jsonify(record.to_hash())