def set_activity_content(self, lesson, activity_content, errors=None): """Updates the content of an activity.""" if errors is None: errors = [] path = self._app_context.fs.impl.physical_to_logical( self.get_activity_filename(lesson.unit_id, lesson.lesson_id)) root_name = 'activity' try: content, noverify_text = verify.convert_javascript_to_python( activity_content, root_name) activity = verify.evaluate_python_expression_from_text( content, root_name, verify.Activity().scope, noverify_text) except Exception: # pylint: disable-msg=broad-except errors.append('Unable to parse %s:\n%s' % ( root_name, str(sys.exc_info()[1]))) return verifier = verify.Verifier() try: verifier.verify_activity_instance(activity, path) except verify.SchemaException: errors.append('Error validating %s\n' % root_name) return fs = self.app_context.fs fs.put( path, vfs.string_to_stream(activity_content), is_draft=not lesson.now_available)
def verify_activity(activity_text): """Parses and semantically verifies activity.""" activity = ActivityParser13.parse_string_in_scope( activity_text, verify.Activity().scope, 'activity') assert activity verifier = verify.Verifier() verifier.verify_activity_instance(activity, 'test')
def test_activity_ast(): """Test a mix of various activities using legacy and new parser.""" activity_text = ("""activity = [ '<p>This is just some <i>HTML</i> text!</p>', { questionType: 'multiple choice', questionHTML: '<p>What letter am I thinking about now?</p>', choices: [ ['A', false, '"A" is wrong, try again.'], ['B', true, '"B" is correct!'], ['C', false, '"C" is wrong, try again.'], ['D', false, '"D" is wrong, try again.'] ] }, { questionType: 'freetext', questionHTML: '<p>What color is the snow?</p>', correctAnswerRegex: regex("/white/i"), correctAnswerOutput: 'Correct!', incorrectAnswerOutput: 'Try again.', showAnswerOutput: 'Our search expert says: white!' }, { questionType: 'multiple choice group', questionGroupHTML: '<p>This section will test you on colors and numbers.</p>', allCorrectMinCount: 2, questionsList: [ {questionHTML: 'Pick all <i>odd</i> numbers:', choices: ['1', '2', '3', '4', '5'], correctIndex: [0, 2, 4]}, {questionHTML: 'Pick one <i>even</i> number:', choices: ['1', '2', '3', '4', '5'], correctIndex: [1, 3], multiSelect: false}, {questionHTML: 'What color is the sky?', choices: ['#00FF00', '#00FF00', '#0000FF'], correctIndex: 2} ], allCorrectOutput: 'Great job! You know the material well.', someIncorrectOutput: 'You must answer at least two questions correctly.' } ]; """) verify_activity(activity_text) scope = verify.Activity().scope current_ast = ActivityParser13.parse_string_in_scope( activity_text, scope, 'activity') expected_ast = verify.legacy_eval_python_expression_for_test( activity_text, scope, 'activity') same = (len(current_ast.get('activity')) == 4 and current_ast.get('activity') == expected_ast.get('activity') and current_ast == expected_ast) if not same: import pprint # # pylint: disable-msg=g-import-not-at-top pprint.pprint(current_ast.get('activity')) pprint.pprint(expected_ast.get('activity')) assert same
def get_activity_as_python(self, unit_id, lesson_id): """Gets the corresponding activity as a Python object.""" root_name = 'activity' course = self._get_course() activity_text = course.app_context.fs.get( os.path.join(course.app_context.get_home(), course.get_activity_filename(unit_id, lesson_id))) content, noverify_text = verify.convert_javascript_to_python( activity_text, root_name) activity = verify.evaluate_python_expression_from_text( content, root_name, verify.Activity().scope, noverify_text) return activity