def upload_component_file(user, c_id):
    COMPONENT_FILES = models.COMPONENT_FILES

    component = v1_utils.verify_existence_and_get(c_id, _TABLE)
    if str(component['topic_id']) not in v1_utils.user_topic_ids(user):
        raise dci_exc.Unauthorized()

    store = dci_config.get_store('components')

    file_id = utils.gen_uuid()
    file_path = files_utils.build_file_path(component['topic_id'],
                                            c_id,
                                            file_id)
    content = files_utils.get_stream_or_content_from_request(flask.request)
    store.upload(file_path, content)
    s_file = store.head(file_path)

    values = dict.fromkeys(['md5', 'mime', 'component_id', 'name'])

    values.update({
        'id': file_id,
        'component_id': c_id,
        'name': file_id,
        'created_at': datetime.datetime.utcnow().isoformat(),
        'md5': s_file['etag'],
        'mime': s_file['content-type'],
        'size': s_file['content-length']
    })

    query = COMPONENT_FILES.insert().values(**values)

    flask.g.db_conn.execute(query)
    result = json.dumps({'component_file': values})
    return flask.Response(result, 201, content_type='application/json')
def delete_component_by_id(user, c_id):
    component = v1_utils.verify_existence_and_get(c_id, _TABLE)

    if str(component['topic_id']) not in v1_utils.user_topic_ids(user):
        raise dci_exc.Unauthorized()

    values = {'state': 'archived'}
    where_clause = sql.and_(_TABLE.c.id == c_id)
    query = _TABLE.update().where(where_clause).values(**values)

    result = flask.g.db_conn.execute(query)

    if not result.rowcount:
        raise dci_exc.DCIDeleteConflict('Component', c_id)

    return flask.Response(None, 204, content_type='application/json')
def create_components(user):
    values = flask.request.json
    check_json_is_valid(create_component_schema, values)
    values.update(v1_utils.common_values_dict())

    if str(values['topic_id']) not in v1_utils.user_topic_ids(user):
        raise dci_exc.Unauthorized()

    query = _TABLE.insert().values(**values)

    try:
        flask.g.db_conn.execute(query)
    except sa_exc.IntegrityError:
        raise dci_exc.DCICreationConflict(_TABLE.name, 'name')

    result = json.dumps({'component': values})
    return flask.Response(result, 201, content_type='application/json')
Example #4
0
def get_all_topics(user):
    def _get_user_product_ids():
        _JPT = models.JOIN_PRODUCTS_TEAMS
        query = v1_utils.QueryBuilder(
            models.PRODUCTS,
            {},
            {},
            root_join_table=_JPT,
            root_join_condition=sql.and_(
                _JPT.c.product_id == models.PRODUCTS.c.id,  # noqa
                _JPT.c.team_id.in_(user.teams_ids)))  # noqa
        user_products = query.execute(fetchall=True)
        return [up['products_id'] for up in user_products]

    args = check_and_get_args(flask.request.args.to_dict())
    # if the user is an admin then he can get all the topics
    q_get_topics = v1_utils.QueryBuilder(_TABLE, args, _T_COLUMNS)

    if (user.is_not_super_admin() and user.is_not_read_only_user()
            and user.is_not_epm()):
        if 'teams' in args['embed']:
            raise dci_exc.DCIException('embed=teams not authorized.',
                                       status_code=401)
        # if regular user, retrieve the topics associated to the user's team
        # get only topics with export_control==True or with explicit association
        # between the user's teams and the topic
        product_ids = _get_user_product_ids()
        q_get_topics.add_extra_condition(_TABLE.c.product_id.in_(product_ids))
        q_get_topics.add_extra_condition(
            sql.or_(
                _TABLE.c.export_control == True,  # noqa
                _TABLE.c.id.in_(v1_utils.user_topic_ids(user))))  # noqa

    q_get_topics.add_extra_condition(_TABLE.c.state != 'archived')

    # get the number of rows for the '_meta' section
    nb_rows = q_get_topics.get_number_of_rows()
    rows = q_get_topics.execute(fetchall=True)
    rows = v1_utils.format_result(rows, _TABLE.name, args['embed'],
                                  _EMBED_MANY)

    return flask.jsonify({'topics': rows, '_meta': {'count': nb_rows}})
Example #5
0
def get_all_topics(user):
    args = schemas.args(flask.request.args.to_dict())
    # if the user is an admin then he can get all the topics
    query = v1_utils.QueryBuilder(_TABLE, args, _T_COLUMNS)

    if not auth.is_admin(user):
        if 'teams' in args['embed']:
            raise dci_exc.DCIException('embed=teams not authorized.',
                                       status_code=401)
        query.add_extra_condition(
            _TABLE.c.id.in_(v1_utils.user_topic_ids(user)))  # noqa
    query.add_extra_condition(_TABLE.c.state != 'archived')

    # get the number of rows for the '_meta' section
    nb_rows = query.get_number_of_rows()
    rows = query.execute(fetchall=True)
    rows = v1_utils.format_result(rows, _TABLE.name, args['embed'],
                                  _EMBED_MANY)

    return flask.jsonify({'topics': rows, '_meta': {'count': nb_rows}})
def delete_component_file(user, c_id, f_id):
    COMPONENT_FILES = models.COMPONENT_FILES
    component = v1_utils.verify_existence_and_get(c_id, _TABLE)
    if str(component['topic_id']) not in v1_utils.user_topic_ids(user):
        raise dci_exc.Unauthorized()
    v1_utils.verify_existence_and_get(f_id, COMPONENT_FILES)

    where_clause = COMPONENT_FILES.c.id == f_id

    query = COMPONENT_FILES.delete().where(where_clause)

    result = flask.g.db_conn.execute(query)

    if not result.rowcount:
        raise dci_exc.DCIDeleteConflict('Component File', f_id)

    store = dci_config.get_store('components')
    file_path = files_utils.build_file_path(component['topic_id'], c_id, f_id)
    store.delete(file_path)

    return flask.Response(None, 204, content_type='application/json')
Example #7
0
def get_all_jobdefinitions(user):
    topic_ids = v1_utils.user_topic_ids(user)
    return list_jobdefinitions(user, topic_ids, by_topic=False)