Make POST, PUT and PATCH return created/updated dashboards/widgets

develop
esensar 2018-10-24 21:52:00 +02:00
parent 85f075e567
commit 7d5483002f
8 changed files with 58 additions and 28 deletions

View File

@ -57,27 +57,25 @@ class DashboardResource(ProtectedResource):
@swag_from('swagger/update_dashboard_spec.yaml')
def put(self, args, dashboard_id):
validate_dashboard_ownership(dashboard_id)
success = dashboard.patch_dashboard(
updated_dashboard = dashboard.patch_dashboard(
g.current_account.id,
dashboard_id,
args['dashboard_data'],
args['active'],
args['name'])
if success:
return '', 204
return DashboardSchema().dump(updated_dashboard), 200
@use_args(DashboardSchema(partial=True), locations=('json',))
@swag_from('swagger/update_dashboard_spec.yaml')
def patch(self, args, dashboard_id):
validate_dashboard_ownership(dashboard_id)
success = dashboard.patch_dashboard(
updated_dashboard = dashboard.patch_dashboard(
g.current_account.id,
dashboard_id,
args.get('dashboard_data'),
args.get('active'),
args.get('name'))
if success:
return '', 204
return DashboardSchema().dump(updated_dashboard), 200
@swag_from('swagger/delete_dashboard_spec.yaml')
def delete(self, dashboard_id):
@ -90,12 +88,11 @@ class DashboardListResource(ProtectedResource):
@use_args(DashboardSchema(), locations=('json',))
@swag_from('swagger/create_dashboard_spec.yaml')
def post(self, args):
success = dashboard.create_dashboard(
created_dashboard = dashboard.create_dashboard(
args['dashboard_data'],
args['name'],
g.current_account.id)
if success:
return '', 201
return DashboardSchema().dump(created_dashboard), 201
@swag_from('swagger/get_dashboards_spec.yaml')
def get(self):
@ -112,7 +109,7 @@ class DashboardWidgetListResource(ProtectedResource):
def post(self, args, dashboard_id):
validate_dashboard_ownership(dashboard_id)
validate_device_ownership(args['device_id'])
success = dashboard.create_widget(
created_widget = dashboard.create_widget(
dashboard_id,
args['device_id'],
args['height'],
@ -121,8 +118,7 @@ class DashboardWidgetListResource(ProtectedResource):
args['y'],
args['chart_type'],
args['filters'])
if success:
return '', 201
return DashboardWidgetSchema().dump(created_widget), 201
@swag_from('swagger/get_dashboard_widgets_spec.yaml')
def get(self, dashboard_id):
@ -143,7 +139,7 @@ class DashboardWidgetResource(ProtectedResource):
def put(self, args, dashboard_id, widget_id):
validate_dashboard_ownership(dashboard_id)
validate_device_ownership(args['device_id'])
success = dashboard.patch_widget(
updated_widget = dashboard.patch_widget(
widget_id,
args['device_id'],
args['height'],
@ -152,8 +148,7 @@ class DashboardWidgetResource(ProtectedResource):
args['y'],
args['chart_type'],
args['filters'])
if success:
return '', 204
return DashboardWidgetSchema().dump(updated_widget), 200
@use_args(DashboardWidgetSchema(partial=True), locations=('json',))
@swag_from('swagger/update_dashboard_widget_spec.yaml')
@ -161,7 +156,7 @@ class DashboardWidgetResource(ProtectedResource):
validate_dashboard_ownership(dashboard_id)
if args.get('device_id') is not None:
validate_device_ownership(args['device_id'])
success = dashboard.patch_widget(
updated_widget = dashboard.patch_widget(
widget_id,
args.get('device_id'),
args.get('height'),
@ -170,8 +165,7 @@ class DashboardWidgetResource(ProtectedResource):
args.get('y'),
args.get('chart_type'),
args.get('filters'))
if success:
return '', 204
return DashboardWidgetSchema().dump(updated_widget), 200
@swag_from('swagger/delete_dashboard_widget_spec.yaml')
def delete(self, dashboard_id, widget_id):

View File

@ -11,5 +11,12 @@ parameters:
type: object
$ref: '#/definitions/DashboardCreation'
responses:
201:
description: Successful creation
200:
description: Success
schema:
type: object
required:
- content
properties:
content:
$ref: '#/definitions/Dashboard'

View File

@ -16,5 +16,12 @@ parameters:
type: object
$ref: '#/definitions/WidgetCreation'
responses:
201:
description: Successful creation
200:
description: Success
schema:
type: object
required:
- content
properties:
content:
$ref: '#/definitions/Widget'

View File

@ -1,4 +1,4 @@
Deletes a dashboard
Deletes a widget
---
tags:
- Dashboard
@ -8,6 +8,11 @@ parameters:
required: true
type: integer
description: Id of the dashboard
- in: path
name: widget_id
required: true
type: integer
description: Id of the widget
responses:
204:
description: Success

View File

@ -16,5 +16,12 @@ parameters:
type: object
$ref: '#/definitions/DashboardCreation'
responses:
204:
200:
description: Success
schema:
type: object
required:
- content
properties:
content:
$ref: '#/definitions/Dashboard'

View File

@ -20,5 +20,12 @@ parameters:
type: object
$ref: '#/definitions/WidgetCreation'
responses:
204:
200:
description: Success
schema:
type: object
required:
- content
properties:
content:
$ref: '#/definitions/Widget'

View File

@ -17,6 +17,7 @@ def create_dashboard(dashboard_data, name, account_id):
"""
dashboard = Dashboard(account_id, dashboard_data, name)
dashboard.save()
return dashboard
def get_dashboard(dashboard_id):
@ -49,6 +50,8 @@ def patch_dashboard(account_id, dashboard_id,
dashboard.save()
if active:
set_active_dashboard(account_id, dashboard_id)
dashboard.active = True
return dashboard
def delete_dashboard(dashboard_id):
@ -178,4 +181,4 @@ def patch_widget(widget_id, device_id=None, height=None, width=None,
widget.filters = filters
widget.save()
print("Saved widget")
return widget

View File

@ -5,7 +5,7 @@ from sqlalchemy.dialects.postgresql import JSON
class Dashboard(db.Model):
__tablename__ = 'dashboards'
id = db.Column(db.Integer, primary_key=True)
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
dashboard_data = db.Column(JSON, nullable=False)
account_id = db.Column(db.Integer, db.ForeignKey('accounts.id'),
primary_key=True)
@ -121,7 +121,7 @@ class Dashboard(db.Model):
class DashboardWidget(db.Model):
__tablename__ = 'dashboard_widgets'
id = db.Column(db.Integer, primary_key=True)
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
dashboard_id = db.Column(db.Integer, db.ForeignKey('dashboards.id'))
device_id = db.Column(db.Integer, db.ForeignKey('devices.id'))
height = db.Column(db.Integer, nullable=False)