def create_rating(): body = request.get_json() if 'username' not in body: abort(422) course_id = body.get('course_id') username = body.get('username') comment = body.get('comment') difficulty_level = body.get('difficulty_level') content_density = body.get('content_density') content_update = body.get('content_update') satisfaction = body.get('satisfaction') rating = Rating.query.filter(Rating.user_id == username, Rating.course_id == course_id).first() if rating is not None: abort(422) course = Course.query.get(course_id) user = User.query.get(username) if course is None or user is None: abort(404) total_rate = (difficulty_level + content_density + content_update + satisfaction) / 4 rating = Rating(user_id=username, course_id=course_id, comment=comment, difficulty_level=difficulty_level, content_density=content_density, content_update=content_update, satisfaction=satisfaction, total_rate=total_rate) rating.insert() update_rating(course) notify(course, username) return jsonify({'ratings': [rating.format() for rating in course.ratings]})