Exemplo n.º 1
0
def create_from_yaml(
    yaml_content, user_id, title, category, exploration_id=None,
        image_id=None):
    """Creates an exploration from a YAML text string."""
    exploration_dict = utils.dict_from_yaml(yaml_content)
    init_state_name = exploration_dict['states'][0]['name']

    exploration = Exploration.get(create_new(
        user_id, title, category, exploration_id=exploration_id,
        init_state_name=init_state_name, image_id=image_id))

    init_state = State.get_by_name(init_state_name, exploration)

    try:
        exploration.parameters = [Parameter(
            name=param['name'], obj_type=param['obj_type'],
            values=param['values']
        ) for param in exploration_dict['parameters']]

        state_list = []
        for state_description in exploration_dict['states']:
            state_name = state_description['name']
            state = (init_state if state_name == init_state_name
                     else exploration.add_state(state_name))
            state_list.append({'state': state, 'desc': state_description})

        for index, state in enumerate(state_list):
            modify_using_dict(exploration.id, state['state'].id, state['desc'])
    except Exception:
        exploration.delete()
        raise

    return exploration.id
Exemplo n.º 2
0
    def test_create_and_get_state(self):
        """Test creation and retrieval of states."""
        id_1 = '123'
        name_1 = 'State 1'
        state_1 = self.exploration.add_state(name_1, state_id=id_1)

        fetched_state_1 = self.exploration.get_state_by_id(id_1)
        self.assertEqual(fetched_state_1, state_1)

        fetched_state_by_name_1 = State.get_by_name(name_1, self.exploration)
        self.assertEqual(fetched_state_by_name_1, state_1)

        # Test the failure cases.
        id_2 = 'fake_id'
        name_2 = 'fake_name'
        with self.assertRaises(Exception):
            self.exploration.get(id_2)

        fetched_state_by_name_2 = State.get_by_name(
            name_2, self.exploration, strict=False)
        self.assertIsNone(fetched_state_by_name_2)
        with self.assertRaises(Exception):
            State.get_by_name(name_2, self.exploration, strict=True)
        # The default behavior is to fail noisily.
        with self.assertRaises(Exception):
            State.get_by_name(name_2, self.exploration)
Exemplo n.º 3
0
    def create_from_yaml(
        cls, yaml_file, user, title, category, exploration_id=None,
            image_id=None):
        """Creates an exploration from a YAML file."""
        exploration_dict = utils.dict_from_yaml(yaml_file)
        init_state_name = exploration_dict['states'][0]['name']

        exploration = cls.create(
            user, title, category, exploration_id=exploration_id,
            init_state_name=init_state_name, image_id=image_id)

        init_state = State.get_by_name(init_state_name, exploration)

        try:
            for param in exploration_dict['parameters']:
                exploration.parameters.append(Parameter(
                    name=param['name'], obj_type=param['obj_type'],
                    values=param['values'])
                )

            state_list = []
            exploration_states = exploration_dict['states']
            for state_description in exploration_states:
                state_name = state_description['name']
                state = (init_state if state_name == init_state_name
                         else exploration.add_state(state_name))
                state_list.append({'state': state, 'desc': state_description})

            for index, state in enumerate(state_list):
                State.modify_using_dict(
                    exploration, state['state'], state['desc'])
        except Exception:
            exploration.delete()
            raise

        return exploration