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

View File

@ -2,18 +2,20 @@ from .models import Dashboard
# Public interface
def create_dashboard(dashboard_data, account_id):
def create_dashboard(dashboard_data, name, account_id):
"""
Tries to create dashboard with given parameters
:param dashboard_data: JSON dashboard data
:param account_id: Id of owner of this dashboard
:param name: Name of the dashboard
:type name: JSON
:type account_id: int
:type name: string
:returns: True if dashboard is successfully created
:rtype: Boolean
"""
dashboard = Dashboard(account_id, dashboard_data)
dashboard = Dashboard(account_id, dashboard_data, name)
dashboard.save()
@ -30,7 +32,7 @@ def get_dashboard(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
@ -39,10 +41,12 @@ def patch_dashboard(account_id, dashboard_id,
:type name: JSON
:type dashboard_id: int
"""
dashboard = Dashboard.get(id=dashboard_id)
if dashboard_data is not None:
dashboard = Dashboard.get(id=dashboard_id)
dashboard.dashboard_data = dashboard_data
dashboard.save()
if name is not None:
dashboard.name = name
dashboard.save()
if active:
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'),
primary_key=True)
active = db.Column(db.Boolean, nullable=False, default=False)
name = db.Column(db.String, nullable=False, default="")
created_at = db.Column(db.DateTime,
nullable=False,
default=db.func.current_timestamp())
@ -18,9 +19,10 @@ class Dashboard(db.Model):
default=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.dashboard_data = dashboard_data
self.name = name
def save(self):
"""

View File

@ -214,7 +214,9 @@ definitions:
type: object
required:
- id
- active
- dashboard_data
- name
properties:
id:
$ref: '#/definitions/id'
@ -226,6 +228,7 @@ definitions:
required:
- active
- dashboard_data
- name
properties:
dashboard_data:
$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 ###