Esempio n. 1
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()
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()
Esempio n. 3
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()
def edit_collection(request):
    """
    Edit collection
    :param request:
    :return:
    """
    collection = get(request, Collection, as_dict=False)
    data = get_values(request, attrs, required_attrs)
    edit(collection, **data)

    return collection.as_dict(request.authenticated_userid)
Esempio n. 5
0
def edit_data_source_view(request):
    """
    Edit data_source
    :param request:
    :return:
    """
    data_source = get(request, DataSource, as_dict=False)
    data = get_values(request, attrs, required_attrs)
    edit(data_source, **data)

    return data_source.as_dict()
Esempio n. 6
0
def edit_query_view(request):
    """
    Edit query
    :param request:
    :return:
    """

    query = get(request, Query, as_dict=False)
    data = get_values(request, attrs, required_attrs)
    edit(query, **data)
    return query.as_dict()
Esempio n. 7
0
def reorder_chart_view(request):
    """
    Delete a chart view
    :param request:
    :return:
    """
    chart = get(request, Chart, as_dict=False)
    data = get_values(request, ['index'], ['index'])
    dashboard_charts = session.query(Chart).filter(
        chart.dashboard_id == chart.dashboard_id)
    reorder(data.get('index'), chart, Chart, dashboard_charts)
    return {}
Esempio n. 8
0
def edit_dashboard_view(request):
    """
    Edit dashboard
    :param request:
    :return:
    """

    dashboard = get(request, Dashboard, as_dict=False)

    data = get_values(request, attrs, required_attrs)
    edit(dashboard, **data)

    return dashboard.as_dict(request.authenticated_userid)
Esempio n. 9
0
def reorder_dashboard_view(request):
    """
    Reorder a dashboard in a list view
    :param request:
    :return Dashboard:
    """

    dashboard = get(request, Dashboard, as_dict=False)
    data = get_values(request, ['index'], ['index'])
    collection_dashboards = session.query(Dashboard).filter(
        Dashboard.collection_id == dashboard.collection_id)
    reorder(data.get('index'), dashboard, Dashboard, collection_dashboards)
    return {}
Esempio n. 10
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()
Esempio n. 11
0
def edit_user(request):
    """
    Edit user
    :param request:
    :return:
    """

    user_id = request.matchdict.get('id')
    email = request.json_body.get('email')
    telephone = request.json_body.get('telephone')
    password = request.json_body.get('password')
    role = request.json_body.get('role')

    user = get(request, User, as_dict=False)
    data = get_values(request, attrs, required_attrs)

    # only admin or owner of user profile can edit profile
    if user_id != request.authenticated_userid:
        admin_user = get_user(user_id=request.authenticated_userid, role=Administrator)
        if not admin_user:
            raise exc.HTTPForbidden()

    if telephone:
        usr = get_user(telephone=telephone)
        if usr and (usr.id != user_id):
            raise exc.HTTPBadRequest(json_body={'message': 'User with telephone number %s already exists' % telephone})

    if email:
        usr = get_user(email=email)
        if usr and (usr.id != user_id):
            raise exc.HTTPBadRequest(json_body={'message': 'User with email %s already exists' % email})
    if not request.json_body.get('role'):
        raise exc.HTTPBadRequest(json_body={'message': 'User role is required'})
    if request.json_body.get('confirmed_registration') is None:
        data['confirmed_registration'] = False

    edit(user, **data)

    if password:
        user.set_password(password)

    # update user role
    existing_role = session.query(Role).filter(Role.name == role).first()
    user_role = session.query(UserRole).filter(UserRole.user_id == user_id, UserRole.role_id == existing_role.id).first()
    if not user_role:
        user.roles = []
        user.roles.append(existing_role)

    return user.as_dict()
Esempio n. 12
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)
Esempio n. 13
0
def edit_chart(request):
    """
    Edit chart
    :param request:
    :return:
    """
    chart = get(request, Chart, as_dict=False)
    data = get_values(request, attrs, required_attrs)
    edit(chart, **data)

    group_by = request.json_body.get('group_by', [])
    for group in chart.group_by:
        for data in group_by:
            if data['dashboard_view_id'] == group.dashboard_view_id:
                group.value = data['value']
                break

    return chart.as_dict()
Esempio n. 14
0
def edit_single_stat_view(request):
    """
    Edit single_stat
    :param request:
    :return:
    """

    single_stat = get(request, SingleStat, as_dict=False)
    thresholds = request.json_body.get('thresholds', '')
    colors = request.json_body.get('colors', '')

    # check threshold and colors values
    if len(thresholds.split(',')) != len(colors.split(',')) - 1:
        raise exc.HTTPBadRequest(
            json_body={'message': 'Need one more color than thresholds'})

    data = get_values(request, attrs, required_attrs)
    edit(single_stat, **data)

    return single_stat.as_dict()
Esempio n. 15
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()
Esempio n. 16
0
def edit_variable_view(request):
    """
    Edit variable
    :param request:
    :return:
    """
    variable = get(request, Variable, as_dict=False)
    name = request.json_body.get('name')

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

    data = get_values(request, attrs, required_attrs)
    edit(variable, **data)

    return variable.as_dict()
Esempio n. 17
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)
Esempio n. 18
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()