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
评论列表
文章目录