Пример #1
0
def handle_release_transaction(program, project, **tx_kwargs):
    """
    Create and execute a single transaction.

    Return:
        Tuple[flask.Response, int]: (API response json, status code)
    """
    is_async = tx_kwargs.pop("is_async", utils.is_flag_set(FLAG_IS_ASYNC))
    db_driver = tx_kwargs.pop("db_driver", flask.current_app.db)

    transaction = ReleaseTransaction(
        program=program,
        project=project,
        logger=flask.current_app.logger,
        index_client=flask.current_app.index_client,
        db_driver=db_driver,
        **tx_kwargs)

    if is_async:
        session = transaction.db_driver.session_scope()
        with session, transaction:
            response = {
                "code": 200,
                "message": "Transaction submitted.",
                "transaction_id": transaction.transaction_id,
            }
        flask.current_app.async_pool.schedule(transaction_worker, transaction)
        return flask.jsonify(response), 200
    else:
        response, code = transaction_worker(transaction)
        return flask.jsonify(response), code
Пример #2
0
def handle_submission_transaction(program, project, *doc_args, **tx_kwargs):
    """
    Create and execute a single (not bulk) transaction.

    Return:
        Tuple[flask.Response, int]: (API response json, status code)
    """
    is_async = tx_kwargs.pop('is_async', utils.is_flag_set(FLAG_IS_ASYNC))
    db_driver = tx_kwargs.pop('db_driver', flask.current_app.db)

    transaction = SubmissionTransaction(program=program,
                                        project=project,
                                        user=flask.g.user,
                                        logger=flask.current_app.logger,
                                        signpost=flask.current_app.signpost,
                                        db_driver=db_driver,
                                        **tx_kwargs)

    if is_async:
        session = transaction.db_driver.session_scope()
        with session, transaction:
            response = {
                "code": 200,
                "message": "Transaction submitted.",
                "transaction_id": transaction.transaction_id,
            }
        flask.current_app.async_pool.schedule(transaction_worker, transaction)
        return flask.jsonify(response), 200
    else:
        response, code = transaction_worker(transaction)
        return flask.jsonify(response), code
Пример #3
0
def handle_deletion_request(program, project, ids, **tx_kwargs):
    """
    Create and execute a single deletion transaction.

    Return:
        Tuple[flask.Response, int]: (API json response, status code)
    """
    is_async = tx_kwargs.pop('is_async', utils.is_flag_set(FLAG_IS_ASYNC))
    db_driver = tx_kwargs.pop('db_driver', flask.current_app.db)

    transaction = DeletionTransaction(program=program,
                                      project=project,
                                      user=flask.g.user,
                                      logger=flask.current_app.logger,
                                      signpost=flask.current_app.signpost,
                                      db_driver=db_driver,
                                      **tx_kwargs)

    if is_async:
        session = transaction.db_driver.session_scope()
        with session, transaction:
            response = {
                'code': 200,
                'message': 'Transaction submitted.',
                'transaction_id': transaction.transaction_id,
            }

        flask.current_app.async_pool.schedule(transaction_worker, transaction,
                                              ids)
        return flask.jsonify(response), 200

    else:
        response, code = transaction_worker(transaction, ids)
        return flask.jsonify(response), code
Пример #4
0
def _single_transaction(tx_cls, program, project, **tx_kwargs):
    """
    Create and execute a single (not bulk) transaction.

    Args:
        tx_cls: The transaction class

    Return:
        Tuple[flask.Response, int]: (API response json, status code)
    """
    is_async = tx_kwargs.pop('is_async', utils.is_flag_set(FLAG_IS_ASYNC))
    db_driver = tx_kwargs.pop('db_driver', flask.current_app.db)

    transaction = tx_cls(program=program,
                         project=project,
                         logger=flask.current_app.logger,
                         signpost=flask.current_app.signpost,
                         db_driver=db_driver,
                         **tx_kwargs)

    if is_async:
        session = transaction.db_driver.session_scope()
        with session, transaction:
            response = {
                'code': 200,
                'message': 'Transaction submitted.',
                'transaction_id': transaction.transaction_id,
            }
        flask.current_app.async_pool.schedule(transaction_worker, transaction)
        return flask.jsonify(response), 200
    else:
        response, code = transaction_worker(transaction)
        return flask.jsonify(response), code
Пример #5
0
def handle_deletion_request(program,
                            project,
                            ids,
                            to_delete=None,
                            **tx_kwargs):
    """Create and execute a single deletion transaction.

    A user with administrator privileges can mark the sysan of
    the nodes as to_delete=True/False. The purpose of this is so
    esbuild will not use the nodes in creating Elastic Search indices.

    ex:
        /delete
        /to_delete/true
        /to_delete/false

    Args:
        program (string): program name
        project (string): project code
        ids (string): comma separated "list" of UUIDs to be deleted
        to_delete (bool): mark node with sysan['to_delete']=True/False
            if present
        tx_kwargs (dict): other transaction related variables

    Returns:
        flask.Response: API json response
        int: status code
    """

    is_async = tx_kwargs.pop("is_async", utils.is_flag_set(FLAG_IS_ASYNC))
    db_driver = tx_kwargs.pop("db_driver", flask.current_app.db)
    transaction = DeletionTransaction(
        program=program,
        project=project,
        logger=flask.current_app.logger,
        index_client=flask.current_app.index_client,
        to_delete=to_delete,
        db_driver=db_driver,
        **tx_kwargs)

    if is_async:
        session = transaction.db_driver.session_scope()
        with session, transaction:
            response = {
                "code": 200,
                "message": "Transaction submitted.",
                "transaction_id": transaction.transaction_id,
            }

        flask.current_app.async_pool.schedule(transaction_worker, transaction,
                                              ids)
        return flask.jsonify(response), 200

    else:
        response, code = transaction_worker(transaction, ids)
        return flask.jsonify(response), code
Пример #6
0
def handle_single_transaction(role, program, project, **tx_kwargs):
    """
    Main entry point for single file transactions.

    This function multiplexes on the content-type to call the appropriate
    transaction handler.
    """
    doc = flask.request.get_data().decode("utf-8")
    content_type = flask.request.headers.get("Content-Type", "").lower()
    if content_type == "text/csv":
        doc_format = "csv"
        data, errors = utils.transforms.CSVToJSONConverter().convert(doc)
    elif content_type in ["text/tab-separated-values", "text/tsv"]:
        doc_format = "tsv"
        data, errors = utils.transforms.TSVToJSONConverter().convert(doc)
    else:
        doc_format = "json"
        data = utils.parse.parse_request_json()
        errors = None
    # TODO: use errors value?
    name = flask.request.headers.get("X-Document-Name", None)
    doc_args = [name, doc_format, doc, data]
    is_async = tx_kwargs.pop("is_async", utils.is_flag_set(FLAG_IS_ASYNC))
    db_driver = tx_kwargs.pop("db_driver", flask.current_app.db)
    transaction = UploadTransaction(
        program=program,
        project=project,
        role=role,
        logger=flask.current_app.logger,
        flask_config=flask.current_app.config,
        index_client=flask.current_app.index_client,
        external_proxies=utils.get_external_proxies(),
        db_driver=db_driver,
        **tx_kwargs)
    if is_async:
        session = transaction.db_driver.session_scope(can_inherit=False)
        with session, transaction:
            response = {
                "code": 200,
                "message": "Transaction submitted.",
                "transaction_id": transaction.transaction_id,
            }
        flask.current_app.async_pool.schedule(single_transaction_worker,
                                              transaction, *doc_args)

        # create the resource in arborist
        auth.create_resource(program, project, doc_args[3])

        return flask.jsonify(response)
    else:
        response, code = single_transaction_worker(transaction, *doc_args)

        # create the resource in arborist
        auth.create_resource(program, project, doc_args[3])

        return flask.jsonify(response), code
Пример #7
0
def handle_single_transaction(role, program, project, **tx_kwargs):
    """
    Main entry point for single file transactions.

    This function multiplexes on the content-type to call the appropriate
    transaction handler.
    """
    doc = flask.request.get_data()
    content_type = flask.request.headers.get('Content-Type', '').lower()
    if content_type == 'text/csv':
        doc_format = 'csv'
        data, errors = utils.transforms.CSVToJSONConverter().convert(doc)
    elif content_type in ['text/tab-separated-values', 'text/tsv']:
        doc_format = 'tsv'
        data, errors = utils.transforms.TSVToJSONConverter().convert(doc)
    else:
        doc_format = 'json'
        data = utils.parse.parse_request_json()
        errors = None
    # TODO: use errors value?
    name = flask.request.headers.get('X-Document-Name', None)
    doc_args = [name, doc_format, doc, data]
    is_async = tx_kwargs.pop('is_async', utils.is_flag_set(FLAG_IS_ASYNC))
    db_driver = tx_kwargs.pop('db_driver', flask.current_app.db)
    transaction = UploadTransaction(program=program,
                                    project=project,
                                    role=role,
                                    user=flask.g.user,
                                    logger=flask.current_app.logger,
                                    flask_config=flask.current_app.config,
                                    signpost=flask.current_app.signpost,
                                    external_proxies=utils.get_external_proxies(),
                                    db_driver=db_driver,
                                    **tx_kwargs)
    if is_async:
        session = transaction.db_driver.session_scope(can_inherit=False)
        with session, transaction:
            response = {
                "code": 200,
                "message": "Transaction submitted.",
                "transaction_id": transaction.transaction_id,
            }
        flask.current_app.async_pool.schedule(
            single_transaction_worker, transaction, *doc_args
        )
        return flask.jsonify(response)
    else:
        response, code = single_transaction_worker(transaction, *doc_args)
        return flask.jsonify(response), code
Пример #8
0
def handle_bulk_transaction(role, program, project, **tx_kwargs):
    """
    TODO
    """
    wrappers = utils.parse.parse_request_json()
    # Assert wrapper is list of JSON objects
    invalid_format_msg = (
        'Bulk transfers must be an array of JSON objects of format: {\n'
        '    "name": string,\n'
        '    "doc_format": string,\n'
        '    "doc": string,\n'
        '}'
    )
    if not isinstance(wrappers, list):
        raise UserError(invalid_format_msg)

    for wrapper in wrappers:
        if not isinstance(wrapper, dict):
            raise UserError(invalid_format_msg)

    is_async = tx_kwargs.pop('is_async', utils.is_flag_set(FLAG_IS_ASYNC))

    transaction = BulkUploadTransaction(
        program=program,
        project=project,
        role=role,
        user=flask.g.user,
        logger=flask.current_app.logger,
        signpost=flask.current_app.signpost,
        db_driver=flask.current_app.db,
        external_proxies=utils.get_external_proxies(),
        **tx_kwargs
    )

    if is_async:
        session = transaction.db_driver.session_scope(can_inherit=False)
        with session, transaction:
            response = {
                "code": 200,
                "message": "Transaction submitted.",
                "transaction_id": transaction.transaction_id,
            }
        flask.current_app.async_pool.schedule(
            bulk_transaction_worker, transaction, wrappers
        )
        return flask.jsonify(response)
    else:
        response, code = bulk_transaction_worker(transaction, wrappers)
        return flask.jsonify(response), code
Пример #9
0
def _single_transaction(role, program, project, *doc_args, **tx_kwargs):
    """
    Create and execute a single (not bulk) transaction.

    Most non-bulk variations of upload actions (based on content-type,
    etc.) should wrap this function.

    Return:
        Tuple[flask.Response, int]: (API response json, status code)
    """
    is_async = tx_kwargs.pop('is_async', utils.is_flag_set(FLAG_IS_ASYNC))
    db_driver = tx_kwargs.pop('db_driver', flask.current_app.db)

    transaction = UploadTransaction(
        program=program,
        project=project,
        role=role,
        user=flask.g.user,
        logger=flask.current_app.logger,
        signpost=flask.current_app.signpost,
        flask_config=flask.current_app.config,
        db_driver=db_driver,
        external_proxies=utils.get_external_proxies(),
        **tx_kwargs
    )

    if is_async:
        session = transaction.db_driver.session_scope(can_inherit=False)
        with session, transaction:
            response = {
                "code": 200,
                "message": "Transaction submitted.",
                "transaction_id": transaction.transaction_id,
            }
        flask.current_app.async_pool.schedule(
            single_transaction_worker, transaction, *doc_args
        )
        return flask.jsonify(response)
    else:
        response, code = single_transaction_worker(transaction, *doc_args)
        return flask.jsonify(response), code
Пример #10
0
def handle_submission_transaction(program, project, *doc_args, **tx_kwargs):
    """
    Create and execute a single (not bulk) transaction.

    Return:
        Tuple[flask.Response, int]: (API response json, status code)
    """
    is_async = tx_kwargs.pop("is_async", utils.is_flag_set(FLAG_IS_ASYNC))
    db_driver = tx_kwargs.pop("db_driver", flask.current_app.db)

    smtp_conf = None
    if utils.should_send_email(flask.current_app.config):
        smtp_conf = flask.current_app.get_smtp_conf()

    transaction = SubmissionTransaction(
        smtp_conf=smtp_conf,
        program=program,
        project=project,
        logger=flask.current_app.logger,
        index_client=flask.current_app.index_client,
        db_driver=db_driver,
        **tx_kwargs
    )

    if is_async:
        session = transaction.db_driver.session_scope()
        with session, transaction:
            response = {
                "code": 200,
                "message": "Transaction submitted.",
                "transaction_id": transaction.transaction_id,
            }
        flask.current_app.async_pool.schedule(transaction_worker, transaction)
        return flask.jsonify(response), 200
    else:
        response, code = transaction_worker(transaction)
        return flask.jsonify(response), code