2018-10-15 17:51:51 +00:00
|
|
|
from flask import g, request
|
2018-09-20 13:19:41 +00:00
|
|
|
from flask_restful import abort
|
2018-10-22 22:31:09 +00:00
|
|
|
from marshmallow import fields, Schema
|
2018-09-19 23:42:37 +00:00
|
|
|
from webargs.flaskparser import use_args
|
|
|
|
from flasgger import swag_from
|
2018-10-06 11:46:32 +00:00
|
|
|
import app.dashboards.api as dashboard
|
2018-10-22 21:54:31 +00:00
|
|
|
import app.devices.api as device
|
2018-10-06 12:07:40 +00:00
|
|
|
from app.api.auth_protection import ProtectedResource
|
2018-10-08 19:50:34 +00:00
|
|
|
from app.api.schemas import BaseResourceSchema
|
2018-09-19 23:42:37 +00:00
|
|
|
|
|
|
|
|
2018-10-22 22:31:09 +00:00
|
|
|
class BasicDashboardWidgetSchema(Schema):
|
2018-10-22 21:54:31 +00:00
|
|
|
id = fields.Integer(dump_only=True)
|
|
|
|
device_id = fields.Integer()
|
|
|
|
height = fields.Integer()
|
|
|
|
width = fields.Integer()
|
|
|
|
x = fields.Integer()
|
|
|
|
y = fields.Integer()
|
|
|
|
chart_type = fields.String()
|
|
|
|
filters = fields.Raw()
|
|
|
|
|
|
|
|
|
2018-10-22 22:31:09 +00:00
|
|
|
class DashboardWidgetSchema(BaseResourceSchema, BasicDashboardWidgetSchema):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class DashboardSchema(BaseResourceSchema):
|
|
|
|
id = fields.Integer(dump_only=True)
|
|
|
|
active = fields.Boolean(required=False)
|
|
|
|
dashboard_data = fields.Raw()
|
|
|
|
name = fields.String()
|
|
|
|
widgets = fields.Nested(BasicDashboardWidgetSchema, dump_only=True,
|
|
|
|
many=True)
|
|
|
|
|
|
|
|
|
|
|
|
def validate_dashboard_ownership(dashboard_id):
|
|
|
|
requested_dashboard = dashboard.get_dashboard(dashboard_id)
|
|
|
|
if requested_dashboard.account_id != g.current_account.id:
|
|
|
|
abort(403, message='You are not allowed to access this dashboard',
|
|
|
|
status='error')
|
|
|
|
return requested_dashboard
|
|
|
|
|
|
|
|
|
|
|
|
def validate_device_ownership(device_id):
|
|
|
|
if not device.can_user_access_device(g.current_account.id, device_id):
|
|
|
|
abort(403, message='You are not allowed to access this device',
|
|
|
|
status='error')
|
|
|
|
|
|
|
|
|
2018-09-19 23:42:37 +00:00
|
|
|
class DashboardResource(ProtectedResource):
|
|
|
|
@swag_from('swagger/get_dashboard_spec.yaml')
|
|
|
|
def get(self, dashboard_id):
|
2018-10-22 22:31:09 +00:00
|
|
|
requested_dashboard = validate_dashboard_ownership(dashboard_id)
|
2018-10-08 19:50:34 +00:00
|
|
|
return DashboardSchema().dump(requested_dashboard), 200
|
2018-09-19 23:42:37 +00:00
|
|
|
|
2018-10-08 19:50:34 +00:00
|
|
|
@use_args(DashboardSchema(), locations=('json',))
|
2018-09-19 23:42:37 +00:00
|
|
|
@swag_from('swagger/update_dashboard_spec.yaml')
|
2018-09-23 13:04:02 +00:00
|
|
|
def put(self, args, dashboard_id):
|
2018-10-22 22:31:09 +00:00
|
|
|
validate_dashboard_ownership(dashboard_id)
|
2018-10-24 19:52:00 +00:00
|
|
|
updated_dashboard = dashboard.patch_dashboard(
|
2018-10-15 17:51:51 +00:00
|
|
|
g.current_account.id,
|
|
|
|
dashboard_id,
|
|
|
|
args['dashboard_data'],
|
2018-10-22 19:29:20 +00:00
|
|
|
args['active'],
|
|
|
|
args['name'])
|
2018-10-24 19:52:00 +00:00
|
|
|
return DashboardSchema().dump(updated_dashboard), 200
|
2018-10-15 17:51:51 +00:00
|
|
|
|
2018-10-22 19:29:20 +00:00
|
|
|
@use_args(DashboardSchema(partial=True), locations=('json',))
|
2018-10-15 17:51:51 +00:00
|
|
|
@swag_from('swagger/update_dashboard_spec.yaml')
|
|
|
|
def patch(self, args, dashboard_id):
|
2018-10-22 22:31:09 +00:00
|
|
|
validate_dashboard_ownership(dashboard_id)
|
2018-10-24 19:52:00 +00:00
|
|
|
updated_dashboard = dashboard.patch_dashboard(
|
2018-10-15 17:51:51 +00:00
|
|
|
g.current_account.id,
|
2018-09-19 23:42:37 +00:00
|
|
|
dashboard_id,
|
2018-10-15 17:51:51 +00:00
|
|
|
args.get('dashboard_data'),
|
2018-10-22 19:29:20 +00:00
|
|
|
args.get('active'),
|
|
|
|
args.get('name'))
|
2018-10-24 19:52:00 +00:00
|
|
|
return DashboardSchema().dump(updated_dashboard), 200
|
2018-09-19 23:42:37 +00:00
|
|
|
|
2018-10-10 20:19:13 +00:00
|
|
|
@swag_from('swagger/delete_dashboard_spec.yaml')
|
|
|
|
def delete(self, dashboard_id):
|
2018-10-22 22:31:09 +00:00
|
|
|
validate_dashboard_ownership(dashboard_id)
|
2018-10-10 20:19:13 +00:00
|
|
|
dashboard.delete_dashboard(dashboard_id)
|
|
|
|
return '', 204
|
|
|
|
|
2018-09-19 23:42:37 +00:00
|
|
|
|
|
|
|
class DashboardListResource(ProtectedResource):
|
2018-10-08 19:50:34 +00:00
|
|
|
@use_args(DashboardSchema(), locations=('json',))
|
2018-09-19 23:42:37 +00:00
|
|
|
@swag_from('swagger/create_dashboard_spec.yaml')
|
|
|
|
def post(self, args):
|
2018-10-24 19:52:00 +00:00
|
|
|
created_dashboard = dashboard.create_dashboard(
|
2018-09-19 23:42:37 +00:00
|
|
|
args['dashboard_data'],
|
2018-10-22 19:29:20 +00:00
|
|
|
args['name'],
|
2018-09-19 23:42:37 +00:00
|
|
|
g.current_account.id)
|
2018-10-24 19:52:00 +00:00
|
|
|
return DashboardSchema().dump(created_dashboard), 201
|
2018-09-19 23:42:37 +00:00
|
|
|
|
|
|
|
@swag_from('swagger/get_dashboards_spec.yaml')
|
|
|
|
def get(self):
|
2018-10-15 17:51:51 +00:00
|
|
|
request_args = request.args
|
2018-10-08 19:50:34 +00:00
|
|
|
return DashboardSchema().dump(
|
2018-10-15 17:51:51 +00:00
|
|
|
dashboard.get_dashboards(
|
|
|
|
g.current_account.id,
|
|
|
|
request_args.get('active')), many=True), 200
|
2018-10-22 21:54:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DashboardWidgetListResource(ProtectedResource):
|
|
|
|
@use_args(DashboardWidgetSchema(), locations=('json',))
|
|
|
|
@swag_from('swagger/create_dashboard_widget_spec.yaml')
|
|
|
|
def post(self, args, dashboard_id):
|
2018-10-22 22:31:09 +00:00
|
|
|
validate_dashboard_ownership(dashboard_id)
|
2018-10-23 19:11:59 +00:00
|
|
|
validate_device_ownership(args['device_id'])
|
2018-10-24 19:52:00 +00:00
|
|
|
created_widget = dashboard.create_widget(
|
2018-10-22 21:54:31 +00:00
|
|
|
dashboard_id,
|
|
|
|
args['device_id'],
|
|
|
|
args['height'],
|
|
|
|
args['width'],
|
|
|
|
args['x'],
|
|
|
|
args['y'],
|
|
|
|
args['chart_type'],
|
|
|
|
args['filters'])
|
2018-10-24 19:52:00 +00:00
|
|
|
return DashboardWidgetSchema().dump(created_widget), 201
|
2018-10-22 21:54:31 +00:00
|
|
|
|
|
|
|
@swag_from('swagger/get_dashboard_widgets_spec.yaml')
|
|
|
|
def get(self, dashboard_id):
|
2018-10-22 22:31:09 +00:00
|
|
|
validate_dashboard_ownership(dashboard_id)
|
2018-10-22 21:54:31 +00:00
|
|
|
return DashboardWidgetSchema().dump(
|
|
|
|
dashboard.get_widgets(dashboard_id), many=True), 200
|
|
|
|
|
|
|
|
|
|
|
|
class DashboardWidgetResource(ProtectedResource):
|
|
|
|
@swag_from('swagger/get_dashboard_widget_spec.yaml')
|
|
|
|
def get(self, dashboard_id, widget_id):
|
2018-10-22 22:31:09 +00:00
|
|
|
validate_dashboard_ownership(dashboard_id)
|
2018-10-22 21:54:31 +00:00
|
|
|
requested_widget = dashboard.get_widget(widget_id)
|
|
|
|
return DashboardWidgetSchema().dump(requested_widget), 200
|
|
|
|
|
|
|
|
@use_args(DashboardWidgetSchema(), locations=('json',))
|
|
|
|
@swag_from('swagger/update_dashboard_widget_spec.yaml')
|
|
|
|
def put(self, args, dashboard_id, widget_id):
|
2018-10-22 22:31:09 +00:00
|
|
|
validate_dashboard_ownership(dashboard_id)
|
2018-10-23 19:11:59 +00:00
|
|
|
validate_device_ownership(args['device_id'])
|
2018-10-24 19:52:00 +00:00
|
|
|
updated_widget = dashboard.patch_widget(
|
2018-10-22 21:54:31 +00:00
|
|
|
widget_id,
|
|
|
|
args['device_id'],
|
|
|
|
args['height'],
|
|
|
|
args['width'],
|
|
|
|
args['x'],
|
|
|
|
args['y'],
|
|
|
|
args['chart_type'],
|
|
|
|
args['filters'])
|
2018-10-24 19:52:00 +00:00
|
|
|
return DashboardWidgetSchema().dump(updated_widget), 200
|
2018-10-22 21:54:31 +00:00
|
|
|
|
|
|
|
@use_args(DashboardWidgetSchema(partial=True), locations=('json',))
|
|
|
|
@swag_from('swagger/update_dashboard_widget_spec.yaml')
|
|
|
|
def patch(self, args, dashboard_id, widget_id):
|
2018-10-22 22:31:09 +00:00
|
|
|
validate_dashboard_ownership(dashboard_id)
|
2018-10-22 21:54:31 +00:00
|
|
|
if args.get('device_id') is not None:
|
2018-10-23 19:11:59 +00:00
|
|
|
validate_device_ownership(args['device_id'])
|
2018-10-24 19:52:00 +00:00
|
|
|
updated_widget = dashboard.patch_widget(
|
2018-10-22 21:54:31 +00:00
|
|
|
widget_id,
|
|
|
|
args.get('device_id'),
|
|
|
|
args.get('height'),
|
|
|
|
args.get('width'),
|
|
|
|
args.get('x'),
|
|
|
|
args.get('y'),
|
|
|
|
args.get('chart_type'),
|
|
|
|
args.get('filters'))
|
2018-10-24 19:52:00 +00:00
|
|
|
return DashboardWidgetSchema().dump(updated_widget), 200
|
2018-10-22 21:54:31 +00:00
|
|
|
|
|
|
|
@swag_from('swagger/delete_dashboard_widget_spec.yaml')
|
|
|
|
def delete(self, dashboard_id, widget_id):
|
2018-10-22 22:31:09 +00:00
|
|
|
validate_dashboard_ownership(dashboard_id)
|
2018-10-22 21:54:31 +00:00
|
|
|
dashboard.delete_widget(widget_id)
|
|
|
|
return '', 204
|