def get_submission_peer(request): """ Gets a submission for the Peer grading view """ try: location = util._value_or_default(request.GET['location'], None) grader_id = util._value_or_default(request.GET['grader_id'], None) except KeyError: return util._error_response("'get_submission' requires parameters 'location', 'grader_id'", _INTERFACE_VERSION) pl = peer_grading_util.PeerLocation(location, grader_id) found, sub_id = pl.next_item() if not found: return util._error_response("Nothing to grade.", _INTERFACE_VERSION) util.log_connection_data() return util._success_response({'submission_id' : sub_id}, _INTERFACE_VERSION)
def get_submission_peer(request): """ Gets a submission for the Peer grading view """ try: location = util._value_or_default(request.GET["location"], None) grader_id = util._value_or_default(request.GET["grader_id"], None) except KeyError: return util._error_response("'get_submission' requires parameters 'location', 'grader_id'", _INTERFACE_VERSION) found, sub_id = peer_grading_util.get_single_peer_grading_item(location, grader_id) if not found: return util._error_response("Nothing to grade.", _INTERFACE_VERSION) # Insert timing initialization code initialize_timing(sub_id) util.log_connection_data() return util._success_response({"submission_id": sub_id}, _INTERFACE_VERSION)
def get_submission_peer(request): """ Gets a submission for the Peer grading view """ try: location = util._value_or_default(request.GET['location'], None) grader_id = util._value_or_default(request.GET['grader_id'], None) except KeyError: return util._error_response("'get_submission' requires parameters 'location', 'grader_id'", _INTERFACE_VERSION) found, sub_id = peer_grading_util.get_single_peer_grading_item(location, grader_id) if not found: return util._error_response("Nothing to grade.", _INTERFACE_VERSION) #Insert timing initialization code initialize_timing(sub_id) util.log_connection_data() return util._success_response({'submission_id' : sub_id}, _INTERFACE_VERSION)
def get_submission_instructor(request): """ Gets a submission for the Instructor grading view """ try: course_id = util._value_or_default(request.GET['course_id'], None) except Exception: return util._error_response("'get_submission' requires parameter 'course_id'", _INTERFACE_VERSION) sc = staff_grading_util.StaffCourse(course_id) found, sub_id = sc.next_item() if not found: return util._error_response("Nothing to grade.", _INTERFACE_VERSION) util.log_connection_data() return util._success_response({'submission_id' : sub_id}, _INTERFACE_VERSION)
def get_submission_instructor(request): """ Gets a submission for the Instructor grading view """ try: course_id = util._value_or_default(request.GET["course_id"], None) except: return util._error_response("'get_submission' requires parameter 'course_id'", _INTERFACE_VERSION) found, sub_id = staff_grading_util.get_single_instructor_grading_item(course_id) if not found: return util._error_response("Nothing to grade.", _INTERFACE_VERSION) # Insert timing initialization code initialize_timing(sub_id) util.log_connection_data() return util._success_response({"submission_id": sub_id}, _INTERFACE_VERSION)
def get_submission_instructor(request): """ Gets a submission for the Instructor grading view """ try: course_id = util._value_or_default(request.GET['course_id'], None) except: return util._error_response("'get_submission' requires parameter 'course_id'", _INTERFACE_VERSION) found, sub_id = staff_grading_util.get_single_instructor_grading_item(course_id) if not found: return util._error_response("Nothing to grade.", _INTERFACE_VERSION) #Insert timing initialization code initialize_timing(sub_id) util.log_connection_data() return util._success_response({'submission_id' : sub_id}, _INTERFACE_VERSION)
def get_submission_instructor(request): """ Gets a submission for the Instructor grading view """ try: course_id = util._value_or_default(request.GET['course_id'], None) except Exception: return util._error_response( "'get_submission' requires parameter 'course_id'", _INTERFACE_VERSION) sc = staff_grading_util.StaffCourse(course_id) found, sub_id = sc.next_item() if not found: return util._error_response("Nothing to grade.", _INTERFACE_VERSION) util.log_connection_data() return util._success_response({'submission_id': sub_id}, _INTERFACE_VERSION)
def submit(request): ''' Xqueue pull script posts objects here. Input: request - dict with keys xqueue_header and xqueue_body xqueue_header needs submission_id,submission_key,queue_name xqueue_body needs grader_payload, student_info, student_response, max_score grader_payload needs location, course_id,problem_id,grader student_info needs anonymous_student_id, submission_time Output: Returns status code indicating success (0) or failure (1) and message ''' transaction.commit_unless_managed() if request.method != 'POST': return util._error_response("'submit' must use HTTP POST", _INTERFACE_VERSION) else: #Minimal parsing of reply reply_is_valid, header, body = _is_valid_reply(request.POST.copy()) if not reply_is_valid: log.error("Invalid xqueue object added: request_ip: {0} request.POST: {1}".format( util.get_request_ip(request), request.POST, )) statsd.increment("open_ended_assessment.grading_controller.controller.xqueue_interface.submit", tags=["success:Exception"]) return util._error_response('Incorrect format', _INTERFACE_VERSION) else: try: #Retrieve individual values from xqueue body and header. prompt = util._value_or_default(body['grader_payload']['prompt'], "") rubric = util._value_or_default(body['grader_payload']['rubric'], "") student_id = util._value_or_default(body['student_info']['anonymous_student_id']) location = util._value_or_default(body['grader_payload']['location']) course_id = util._value_or_default(body['grader_payload']['course_id']) problem_id = util._value_or_default(body['grader_payload']['problem_id'], location) grader_settings = util._value_or_default(body['grader_payload']['grader_settings'], "") student_response = util._value_or_default(body['student_response']) student_response = util.sanitize_html(student_response) xqueue_submission_id = util._value_or_default(header['submission_id']) xqueue_submission_key = util._value_or_default(header['submission_key']) state_code = SubmissionState.waiting_to_be_graded xqueue_queue_name = util._value_or_default(header["queue_name"]) max_score = util._value_or_default(body['max_score']) submission_time_string = util._value_or_default(body['student_info']['submission_time']) student_submission_time = datetime.strptime(submission_time_string, "%Y%m%d%H%M%S") skip_basic_checks = util._value_or_default(body['grader_payload']['skip_basic_checks'], False) if isinstance(skip_basic_checks, basestring): skip_basic_checks = (skip_basic_checks.lower() == "true") #TODO: find a better way to do this #Need to set rubric to whatever the first submission for this location had #as its rubric. If the rubric is changed in the course XML, it will break things. try: first_sub_for_location=Submission.objects.filter(location=location).order_by('date_created')[0] rubric= first_sub_for_location.rubric except: error_message="Could not find an existing submission in location. rubric is original." log.info(error_message) initial_display="" if 'initial_display' in body['grader_payload'].keys(): initial_display = util._value_or_default(body['grader_payload']['initial_display'], "") answer="" if 'answer' in body['grader_payload'].keys(): answer = util._value_or_default(body['grader_payload']['answer'], "") #Sleep for some time to allow other pull_from_xqueue processes to get behind/ahead time_sleep_value = random.uniform(0, .1) time.sleep(time_sleep_value) transaction.commit_unless_managed() #Without this, sometimes a race condition creates duplicate submissions sub_count = Submission.objects.filter( prompt=prompt, rubric=rubric, student_id=student_id, problem_id=problem_id, student_submission_time=student_submission_time, xqueue_submission_id=xqueue_submission_id, xqueue_submission_key=xqueue_submission_key, xqueue_queue_name=xqueue_queue_name, location=location, course_id=course_id, grader_settings=grader_settings, ).count() if sub_count>0: log.error("Exact submission already exists.") return util._error_response('Submission already exists.', _INTERFACE_VERSION) transaction.commit_unless_managed() #Create submission object sub, created = Submission.objects.get_or_create( prompt=prompt, rubric=rubric, student_id=student_id, problem_id=problem_id, state=state_code, student_response=student_response, student_submission_time=student_submission_time, xqueue_submission_id=xqueue_submission_id, xqueue_submission_key=xqueue_submission_key, xqueue_queue_name=xqueue_queue_name, location=location, course_id=course_id, max_score=max_score, grader_settings=grader_settings, initial_display=initial_display, answer=answer, skip_basic_checks = skip_basic_checks, ) transaction.commit_unless_managed() if created==False: log.error("Exact submission already exists.") return util._error_response('Submission already exists.', _INTERFACE_VERSION) except Exception as err: xqueue_submission_id = util._value_or_default(header['submission_id']) xqueue_submission_key = util._value_or_default(header['submission_key']) log.exception( "Error creating submission and adding to database: sender: {0}, submission_id: {1}, submission_key: {2}".format( util.get_request_ip(request), xqueue_submission_id, xqueue_submission_key, )) statsd.increment("open_ended_assessment.grading_controller.controller.xqueue_interface.submit", tags=["success:Exception"]) return util._error_response('Unable to create submission.', _INTERFACE_VERSION) #Handle submission and write to db success = handle_submission(sub) statsd.increment("open_ended_assessment.grading_controller.controller.xqueue_interface.submit", tags=[ "success:{0}".format(success), "location:{0}".format(sub.location), "course_id:{0}".format(course_id), ]) transaction.commit_unless_managed() if not success: return util._error_response("Failed to handle submission.", _INTERFACE_VERSION) util.log_connection_data() transaction.commit_unless_managed() return util._success_response({'message': "Saved successfully."}, _INTERFACE_VERSION)
def submit(request): ''' Xqueue pull script posts objects here. Input: request - dict with keys xqueue_header and xqueue_body xqueue_header needs submission_id,submission_key,queue_name xqueue_body needs grader_payload, student_info, student_response, max_score grader_payload needs location, course_id,problem_id,grader student_info needs anonymous_student_id, submission_time Output: Returns status code indicating success (0) or failure (1) and message ''' transaction.commit_unless_managed() if request.method != 'POST': return util._error_response("'submit' must use HTTP POST", _INTERFACE_VERSION) else: #Minimal parsing of reply reply_is_valid, header, body = _is_valid_reply(request.POST.copy()) if not reply_is_valid: log.error( "Invalid xqueue object added: request_ip: {0} request.POST: {1}" .format( util.get_request_ip(request), request.POST, )) statsd.increment( "open_ended_assessment.grading_controller.controller.xqueue_interface.submit", tags=["success:Exception"]) return util._error_response('Incorrect format', _INTERFACE_VERSION) else: try: #Retrieve individual values from xqueue body and header. prompt = util._value_or_default( body['grader_payload']['prompt'], "") rubric = util._value_or_default( body['grader_payload']['rubric'], "") student_id = util._value_or_default( body['student_info']['anonymous_student_id']) location = util._value_or_default( body['grader_payload']['location']) course_id = util._value_or_default( body['grader_payload']['course_id']) problem_id = util._value_or_default( body['grader_payload']['problem_id'], location) grader_settings = util._value_or_default( body['grader_payload']['grader_settings'], "") student_response = util._value_or_default( body['student_response']) student_response = util.sanitize_html(student_response) xqueue_submission_id = util._value_or_default( header['submission_id']) xqueue_submission_key = util._value_or_default( header['submission_key']) state_code = SubmissionState.waiting_to_be_graded xqueue_queue_name = util._value_or_default( header["queue_name"]) max_score = util._value_or_default(body['max_score']) submission_time_string = util._value_or_default( body['student_info']['submission_time']) student_submission_time = datetime.strptime( submission_time_string, "%Y%m%d%H%M%S") control_fields = body['grader_payload'].get('control', {}) try: control_fields = json.loads(control_fields) except Exception: pass skip_basic_checks = util._value_or_default( body['grader_payload']['skip_basic_checks'], False) if isinstance(skip_basic_checks, basestring): skip_basic_checks = (skip_basic_checks.lower() == "true") #TODO: find a better way to do this #Need to set rubric to whatever the first submission for this location had #as its rubric. If the rubric is changed in the course XML, it will break things. try: first_sub_for_location = Submission.objects.filter( location=location).order_by('date_created')[0] rubric = first_sub_for_location.rubric except Exception: error_message = "Could not find an existing submission in location. rubric is original." log.info(error_message) initial_display = "" if 'initial_display' in body['grader_payload'].keys(): initial_display = util._value_or_default( body['grader_payload']['initial_display'], "") answer = "" if 'answer' in body['grader_payload'].keys(): answer = util._value_or_default( body['grader_payload']['answer'], "") #Sleep for some time to allow other pull_from_xqueue processes to get behind/ahead time_sleep_value = random.uniform(0, .1) time.sleep(time_sleep_value) transaction.commit_unless_managed() #Without this, sometimes a race condition creates duplicate submissions sub_count = Submission.objects.filter( prompt=prompt, rubric=rubric, student_id=student_id, problem_id=problem_id, student_submission_time=student_submission_time, xqueue_submission_id=xqueue_submission_id, xqueue_submission_key=xqueue_submission_key, xqueue_queue_name=xqueue_queue_name, location=location, course_id=course_id, grader_settings=grader_settings, ).count() if sub_count > 0: return util._error_response('Submission already exists.', _INTERFACE_VERSION) transaction.commit_unless_managed() #Create submission object sub, created = Submission.objects.get_or_create( prompt=prompt, rubric=rubric, student_id=student_id, problem_id=problem_id, state=state_code, student_response=student_response, student_submission_time=student_submission_time, xqueue_submission_id=xqueue_submission_id, xqueue_submission_key=xqueue_submission_key, xqueue_queue_name=xqueue_queue_name, location=location, course_id=course_id, max_score=max_score, grader_settings=grader_settings, initial_display=initial_display, answer=answer, skip_basic_checks=skip_basic_checks, control_fields=json.dumps(control_fields)) transaction.commit_unless_managed() if created == False: return util._error_response('Submission already exists.', _INTERFACE_VERSION) except Exception as err: xqueue_submission_id = util._value_or_default( header['submission_id']) xqueue_submission_key = util._value_or_default( header['submission_key']) log.exception( "Error creating submission and adding to database: sender: {0}, submission_id: {1}, submission_key: {2}" .format( util.get_request_ip(request), xqueue_submission_id, xqueue_submission_key, )) statsd.increment( "open_ended_assessment.grading_controller.controller.xqueue_interface.submit", tags=["success:Exception"]) return util._error_response('Unable to create submission.', _INTERFACE_VERSION) #Handle submission and write to db success = handle_submission(sub) statsd.increment( "open_ended_assessment.grading_controller.controller.xqueue_interface.submit", tags=[ "success:{0}".format(success), "location:{0}".format(sub.location), "course_id:{0}".format(course_id), ]) transaction.commit_unless_managed() if not success: return util._error_response("Failed to handle submission.", _INTERFACE_VERSION) util.log_connection_data() transaction.commit_unless_managed() return util._success_response({'message': "Saved successfully."}, _INTERFACE_VERSION)