Commit edd32775 by Victor Shnayder

Tweaks to actually work with the backend.

- specify mime type
- right urls
parent d2cc8696
...@@ -8,8 +8,7 @@ import requests ...@@ -8,8 +8,7 @@ import requests
import sys import sys
from django.conf import settings from django.conf import settings
from django.http import Http404 from django.http import HttpResponse, Http404
from django.http import HttpResponse
from courseware.access import has_access from courseware.access import has_access
from util.json_request import expect_json from util.json_request import expect_json
...@@ -29,7 +28,7 @@ class MockStaffGradingService(object): ...@@ -29,7 +28,7 @@ class MockStaffGradingService(object):
def __init__(self): def __init__(self):
self.cnt = 0 self.cnt = 0
def get_next(self, course_id): def get_next(self, course_id, grader_id):
self.cnt += 1 self.cnt += 1
return json.dumps({'success': True, return json.dumps({'success': True,
'submission_id': self.cnt, 'submission_id': self.cnt,
...@@ -47,16 +46,20 @@ class StaffGradingService(object): ...@@ -47,16 +46,20 @@ class StaffGradingService(object):
""" """
def __init__(self, url): def __init__(self, url):
self.url = url self.url = url
self.get_next_url = url + '/get_next_submission/'
self.save_grade_url = url + '/save_grade/'
# TODO: add auth # TODO: add auth
self.session = requests.session() self.session = requests.session()
def get_next(self, course_id): def get_next(self, course_id, grader_id):
""" """
Get the next thing to grade. Returns json, or raises GradingServiceError Get the next thing to grade. Returns json, or raises GradingServiceError
if there's a problem. if there's a problem.
""" """
try: try:
r = self.session.get(self.url + 'get_next') r = self.session.get(self.get_next_url,
params={'course_id': course_id,
'grader_id': grader_id})
except requests.exceptions.ConnectionError as err: except requests.exceptions.ConnectionError as err:
# 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]
...@@ -78,22 +81,24 @@ class StaffGradingService(object): ...@@ -78,22 +81,24 @@ class StaffGradingService(object):
'submission_id': submission_id, 'submission_id': submission_id,
'score': score, 'score': score,
'feedback': feedback, 'feedback': feedback,
'grader_id': grader} 'grader_id': grader_id}
r = self.session.post(self.url + 'save_grade')
r = self.session.post(self.save_grade_url, data=data)
except requests.exceptions.ConnectionError as err: except requests.exceptions.ConnectionError as err:
# 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 return r.text
#_service = StaffGradingService(settings.STAFF_GRADING_BACKEND_URL) _service = StaffGradingService(settings.STAFF_GRADING_BACKEND_URL)
_service = MockStaffGradingService() #_service = MockStaffGradingService()
def _err_response(msg): def _err_response(msg):
""" """
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 HttpResponse(json.dumps({'success': False, 'error': msg}),
mimetype="application/json")
def _check_access(user, course_id): def _check_access(user, course_id):
...@@ -129,16 +134,17 @@ def get_next(request, course_id): ...@@ -129,16 +134,17 @@ def get_next(request, course_id):
""" """
_check_access(request.user, course_id) _check_access(request.user, course_id)
return HttpResponse(_get_next(course_id)) return HttpResponse(_get_next(course_id, request.user.id),
mimetype="application/json")
def _get_next(course_id): def _get_next(course_id, grader_id):
""" """
Implementation of get_next (also called from save_grade) -- returns a json string Implementation of get_next (also called from save_grade) -- returns a json string
""" """
try: try:
return _service.get_next(course_id) return _service.get_next(course_id, grader_id)
except GradingServiceError: except GradingServiceError:
log.exception("Error from grading service") log.exception("Error from grading service")
return json.dumps({'success': False, 'error': 'Could not connect to grading service'}) return json.dumps({'success': False, 'error': 'Could not connect to grading service'})
...@@ -168,11 +174,12 @@ def save_grade(request, course_id): ...@@ -168,11 +174,12 @@ def save_grade(request, course_id):
if k not in request.POST.keys(): if k not in request.POST.keys():
return _err_response('Missing required key {0}'.format(k)) return _err_response('Missing required key {0}'.format(k))
grader_id = request.user.id
p = request.POST p = request.POST
try: try:
result_json = _service.save_grade(course_id, result_json = _service.save_grade(course_id,
request.user.id, grader_id,
p['submission_id'], p['submission_id'],
p['score'], p['score'],
p['feedback']) p['feedback'])
...@@ -183,6 +190,7 @@ def save_grade(request, course_id): ...@@ -183,6 +190,7 @@ def save_grade(request, course_id):
try: try:
result = json.loads(result_json) result = json.loads(result_json)
except ValueError: except ValueError:
log.exception("save_grade returned broken json: %s", result_json)
return _err_response('Grading service returned mal-formatted data.') return _err_response('Grading service returned mal-formatted data.')
if not result.get('success', False): if not result.get('success', False):
...@@ -190,5 +198,6 @@ def save_grade(request, course_id): ...@@ -190,5 +198,6 @@ def save_grade(request, course_id):
return _err_response('Grading service failed') return _err_response('Grading service failed')
# Ok, save_grade seemed to work. Get the next submission to grade. # Ok, save_grade seemed to work. Get the next submission to grade.
return HttpResponse(_get_next(course_id)) return HttpResponse(_get_next(course_id, grader_id),
mimetype="application/json")
...@@ -102,6 +102,9 @@ SUBDOMAIN_BRANDING = { ...@@ -102,6 +102,9 @@ SUBDOMAIN_BRANDING = {
COMMENTS_SERVICE_KEY = "PUT_YOUR_API_KEY_HERE" COMMENTS_SERVICE_KEY = "PUT_YOUR_API_KEY_HERE"
################################# Staff grading config #####################
STAFF_GRADING_BACKEND_URL = "http://127.0.0.1:3033/staff_grading"
################################ LMS Migration ################################# ################################ LMS Migration #################################
......
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