def handle_places(): '''Returns all the places with a GET request, or adds a new city to the database with a POST request. The parameters passed to the POST request iterate through the data and set the corresponding attributes of the instance to be inserted into the database. Will not set attribute passed as `updated_at` or `created_at`. ''' if request.method == 'GET': list = ListStyle().list(Place.select(), request) return jsonify(list), 200 elif request.method == 'POST': params = request.values place = Place() '''Check that all the required parameters are made in request.''' required = set(["owner", "city", "name"]) <= set(request.values.keys()) if required is False: return jsonify(msg="Missing parameter."), 400 for key in params: if key == 'updated_at' or key == 'created_at': continue setattr(place, key, params.get(key)) place.save() return jsonify(place.to_dict()), 200
def places(): if request.method == 'GET': list_places = Place.select() return ListStyle.list(list_places, request), 200 elif request.method == 'POST': if "name" not in request.form or "owner_id" not in request.form or "city" not in request.form: return json_response(status_=400, code=40000, msg="missing parameters") test = Place.select().where(Place.name == request.form["name"]) if test.wrapped_count() > 0: return json_response(status_=409, code=10002, msg="place already exists with this name") try: entry = Place(owner=request.form["owner_id"], name=request.form["name"], city=request.form["city"]) if request.form['description']: entry.description = str(request.form['description']) if request.form['number_rooms']: entry.number_rooms = int(request.form['number_rooms']) if request.form['number_bathrooms']: entry.number_bathrooms = int(request.form['number_bathrooms']) if request.form['max_guest']: entry.max_guest = int(request.form['max_guest']) if request.form['price_by_night']: entry.price_by_night = int(request.form['price_by_night']) if request.form['latitude']: entry.latitude = float(request.form['latitude']) if request.form['longitude']: entry.longitude = float(request.form['longitude']) entry.save() return jsonify(entry.to_dict()), 201 except IntegrityError: return json_response(status_=400, msg="you are missing a field in your post request")
def handle_place_city_id(state_id, city_id): '''With a GET request method, select the places belonging to the particular city (based on the city id) and store their hashes in an array to be returned. Will not set attribute passed as `updated_at` or `created_at`. Keyword arguments: state_id: The id of the place. city_id: The id of the city. ''' if request.method == 'GET': try: places = Place.select().where(Place.city == city_id).get() except Place.DoesNotExist: return jsonify("There is no place with this id, in this state."), 400 arr = [] for place in Place.select().where(Place.city == city_id): arr.append(place.to_dict()) return jsonify(arr), 200 elif request.method == 'POST': params = request.values place = Place() '''Check that all the required parameters are made in request.''' required = set(["owner", "name"]) <= set(request.values.keys()) if required is False: return jsonify(msg="Missing parameter."), 400 for key in params: if key == 'updated_at' or key == 'created_at': continue setattr(place, key, params.get(key)) place.city = city_id place.save() return jsonify(place.to_dict()), 200
def state_city_place(state_id, city_id): if request.method == "GET": try: state_test = State.select().where(State.id == state_id) if state_test.wrapped_count() < 1: return json_response(status_=404, code=10002, msg="state not found") city_test = City.select().where(City.id == city_id) if city_test.wrapped_count() < 1: return json_response(status_=404, code=10002, msg="state not found") query = Place.select().where(Place.city == city_id) return ListStyle.list(query, request), 200 except Place.DoesNotExist: return json_response(status_=404, code=404, msg="not found") elif request.method == "POST": if "name" not in request.form or "owner_id" not in request.form: return json_response(status_=400, code=40000, msg="missing parameters") try: city = City.get(City.id == city_id, City.state_id == state_id) except City.DoesNotExist: return json_response(status_=404, msg="City does not exist") try: place = Place(owner=request.form['owner_id'], city=city_id, name=request.form['name']) if request.form['description']: place.description = str(request.form['description']) if request.form['number_rooms']: place.number_rooms = int(request.form['number_rooms']) if request.form['number_bathrooms']: place.number_bathrooms = int(request.form['number_bathrooms']) if request.form['max_guest']: place.max_guest = int(request.form['max_guest']) if request.form['price_by_night']: place.price_by_night = int(request.form['price_by_night']) if request.form['latitude']: place.latitude = float(request.form['latitude']) if request.form['longitude']: place.longitude = float(request.form['longitude']) place.save() except IntegrityError: return json_response(status_=409, msg="Name already exists") return jsonify(place.to_dict()), 201