def post(self, widget_id): """Handles POST requests, for parameterized widgets.""" payload = json.loads(self.request.get('payload')) logging.info(payload) params = 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 = 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'])) response = InteractiveWidget.get_with_params( widget_id, params=utils.parse_dict_with_params( params, state_params_dict) ) self.response.write(json.dumps({'widget': response}))
def post(self, exploration_id, state_id): """Handles feedback interactions with readers.""" values = {'error': []} exploration = Exploration.get(exploration_id) state = State.get(state_id, exploration) old_state = state payload = json.loads(self.request.get('payload')) # The 0-based index of the last content block already on the page. block_number = payload.get('block_number') params = self.get_params(state, payload.get('params')) # The reader's answer. answer = payload.get('answer') # Add the reader's answer to the parameter list. This must happen before # the interactive widget is constructed. params['answer'] = answer interactive_widget_properties = ( InteractiveWidget.get_with_params( state.widget.widget_id)['actions']['submit']) dest_id, feedback, rule, recorded_answer = state.transition( answer, params, interactive_widget_properties) if recorded_answer: EventHandler.record_rule_hit( exploration_id, state_id, rule, recorded_answer) # Add this answer to the state's 'unresolved answers' list. if recorded_answer not in old_state.unresolved_answers: old_state.unresolved_answers[recorded_answer] = 0 old_state.unresolved_answers[recorded_answer] += 1 # TODO(sll): Make this async? old_state.put() assert dest_id html_output, widget_output = '', [] # TODO(sll): The following is a special-case for multiple choice input, # in which the choice text must be displayed instead of the choice # number. We might need to find a way to do this more generically. if state.widget.widget_id == 'MultipleChoiceInput': answer = state.widget.params['choices'][int(answer)] # Append reader's answer. values['reader_html'] = feconf.JINJA_ENV.get_template( 'reader_response.html').render({'response': answer}) if dest_id == feconf.END_DEST: # This leads to a FINISHED state. if feedback: html_output, widget_output = self.append_feedback( feedback, html_output, widget_output, block_number, params) EventHandler.record_exploration_completed(exploration_id) else: state = State.get(dest_id, exploration) EventHandler.record_state_hit(exploration_id, state_id) if feedback: html_output, widget_output = self.append_feedback( feedback, html_output, widget_output, block_number, params) # Append text for the new state only if the new and old states # differ. if old_state.id != state.id: state_html, state_widgets = parse_content_into_html( state.content, block_number, params) # Separate text for the new state and feedback for the old state # by an additional line. if state_html and feedback: html_output += '<br>' html_output += state_html widget_output.append(state_widgets) if state.widget.widget_id in DEFAULT_ANSWERS: values['default_answer'] = DEFAULT_ANSWERS[state.widget.widget_id] values['exploration_id'] = exploration.id values['state_id'] = state.id values['oppia_html'] = html_output values['widgets'] = widget_output values['block_number'] = block_number + 1 values['params'] = params if dest_id != feconf.END_DEST: values['finished'] = False if state.widget.sticky and ( state.widget.widget_id == old_state.widget.widget_id): values['interactive_widget_html'] = '' values['sticky_interactive_widget'] = True else: values['interactive_widget_html'] = ( InteractiveWidget.get_raw_code( state.widget.widget_id, params=utils.parse_dict_with_params( state.widget.params, params) ) ) else: values['finished'] = True values['interactive_widget_html'] = '' self.response.write(json.dumps(values))
def get(self, widget_id): """Handles GET requests.""" response = InteractiveWidget.get_with_params(widget_id) self.response.write(json.dumps({'widget': response}))