Пример #1
0
def block_schema_edit(verbose, name, community, deprecated, block_schema_id):
    try:
        UUID(block_schema_id, version=4)
    except ValueError:
        raise click.BadParameter("""BLOCK_SCHEMA_ID is not a valid UUID
         (hexadecimal numbers and dashes e.g.
         fa52bec3-a847-4602-8af5-b8d41a5215bc )""")
    try:
        block_schema = BlockSchema.get_block_schema(schema_id=block_schema_id)
    except BlockSchemaDoesNotExistError:
        raise click.BadParameter("No block_schema with id %s" %
                                 block_schema_id)
    if not (name or community or deprecated):
        raise click.ClickException("""Noting to edit - at least one of name,
        community or deprecated must be provided.""")
    data = {}
    if name:
        data['name'] = name
    if community:
        comm = get_community_by_name_or_id(community)
        if comm:
            data['community'] = comm.id
        else:
            click.secho("""Community not changed : no community exists with
            name or id: %s""" % community)
    if deprecated:
        if type(deprecated) != bool:
            raise click.BadParameter("""Deprecated should be True or False
             starting with a capital""")
        data['deprecated'] = deprecated
    block_schema.update(data)
    db.session.commit()
Пример #2
0
def update_or_set_community_root_schema(community, root_schema_version):
    """Updates or sets the root schema version for a community.

    The complete schema of a community contains a copy of the root metadata
    schema and a set of community-specific metadata block schemas. Currently
    we assume there's only one allowed metadata block schema for a community,
    and this command creates it or makes a new version for it.

    By calling this function a new community schema version and block schema version
    (cloned from latest if present) are created.

    - community is the ID or NAME of the community to be updated.
    - root_schema_version is the index of a known root schema.
    """

    comm = get_community_by_name_or_id(community)
    if not comm:
        raise click.BadParameter(
            "There is no community by this name or ID: %s" % community)

    # get latest version of community schema
    try:
        community_schema = CommunitySchema.get_community_schema(comm.id)
        comm_schema_json = json.loads(community_schema.community_schema)

        if community_schema.block_schema_id > "":
            _create_community_schema(comm, comm_schema_json,
                                     root_schema_version)
        else:
            _create_community_schema_no_block(comm, root_schema_version)
    except:
        return False

    db.session.commit()
    click.secho("Succesfully processed root schema", fg='green')
Пример #3
0
def block_schema_list(verbose, community):
    """Lists all block schemas for this b2share instance (filtered for a
    community)."""
    comm = None
    if community:
        comm = get_community_by_name_or_id(community)
    community_id = None
    if comm:
        community_id = comm.id
    if verbose:
        click.secho("filtering for community %s" % comm)
    try:
        block_schemas = \
        BlockSchema.get_all_block_schemas(community_id=community_id)
    except BlockSchemaDoesNotExistError:
        raise click.ClickException("""No block schemas found, community
            parameter was: %s""" % community)
    click.secho(
        """BLOCK SCHEMA ID\t\t\t\tNAME\t\tMAINTAINER\tDEPRECATED\t#VERSIONS""")
    for bs in block_schemas:
        bs_comm = Community.get(id=bs.community)
        click.secho(
            "%s\t%s\t%s\t%s\t\t%d" %
            (bs.id, bs.name[0:15].ljust(15), bs_comm.name[0:15].ljust(15),
             bs.deprecated, len(bs.versions)))
Пример #4
0
def community_schema_list(verbose, community):
    """Lists all community schema versions for this b2share instance (filtered for a
    community)."""
    comm = None
    if community:
        comm = get_community_by_name_or_id(community)
    community_id = None
    if comm:
        community_id = comm.id
    if verbose:
        click.secho("filtering for community %s" % comm)
    try:
        community_schemas = \
        CommunitySchema.get_all_community_schemas(community_id=community_id)
    except CommunitySchemaDoesNotExistError:
        raise click.ClickException("""No community schemas found, community
            parameter was: %s""" % community)
    click.secho(
        """COMMUNITY ID\t\t\t\tNAME\t\tVERSION\tROOT SCHEMA\tRELEASED\t\t\t\tBLOCK SCHEMA ID"""
    )
    for cs in community_schemas:
        cs_comm = Community.get(id=cs.community)
        click.secho("%s\t%s\t%d\t%d\t\t%s\t\t%s" %
                    (cs.community, cs_comm.name[0:15].ljust(15), cs.version,
                     cs.root_schema, cs.released, cs.block_schema_id))
Пример #5
0
def update_or_set_community_schema(community, json_file):
    """Updates or sets the schema for a community.

    The complete schema of a community contains a copy of the root metadata
    schema and a set of community-specific metadata block schemas. Currently
    we assume there's only one allowed metadata block schema for a community,
    and this command creates it or makes a new version for it.

    - community is the ID or NAME of the community to be updated.
    - json_file is a file path to the json-schema file describing the
    community-specific block schema.

    See also `b2share schemas block_schema_version_generate_json`"""

    from b2share.modules.communities.helpers import get_community_by_name_or_id

    comm = get_community_by_name_or_id(community)
    if not comm:
        raise click.BadParameter(
            "There is no community by this name or ID: %s" % community)
    if not os.path.isfile(json_file):
        raise click.ClickException("%s does not exist on the filesystem" %
                                   json_file)

    schema_dict = {}
    with open(json_file, 'r') as f:
        try:
            schema_dict = json.load(f)
        except ValueError:
            raise click.ClickException("%s is not valid JSON" % json_file)

    try:
        validate_metadata_schema(schema_dict)
    except Exception as e:
        print("schema validation error:", e)
        raise click.ClickException("""%s is not a valid metadata schema for
        a B2SHARE community""" % json_file)

    #create new block version schema
    try:
        community_schema = CommunitySchema.get_community_schema(comm.id)
        comm_schema_json = json.loads(community_schema.community_schema)
        if 'properties' not in comm_schema_json.keys():
            raise click.ClickException("""Invalid community schema
                for community %s""" % community.name)

        if len(comm_schema_json['properties']) > 1:
            raise click.ClickException(
                """Multiple block schemas not supported.""")
        #we can by configuration also have a community schema that does not refer to a blockschema
        if len(comm_schema_json['properties']) == 0:
            _create_community_schema(comm, schema_dict)
        else:
            _update_community_schema(comm, comm_schema_json, schema_dict)
    except CommunitySchemaDoesNotExistError:
        _create_community_schema(comm, schema_dict)
    db.session.commit()
    click.secho("Succesfully processed new metadata schema", fg='green')
Пример #6
0
def test_create_community(app, test_communities):
    with app.app_context():
        runner = CliRunner()
        script_info = ScriptInfo(create_app=lambda info: app)
        result = runner.invoke(
            communities_cmd,
            ["create", "COMM_NAME", "Description comm", "bbmri.png"],
            obj=script_info)
        assert result.exit_code == 0
        retrieved_comm = get_community_by_name_or_id("COMM_NAME")
        assert retrieved_comm.name == 'COMM_NAME'
Пример #7
0
def test_edit_community(app, test_communities):
    with app.app_context():
        runner = CliRunner()
        script_info = ScriptInfo(create_app=lambda info: app)
        result = runner.invoke(communities_cmd, [
            "edit", "cccccccc-1111-1111-1111-111111111111", "--name",
            "NEW_NAME2"
        ],
                               obj=script_info)
        result = runner.invoke(communities_cmd, ["list"], obj=script_info)
        retrieved_comm = get_community_by_name_or_id("NEW_NAME2")
        assert retrieved_comm.name == 'NEW_NAME2'
Пример #8
0
def test_edit_community(app, test_communities):
    with app.app_context():
        runner = CliRunner()
        script_info = ScriptInfo(create_app=lambda info:app)
        result = runner.invoke(
            communities_cmd,
            ["edit",
                "cccccccc-1111-1111-1111-111111111111",
                "--name",
                "NEW_NAME2"],
            obj=script_info)
        result = runner.invoke(communities_cmd, ["list"], obj=script_info)
        retrieved_comm = get_community_by_name_or_id("NEW_NAME2")
        assert retrieved_comm.name=='NEW_NAME2'
Пример #9
0
def test_create_community(app, test_communities):
    with app.app_context():
        runner = CliRunner()
        script_info = ScriptInfo(create_app=lambda info:app)
        result = runner.invoke(
            communities_cmd,
            ["create",
                "COMM_NAME",
                "Description comm",
                "bbmri.png"],
            obj=script_info)
        assert result.exit_code == 0
        retrieved_comm = get_community_by_name_or_id("COMM_NAME")
        assert retrieved_comm.name=='COMM_NAME'
Пример #10
0
    def get_all_community_schemas(cls, community_id=None):
        try:
            comm = get_community_by_name_or_id(community_id)
        except:
            # allow no community id
            pass
        from .models import CommunitySchemaVersion as CommunitySchemaModel
        try:
            filters = []
            if community_id is not None:
                filters.append(CommunitySchemaModel.community == comm.id)

            return [
                cls(model) for model in CommunitySchemaModel.query.filter(
                    *filters).order_by(CommunitySchemaModel.released).all()
            ]
        except NoResultFound as e:
            raise CommunitySchemaDoesNotExistError() from e
Пример #11
0
def block_schema_add(verbose, community, name):
    """Adds a block schema to the database. Community is the ID or NAME of the
    maintaining community for this block schema. Name is the name as displayed
    in block_schema_list command."""
    if verbose:
        click.secho("""Creating block schema with name %s to be maintained by
        community %s""" % (name, community))
    comm = get_community_by_name_or_id(community)
    if not comm:
        raise click.BadParameter(
            "There is no community by this name or ID: %s" % community)
    if len(name) > 255:
        raise click.BadParameter("""NAME parameter is longer than the 255
        character maximum""")
    block_schema = BlockSchema.create_block_schema(comm.id, name)
    db.session.commit()
    if verbose:
        click.secho("Created block schema with name %s and id %s" %
                    (name, block_schema.id))
Пример #12
0
def community_schema_list_block_schema_versions(verbose,
                                                community,
                                                version=None):
    """Show the block schema versions in the community schema element of a
        specific community"""
    comm = get_community_by_name_or_id(community)
    if not comm:
        raise click.BadParameter(
            "There is no community by this name or ID: %s" % community)
    community_schema = CommunitySchema.get_community_schema(comm.id, version)
    if not community_schema:
        raise click.ClickException("""Community %s does not have a community
            schema""" % comm.name)
    community_schema_dict = json.loads(community_schema.community_schema)
    props = community_schema_dict['properties']
    click.secho("""The following block schema versions are listed for community
        %s, community schema version %s""" %
                (comm.name, "latest %d" %
                 community_schema.version if not version else version))
    for key in props:
        click.secho("Block schema: %s, version url: %s" %
                    (key, props[key]['$ref']))