def reload_widgets(): """Reload the default classifiers and widgets.""" Classifier.delete_all_classifiers() Classifier.load_default_classifiers() Widget.delete_all_widgets() InteractiveWidget.load_default_widgets() NonInteractiveWidget.load_default_widgets()
def reload_demos(): """Reload default classifiers, widgets, and explorations (in that order).""" Classifier.delete_all_classifiers() Classifier.load_default_classifiers() Widget.delete_all_widgets() InteractiveWidget.load_default_widgets() NonInteractiveWidget.load_default_widgets() Exploration.delete_demo_explorations() Exploration.load_demo_explorations()
def post(self, widget_id): """Handles POST requests, for parameterized widgets.""" params = self.payload.get('params', {}) if isinstance(params, list): new_params = {} for item in params: new_params[item['name']] = item['default_value'] params = new_params state_params_dict = {} state_params_given = self.payload.get('state_params') if state_params_given: for param in state_params_given: # Pick a random parameter for each key. state_params_dict[param['name']] = ( utils.get_random_choice(param['values'])) # TODO(sll): In order to unify this with InteractiveWidgetHandler, # we need a convention for which params must be JSONified and which # should not. Fix this. response = NonInteractiveWidget.get_with_params(widget_id, params) self.render_json({ 'widget': response, 'parent_index': self.request.get('parent_index'), })
def get(self, widget_id): """Handles GET requests.""" try: self.render_json({ 'widget': NonInteractiveWidget.get_with_params(widget_id, {}), }) except: raise self.PageNotFoundException
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 test_loading_and_deletion_of_widgets(self): """Test loading and deletion of the default widgets.""" self.assertEqual(Widget.query().count(), 0) InteractiveWidget.load_default_widgets() self.assertEqual(Widget.query().count(), 7) self.assertEqual(InteractiveWidget.query().count(), 7) self.assertEqual(NonInteractiveWidget.query().count(), 0) Widget.delete_all_widgets() self.assertEqual(Widget.query().count(), 0)
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.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.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