Add names for dashboards

develop
esensar 2018-10-22 21:29:20 +02:00
parent 26d10a1216
commit dc1809924d
5 changed files with 52 additions and 18 deletions

View File

@ -1,6 +1,6 @@
from flask import g, request from flask import g, request
from flask_restful import abort from flask_restful import abort
from marshmallow import fields, Schema from marshmallow import fields
from webargs.flaskparser import use_args from webargs.flaskparser import use_args
from flasgger import swag_from from flasgger import swag_from
import app.dashboards.api as dashboard import app.dashboards.api as dashboard
@ -12,10 +12,7 @@ class DashboardSchema(BaseResourceSchema):
id = fields.Integer(dump_only=True) id = fields.Integer(dump_only=True)
active = fields.Boolean(required=False) active = fields.Boolean(required=False)
dashboard_data = fields.Raw() dashboard_data = fields.Raw()
name = fields.String()
class DashboardIdSchema(Schema):
id = fields.Integer()
class DashboardResource(ProtectedResource): class DashboardResource(ProtectedResource):
@ -34,19 +31,16 @@ class DashboardResource(ProtectedResource):
if requested_dashboard.account_id != g.current_account.id: if requested_dashboard.account_id != g.current_account.id:
abort(403, message='You are not allowed to access this dashboard', abort(403, message='You are not allowed to access this dashboard',
status='error') status='error')
if args.get('dashboard_data') is None:
abort(400, message='Missing dashboard_data', status='error')
if args.get('active') is None:
abort(400, message='Missing active', status='error')
success = dashboard.patch_dashboard( success = 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'])
if success: if success:
return '', 204 return '', 204
@use_args(DashboardSchema(), 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):
requested_dashboard = dashboard.get_dashboard(dashboard_id) requested_dashboard = dashboard.get_dashboard(dashboard_id)
@ -57,7 +51,8 @@ class DashboardResource(ProtectedResource):
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'))
if success: if success:
return '', 204 return '', 204
@ -77,6 +72,7 @@ class DashboardListResource(ProtectedResource):
def post(self, args): def post(self, args):
success = dashboard.create_dashboard( success = dashboard.create_dashboard(
args['dashboard_data'], args['dashboard_data'],
args['name'],
g.current_account.id) g.current_account.id)
if success: if success:
return '', 201 return '', 201

View File

@ -2,18 +2,20 @@ from .models import Dashboard
# Public interface # Public interface
def create_dashboard(dashboard_data, account_id): def create_dashboard(dashboard_data, name, account_id):
""" """
Tries to create dashboard with given parameters Tries to create dashboard with given parameters
:param dashboard_data: JSON dashboard data :param dashboard_data: JSON dashboard data
:param account_id: Id of owner of this dashboard :param account_id: Id of owner of this dashboard
:param name: Name of the dashboard
:type name: JSON :type name: JSON
:type account_id: int :type account_id: int
:type name: string
:returns: True if dashboard is successfully created :returns: True if dashboard is successfully created
:rtype: Boolean :rtype: Boolean
""" """
dashboard = Dashboard(account_id, dashboard_data) dashboard = Dashboard(account_id, dashboard_data, name)
dashboard.save() dashboard.save()
@ -30,7 +32,7 @@ def get_dashboard(dashboard_id):
def patch_dashboard(account_id, dashboard_id, def patch_dashboard(account_id, dashboard_id,
dashboard_data=None, active=None): dashboard_data=None, active=None, name=None):
""" """
Tries to update dashboard with given parameters Tries to update dashboard with given parameters
@ -39,9 +41,11 @@ def patch_dashboard(account_id, dashboard_id,
:type name: JSON :type name: JSON
:type dashboard_id: int :type dashboard_id: int
""" """
if dashboard_data is not None:
dashboard = Dashboard.get(id=dashboard_id) dashboard = Dashboard.get(id=dashboard_id)
if dashboard_data is not None:
dashboard.dashboard_data = dashboard_data dashboard.dashboard_data = dashboard_data
if name is not None:
dashboard.name = name
dashboard.save() dashboard.save()
if active: if active:
set_active_dashboard(account_id, dashboard_id) set_active_dashboard(account_id, dashboard_id)

View File

@ -10,6 +10,7 @@ class Dashboard(db.Model):
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)
active = db.Column(db.Boolean, nullable=False, default=False) active = db.Column(db.Boolean, nullable=False, default=False)
name = db.Column(db.String, nullable=False, default="")
created_at = db.Column(db.DateTime, created_at = db.Column(db.DateTime,
nullable=False, nullable=False,
default=db.func.current_timestamp()) default=db.func.current_timestamp())
@ -18,9 +19,10 @@ class Dashboard(db.Model):
default=db.func.current_timestamp(), default=db.func.current_timestamp(),
onupdate=db.func.current_timestamp()) onupdate=db.func.current_timestamp())
def __init__(self, account_id, dashboard_data): def __init__(self, account_id, dashboard_data, name):
self.account_id = account_id self.account_id = account_id
self.dashboard_data = dashboard_data self.dashboard_data = dashboard_data
self.name = name
def save(self): def save(self):
""" """

View File

@ -214,7 +214,9 @@ definitions:
type: object type: object
required: required:
- id - id
- active
- dashboard_data - dashboard_data
- name
properties: properties:
id: id:
$ref: '#/definitions/id' $ref: '#/definitions/id'
@ -226,6 +228,7 @@ definitions:
required: required:
- active - active
- dashboard_data - dashboard_data
- name
properties: properties:
dashboard_data: dashboard_data:
$ref: '#/definitions/dashboarddata' $ref: '#/definitions/dashboarddata'

View File

@ -0,0 +1,29 @@
"""empty message
Revision ID: 5cce35244087
Revises: 4945e4c8fbca
Create Date: 2018-10-22 21:06:48.532624
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '5cce35244087'
down_revision = '4945e4c8fbca'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('dashboards', sa.Column('name', sa.String(),
nullable=False, server_default='Legacy dashboard'))
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('dashboards', 'name')
# ### end Alembic commands ###