Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
May 30, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cms/djangoapps/contentstore/features/problem-editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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):
Expand All @@ -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],
])


Expand Down
2 changes: 2 additions & 0 deletions common/lib/capa/capa/capa_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def __init__( # pylint: disable=invalid-na
seed, # Why do we do this if we have self.seed?
STATIC_URL, # pylint: disable=invalid-name
xqueue,
matlab_api_key=None
):
self.ajax_url = ajax_url
self.anonymous_student_id = anonymous_student_id
Expand All @@ -105,6 +106,7 @@ def __init__( # pylint: disable=invalid-na
self.seed = seed # Why do we do this if we have self.seed?
self.STATIC_URL = STATIC_URL # pylint: disable=invalid-name
self.xqueue = xqueue
self.matlab_api_key = matlab_api_key


class LoncapaProblem(object):
Expand Down
13 changes: 6 additions & 7 deletions common/lib/capa/capa/inputtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,14 +783,10 @@ class MatlabInput(CodeInput):
"""
InputType for handling Matlab code input

TODO: API_KEY will go away once we have a way to specify it per-course
Example:
<matlabinput rows="10" cols="80" tabsize="4">
Initial Text
<plot_payload>
%api_key=API_KEY
</plot_payload>
</matlabinput>
</matlabinput>
"""
template = "matlabinput.html"
tags = ['matlabinput']
Expand All @@ -807,8 +803,8 @@ def setup(self):
self.setup_code_response_rendering()

xml = self.xml
self.plot_payload = xml.findtext('./plot_payload')

self.plot_payload = xml.findtext('./plot_payload')
# Check if problem has been queued
self.queuename = 'matlab'
self.queue_msg = ''
Expand Down Expand Up @@ -957,7 +953,10 @@ def _plot_data(self, data):
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,
Expand Down
13 changes: 10 additions & 3 deletions common/lib/capa/capa/responsetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1876,11 +1876,18 @@ def _parse_coderesponse_xml(self, codeparam):
self.answer (an answer to display to the student in the LMS)
self.payload
"""
# Note that CodeResponse is agnostic to the specific contents of
# grader_payload
grader_payload = codeparam.find('grader_payload')
grader_payload = grader_payload.text if grader_payload is not None else ''
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', '')
Expand Down
16 changes: 16 additions & 0 deletions common/lib/capa/capa/tests/test_inputtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,22 @@ def test_matlab_response_migration_of_queuetime(self, time):
context = the_input._get_render_context()
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.assertEqual('test_api_key', payload['token'])
self.assertEqual('2', payload['endpoint_version'])

def test_get_html(self):
# usual output
Expand Down
10 changes: 10 additions & 0 deletions common/lib/xmodule/xmodule/capa_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,15 @@ class CapaFields(object):
default=False,
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):
Expand Down Expand Up @@ -292,6 +301,7 @@ def new_lcp(self, state, text=None):
seed=self.runtime.seed, # Why do we do this if we have self.seed?
STATIC_URL=self.runtime.STATIC_URL,
xqueue=self.runtime.xqueue,
matlab_api_key=self.matlab_api_key
)

return LoncapaProblem(
Expand Down
10 changes: 9 additions & 1 deletion common/lib/xmodule/xmodule/modulestore/inheritance.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,15 @@ class InheritanceMixin(XBlockMixin):
"If the value is not set, infinite attempts are allowed."),
values={"min": 0}, 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
)


def compute_inherited_metadata(descriptor):
Expand Down