From 0513e144b1be90a49e45cc7c8b7cd30da8dedbb0 Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Fri, 28 Mar 2014 15:26:19 -0400 Subject: [PATCH] Actually push json parsing all the way down to the bottom of the GradingService get and post methods --- .../controller_query_service.py | 15 ++++-------- .../grading_service_module.py | 18 +++++++-------- .../peer_grading_service.py | 23 ++++++++----------- .../staff_grading_service.py | 20 +++++++++------- 4 files changed, 35 insertions(+), 41 deletions(-) diff --git a/common/lib/xmodule/xmodule/open_ended_grading_classes/controller_query_service.py b/common/lib/xmodule/xmodule/open_ended_grading_classes/controller_query_service.py index c5ecc05c28..0577a9034b 100644 --- a/common/lib/xmodule/xmodule/open_ended_grading_classes/controller_query_service.py +++ b/common/lib/xmodule/xmodule/open_ended_grading_classes/controller_query_service.py @@ -28,8 +28,7 @@ class ControllerQueryService(GradingService): params = { 'location': location, } - response = self.get(self.check_eta_url, params) - data = response.json() + data = self.get(self.check_eta_url, params) self._record_result('check_for_eta', data) dog_stats_api.histogram(self._metric_name('check_for_eta.eta'), data.get('eta', 0)) @@ -43,8 +42,7 @@ class ControllerQueryService(GradingService): 'last_time_viewed': last_time_viewed, } log.debug(self.combined_notifications_url) - response = self.get(self.combined_notifications_url, params) - data = response.json() + data = self.get(self.combined_notifications_url, params) tags = [u'course_id:{}'.format(course_id), u'user_is_staff:{}'.format(user_is_staff)] tags.extend( @@ -61,8 +59,7 @@ class ControllerQueryService(GradingService): 'course_id': course_id, } - response = self.get(self.grading_status_list_url, params) - data = response.json() + data = self.get(self.grading_status_list_url, params) tags = [u'course_id:{}'.format(course_id)] self._record_result('get_grading_status_list', data, tags) @@ -78,8 +75,7 @@ class ControllerQueryService(GradingService): 'course_id': course_id, } - response = self.get(self.flagged_problem_list_url, params) - data = response.json() + data = self.get(self.flagged_problem_list_url, params) tags = [u'course_id:{}'.format(course_id)] self._record_result('get_flagged_problem_list', data, tags) @@ -97,8 +93,7 @@ class ControllerQueryService(GradingService): 'action_type': action_type } - response = self.post(self.take_action_on_flags_url, params) - data = response.json() + data = self.post(self.take_action_on_flags_url, params) tags = [u'course_id:{}'.format(course_id), u'action_type:{}'.format(action_type)] self._record_result('take_action_on_flags', data, tags) diff --git a/common/lib/xmodule/xmodule/open_ended_grading_classes/grading_service_module.py b/common/lib/xmodule/xmodule/open_ended_grading_classes/grading_service_module.py index c6915ded21..e6c947da53 100644 --- a/common/lib/xmodule/xmodule/open_ended_grading_classes/grading_service_module.py +++ b/common/lib/xmodule/xmodule/open_ended_grading_classes/grading_service_module.py @@ -73,38 +73,38 @@ class GradingService(object): def post(self, url, data, allow_redirects=False): """ - Make a post request to the grading controller + Make a post request to the grading controller. Returns the parsed json results of that request. """ try: op = lambda: self.session.post(url, data=data, allow_redirects=allow_redirects) - r = self._try_with_login(op) - except (RequestException, ConnectionError, HTTPError) as err: + response_json = self._try_with_login(op) + except (RequestException, ConnectionError, HTTPError, ValueError) as err: # reraise as promised GradingServiceError, but preserve stacktrace. #This is a dev_facing_error error_string = "Problem posting data to the grading controller. URL: {0}, data: {1}".format(url, data) log.error(error_string) raise GradingServiceError(error_string) - return r.text + return response_json def get(self, url, params, allow_redirects=False): """ - Make a get request to the grading controller + Make a get request to the grading controller. Returns the parsed json results of that request. """ op = lambda: self.session.get(url, allow_redirects=allow_redirects, params=params) try: - r = self._try_with_login(op) - except (RequestException, ConnectionError, HTTPError) as err: + response_json = self._try_with_login(op) + except (RequestException, ConnectionError, HTTPError, ValueError) as err: # reraise as promised GradingServiceError, but preserve stacktrace. #This is a dev_facing_error error_string = "Problem getting data from the grading controller. URL: {0}, params: {1}".format(url, params) log.error(error_string) raise GradingServiceError(error_string) - return r.text + return response_json def _try_with_login(self, operation): """ @@ -128,7 +128,7 @@ class GradingService(object): response = operation() response.raise_for_status() - return response + return resp_json def _render_rubric(self, response, view_only=False): """ diff --git a/common/lib/xmodule/xmodule/open_ended_grading_classes/peer_grading_service.py b/common/lib/xmodule/xmodule/open_ended_grading_classes/peer_grading_service.py index 630c509795..875cc5f680 100644 --- a/common/lib/xmodule/xmodule/open_ended_grading_classes/peer_grading_service.py +++ b/common/lib/xmodule/xmodule/open_ended_grading_classes/peer_grading_service.py @@ -31,8 +31,7 @@ class PeerGradingService(GradingService): def get_data_for_location(self, problem_location, student_id): params = {'location': problem_location, 'student_id': student_id} - response = self.get(self.get_data_for_location_url, params) - result = response.json() + result = self.get(self.get_data_for_location_url, params) self._record_result('get_data_for_location', result) for key in result.keys(): if key in ('success', 'error', 'version'): @@ -45,27 +44,26 @@ class PeerGradingService(GradingService): return result def get_next_submission(self, problem_location, grader_id): - response = self.get( + result = self._render_rubric(self.get( self.get_next_submission_url, { 'location': problem_location, 'grader_id': grader_id } - ) - result = self._render_rubric(response.json()) + )) self._record_result('get_next_submission', result) return result def save_grade(self, **kwargs): data = kwargs data.update({'rubric_scores_complete': True}) - result = self.post(self.save_grade_url, data).json() + result = self.post(self.save_grade_url, data) self._record_result('save_grade', result) return result def is_student_calibrated(self, problem_location, grader_id): params = {'problem_id': problem_location, 'student_id': grader_id} - result = self.get(self.is_student_calibrated_url, params).json() + result = self.get(self.is_student_calibrated_url, params) self._record_result( 'is_student_calibrated', result, @@ -75,22 +73,20 @@ class PeerGradingService(GradingService): def show_calibration_essay(self, problem_location, grader_id): params = {'problem_id': problem_location, 'student_id': grader_id} - response = self.get(self.show_calibration_essay_url, params) - result = self._render_rubric(response.json()) + result = self._render_rubric(self.get(self.show_calibration_essay_url, params)) self._record_result('show_calibration_essay', result) return result def save_calibration_essay(self, **kwargs): data = kwargs data.update({'rubric_scores_complete': True}) - result = self.post(self.save_calibration_essay_url, data).json() + result = self.post(self.save_calibration_essay_url, data) self._record_result('show_calibration_essay', result) return result def get_problem_list(self, course_id, grader_id): params = {'course_id': course_id, 'student_id': grader_id} - response = self.get(self.get_problem_list_url, params) - result = response.json() + result = self.get(self.get_problem_list_url, params) self._record_result('get_problem_list', result) dog_stats_api.histogram( self._metric_name('get_problem_list.result.length'), @@ -100,8 +96,7 @@ class PeerGradingService(GradingService): def get_notifications(self, course_id, grader_id): params = {'course_id': course_id, 'student_id': grader_id} - response = self.get(self.get_notifications_url, params) - result = response.json() + result = self.get(self.get_notifications_url, params) self._record_result( 'get_notifications', result, diff --git a/lms/djangoapps/open_ended_grading/staff_grading_service.py b/lms/djangoapps/open_ended_grading/staff_grading_service.py index e2057922c0..843d112b65 100644 --- a/lms/djangoapps/open_ended_grading/staff_grading_service.py +++ b/lms/djangoapps/open_ended_grading/staff_grading_service.py @@ -116,7 +116,7 @@ class StaffGradingService(GradingService): GradingServiceError: something went wrong with the connection. """ params = {'course_id': course_id, 'grader_id': grader_id} - result = self.get(self.get_problem_list_url, params).json() + result = self.get(self.get_problem_list_url, params) tags = [u'course_id:{}'.format(course_id)] self._record_result('get_problem_list', result, tags) dog_stats_api.histogram( @@ -143,10 +143,15 @@ class StaffGradingService(GradingService): Raises: GradingServiceError: something went wrong with the connection. """ - response = self.get(self.get_next_url, - params={'location': location, - 'grader_id': grader_id}) - result = self._render_rubric(response.json()) + result = self._render_rubric( + self.get( + self.get_next_url, + params={ + 'location': location, + 'grader_id': grader_id + } + ) + ) tags = [u'course_id:{}'.format(course_id)] self._record_result('get_next', result, tags) return result @@ -174,15 +179,14 @@ class StaffGradingService(GradingService): 'rubric_scores_complete': True, 'submission_flagged': submission_flagged} - response = self.post(self.save_grade_url, data=data) - result = self._render_rubric(response.json()) + result = self._render_rubric(self.post(self.save_grade_url, data=data)) tags = [u'course_id:{}'.format(course_id)] self._record_result('save_grade', result, tags) return result def get_notifications(self, course_id): params = {'course_id': course_id} - result = self.get(self.get_notifications_url, params).json() + result = self.get(self.get_notifications_url, params) tags = [ u'course_id:{}'.format(course_id), u'staff_needs_to_grade:{}'.format(result.get('staff_needs_to_grade'))