def create_place(city_id): ''' creates a place linked to a city using the city id''' try: content = request.get_json() except: return (jsonify({"error": "Not a JSON"}), 400) my_city = storage.get("City", city_id) if my_city is None: abort(404) user_id = content.get("user_id") if user_id is None: return (jsonify({"error": "Missing user_id"}), 400) my_user = storage.get("User", user_id) if my_user is None: abort(404) name = content.get("name") if name is None: return (jsonify({"error": "Missing name"}), 400) new_place = Place() new_place.city_id = my_city.id for key, val in content.items(): setattr(new_place, key, val) new_place.save() return (jsonify(new_place.to_dict()), 201)
def get_places(city_id): """ Retrieves list of all Place objects """ city = storage.get("City", city_id) if city is None: abort(404) if request.method == 'GET': return jsonify([obj.to_dict() for obj in storage.all("Place").values() if obj.city_id == city_id]) if request.method == 'POST': if not request.json: abort(400, 'Not a JSON') if 'user_id' not in request.json: abort(400, 'Missing user_id') if storage.get('User', request.json['user_id']) is None: abort(404) if 'name' not in request.json: abort(400, 'Missing name') data = Place(**request.get_json()) data.city_id = city_id data.save() return make_response(jsonify(data.to_dict()), 201)