def api_ratings (): """ Returns the list of ratings for a course. In non-admin mode, will only return the active ratings. Mandatory URL parameters: - course_id """ session = core.Session() try: course_id = flask.request.args.get('course_id') # Load the course (to check that it exists) and the ratings course = core.get_course(session, course_id) ratings = core.get_ratings_for_course(session, course_id, not admin_mode()) # Return as JSON return json_responses.SuccessJsonResponse( { 'ratings': map(core.serialize_rating, ratings) } ) except core.exceptions.CourseNotFound: flask.abort(412)
def api_post_rating (): """ Posts a rating on a course. Mandatory POST parameters: - course_id - stars (integer between 0 and 5) - remark (text) - student_email without @student.ecp.fr suffix """ session = core.Session() courses = core.get_all_courses(session, True) # Validate POST data try: data = { 'course_id': flask.request.form['course_id'], 'stars': flask.request.form['stars'], 'remark': flask.request.form['remark'], 'student_email': flask.request.form['student_email'] + core.const.EMAIL_SUFFIX } # TODO validation (throwing HTTP 400 for errors) except Exception: return json_responses.BadRequestJsonResponse('validation_failed') # Check that the course exists, else throw HTTP 412 try: course = core.get_course(session, data['course_id']) except core.exceptions.CourseNotFound: return json_responses.PreconditionFailedJsonResponse( 'course_not_found', 'No course was found with the given course_id.', {'course_id': data['course_id']} ) # Initialize and persist the rating # Throws HTTP 412 if this rating conflicts with another from the same student for this course try: rating = core.CourseRating( course=course, stars=data['stars'], remark=data['remark'], student_email=data['student_email'] ) core.save_course_rating(session, rating) except core.exceptions.ConcurrentRatings: return json_responses.PreconditionFailedJsonResponse( 'concurrent_ratings', 'Another rating for this course with the same student_email already exists.' ) return json_responses.SuccessJsonResponse()