def post(self):
"""????(???)"""
form = request.form
mid = form.get('movieId', '')
movie = Movie.query.get(mid)
if movie is None:
return {'message': '?????'}, 233
try:
rating = int(form.get('rating', ''))
if rating < 0 or rating > 5:
return {'message': '????'}, 233
except ValueError:
return {'message': '????'}, 233
content = form.get('content', '').strip()
if len(content) == 0:
return {'message': '????????'}, 233
comment = Comment()
comment.id = UUID()
comment.rating = rating
comment.content = content
comment.movieId = mid
comment.username = current_user.id
db.session.add(comment)
total = movie.ratingNum * movie.rating
movie.ratingNum += 1
movie.rating = (total + rating) / movie.ratingNum
db.session.commit()
return {'message': '????', 'id': comment.id}, 200
评论列表
文章目录