Commit d1c55208 by Vik Paruchuri

Clean up response code

parent c1583dbb
...@@ -12,7 +12,6 @@ import requests ...@@ -12,7 +12,6 @@ import requests
import sys import sys
from django.conf import settings from django.conf import settings
from django.http import HttpResponse, Http404
from combined_open_ended_rubric import CombinedOpenEndedRubric from combined_open_ended_rubric import CombinedOpenEndedRubric
from lxml import etree from lxml import etree
...@@ -81,8 +80,7 @@ class PeerGradingModule(XModule): ...@@ -81,8 +80,7 @@ class PeerGradingModule(XModule):
""" """
Return a HttpResponse with a json dump with success=False, and the given error message. Return a HttpResponse with a json dump with success=False, and the given error message.
""" """
return HttpResponse(json.dumps({'success': False, 'error': msg}), return {'success': False, 'error': msg}
mimetype="application/json")
def _check_required(self, get, required): def _check_required(self, get, required):
actual = set(get.keys()) actual = set(get.keys())
...@@ -107,7 +105,7 @@ class PeerGradingModule(XModule): ...@@ -107,7 +105,7 @@ class PeerGradingModule(XModule):
Needs to be implemented by child modules. Handles AJAX events. Needs to be implemented by child modules. Handles AJAX events.
@return: @return:
""" """
log.debug(get)
handlers = { handlers = {
'get_next_submission': self.get_next_submission, 'get_next_submission': self.get_next_submission,
'show_calibration_essay': self.show_calibration_essay, 'show_calibration_essay': self.show_calibration_essay,
...@@ -123,7 +121,7 @@ class PeerGradingModule(XModule): ...@@ -123,7 +121,7 @@ class PeerGradingModule(XModule):
d = handlers[dispatch](get) d = handlers[dispatch](get)
log.debug(d) log.debug(d)
return json.dumps(d, cls=ComplexEncoder) return json.dumps(d, cls=ComplexEncoder)
def get_progress(self): def get_progress(self):
...@@ -159,13 +157,12 @@ class PeerGradingModule(XModule): ...@@ -159,13 +157,12 @@ class PeerGradingModule(XModule):
try: try:
response = self.peer_gs.get_next_submission(location, grader_id) response = self.peer_gs.get_next_submission(location, grader_id)
return HttpResponse(response, return response
mimetype="application/json")
except GradingServiceError: except GradingServiceError:
log.exception("Error getting next submission. server url: {0} location: {1}, grader_id: {2}" log.exception("Error getting next submission. server url: {0} location: {1}, grader_id: {2}"
.format(self.peer_gs.url, location, grader_id)) .format(self.peer_gs.url, location, grader_id))
return json.dumps({'success': False, return {'success': False,
'error': 'Could not connect to grading service'}) 'error': 'Could not connect to grading service'}
def save_grade(self, get): def save_grade(self, get):
""" """
...@@ -199,15 +196,17 @@ class PeerGradingModule(XModule): ...@@ -199,15 +196,17 @@ class PeerGradingModule(XModule):
try: try:
response = self.peer_gs.save_grade(location, grader_id, submission_id, response = self.peer_gs.save_grade(location, grader_id, submission_id,
score, feedback, submission_key, rubric_scores, submission_flagged) score, feedback, submission_key, rubric_scores, submission_flagged)
return HttpResponse(response, mimetype="application/json") return response
except GradingServiceError: except GradingServiceError:
log.exception("""Error saving grade. server url: {0}, location: {1}, submission_id:{2}, log.exception("""Error saving grade. server url: {0}, location: {1}, submission_id:{2},
submission_key: {3}, score: {4}""" submission_key: {3}, score: {4}"""
.format(self.peer_gs.url, .format(self.peer_gs.url,
location, submission_id, submission_key, score) location, submission_id, submission_key, score)
) )
return json.dumps({'success': False, return {
'error': 'Could not connect to grading service'}) 'success': False,
'error': 'Could not connect to grading service'
}
def is_student_calibrated(self, get): def is_student_calibrated(self, get):
""" """
...@@ -237,12 +236,14 @@ class PeerGradingModule(XModule): ...@@ -237,12 +236,14 @@ class PeerGradingModule(XModule):
try: try:
response = self.peer_gs.is_student_calibrated(location, grader_id) response = self.peer_gs.is_student_calibrated(location, grader_id)
return HttpResponse(response, mimetype="application/json") return response
except GradingServiceError: except GradingServiceError:
log.exception("Error from grading service. server url: {0}, grader_id: {0}, location: {1}" log.exception("Error from grading service. server url: {0}, grader_id: {0}, location: {1}"
.format(self.peer_gs.url, grader_id, location)) .format(self.peer_gs.url, grader_id, location))
return json.dumps({'success': False, return {
'error': 'Could not connect to grading service'}) 'success': False,
'error': 'Could not connect to grading service'
}
def show_calibration_essay(self, get): def show_calibration_essay(self, get):
""" """
...@@ -278,18 +279,18 @@ class PeerGradingModule(XModule): ...@@ -278,18 +279,18 @@ class PeerGradingModule(XModule):
location = get['location'] location = get['location']
try: try:
response = self.peer_gs.show_calibration_essay(location, grader_id) response = self.peer_gs.show_calibration_essay(location, grader_id)
return HttpResponse(response, mimetype="application/json") return response
except GradingServiceError: except GradingServiceError:
log.exception("Error from grading service. server url: {0}, location: {0}" log.exception("Error from grading service. server url: {0}, location: {0}"
.format(self.peer_gs.url, location)) .format(self.peer_gs.url, location))
return json.dumps({'success': False, return {'success': False,
'error': 'Could not connect to grading service'}) 'error': 'Could not connect to grading service'}
# if we can't parse the rubric into HTML, # if we can't parse the rubric into HTML,
except etree.XMLSyntaxError: except etree.XMLSyntaxError:
log.exception("Cannot parse rubric string. Raw string: {0}" log.exception("Cannot parse rubric string. Raw string: {0}"
.format(rubric)) .format(rubric))
return json.dumps({'success': False, return {'success': False,
'error': 'Error displaying submission'}) 'error': 'Error displaying submission'}
def save_calibration_essay(self, get): def save_calibration_essay(self, get):
...@@ -326,7 +327,7 @@ class PeerGradingModule(XModule): ...@@ -326,7 +327,7 @@ class PeerGradingModule(XModule):
try: try:
response = self.peer_gs.save_calibration_essay(location, grader_id, calibration_essay_id, response = self.peer_gs.save_calibration_essay(location, grader_id, calibration_essay_id,
submission_key, score, feedback, rubric_scores) submission_key, score, feedback, rubric_scores)
return HttpResponse(response, mimetype="application/json") return response
except GradingServiceError: except GradingServiceError:
log.exception("Error saving calibration grade, location: {0}, submission_id: {1}, submission_key: {2}, grader_id: {3}".format(location, submission_id, submission_key, grader_id)) log.exception("Error saving calibration grade, location: {0}, submission_id: {1}, submission_key: {2}, grader_id: {3}".format(location, submission_id, submission_key, grader_id))
return _err_response('Could not connect to grading service') return _err_response('Could not connect to grading service')
......
...@@ -36,7 +36,7 @@ class PeerGradingService(): ...@@ -36,7 +36,7 @@ class PeerGradingService():
def get_next_submission(self, problem_location, grader_id): def get_next_submission(self, problem_location, grader_id):
response = self.get(self.get_next_submission_url, response = self.get(self.get_next_submission_url,
{'location': problem_location, 'grader_id': grader_id}) {'location': problem_location, 'grader_id': grader_id})
return json.dumps(self._render_rubric(response)) return self._render_rubric(response)
def save_grade(self, location, grader_id, submission_id, score, feedback, submission_key, rubric_scores, submission_flagged): def save_grade(self, location, grader_id, submission_id, score, feedback, submission_key, rubric_scores, submission_flagged):
data = {'grader_id' : grader_id, data = {'grader_id' : grader_id,
...@@ -58,7 +58,7 @@ class PeerGradingService(): ...@@ -58,7 +58,7 @@ class PeerGradingService():
def show_calibration_essay(self, problem_location, grader_id): def show_calibration_essay(self, problem_location, grader_id):
params = {'problem_id' : problem_location, 'student_id': grader_id} params = {'problem_id' : problem_location, 'student_id': grader_id}
response = self.get(self.show_calibration_essay_url, params) response = self.get(self.show_calibration_essay_url, params)
return json.dumps(self._render_rubric(response)) return self._render_rubric(response)
def save_calibration_essay(self, problem_location, grader_id, calibration_essay_id, submission_key, def save_calibration_essay(self, problem_location, grader_id, calibration_essay_id, submission_key,
score, feedback, rubric_scores): score, feedback, rubric_scores):
...@@ -111,7 +111,13 @@ class PeerGradingService(): ...@@ -111,7 +111,13 @@ class PeerGradingService():
# reraise as promised GradingServiceError, but preserve stacktrace. # reraise as promised GradingServiceError, but preserve stacktrace.
raise GradingServiceError, str(err), sys.exc_info()[2] raise GradingServiceError, str(err), sys.exc_info()[2]
return r.text text = r.text
try:
text= json.loads(text)
except:
pass
return text
def get(self, url, params, allow_redirects=False): def get(self, url, params, allow_redirects=False):
""" """
...@@ -127,7 +133,13 @@ class PeerGradingService(): ...@@ -127,7 +133,13 @@ class PeerGradingService():
# reraise as promised GradingServiceError, but preserve stacktrace. # reraise as promised GradingServiceError, but preserve stacktrace.
raise GradingServiceError, str(err), sys.exc_info()[2] raise GradingServiceError, str(err), sys.exc_info()[2]
return r.text text = r.text
try:
text= json.loads(text)
except:
pass
return text
def _try_with_login(self, operation): def _try_with_login(self, operation):
...@@ -163,6 +175,10 @@ class PeerGradingService(): ...@@ -163,6 +175,10 @@ class PeerGradingService():
""" """
try: try:
response_json = json.loads(response) response_json = json.loads(response)
except:
response_json = response
try:
if 'rubric' in response_json: if 'rubric' in response_json:
rubric = response_json['rubric'] rubric = response_json['rubric']
rubric_renderer = CombinedOpenEndedRubric(self.system, False) rubric_renderer = CombinedOpenEndedRubric(self.system, False)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment