예제 #1
0
def create_new(
    user_id, title, category, exploration_id=None,
        init_state_name=feconf.DEFAULT_STATE_NAME, image_id=None):
    """Creates, saves and returns a new exploration id."""
    # Generate a new exploration id, if one wasn't passed in.
    exploration_id = exploration_id or ExplorationModel.get_new_id(title)

    state_id = State.get_new_id(init_state_name)
    new_state = State(id=state_id, name=init_state_name)
    new_state.put()

    # Note that demo explorations do not have owners, so user_id may be None.
    exploration = ExplorationModel(
        id=exploration_id, title=title, category=category,
        image_id=image_id, state_ids=[state_id],
        editor_ids=[user_id] if user_id else [])

    exploration.put()

    return exploration.id
예제 #2
0
def count_explorations():
    """Returns the total number of explorations."""
    return ExplorationModel.get_exploration_count()
예제 #3
0
def get_viewable_explorations(user_id):
    """Returns domain objects for explorations viewable by the given user."""
    return [Exploration(e) for e in
            ExplorationModel.get_viewable_explorations(user_id)]
예제 #4
0
def get_public_explorations():
    """Returns a list of domain objects representing public explorations."""
    return [Exploration(e) for e in ExplorationModel.get_public_explorations()]
예제 #5
0
def get_all_explorations():
    """Returns a list of domain objects representing all explorations."""
    return [Exploration(e) for e in ExplorationModel.get_all()]
예제 #6
0
 def get(cls, exploration_id, strict=True):
     """Returns a domain object representing an exploration."""
     exploration_model = ExplorationModel.get(exploration_id, strict=strict)
     if exploration_model is None:
         return None
     return cls(exploration_model)
예제 #7
0
    def test_exploration_class(self):
        """Test the Exploration model class."""
        exploration = ExplorationModel(id='The exploration hash id')

        # A new exploration should have a default title property.
        self.assertEqual(exploration.title, 'New exploration')

        # A new exploration should have a default is_public property.
        self.assertEqual(exploration.is_public, False)

        state = State(id='The state hash id')
        state.put()

        # The 'state_ids' property must be a list of strings.
        with self.assertRaises(SyntaxError):
            exploration.state_ids = 'A string'
            exploration.put()
        with self.assertRaises(ValidationError):
            exploration.state_ids = [state]
            exploration.put()
        exploration.state_ids = [state.id]

        # An Exploration must have a category.
        with self.assertRaises(ValidationError):
            exploration.put()
        exploration.category = 'The category'

        # The 'parameters' property must be a list of Parameter objects.
        with self.assertRaises(ValidationError):
            exploration.parameters = 'A string'
            exploration.put()
        exploration.parameters = []
        parameter = Parameter(name='theParameter', obj_type='Int')
        with self.assertRaises(AttributeError):
            exploration.parameters = [parameter.key]
        exploration.parameters = [parameter]

        # The 'is_public' property must be a boolean.
        with self.assertRaises(ValidationError):
            exploration.is_public = 'true'
            exploration.put()
        exploration.is_public = True

        # The 'image_id' property must be a string.
        # TODO: This fails now because of the behaviour of CharField
        image = Image(id='The image')
        with self.assertRaises(ValidationError):
            exploration.image_id = image
            exploration.put()
        exploration.image_id = 'A string'

        exploration.editor_ids = ['A user id']

        # Put and retrieve the exploration.
        exploration.put()

        retrieved_exploration = Exploration.get('The exploration hash id')
        self.assertEqual(retrieved_exploration.category, 'The category')
        self.assertEqual(retrieved_exploration.title, 'New exploration')
        self.assertEqual(retrieved_exploration.state_ids, [state.id])
        self.assertEqual(retrieved_exploration.parameters, [parameter])
        self.assertEqual(retrieved_exploration.is_public, True)
        self.assertEqual(retrieved_exploration.image_id, 'A string')
        self.assertEqual(retrieved_exploration.editor_ids, ['A user id'])