def modify_using_dict(cls, exploration, state, sdict): """Modifies the properties of a state using values from a dict.""" state.content = [ Content(type=item['type'], value=item['value']) for item in sdict['content'] ] state.param_changes = [ ParamChange(**pc) for pc in sdict['param_changes'] ] wdict = sdict['widget'] state.widget = WidgetInstance( widget_id=wdict['widget_id'], sticky=wdict['sticky']) state.widget.params = wdict['params'] for wp in Widget.get(wdict['widget_id']).params: if wp.name not in wdict['params']: state.widget.params[wp.name] = wp.value state.widget.handlers = [] for handler in wdict['handlers']: state_handler = AnswerHandlerInstance(name=handler['name']) for rule in handler['rules']: rule_dest = ( feconf.END_DEST if rule['dest'] == feconf.END_DEST else State.get_by_name(rule['dest'], exploration).id) state_handler.rules.append(Rule( feedback=rule['feedback'], inputs=rule['inputs'], name=rule['name'], dest=rule_dest )) state.widget.handlers.append(state_handler) state.put() return state
def verify_state_dict(self, state_dict, state_name_list): """Verifies a state dictionary.""" STATE_DICT_SCHEMA = [ ('name', basestring), ('content', list), ('param_changes', list), ('widget', dict)] self.verify_dict_keys_and_types(state_dict, STATE_DICT_SCHEMA) curr_state = state_dict['name'] # TODO(sll): Change the following verification once we move 'content' # to become a typed object. CONTENT_ITEM_SCHEMA = [('type', basestring), ('value', basestring)] for content_item in state_dict['content']: self.verify_dict_keys_and_types(content_item, CONTENT_ITEM_SCHEMA) self.assertIn(content_item['type'], ['text', 'image', 'video']) PARAM_CHANGES_SCHEMA = [ ('name', basestring), ('values', list), ('obj_type', basestring)] for param_change in state_dict['param_changes']: self.verify_dict_keys_and_types(param_change, PARAM_CHANGES_SCHEMA) # TODO(sll): Test that the elements of 'values' are of the correct # type. WIDGET_SCHEMA = [ ('widget_id', basestring), ('params', dict), ('handlers', list), ('sticky', bool)] self.verify_dict_keys_and_types(state_dict['widget'], WIDGET_SCHEMA) for handler in state_dict['widget']['handlers']: HANDLER_SCHEMA = [('name', basestring), ('rules', list)] self.verify_dict_keys_and_types(handler, HANDLER_SCHEMA) # Check that the list of rules is non-empty. self.assertTrue(handler['rules']) for rule in handler['rules']: RULE_SCHEMA = [ ('dest', basestring), ('feedback', list), ('inputs', dict), ('name', basestring), ('param_changes', list) ] self.verify_dict_keys_and_types(rule, RULE_SCHEMA) # Check that the destination is a valid one. if rule['dest'] != feconf.END_DEST: self.assertIn(rule['dest'], state_name_list) # Check that there are no feedback-less self-loops. self.assertFalse( rule['dest'] == curr_state and not rule['feedback'] and not state_dict['widget']['sticky'], msg='State %s has a self-loop with no feedback. This is ' 'likely to frustrate the reader.' % curr_state) # TODO(sll): Does 'inputs' need any tests? # TODO(sll): Check that the name corresponds to a valid one # from the relevant exploration -- maybe. (Need to decide if # we allow initialization of parameters midway through states.) for param_change in state_dict['param_changes']: self.verify_dict_keys_and_types( param_change, PARAM_CHANGES_SCHEMA) # TODO(sll): Test that the elements of 'values' are of the # correct type. for wp_name, wp_value in state_dict['widget']['params'].iteritems(): self.assertTrue(isinstance(wp_name, basestring)) # Check that the parameter name is valid. widget_params = Widget.get(state_dict['widget']['widget_id']).params widget_param_names = [wp.name for wp in widget_params] self.assertIn( wp_name, widget_param_names, msg='Parameter %s is not a valid parameter for widget %s' % ( wp_name, state_dict['widget']['widget_id'])) # Get the object class used to normalize the value for this param. for wp in widget_params: if wp.name == wp_name: obj_class = get_object_class(wp.obj_type) self.assertIsNotNone(obj_class) break # Check that the parameter value has the correct type. obj_class.normalize(wp_value)