university-final-iot-backend/app/api/resources/dashboard.py

56 lines
1.7 KiB
Python

from flask import g
from marshmallow import Schema, fields
from webargs.flaskparser import use_args
from flasgger import swag_from
import app.dashboard as dashboard
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):
return DashboardWrapperSchema().dump(
{'dashboard': dashboard.get_dashboard(dashboard_id)}), 200
@use_args(DashboardWrapperSchema())
@swag_from('swagger/update_dashboard_spec.yaml')
def put(self, dashboard_id, args):
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