2018-09-19 23:42:37 +00:00
|
|
|
from flask import g
|
2018-09-20 13:19:41 +00:00
|
|
|
from flask_restful import abort
|
2018-09-19 23:42:37 +00:00
|
|
|
from marshmallow import Schema, fields
|
|
|
|
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-09-19 23:42:37 +00:00
|
|
|
from app.api import ProtectedResource
|
|
|
|
|
|
|
|
|
|
|
|
class DashboardSchema(Schema):
|
|
|
|
id = fields.Integer(dump_only=True)
|
|
|
|
dashboard_data = fields.Raw()
|
|
|
|
|
|
|
|
|
|
|
|
class DashboardWrapperSchema(Schema):
|
|
|
|
dashboard = fields.Nested(DashboardSchema, required=True, location='json')
|
|
|
|
|
|
|
|
|
|
|
|
class DashboardsSchema(Schema):
|
|
|
|
dashboards = fields.Nested(DashboardSchema, required=True, location='json',
|
|
|
|
many=True)
|
|
|
|
|
|
|
|
|
|
|
|
class DashboardResource(ProtectedResource):
|
|
|
|
@swag_from('swagger/get_dashboard_spec.yaml')
|
|
|
|
def get(self, dashboard_id):
|
2018-09-20 13:19:41 +00:00
|
|
|
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')
|
2018-09-19 23:42:37 +00:00
|
|
|
return DashboardWrapperSchema().dump(
|
2018-09-20 13:19:41 +00:00
|
|
|
{'dashboard': requested_dashboard}), 200
|
2018-09-19 23:42:37 +00:00
|
|
|
|
|
|
|
@use_args(DashboardWrapperSchema())
|
|
|
|
@swag_from('swagger/update_dashboard_spec.yaml')
|
2018-09-23 13:04:02 +00:00
|
|
|
def put(self, args, dashboard_id):
|
2018-09-20 13:19:41 +00:00
|
|
|
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')
|
2018-09-19 23:42:37 +00:00
|
|
|
args = args['dashboard']
|
|
|
|
success = dashboard.update_dashboard(
|
|
|
|
dashboard_id,
|
|
|
|
args['dashboard_data'])
|
|
|
|
if success:
|
|
|
|
return '', 204
|
|
|
|
|
|
|
|
|
|
|
|
class DashboardListResource(ProtectedResource):
|
|
|
|
@use_args(DashboardWrapperSchema())
|
|
|
|
@swag_from('swagger/create_dashboard_spec.yaml')
|
|
|
|
def post(self, args):
|
|
|
|
args = args['dashboard']
|
|
|
|
success = dashboard.create_dashboard(
|
|
|
|
args['dashboard_data'],
|
|
|
|
g.current_account.id)
|
|
|
|
if success:
|
|
|
|
return '', 201
|
|
|
|
|
|
|
|
@swag_from('swagger/get_dashboards_spec.yaml')
|
|
|
|
def get(self):
|
|
|
|
return DashboardsSchema().dump(
|
|
|
|
{'dashboards':
|
|
|
|
dashboard.get_dashboards(g.current_account.id)}), 200
|