From 1aa3580dcbff1647ba611e13ebc0775cc5ec79c9 Mon Sep 17 00:00:00 2001 From: Diana Huang Date: Tue, 5 Mar 2013 17:04:23 -0500 Subject: [PATCH 1/2] Switch over to using the inputtype ajax handler for chemical input. --- common/lib/capa/capa/inputtypes.py | 41 +++++++++++++++++++ .../capa/templates/chemicalequationinput.html | 2 +- .../js/capa/chemical_equation_preview.js | 7 +++- lms/djangoapps/courseware/module_render.py | 37 ----------------- lms/urls.py | 8 ---- 5 files changed, 47 insertions(+), 48 deletions(-) diff --git a/common/lib/capa/capa/inputtypes.py b/common/lib/capa/capa/inputtypes.py index 1d6c340f37..8f6f0b8c09 100644 --- a/common/lib/capa/capa/inputtypes.py +++ b/common/lib/capa/capa/inputtypes.py @@ -45,8 +45,10 @@ import re import shlex # for splitting quoted strings import sys import os +import pyparsing from registry import TagRegistry +from capa.chem import chemcalc log = logging.getLogger('mitx.' + __name__) @@ -752,6 +754,45 @@ class ChemicalEquationInput(InputTypeBase): """ return {'previewer': '/static/js/capa/chemical_equation_preview.js', } + def handle_ajax(self, dispatch, get): + ''' + Since we only have one ajax handler for this input, check to see if it + matches the corresponding dispatch and send it through if it does + ''' + if dispatch == 'preview_chemcalc': + return self.preview_chemcalc(get) + return {} + + def preview_chemcalc(self, get): + """ + Render an html preview of a chemical formula or equation. get should + contain a key 'formula' and value 'some formula string'. + + Returns a json dictionary: + { + 'preview' : 'the-preview-html' or '' + 'error' : 'the-error' or '' + } + """ + + result = {'preview': '', + 'error': ''} + formula = get['formula'] + if formula is None: + result['error'] = "No formula specified." + return result + + try: + result['preview'] = chemcalc.render_to_html(formula) + except pyparsing.ParseException as p: + result['error'] = "Couldn't parse formula: {0}".format(p) + except Exception: + # this is unexpected, so log + log.warning("Error while previewing chemical formula", exc_info=True) + result['error'] = "Error while rendering preview" + + return result + registry.register(ChemicalEquationInput) #----------------------------------------------------------------------------- diff --git a/common/lib/capa/capa/templates/chemicalequationinput.html b/common/lib/capa/capa/templates/chemicalequationinput.html index dd177dc920..17c84114e5 100644 --- a/common/lib/capa/capa/templates/chemicalequationinput.html +++ b/common/lib/capa/capa/templates/chemicalequationinput.html @@ -11,7 +11,7 @@
% endif - Date: Wed, 6 Mar 2013 14:53:13 -0500 Subject: [PATCH 2/2] Update tests and documentation. --- common/lib/capa/capa/inputtypes.py | 2 +- common/lib/capa/capa/tests/test_inputtypes.py | 30 ++++++++++++++----- .../js/capa/chemical_equation_preview.js | 4 ++- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/common/lib/capa/capa/inputtypes.py b/common/lib/capa/capa/inputtypes.py index 8f6f0b8c09..70770c63ff 100644 --- a/common/lib/capa/capa/inputtypes.py +++ b/common/lib/capa/capa/inputtypes.py @@ -756,7 +756,7 @@ class ChemicalEquationInput(InputTypeBase): def handle_ajax(self, dispatch, get): ''' - Since we only have one ajax handler for this input, check to see if it + Since we only have chemcalc preview this input, check to see if it matches the corresponding dispatch and send it through if it does ''' if dispatch == 'preview_chemcalc': diff --git a/common/lib/capa/capa/tests/test_inputtypes.py b/common/lib/capa/capa/tests/test_inputtypes.py index 4a5ea5c429..54736cfd3c 100644 --- a/common/lib/capa/capa/tests/test_inputtypes.py +++ b/common/lib/capa/capa/tests/test_inputtypes.py @@ -482,27 +482,43 @@ class ChemicalEquationTest(unittest.TestCase): ''' Check that chemical equation inputs work. ''' - - def test_rendering(self): - size = "42" - xml_str = """""".format(size=size) + def setUp(self): + self.size = "42" + xml_str = """""".format(size=self.size) element = etree.fromstring(xml_str) state = {'value': 'H2OYeah', } - the_input = lookup_tag('chemicalequationinput')(test_system, element, state) + self.the_input = lookup_tag('chemicalequationinput')(test_system, element, state) - context = the_input._get_render_context() + + def test_rendering(self): + ''' Verify that the render context matches the expected render context''' + context = self.the_input._get_render_context() expected = {'id': 'prob_1_2', 'value': 'H2OYeah', 'status': 'unanswered', 'msg': '', - 'size': size, + 'size': self.size, 'previewer': '/static/js/capa/chemical_equation_preview.js', } self.assertEqual(context, expected) + + def test_chemcalc_ajax_sucess(self): + ''' Verify that using the correct dispatch and valid data produces a valid response''' + + data = {'formula': "H"} + response = self.the_input.handle_ajax("preview_chemcalc", data) + + self.assertTrue('preview' in response) + self.assertNotEqual(response['preview'], '') + self.assertEqual(response['error'], "") + + + + class DragAndDropTest(unittest.TestCase): ''' diff --git a/common/static/js/capa/chemical_equation_preview.js b/common/static/js/capa/chemical_equation_preview.js index 73c06972b1..10a6b54655 100644 --- a/common/static/js/capa/chemical_equation_preview.js +++ b/common/static/js/capa/chemical_equation_preview.js @@ -13,7 +13,9 @@ prev_id = "#" + this.id + "_preview"; preview_div = $(prev_id); - url = $(this).parents('.problems-wrapper').data('url'); + // find the closest parent problems-wrapper and use that url + url = $(this).closest('.problems-wrapper').data('url'); + // grab the input id from the input input_id = $(this).data('input-id') Problem.inputAjax(url, input_id, 'preview_chemcalc', {"formula" : this.value}, create_handler(preview_div));