def get_raw_static_code(cls, widget_id, params=None): """Gets the static rendering raw code for a parameterized widget.""" # TODO(kashida): Make this consistent with get_raw_code. if params is None: params = {} widget = cls.get(widget_id) return utils.parse_with_jinja(widget.static_template, params)
def _get_exploration_params(self, exploration): # TODO(yanamal/sll): consider merging with get_params somehow, since the # process is largely the same params = {} for item in exploration.parameters: value = item.value params[item.name] = (None if value is None else utils.parse_with_jinja(value, params, value)) return params
def parse_content_into_html(content_array, block_number, params=None): """Takes a Content array and transforms it into HTML. Args: content_array: an array, each of whose members is of type Content. This object has two keys: type and value. The 'type' is one of the following: - 'text'; then the value is a text string - 'image'; then the value is an image ID - 'video'; then the value is a video ID - 'widget'; then the value is a widget ID block_number: the number of content blocks preceding this one. params: any parameters used for templatizing text strings. Returns: the HTML string representing the array. Raises: InvalidInputException: if content has no 'type' attribute, or an invalid 'type' attribute. """ if params is None: params = {} html = '' widget_array = [] widget_counter = 0 for content in content_array: if content.type == 'widget': try: widget = NonInteractiveWidget.get_with_params( content.value, params) widget_counter += 1 html += feconf.JINJA_ENV.get_template('content.html').render({ 'type': content.type, 'blockIndex': block_number, 'index': widget_counter}) widget_array.append({ 'blockIndex': block_number, 'index': widget_counter, 'code': widget.raw}) except utils.EntityIdNotFoundError: # Ignore empty widget content. pass elif (content.type in ['text', 'image', 'video']): if content.type == 'text': value = utils.parse_with_jinja(content.value, params) else: value = content.value html += feconf.JINJA_ENV.get_template('content.html').render({ 'type': content.type, 'value': value}) else: raise utils.InvalidInputException( 'Invalid content type %s', content.type) return html, widget_array
def get_params(state, existing_params=None): """Updates existing parameters based on changes in the given state.""" if existing_params is None: existing_params = {} # Modify params using param_changes. for item in state.param_changes: # Pick a random parameter for this key. value = item.value existing_params[item.name] = ( None if value is None else utils.parse_with_jinja( value, existing_params, value)) return existing_params
def test_parse_with_jinja(self): """Test parse_with_jinja method.""" parsed_str = utils.parse_with_jinja('{{test}}', {'test': 'hi'}) self.assertEqual(parsed_str, 'hi') # Some parameters are missing. parsed_str = utils.parse_with_jinja( '{{test}} and {{test2}}', {'test2': 'hi'}) self.assertEqual(parsed_str, ' and hi') # All parameters are missing. parsed_str = utils.parse_with_jinja('{{test}} and {{test2}}', {}) self.assertEqual(parsed_str, ' and ') # Default parameters are used. parsed_str = utils.parse_with_jinja('{{test}} and {{test2}}', {}, 'def') self.assertEqual(parsed_str, 'def and def') # The string has no parameters. parsed_str = utils.parse_with_jinja('no params', {'param': 'hi'}) self.assertEqual(parsed_str, 'no params') # Integer parameters are used. parsed_str = utils.parse_with_jinja('int {{i}}', {'i': 2}) self.assertEqual(parsed_str, 'int 2')
def get_raw_code(cls, widget_id, params=None): """Gets the raw code for a parameterized widget.""" if params is None: params = {} widget = cls.get(widget_id) # Parameters used to generate the raw code for the widget. parameters = {} for param in widget.params: parameters[param.name] = params.get(param.name, utils.convert_to_js_string(param.value)) return utils.parse_with_jinja(widget.template, parameters)
def get_raw_code(cls, widget_id, params=None): """Gets the raw code for a parameterized widget. This method should be called on a subclass of Widget. """ if cls.__name__ == 'Widget': raise NotImplementedError if params is None: params = {} widget = cls.get(widget_id) # Parameters used to generate the raw code for the widget. # TODO(sll): Why do we convert only the default value to a JS string? parameters = dict( (param.name, params.get( param.name, utils.convert_to_js_string(param.value)) ) for param in widget.params) return utils.parse_with_jinja(widget.template, parameters)
def get_classifier_info(self, widget_id, handler_name, rule, state_params): classifier_func = rule.name.replace(' ', '') first_bracket = classifier_func.find('(') mutable_rule = InteractiveWidget.objects.get(id=widget_id).get_readable_name( handler_name, rule.name) func_name = classifier_func[: first_bracket] str_params = classifier_func[first_bracket + 1: -1].split(',') param_list = [] for index, param in enumerate(str_params): parsed_param = rule.inputs[param] if isinstance(parsed_param, basestring) and '{{' in parsed_param: parsed_param = utils.parse_with_jinja( parsed_param, state_params) typed_object = self.get_typed_object(mutable_rule, param) normalized_param = typed_object.normalize(parsed_param) param_list.append(normalized_param) return func_name, param_list
def parse_content_into_html(content_array, block_number, params=None): """Takes a Content array and transforms it into HTML. Args: content_array: an array, each of whose members is of type Content. This object has two keys: type and value. The 'type' is one of the following: - 'text'; then the value is a text string - 'image'; then the value is an image ID - 'video'; then the value is a video ID - 'widget'; then the value is a JSON-encoded dict with keys 'id' and 'params', from which the raw widget HTML can be constructed block_number: the number of content blocks preceding this one. params: any parameters used for templatizing text strings. Returns: the HTML string representing the array. Raises: InvalidInputException: if content has no 'type' attribute, or an invalid 'type' attribute. """ if params is None: params = {} html = '' widget_array = [] widget_counter = 0 for content in content_array: if content.type in ['text', 'image', 'video']: if content.type == 'text': value = utils.parse_with_jinja(content.value, params) else: value = content.value html += feconf.OPPIA_JINJA_ENV.get_template( 'reader/content.html').render({ 'type': content.type, 'value': value}) elif content.type == 'widget': # Ignore empty widget specifications. if not content.value: continue widget_dict = json.loads(content.value) widget = NonInteractiveWidget.get_with_params( widget_dict['id'], widget_dict['params']) html += feconf.OPPIA_JINJA_ENV.get_template( 'reader/content.html').render({ 'blockIndex': block_number, 'index': widget_counter, 'type': content.type, }) widget_array.append({ 'blockIndex': block_number, 'index': widget_counter, 'raw': widget['raw'], }) widget_counter += 1 else: raise utils.InvalidInputException( 'Invalid content type %s', content.type) return html, widget_array