Ejemplo n.º 1
0
def add_collection_user_access_view(request):
    """
    Add user access role for this collection
    :param request:
    :return:
    """

    collection_id = request.matchdict['id']
    user_id = request.json_body['user_id']
    role_name = request.json_body['role']
    role = session.query(Role).filter(Role.name == role_name).first()
    user_access = create(UserCollection,
                         user_id=user_id,
                         collection_id=collection_id,
                         role_id=role.id)

    # add access to dashboards in collection
    dashboards = session.query(
        Dashboard.id).filter(Dashboard.collection_id == collection_id).all()
    for dash in dashboards:
        if not session.query(UserDashboard).filter_by(dashboard_id=dash.id,
                                                      user_id=user_id).count():
            create(UserDashboard,
                   user_id=user_id,
                   dashboard_id=dash.id,
                   role_id=role.id)

    return user_access.as_dict()
Ejemplo n.º 2
0
def create_chart_view(request):
    """
    Create Chart
    :param request:
    :return Chart:
    """

    data = get_values(request, attrs, required_attrs)
    group_by = request.json_body.get('group_by', [])

    if data.get('decimals') is None:
        data['decimals'] = 2
    if data.get('index') is None:
        prev = session.query(Chart).filter_by(
            dashboard_id=data['dashboard_id']).order_by(
                Chart.index.desc()).first()
        index = 0
        if prev:
            index = prev.index + 1
        data['index'] = index

    chart = create(Chart, **data)
    for group in group_by:
        group['chart_id'] = chart.id
        create(ChartGroupBy, **group)

    return chart.as_dict()
Ejemplo n.º 3
0
def create_single_stat_view(request):
    """
    Create SingleStat
    :param request:
    :return SingleStat:
    """

    data = get_values(request, attrs, required_attrs)

    thresholds = request.json_body.get('thresholds')
    colors = request.json_body.get('colors')
    if not thresholds:
        if len(colors.split(',')) != 1:
            raise exc.HTTPBadRequest(
                json_body={'message': 'Need one more color than thresholds'})
    elif len(thresholds.split(',')) != len(colors.split(',')) - 1:
        raise exc.HTTPBadRequest(
            json_body={'message': 'Need one more color than thresholds'})
    if data.get('decimals') is None:
        data['decimals'] = 0
    if data.get('format') is None:
        data['format'] = ''

    stat = create(SingleStat, **data)
    return stat.as_dict()
Ejemplo n.º 4
0
def create_collection(request):
    """
    Create a Collection
    :param request:
    :return Collection:
    """
    data = get_values(request, attrs, required_attrs)
    collection = create(Collection, **data)

    # add logged in user to collection with admin role
    admin_role = session.query(Role).filter_by(name=Administrator).first()
    create(UserCollection,
           user_id=request.authenticated_userid,
           collection_id=collection.id,
           role_id=admin_role.id)

    return collection.as_dict(request.authenticated_userid)
Ejemplo n.º 5
0
def create_data_source_view(request):
    """
    Create a DataSource
    :param request:
    :return DataSource:
    """
    data = get_values(request, attrs, required_attrs)
    data_source = create(DataSource, **data)
    return data_source.as_dict()
Ejemplo n.º 6
0
def paste_chart_view(request):
    """
    Paste a chart view
    :param request:
    :return Chart:
    """

    chart = get(request, Chart, as_dict=False)
    data = {attr: getattr(chart, attr) for attr in attrs}

    pasted_chart = paste(request, Chart, data, 'dashboard_id', 'title')

    for group in chart.group_by:
        create(ChartGroupBy,
               chart_id=pasted_chart.id,
               dashboard_view_id=group.dashboard_view_id,
               value=group.value)

    return pasted_chart.as_dict()
Ejemplo n.º 7
0
def create_dashboard_view(request):
    """
    Create Dashboard
    :param request:
    :return Dashboard:
    """

    data = get_values(request, attrs, required_attrs)
    collection_id = request.json_body.get('collection_id')

    if data.get('index') is None and collection_id:
        result = session.query(func.count(Dashboard.id)).filter(
            Dashboard.collection_id == collection_id).first()
        data['index'] = result[0]

    dashboard = create(Dashboard, **data)

    # when creating a dashboard in a collection copy the user access from the collection
    user_access = []
    if collection_id:
        user_access = session.query(UserCollection).filter(
            UserCollection.collection_id == collection_id).all()
        for access in user_access:
            create(UserDashboard,
                   user_id=access.user_id,
                   dashboard_id=dashboard.id,
                   role_id=access.role_id)

    if not [
            access for access in user_access
            if access.user_id == request.authenticated_userid
    ]:
        # add logged in user to dashboard with admin role
        admin_role = session.query(Role).filter_by(name=Administrator).first()
        create(UserDashboard,
               user_id=request.authenticated_userid,
               dashboard_id=dashboard.id,
               role_id=admin_role.id)

    return dashboard.as_dict(request.authenticated_userid)
Ejemplo n.º 8
0
def create_query_view(request):
    """
    Create Query
    :param request:
    :return Query:
    """

    data = get_values(request, attrs, required_attrs)
    existing = session.query(Query).filter_by(name=data['name'], dashboard_id=data['dashboard_id']).first()
    if existing:
        raise exc.HTTPBadRequest(json_body={'message': 'Query %s already exists for this dashboard' % data['name']})
    query = create(Query, **data)

    return query.as_dict()
Ejemplo n.º 9
0
def paste_collection_view(request):
    """
    Paste a Collection view
    :param request:
    :return Collection:
    """
    collection = get(request, Collection, as_dict=False)
    data = {attr: getattr(collection, attr) for attr in attrs}

    pasted_collection = paste(request, Collection, data, None, 'title')

    # copy list of users
    user_access = session.query(UserCollection).filter(
        UserCollection.collection_id == collection.id).all()
    for access in user_access:
        create(UserCollection,
               user_id=access.user_id,
               collection_id=pasted_collection.id,
               role_id=access.role_id)

    # copy list of variables
    for variable in collection.variables:
        create(Variable,
               name=variable.name,
               value=variable.value,
               container_id=pasted_collection.id)

    # copy dashboards in collection
    for dashboard in collection.dashboards:
        data = {attr: getattr(dashboard, attr) for attr in dashboard_attrs}
        data['collection_id'] = pasted_collection.id
        data['index'] = dashboard.index
        pasted_dashboard = create(Dashboard, **data)

        _paste_dashboard_relationships(dashboard, pasted_dashboard)

    return pasted_collection.as_dict()
Ejemplo n.º 10
0
def add_dashboard_user_access_view(request):
    """
    Add user access role for this dashboard
    :param request:
    :return:
    """

    dashboard_id = request.matchdict['id']
    user_id = request.json_body['user_id']
    role_name = request.json_body['role']
    role = session.query(Role).filter(Role.name == role_name).first()
    user_access = create(UserDashboard,
                         user_id=user_id,
                         dashboard_id=dashboard_id,
                         role_id=role.id)

    return user_access.as_dict()
Ejemplo n.º 11
0
def create_variable_view(request):
    """
    Create a DataSource
    :param request:
    :return DataSource:
    """
    name = request.json_body.get('name')
    container_id = request.json_body.get('container_id')

    # check duplicates
    existing = session.query(Variable).filter_by(
        name=name, container_id=container_id).first()
    if existing:
        raise exc.HTTPBadRequest(
            json_body={
                'message': 'Variable with same id %s already exists' % name
            })

    data = get_values(request, attrs, required_attrs)
    data_source = create(Variable, **data)
    return data_source.as_dict()
Ejemplo n.º 12
0
def create_user(request):
    """
    Create User
    :param request:
    :return User:
    """

    data = get_values(request, attrs, required_attrs)
    role = request.json_body.get('role')
    password = request.json_body.get('password')
    if not password:
        raise exc.HTTPBadRequest(json_body={'message': 'Password required'})

    user = create(User, **data)

    user.set_password(password)
    user_role = session.query(Role).filter(Role.name == role).first()
    user.roles.append(user_role)
    session.add(user)
    session.flush()

    return user.as_dict()
Ejemplo n.º 13
0
def _paste_dashboard_relationships(dashboard, pasted_dashboard):
    """ Copy all relationships when pasting new dashboard """
    # copy list of users
    user_access = session.query(UserDashboard).filter(
        UserDashboard.dashboard_id == dashboard.id).all()
    for access in user_access:
        create(UserDashboard,
               user_id=access.user_id,
               dashboard_id=pasted_dashboard.id,
               role_id=access.role_id)

    # copy list of variables
    for variable in dashboard.variables:
        create(Variable,
               name=variable.name,
               value=variable.value,
               container_id=pasted_dashboard.id)

    # copy charts
    for chart in session.query(Chart).filter(
            Chart.dashboard_id == dashboard.id).all():
        data = {attr: getattr(chart, attr) for attr in chart_attrs}
        data['dashboard_id'] = pasted_dashboard.id
        data['index'] = chart.index
        pasted_chart = create(Chart, **data)

        for group in chart.group_by:
            create(ChartGroupBy,
                   chart_id=pasted_chart.id,
                   dashboard_view_id=group.dashboard_view_id,
                   value=group.value)

    # copy single stats
    for stat in session.query(SingleStat).filter(
            SingleStat.dashboard_id == dashboard.id).all():
        create(SingleStat,
               title=stat.title,
               query=stat.query,
               decimals=stat.decimals,
               format=stat.format,
               thresholds=stat.thresholds,
               colors=stat.colors,
               dashboard_id=pasted_dashboard.id,
               data_source_id=stat.data_source_id)

    # copy queries
    for query in session.query(Query).filter(
            Query.dashboard_id == dashboard.id).all():
        data = {attr: getattr(query, attr) for attr in query_attrs}
        data['dashboard_id'] = pasted_dashboard.id
        create(Query, **data)