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

View File

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

View File

@ -16,5 +16,12 @@ parameters:
type: object type: object
$ref: '#/definitions/WidgetCreation' $ref: '#/definitions/WidgetCreation'
responses: responses:
201: 200:
description: Successful creation 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: tags:
- Dashboard - Dashboard
@ -8,6 +8,11 @@ parameters:
required: true required: true
type: integer type: integer
description: Id of the dashboard description: Id of the dashboard
- in: path
name: widget_id
required: true
type: integer
description: Id of the widget
responses: responses:
204: 204:
description: Success description: Success

View File

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

View File

@ -20,5 +20,12 @@ parameters:
type: object type: object
$ref: '#/definitions/WidgetCreation' $ref: '#/definitions/WidgetCreation'
responses: responses:
204: 200:
description: Success 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 = Dashboard(account_id, dashboard_data, name)
dashboard.save() dashboard.save()
return dashboard
def get_dashboard(dashboard_id): def get_dashboard(dashboard_id):
@ -49,6 +50,8 @@ def patch_dashboard(account_id, dashboard_id,
dashboard.save() dashboard.save()
if active: if active:
set_active_dashboard(account_id, dashboard_id) set_active_dashboard(account_id, dashboard_id)
dashboard.active = True
return dashboard
def delete_dashboard(dashboard_id): 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.filters = filters
widget.save() widget.save()
print("Saved widget") return widget

View File

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