Commit 964e73f0 by Dave St.Germain

Allow courses to set Matlab API key globally, in advanced settings.

parent 54e1faea
...@@ -92,6 +92,7 @@ class LoncapaSystem(object): ...@@ -92,6 +92,7 @@ class LoncapaSystem(object):
seed, # Why do we do this if we have self.seed? seed, # Why do we do this if we have self.seed?
STATIC_URL, # pylint: disable=invalid-name STATIC_URL, # pylint: disable=invalid-name
xqueue, xqueue,
matlab_api_key=None
): ):
self.ajax_url = ajax_url self.ajax_url = ajax_url
self.anonymous_student_id = anonymous_student_id self.anonymous_student_id = anonymous_student_id
...@@ -105,6 +106,7 @@ class LoncapaSystem(object): ...@@ -105,6 +106,7 @@ class LoncapaSystem(object):
self.seed = seed # Why do we do this if we have self.seed? self.seed = seed # Why do we do this if we have self.seed?
self.STATIC_URL = STATIC_URL # pylint: disable=invalid-name self.STATIC_URL = STATIC_URL # pylint: disable=invalid-name
self.xqueue = xqueue self.xqueue = xqueue
self.matlab_api_key = matlab_api_key
class LoncapaProblem(object): class LoncapaProblem(object):
......
...@@ -783,14 +783,10 @@ class MatlabInput(CodeInput): ...@@ -783,14 +783,10 @@ class MatlabInput(CodeInput):
""" """
InputType for handling Matlab code input InputType for handling Matlab code input
TODO: API_KEY will go away once we have a way to specify it per-course
Example: Example:
<matlabinput rows="10" cols="80" tabsize="4"> <matlabinput rows="10" cols="80" tabsize="4">
Initial Text Initial Text
<plot_payload> </matlabinput>
%api_key=API_KEY
</plot_payload>
</matlabinput>
""" """
template = "matlabinput.html" template = "matlabinput.html"
tags = ['matlabinput'] tags = ['matlabinput']
...@@ -807,8 +803,21 @@ class MatlabInput(CodeInput): ...@@ -807,8 +803,21 @@ class MatlabInput(CodeInput):
self.setup_code_response_rendering() self.setup_code_response_rendering()
xml = self.xml xml = self.xml
self.plot_payload = xml.findtext('./plot_payload')
# the new way to define the api key is to set it in the course advanced settings
api_key = getattr(self.capa_system, 'matlab_api_key', None)
if api_key:
plot_payload = '%api_key={}'.format(api_key)
else:
plot_payload = ''
# the old way to define api_key is to add it to the plot_payload xml.
# are there other things that go in the payload?
xml_payload = xml.findtext('./plot_payload')
if xml_payload:
plot_payload += '\n{}'.format(xml_payload)
self.plot_payload = plot_payload
# Check if problem has been queued # Check if problem has been queued
self.queuename = 'matlab' self.queuename = 'matlab'
self.queue_msg = '' self.queue_msg = ''
......
...@@ -1876,10 +1876,14 @@ class CodeResponse(LoncapaResponse): ...@@ -1876,10 +1876,14 @@ class CodeResponse(LoncapaResponse):
self.answer (an answer to display to the student in the LMS) self.answer (an answer to display to the student in the LMS)
self.payload self.payload
""" """
# Note that CodeResponse is agnostic to the specific contents of
# grader_payload
grader_payload = codeparam.find('grader_payload') grader_payload = codeparam.find('grader_payload')
grader_payload = grader_payload.text if grader_payload is not None else '' grader_payload = grader_payload.text if grader_payload is not None else ''
# matlab api key can be defined in course settings. if so, add it to the grader payload
# only if the problem didn't have an api key already defined.
api_key = getattr(self.capa_system, 'matlab_api_key', None)
if self.xml.find('matlabinput') and api_key and 'api_key' not in grader_payload:
grader_payload += '\n%api_key={}'.format(api_key)
self.payload = {'grader_payload': grader_payload} self.payload = {'grader_payload': grader_payload}
self.initial_display = find_with_default( self.initial_display = find_with_default(
......
...@@ -628,6 +628,21 @@ class MatlabTest(unittest.TestCase): ...@@ -628,6 +628,21 @@ class MatlabTest(unittest.TestCase):
context = the_input._get_render_context() context = the_input._get_render_context()
self.assertEqual(the_input.status, 'unsubmitted') self.assertEqual(the_input.status, 'unsubmitted')
def test_matlab_api_key(self):
"""
Test that api_key ends up in the xqueue payload
"""
elt = etree.fromstring(self.xml)
system = test_capa_system()
system.matlab_api_key = 'test_api_key'
the_input = lookup_tag('matlabinput')(system, elt, {})
data = {'submission': 'x = 1234;'}
response = the_input.handle_ajax("plot", data)
body = system.xqueue['interface'].send_to_queue.call_args[1]['body']
payload = json.loads(body)
self.assertIn('%api_key=test_api_key', payload['grader_payload'])
def test_get_html(self): def test_get_html(self):
# usual output # usual output
......
...@@ -189,6 +189,7 @@ class CapaFields(object): ...@@ -189,6 +189,7 @@ class CapaFields(object):
default=False, default=False,
scope=Scope.settings scope=Scope.settings
) )
matlab_api_key = String(help="API key for Matlab problems", scope=Scope.settings)
class CapaMixin(CapaFields): class CapaMixin(CapaFields):
...@@ -292,6 +293,7 @@ class CapaMixin(CapaFields): ...@@ -292,6 +293,7 @@ class CapaMixin(CapaFields):
seed=self.runtime.seed, # Why do we do this if we have self.seed? seed=self.runtime.seed, # Why do we do this if we have self.seed?
STATIC_URL=self.runtime.STATIC_URL, STATIC_URL=self.runtime.STATIC_URL,
xqueue=self.runtime.xqueue, xqueue=self.runtime.xqueue,
matlab_api_key=self.matlab_api_key
) )
return LoncapaProblem( return LoncapaProblem(
......
...@@ -86,7 +86,10 @@ class InheritanceMixin(XBlockMixin): ...@@ -86,7 +86,10 @@ class InheritanceMixin(XBlockMixin):
"If the value is not set, infinite attempts are allowed."), "If the value is not set, infinite attempts are allowed."),
values={"min": 0}, scope=Scope.settings values={"min": 0}, scope=Scope.settings
) )
matlab_api_key = String(
help="API key for Matlab problems",
scope=Scope.settings
)
def compute_inherited_metadata(descriptor): def compute_inherited_metadata(descriptor):
......
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