def handle_reviews(user_id):
'''Returns the reviews of the user passed the argument `user_id`. This is
accomplished through querying the ReviewUser table, and returning rows in
which `user_id` is equal to the id of a user. Then, using the
ForeignKeyField, retrieve those particular reviews with the to_dict()
method. Will not add attributes `updated_at` or `created_at`. Note: this
route is for reviewing the user with id `user_id`. The user id passed as
a parameter with a POST request is the user making the review.
Keyword arguments:
user_id = The id of the user that is being reviewed.
'''
try:
User.select().where(User.id == user_id).get()
except User.DoesNotExist:
return jsonify(msg="There is no user with this id."), 404
if request.method == 'GET':
arr = []
for review_user in (ReviewUser
.select()
.where(ReviewUser.user == user_id)):
arr.append(review_user.review.to_dict())
return jsonify(arr), 200
elif request.method == 'POST':
params = request.values
review = Review()
'''Check that all the required parameters are made in request.'''
required = set(["message", "user"]) <= 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(review, key, params.get(key))
if review.message is None or review.user_id is None:
return jsonify(msg="Missing required data."), 404
review.save()
'''Save the connection in the ReviewUser table.'''
ReviewUser().create(user=user_id, review=review.id)
return jsonify(review.to_dict()), 201
review.py 文件源码
python
阅读 21
收藏 0
点赞 0
评论 0
评论列表
文章目录