From 0173eaf9d2f1369099a036e447208ee308535ff2 Mon Sep 17 00:00:00 2001 From: "Dave St.Germain" Date: Thu, 15 May 2014 14:46:16 -0400 Subject: [PATCH] Updated payload for new matlab endpoint. --- .../contentstore/features/problem-editor.py | 4 +++- common/lib/capa/capa/inputtypes.py | 20 +++++-------------- common/lib/capa/capa/responsetypes.py | 15 ++++++++------ common/lib/capa/capa/tests/test_inputtypes.py | 3 ++- common/lib/xmodule/xmodule/capa_base.py | 10 +++++++++- .../xmodule/modulestore/inheritance.py | 7 ++++++- 6 files changed, 34 insertions(+), 25 deletions(-) diff --git a/cms/djangoapps/contentstore/features/problem-editor.py b/cms/djangoapps/contentstore/features/problem-editor.py index a57e5bda01..6d636e28b3 100644 --- a/cms/djangoapps/contentstore/features/problem-editor.py +++ b/cms/djangoapps/contentstore/features/problem-editor.py @@ -14,6 +14,7 @@ PROBLEM_WEIGHT = "Problem Weight" RANDOMIZATION = 'Randomization' SHOW_ANSWER = "Show Answer" TIMER_BETWEEN_ATTEMPTS = "Timer Between Attempts" +MATLAB_API_KEY = "Matlab API key" @step('I have created a Blank Common Problem$') def i_created_blank_common_problem(step): @@ -40,11 +41,12 @@ def i_see_advanced_settings_with_values(step): world.verify_all_setting_entries( [ [DISPLAY_NAME, "Blank Common Problem", True], + [MATLAB_API_KEY, "", False], [MAXIMUM_ATTEMPTS, "", False], [PROBLEM_WEIGHT, "", False], [RANDOMIZATION, "Never", False], [SHOW_ANSWER, "Finished", False], - [TIMER_BETWEEN_ATTEMPTS, "0", False] + [TIMER_BETWEEN_ATTEMPTS, "0", False], ]) diff --git a/common/lib/capa/capa/inputtypes.py b/common/lib/capa/capa/inputtypes.py index 5b906d71cc..5087763d61 100644 --- a/common/lib/capa/capa/inputtypes.py +++ b/common/lib/capa/capa/inputtypes.py @@ -804,20 +804,7 @@ class MatlabInput(CodeInput): xml = self.xml - # 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 + self.plot_payload = xml.findtext('./plot_payload') # Check if problem has been queued self.queuename = 'matlab' self.queue_msg = '' @@ -966,7 +953,10 @@ class MatlabInput(CodeInput): contents = { 'grader_payload': self.plot_payload, 'student_info': json.dumps(student_info), - 'student_response': response + 'student_response': response, + 'token': getattr(self.capa_system, 'matlab_api_key', None), + 'endpoint_version': "2", + 'requestor_id': anonymous_student_id, } (error, msg) = qinterface.send_to_queue(header=xheader, diff --git a/common/lib/capa/capa/responsetypes.py b/common/lib/capa/capa/responsetypes.py index 0adbed7eb3..963c01cfae 100644 --- a/common/lib/capa/capa/responsetypes.py +++ b/common/lib/capa/capa/responsetypes.py @@ -1878,13 +1878,16 @@ class CodeResponse(LoncapaResponse): """ grader_payload = codeparam.find('grader_payload') 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} + # matlab api key can be defined in course settings. if so, add it to the grader payload + api_key = getattr(self.capa_system, 'matlab_api_key', None) + if self.xml.find('matlabinput') and api_key: + self.payload['token'] = api_key + self.payload['endpoint_version'] = "2" + self.payload['requestor_id'] = self.capa_system.anonymous_student_id self.initial_display = find_with_default( codeparam, 'initial_display', '') diff --git a/common/lib/capa/capa/tests/test_inputtypes.py b/common/lib/capa/capa/tests/test_inputtypes.py index ebf8506281..4816b0943f 100644 --- a/common/lib/capa/capa/tests/test_inputtypes.py +++ b/common/lib/capa/capa/tests/test_inputtypes.py @@ -642,7 +642,8 @@ class MatlabTest(unittest.TestCase): 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']) + self.assertEqual('test_api_key', payload['token']) + self.assertEqual('2', payload['endpoint_version']) def test_get_html(self): # usual output diff --git a/common/lib/xmodule/xmodule/capa_base.py b/common/lib/xmodule/xmodule/capa_base.py index a2e73469e6..6a95f571ab 100644 --- a/common/lib/xmodule/xmodule/capa_base.py +++ b/common/lib/xmodule/xmodule/capa_base.py @@ -189,7 +189,15 @@ class CapaFields(object): default=False, scope=Scope.settings ) - matlab_api_key = String(help="API key for Matlab problems", scope=Scope.settings) + matlab_api_key = String( + display_name="Matlab API key", + help="Enter the API key provided by MathWorks for accessing the MATLAB Hosted Service. " + "This key is granted for exclusive use by this course for the specified duration. " + "Please do not share the API key with other courses and notify MathWorks immediately " + "if you believe the key is exposed or compromised. To obtain a key for your course, " + "or to report and issue, please contact moocsupport@mathworks.com", + scope=Scope.settings + ) class CapaMixin(CapaFields): diff --git a/common/lib/xmodule/xmodule/modulestore/inheritance.py b/common/lib/xmodule/xmodule/modulestore/inheritance.py index 0fb8a63915..459d008ebd 100644 --- a/common/lib/xmodule/xmodule/modulestore/inheritance.py +++ b/common/lib/xmodule/xmodule/modulestore/inheritance.py @@ -87,7 +87,12 @@ class InheritanceMixin(XBlockMixin): values={"min": 0}, scope=Scope.settings ) matlab_api_key = String( - help="API key for Matlab problems", + display_name="Matlab API key", + help="Enter the API key provided by MathWorks for accessing the MATLAB Hosted Service. " + "This key is granted for exclusive use by this course for the specified duration. " + "Please do not share the API key with other courses and notify MathWorks immediately " + "if you believe the key is exposed or compromised. To obtain a key for your course, " + "or to report and issue, please contact moocsupport@mathworks.com", scope=Scope.settings )