예제 #1
0
    def add_state(self, state_name):
        """Adds a new state, and returns it."""
        if self._has_state_named(state_name):
            raise Exception('Duplicate state name %s' % state_name)

        state = State.create(self, state_name)
        self.states.append(state.key)
        self.put()

        return state
예제 #2
0
    def create(cls, user, title, category, exploration_id=None,
               init_state_name=feconf.DEFAULT_STATE_NAME, image_id=None):
        """Creates and returns a new exploration."""
        # Generate a new exploration id, if one wasn't passed in.
        exploration_id = exploration_id or cls.get_new_id(title)

        # Temporarily create a fake initial state key.
        state_id = State.get_new_id(init_state_name)
        fake_state_key = ndb.Key(Exploration, exploration_id, State, state_id)

        # Note that demo explorations do not have owners, so user may be None.
        exploration = cls(
            id=exploration_id, title=title, init_state=fake_state_key,
            category=category, image_id=image_id, states=[fake_state_key],
            editors=[user] if user else [])
        exploration.put()

        # Finally, create the initial state and check that it has the right key.
        new_init_state = State.create(
            exploration, init_state_name, state_id=state_id)
        assert fake_state_key == new_init_state.key

        return exploration