Beispiel #1
0
def root_create():
    """
    /
    PUT POST PATCH

    Register a program.

    The content of the request is a JSON containing the information
    describing a program.  Authorization for registering programs is
    limited to administrative users.

    :reqheader Content-Type:
        |reqheader_Content-Type|
    :reqheader Accept:
        |reqheader_Accept|
    :reqheader X-Auth-Token:
        |reqheader_X-Auth-Token|
    :resheader Content-Type:
        |resheader_Content-Type|
    :statuscode 200: Registered successfully.
    :statuscode 404: Program not found.
    :statuscode 403: Unauthorized request.

    **Example:**

    .. code-block:: http

           POST /v0/submission/CGCI/ HTTP/1.1
           Host: example.com
           Content-Type: application/json
           X-Auth-Token: MIIDKgYJKoZIhvcNAQcC...
           Accept: application/json

    .. code-block:: JavaScript

        {
            "type": "program",
            "name": "CGCI",
            "dbgap_accession_number": "phs000178"
        }
    """
    auth.admin_auth()
    message = None
    node_id = None
    doc = parse.parse_request_json()
    if not isinstance(doc, dict):
        raise UserError('Root endpoint only supports single documents')
    if doc.get('type') != 'program':
        raise UserError("Invalid type in key type='{}'".format(
            doc.get('type')))
    phsid = doc.get('dbgap_accession_number')
    program = doc.get('name')
    if not program:
        raise UserError("No program specified in key 'name'")

    with current_app.db.session_scope(can_inherit=False) as session:
        node = (current_app.db.nodes(
            models.Program).props(name=program).scalar())
        if node:
            message = 'Program already exists.'
            node_id = node.node_id
        else:
            node_id = str(uuid.uuid5(PROGRAM_SEED, program.encode('utf-8')))
            session.add(
                models.Program(  # pylint: disable=not-callable
                    node_id,
                    name=program,
                    dbgap_accession_number=phsid))
            message = 'Program registered.'

    return flask.jsonify({
        'id': node_id,
        'name': program,
        'message': message,
    })
Beispiel #2
0
def root_create():
    """
    Register a program.

    The content of the request is a JSON containing the information
    describing a program.  Authorization for registering programs is
    limited to administrative users.

    Summary:
        Create a program

    Tags:
        program

    Args:
        body (schema_program): input body

    Responses:
        200: Registered successfully.
        403: Unauthorized request.

    :reqheader Content-Type:
        |reqheader_Content-Type|
    :reqheader Accept:
        |reqheader_Accept|
    :reqheader X-Auth-Token:
        |reqheader_X-Auth-Token|
    :resheader Content-Type:
        |resheader_Content-Type|

    **Example:**

    .. code-block:: http

           POST /v0/submission/CGCI/ HTTP/1.1
           Host: example.com
           Content-Type: application/json
           X-Auth-Token: MIIDKgYJKoZIhvcNAQcC...
           Accept: application/json

    .. code-block:: JavaScript

        {
            "type": "program",
            "name": "CGCI",
            "dbgap_accession_number": "phs000178"
        }
    """
    message = None
    node_id = None
    doc = parse.parse_request_json()
    if not isinstance(doc, dict):
        raise UserError(
            "The program creation endpoint only supports single documents (dict). Received data of type {}"
            .format(type(doc)))
    if doc.get("type") != "program":
        raise UserError("Invalid type in key type='{}'".format(
            doc.get("type")))
    phsid = doc.get("dbgap_accession_number")
    program = doc.get("name")
    if not program:
        raise UserError("No program specified in key 'name'")

    # create the resource in sheepdog DB
    with current_app.db.session_scope(can_inherit=False) as session:
        node = current_app.db.nodes(
            models.Program).props(name=program).scalar()
        if node:
            message = "Program is updated!"
            node_id = node.node_id
            node.props["dbgap_accession_number"] = phsid
        else:
            node_id = str(uuid.uuid5(PROGRAM_SEED, program))
            session.add(
                models.Program(  # pylint: disable=not-callable
                    node_id,
                    name=program,
                    dbgap_accession_number=phsid))
            message = "Program registered."

    # create the resource in arborist
    auth.create_resource(phsid)

    return flask.jsonify({"id": node_id, "name": program, "message": message})
Beispiel #3
0
def create_program():
    """
    Register a program.

    The content of the request is a JSON containing the information
    describing a program.  Authorization for registering programs is
    limited to administrative users.

    Summary:
        Create a program

    Tags:
        program

    Args:
        body (schema_program): input body

    Responses:
        200: Registered successfully.
        403: Unauthorized request.

    :reqheader Content-Type:
        |reqheader_Content-Type|
    :reqheader Accept:
        |reqheader_Accept|
    :reqheader X-Auth-Token:
        |reqheader_X-Auth-Token|
    :resheader Content-Type:
        |resheader_Content-Type|

    **Example:**

    .. code-block:: http

           POST /v0/submission/CGCI/ HTTP/1.1
           Host: example.com
           Content-Type: application/json
           X-Auth-Token: MIIDKgYJKoZIhvcNAQcC...
           Accept: application/json

    .. code-block:: JavaScript

        {
            "type": "program",
            "name": "CGCI",
            "dbgap_accession_number": "phs000178"
        }
    """
    input_doc = flask.request.get_data().decode("utf-8")
    content_type = flask.request.headers.get("Content-Type", "").lower()
    errors = None
    if content_type == "text/csv":
        doc, errors = utils.transforms.CSVToJSONConverter().convert(input_doc)
    elif content_type in ["text/tab-separated-values", "text/tsv"]:
        doc, errors = utils.transforms.TSVToJSONConverter().convert(input_doc)
    else:
        doc = utils.parse.parse_request_json()

    if errors:
        raise UserError("Unable to parse doc '{}': {}".format(
            input_doc, errors))

    if isinstance(doc, list) and len(doc) == 1:
        # handle TSV/CSV submissions that are parsed as lists of 1 element
        doc = doc[0]
    if not isinstance(doc, dict):
        raise UserError(
            "The program creation endpoint only supports single documents (dict). Received data of type {}: {}"
            .format(type(doc), doc))
    if doc.get("type") != "program":
        raise UserError("Invalid type in key type='{}'".format(
            doc.get("type")))
    phsid = doc.get("dbgap_accession_number")
    program = doc.get("name")
    if not program:
        raise UserError("No program specified in key 'name'")

    # create the resource in sheepdog DB
    message = None
    node_id = None
    with current_app.db.session_scope(can_inherit=False) as session:
        node = current_app.db.nodes(
            models.Program).props(name=program).scalar()
        if node:
            message = "Program is updated!"
            node_id = node.node_id
            node.props["dbgap_accession_number"] = phsid
        else:
            node_id = str(uuid.uuid5(PROGRAM_SEED, program))
            session.add(
                models.Program(  # pylint: disable=not-callable
                    node_id,
                    name=program,
                    dbgap_accession_number=phsid))
            message = "Program registered."

    # create the resource in arborist
    auth.create_resource(phsid)

    return flask.jsonify({"id": node_id, "name": program, "message": message})