Commit 8eff9f7c by kimth

Upload to xqueue

parent 9d52c432
...@@ -848,12 +848,13 @@ class CodeResponse(LoncapaResponse): ...@@ -848,12 +848,13 @@ class CodeResponse(LoncapaResponse):
def get_score(self, student_answers): def get_score(self, student_answers):
try: try:
submission = student_answers[self.answer_id] submission = student_answers[self.answer_id] # Note that submission can be a file
except Exception as err: except Exception as err:
log.error('Error in CodeResponse %s: cannot get student answer for %s; student_answers=%s' % (err, self.answer_id, student_answers)) log.error('Error in CodeResponse %s: cannot get student answer for %s; student_answers=%s' %
(err, self.answer_id, convert_files_to_filenames(student_answers)))
raise Exception(err) raise Exception(err)
self.context.update({'submission': unicode(submission)}) # Submission could be a file self.context.update({'submission': unicode(submission)})
# Prepare xqueue request # Prepare xqueue request
#------------------------------------------------------------ #------------------------------------------------------------
...@@ -873,6 +874,11 @@ class CodeResponse(LoncapaResponse): ...@@ -873,6 +874,11 @@ class CodeResponse(LoncapaResponse):
'edX_student_response': unicode(submission)} 'edX_student_response': unicode(submission)}
# Submit request # Submit request
if hasattr(submission, 'read'): # Test for whether submission is a file
error = xqueue_interface.send_to_queue(header=xheader,
body=json.dumps(contents),
file_to_upload=submission)
else:
error = xqueue_interface.send_to_queue(header=xheader, error = xqueue_interface.send_to_queue(header=xheader,
body=json.dumps(contents)) body=json.dumps(contents))
......
...@@ -37,7 +37,7 @@ def make_xheader(lms_callback_url, lms_key, queue_name): ...@@ -37,7 +37,7 @@ def make_xheader(lms_callback_url, lms_key, queue_name):
'queue_name': queue_name }) 'queue_name': queue_name })
def send_to_queue(header, body, xqueue_url=None): def send_to_queue(header, body, file_to_upload=None, xqueue_url=None):
''' '''
Submit a request to xqueue. Submit a request to xqueue.
...@@ -46,6 +46,8 @@ def send_to_queue(header, body, xqueue_url=None): ...@@ -46,6 +46,8 @@ def send_to_queue(header, body, xqueue_url=None):
body: Serialized data for the receipient behind the queueing service. The operation of body: Serialized data for the receipient behind the queueing service. The operation of
xqueue is agnostic to the contents of 'body' xqueue is agnostic to the contents of 'body'
file_to_upload: File object to be uploaded to xqueue along with queue request
Returns an 'error' flag indicating error in xqueue transaction Returns an 'error' flag indicating error in xqueue transaction
''' '''
if xqueue_url is None: if xqueue_url is None:
...@@ -72,9 +74,13 @@ def send_to_queue(header, body, xqueue_url=None): ...@@ -72,9 +74,13 @@ def send_to_queue(header, body, xqueue_url=None):
#------------------------------------------------------------ #------------------------------------------------------------
payload = {'xqueue_header': header, payload = {'xqueue_header': header,
'xqueue_body' : body} 'xqueue_body' : body}
files = None
if file_to_upload is not None:
files = { file_to_upload.name: file_to_upload }
try: try:
# Send request r = s.post(xqueue_url+'/xqueue/submit/', data=payload, files=files)
r = s.post(xqueue_url+'/xqueue/submit/', data=payload)
except Exception as err: except Exception as err:
msg = 'Error in xqueue_interface.send_to_queue %s: Cannot connect to server url=%s' % (err, xqueue_url) msg = 'Error in xqueue_interface.send_to_queue %s: Cannot connect to server url=%s' % (err, xqueue_url)
raise Exception(msg) raise Exception(msg)
......
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