Exemplo n.º 1
0
def get_collection_from_model(collection_model, run_conversion=True):
    """Returns a Collection domain object given a collection model loaded
    from the datastore.

    If run_conversion is True, then the collection's schema version will be
    checked against the current schema version. If they do not match, the
    collection will be automatically updated to the latest schema version.

    IMPORTANT NOTE TO DEVELOPERS: In general, run_conversion should never be
    False. This option is only used for testing that the schema version
    migration works correctly, and it should never be changed otherwise.
    """

    # Ensure the original collection model does not get altered.
    versioned_collection = {
        'schema_version': collection_model.schema_version,
        'nodes': copy.deepcopy(collection_model.nodes)
    }

    # Migrate the collection if it is not using the latest schema version.
    if (run_conversion and collection_model.schema_version !=
            feconf.CURRENT_COLLECTION_SCHEMA_VERSION):
        _migrate_collection_to_latest_schema(versioned_collection)

    return collection_domain.Collection(
        collection_model.id, collection_model.title, collection_model.category,
        collection_model.objective, versioned_collection['schema_version'], [
            collection_domain.CollectionNode.from_dict(collection_node_dict)
            for collection_node_dict in versioned_collection['nodes']
        ], collection_model.version, collection_model.created_on,
        collection_model.last_updated)
Exemplo n.º 2
0
def get_collection_from_model(collection_model, run_conversion=True):
    """Returns a Collection domain object given a collection model loaded
    from the datastore.

    Args:
        collection_model: CollectionModel. The collection model loaded from the
            datastore.
        run_conversion: bool. If true, the the collection's schema version will
            be checked against the current schema version. If they do not match,
            the collection will be automatically updated to the latest schema
            version.

            IMPORTANT NOTE TO DEVELOPERS: In general, run_conversion should
            never be False. This option is only used for testing that the
            schema version migration works correctly, and it should never be
            changed otherwise.

    Returns:
        Collection. A Collection domain object corresponding to the given
        collection model.
    """

    # Ensure the original collection model does not get altered.
    versioned_collection_contents = {
        'schema_version': collection_model.schema_version,
        'collection_contents':
        copy.deepcopy(collection_model.collection_contents)
    }

    # If collection is in version 2, copy nodes data to collection contents.
    if collection_model.schema_version == 2:
        versioned_collection_contents['collection_contents'] = {
            'nodes': copy.deepcopy(collection_model.nodes)
        }

    # Migrate the collection if it is not using the latest schema version.
    if (run_conversion and collection_model.schema_version !=
            feconf.CURRENT_COLLECTION_SCHEMA_VERSION):
        _migrate_collection_contents_to_latest_schema(
            versioned_collection_contents)

    return collection_domain.Collection(
        collection_model.id, collection_model.title, collection_model.category,
        collection_model.objective, collection_model.language_code,
        collection_model.tags, versioned_collection_contents['schema_version'],
        [
            collection_domain.CollectionNode.from_dict(collection_node_dict)
            for collection_node_dict in
            versioned_collection_contents['collection_contents']['nodes']
        ], {
            skill_id: collection_domain.CollectionSkill.from_dict(
                skill_id, skill_dict)
            for skill_id, skill_dict in
            versioned_collection_contents['collection_contents']
            ['skills'].iteritems()
        }, versioned_collection_contents['collection_contents']
        ['next_skill_index'], collection_model.version,
        collection_model.created_on, collection_model.last_updated)
Exemplo n.º 3
0
    def test_from_yaml_with_no_schema_version_specified_raises_error(self):
        collection = collection_domain.Collection(self.COLLECTION_ID, 'title',
                                                  'category', 'objective',
                                                  'en', [], None, [], 0)

        yaml_content = collection.to_yaml()

        with self.assertRaisesRegexp(
                Exception, 'Invalid YAML file: no schema version specified.'):
            collection_domain.Collection.from_yaml(self.COLLECTION_ID,
                                                   yaml_content)
Exemplo n.º 4
0
    def test_from_yaml_with_invalid_schema_version_raises_error(self):
        collection = collection_domain.Collection(self.COLLECTION_ID, 'title',
                                                  'category', 'objective',
                                                  'en', [], 0, [], 0)

        yaml_content = collection.to_yaml()

        with self.assertRaisesRegexp(
                Exception,
                'Sorry, we can only process v1 to .+ collection YAML files at '
                'present.'):
            collection_domain.Collection.from_yaml(self.COLLECTION_ID,
                                                   yaml_content)
Exemplo n.º 5
0
    def test_from_yaml_with_no_schema_version_specified_raises_error(
            self) -> None:
        # Argument schema_version of collection expects int value but here
        # we are passing None which causes MyPy to throw an error. Thus we
        # add an ignore here.
        collection = collection_domain.Collection(self.COLLECTION_ID, 'title',
                                                  'category', 'objective',
                                                  'en', [], None, [],
                                                  0)  # type: ignore[arg-type]

        yaml_content = collection.to_yaml()

        with self.assertRaisesRegex(  # type: ignore[no-untyped-call]
                Exception, 'Invalid YAML file: no schema version specified.'):
            collection_domain.Collection.from_yaml(self.COLLECTION_ID,
                                                   yaml_content)
Exemplo n.º 6
0
def get_collection_from_model(collection_model):
    """Returns a Collection domain object given a collection model loaded
    from the datastore.

    Args:
        collection_model: CollectionModel. The collection model loaded from the
            datastore.

    Returns:
        Collection. A Collection domain object corresponding to the given
        collection model.
    """

    # Ensure the original collection model does not get altered.
    versioned_collection_contents = {
        'schema_version': collection_model.schema_version,
        'collection_contents':
            copy.deepcopy(collection_model.collection_contents)
    }

    # If collection is in version 2, copy nodes data to collection contents.
    if collection_model.schema_version == 2:
        versioned_collection_contents['collection_contents'] = {
            'nodes': copy.deepcopy(collection_model.nodes)
        }

    # Migrate the collection if it is not using the latest schema version.
    if (collection_model.schema_version !=
            feconf.CURRENT_COLLECTION_SCHEMA_VERSION):
        _migrate_collection_contents_to_latest_schema(
            versioned_collection_contents)

    return collection_domain.Collection(
        collection_model.id, collection_model.title,
        collection_model.category, collection_model.objective,
        collection_model.language_code, collection_model.tags,
        versioned_collection_contents['schema_version'], [
            collection_domain.CollectionNode.from_dict(collection_node_dict)
            for collection_node_dict in
            versioned_collection_contents['collection_contents']['nodes']
        ],
        collection_model.version, collection_model.created_on,
        collection_model.last_updated)