Commit 7984c4df by Vik Paruchuri

Move over grading service to xmodule

parent e8a690df
...@@ -5,16 +5,8 @@ import requests ...@@ -5,16 +5,8 @@ import requests
from requests.exceptions import RequestException, ConnectionError, HTTPError from requests.exceptions import RequestException, ConnectionError, HTTPError
import sys import sys
from django.conf import settings
from django.http import HttpResponse, Http404
from courseware.access import has_access
from util.json_request import expect_json
from xmodule.course_module import CourseDescriptor
from xmodule.combined_open_ended_rubric import CombinedOpenEndedRubric, RubricParsingError from xmodule.combined_open_ended_rubric import CombinedOpenEndedRubric, RubricParsingError
from lxml import etree from lxml import etree
from mitxmako.shortcuts import render_to_string
from xmodule.x_module import ModuleSystem
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
...@@ -31,7 +23,7 @@ class GradingService(object): ...@@ -31,7 +23,7 @@ class GradingService(object):
self.url = config['url'] self.url = config['url']
self.login_url = self.url + '/login/' self.login_url = self.url + '/login/'
self.session = requests.session() self.session = requests.session()
self.system = ModuleSystem(None, None, None, render_to_string, None) self.system = config['system']
def _login(self): def _login(self):
""" """
...@@ -42,20 +34,20 @@ class GradingService(object): ...@@ -42,20 +34,20 @@ class GradingService(object):
Returns the decoded json dict of the response. Returns the decoded json dict of the response.
""" """
response = self.session.post(self.login_url, response = self.session.post(self.login_url,
{'username': self.username, {'username': self.username,
'password': self.password,}) 'password': self.password,})
response.raise_for_status() response.raise_for_status()
return response.json return response.json
def post(self, url, data, allow_redirects=False): def post(self, url, data, allow_redirects=False):
""" """
Make a post request to the grading controller Make a post request to the grading controller
""" """
try: try:
op = lambda: self.session.post(url, data=data, op = lambda: self.session.post(url, data=data,
allow_redirects=allow_redirects) allow_redirects=allow_redirects)
r = self._try_with_login(op) r = self._try_with_login(op)
except (RequestException, ConnectionError, HTTPError) as err: except (RequestException, ConnectionError, HTTPError) as err:
# reraise as promised GradingServiceError, but preserve stacktrace. # reraise as promised GradingServiceError, but preserve stacktrace.
...@@ -69,8 +61,8 @@ class GradingService(object): ...@@ -69,8 +61,8 @@ class GradingService(object):
""" """
log.debug(params) log.debug(params)
op = lambda: self.session.get(url, op = lambda: self.session.get(url,
allow_redirects=allow_redirects, allow_redirects=allow_redirects,
params=params) params=params)
try: try:
r = self._try_with_login(op) r = self._try_with_login(op)
except (RequestException, ConnectionError, HTTPError) as err: except (RequestException, ConnectionError, HTTPError) as err:
...@@ -78,7 +70,7 @@ class GradingService(object): ...@@ -78,7 +70,7 @@ class GradingService(object):
raise GradingServiceError, str(err), sys.exc_info()[2] raise GradingServiceError, str(err), sys.exc_info()[2]
return r.text return r.text
def _try_with_login(self, operation): def _try_with_login(self, operation):
""" """
...@@ -96,8 +88,8 @@ class GradingService(object): ...@@ -96,8 +88,8 @@ class GradingService(object):
r = self._login() r = self._login()
if r and not r.get('success'): if r and not r.get('success'):
log.warning("Couldn't log into staff_grading backend. Response: %s", log.warning("Couldn't log into staff_grading backend. Response: %s",
r) r)
# try again # try again
response = operation() response = operation()
response.raise_for_status() response.raise_for_status()
...@@ -113,23 +105,23 @@ class GradingService(object): ...@@ -113,23 +105,23 @@ class GradingService(object):
""" """
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)
success, rubric_html = rubric_renderer.render_rubric(rubric) success, rubric_html = rubric_renderer.render_rubric(rubric)
response_json['rubric'] = rubric_html response_json['rubric'] = rubric_html
return response_json return response_json
# if we can't parse the rubric into HTML, # if we can't parse the rubric into HTML,
except etree.XMLSyntaxError, RubricParsingError: except etree.XMLSyntaxError, RubricParsingError:
log.exception("Cannot parse rubric string. Raw string: {0}" log.exception("Cannot parse rubric string. Raw string: {0}"
.format(rubric)) .format(rubric))
return {'success': False, return {'success': False,
'error': 'Error displaying submission'} 'error': 'Error displaying submission'}
except ValueError: except ValueError:
log.exception("Error parsing response: {0}".format(response)) log.exception("Error parsing response: {0}".format(response))
return {'success': False, return {'success': False,
'error': "Error displaying submission"} 'error': "Error displaying submission"}
\ No newline at end of file
...@@ -12,22 +12,20 @@ from django.conf import settings ...@@ -12,22 +12,20 @@ from django.conf import settings
from combined_open_ended_rubric import CombinedOpenEndedRubric, RubricParsingError from combined_open_ended_rubric import CombinedOpenEndedRubric, RubricParsingError
from lxml import etree from lxml import etree
from grading_service_module import GradingService, GradingServiceError
log=logging.getLogger(__name__) log=logging.getLogger(__name__)
class GradingServiceError(Exception): class GradingServiceError(Exception):
pass pass
class PeerGradingService(): class PeerGradingService(GradingService):
""" """
Interface with the grading controller for peer grading Interface with the grading controller for peer grading
""" """
def __init__(self, config, system): def __init__(self, config, system):
self.username = config['username'] config['system'] = system
self.password = config['password'] super(StaffGradingService, self).__init__(config)
self.url = config['url']
self.login_url = self.url + '/login/'
self.session = requests.session()
self.get_next_submission_url = self.url + '/get_next_submission/' self.get_next_submission_url = self.url + '/get_next_submission/'
self.save_grade_url = self.url + '/save_grade/' self.save_grade_url = self.url + '/save_grade/'
self.is_student_calibrated_url = self.url + '/is_student_calibrated/' self.is_student_calibrated_url = self.url + '/is_student_calibrated/'
...@@ -107,6 +105,14 @@ class PeerGradingService(): ...@@ -107,6 +105,14 @@ class PeerGradingService():
return response.json return response.json
def try_to_decode(self, text):
try:
text= json.loads(text)
except:
pass
return text
def post(self, url, data, allow_redirects=False): def post(self, url, data, allow_redirects=False):
""" """
Make a post request to the grading controller Make a post request to the grading controller
......
...@@ -3,11 +3,12 @@ import logging ...@@ -3,11 +3,12 @@ import logging
import requests import requests
from requests.exceptions import RequestException, ConnectionError, HTTPError from requests.exceptions import RequestException, ConnectionError, HTTPError
import sys import sys
from grading_service import GradingService from xmodule.grading_service_module import GradingService, GradingServiceError
from grading_service import GradingServiceError
from django.conf import settings from django.conf import settings
from django.http import HttpResponse, Http404 from django.http import HttpResponse, Http404
from xmodule.x_module import ModuleSystem
from mitxmako.shortcuts import render_to_string
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
...@@ -16,6 +17,7 @@ class ControllerQueryService(GradingService): ...@@ -16,6 +17,7 @@ class ControllerQueryService(GradingService):
Interface to staff grading backend. Interface to staff grading backend.
""" """
def __init__(self, config): def __init__(self, config):
config['system'] = ModuleSystem(None,None,None,render_to_string,None)
super(ControllerQueryService, self).__init__(config) super(ControllerQueryService, self).__init__(config)
self.check_eta_url = self.url + '/get_submission_eta/' self.check_eta_url = self.url + '/get_submission_eta/'
self.is_unique_url = self.url + '/is_name_unique/' self.is_unique_url = self.url + '/is_name_unique/'
......
...@@ -7,8 +7,7 @@ import logging ...@@ -7,8 +7,7 @@ import logging
import requests import requests
from requests.exceptions import RequestException, ConnectionError, HTTPError from requests.exceptions import RequestException, ConnectionError, HTTPError
import sys import sys
from grading_service import GradingService from xmodule.grading_service_module import GradingService, GradingServiceError
from grading_service import GradingServiceError
from django.conf import settings from django.conf import settings
from django.http import HttpResponse, Http404 from django.http import HttpResponse, Http404
...@@ -22,8 +21,6 @@ from mitxmako.shortcuts import render_to_string ...@@ -22,8 +21,6 @@ from mitxmako.shortcuts import render_to_string
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
class MockStaffGradingService(object): class MockStaffGradingService(object):
""" """
A simple mockup of a staff grading service, testing. A simple mockup of a staff grading service, testing.
...@@ -64,6 +61,7 @@ class StaffGradingService(GradingService): ...@@ -64,6 +61,7 @@ class StaffGradingService(GradingService):
Interface to staff grading backend. Interface to staff grading backend.
""" """
def __init__(self, config): def __init__(self, config):
config['system'] = ModuleSystem(None,None,None,render_to_string,None)
super(StaffGradingService, self).__init__(config) super(StaffGradingService, self).__init__(config)
self.get_next_url = self.url + '/get_next_submission/' self.get_next_url = self.url + '/get_next_submission/'
self.save_grade_url = self.url + '/save_grade/' self.save_grade_url = self.url + '/save_grade/'
......
...@@ -13,7 +13,7 @@ from student.models import unique_id_for_user ...@@ -13,7 +13,7 @@ from student.models import unique_id_for_user
from courseware.courses import get_course_with_access from courseware.courses import get_course_with_access
from controller_query_service import ControllerQueryService from controller_query_service import ControllerQueryService
from grading_service import GradingServiceError from xmodule.grading_service_module import GradingServiceError
import json import json
from .staff_grading import StaffGrading from .staff_grading import StaffGrading
from student.models import unique_id_for_user from student.models import unique_id_for_user
......
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