From 2743b2412f23e2f486b5cf500c63b4346261de7a Mon Sep 17 00:00:00 2001 From: Maxime Beauchemin Date: Fri, 4 Dec 2015 10:02:16 -0800 Subject: [PATCH 1/2] Adding url slug support for dashboard model --- panoramix/models.py | 3 ++- panoramix/views.py | 26 +++++++++++++++++--------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/panoramix/models.py b/panoramix/models.py index 336e55f758..1ab3bdc1a7 100644 --- a/panoramix/models.py +++ b/panoramix/models.py @@ -133,6 +133,7 @@ class Dashboard(Model, AuditMixinNullable): position_json = Column(Text) description = Column(Text) css = Column(Text) + slug = Column(String(255), unique=True) slices = relationship( 'Slice', secondary=dashboard_slices, backref='dashboards') @@ -141,7 +142,7 @@ class Dashboard(Model, AuditMixinNullable): @property def url(self): - return "/panoramix/dashboard/{}/".format(self.id) + return "/panoramix/dashboard/{}/".format(self.slug or self.id) def dashboard_link(self): return '{self.dashboard_title}'.format(self=self) diff --git a/panoramix/views.py b/panoramix/views.py index 16cb5b2951..1d2f549ecf 100644 --- a/panoramix/views.py +++ b/panoramix/views.py @@ -210,7 +210,8 @@ appbuilder.add_view( class DashboardModelView(PanoramixModelView, DeleteMixin): datamodel = SQLAInterface(models.Dashboard) list_columns = ['dashboard_link', 'created_by', 'changed_by', 'changed_on'] - edit_columns = ['dashboard_title', 'slices', 'position_json', 'css'] + edit_columns = [ + 'dashboard_title', 'slug', 'slices', 'position_json', 'css'] add_columns = edit_columns base_order = ('changed_on','desc') description_columns = { @@ -223,7 +224,13 @@ class DashboardModelView(PanoramixModelView, DeleteMixin): "The css for individual dashboards can be altered here, or " "in the dashboard view where changes are immediatly " "visible"), + 'slug': "To get a readable URL for your dashboard", } + def pre_add(self, obj): + obj.slug = obj.slug.strip() or None + + def pre_update(self, obj): + self.pre_add(obj) appbuilder.add_view( @@ -412,15 +419,16 @@ class Panoramix(BaseView): mimetype="application/json") @has_access - @expose("/dashboard//") - def dashboard(self, id_): + @expose("/dashboard//") + def dashboard(self, identifier): session = db.session() - dashboard = ( - session - .query(models.Dashboard) - .filter(models.Dashboard.id == id_) - .first() - ) + qry = session.query(models.Dashboard) + if identifier.isdigit(): + qry = qry.filter_by(id=int(identifier)) + else: + qry = qry.filter_by(slug=identifier) + + dashboard = qry.first() pos_dict = {} if dashboard.position_json: pos_dict = { From 66089a89488556a7e1b0088083de5b6647952285 Mon Sep 17 00:00:00 2001 From: Maxime Beauchemin Date: Fri, 4 Dec 2015 10:13:16 -0800 Subject: [PATCH 2/2] Adding migration file --- .../1a48a5411020_adding_slug_to_dash.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 panoramix/migrations/versions/1a48a5411020_adding_slug_to_dash.py diff --git a/panoramix/migrations/versions/1a48a5411020_adding_slug_to_dash.py b/panoramix/migrations/versions/1a48a5411020_adding_slug_to_dash.py new file mode 100644 index 0000000000..743a73c31e --- /dev/null +++ b/panoramix/migrations/versions/1a48a5411020_adding_slug_to_dash.py @@ -0,0 +1,23 @@ +"""adding slug to dash + +Revision ID: 1a48a5411020 +Revises: 289ce07647b +Create Date: 2015-12-04 09:42:16.973264 + +""" + +# revision identifiers, used by Alembic. +revision = '1a48a5411020' +down_revision = '289ce07647b' + +from alembic import op +import sqlalchemy as sa + +def upgrade(): + op.add_column('dashboards', sa.Column('slug', sa.String(length=255), nullable=True)) + op.create_unique_constraint(None, 'dashboards', ['slug']) + + +def downgrade(): + op.drop_constraint(None, 'dashboards', type_='unique') + op.drop_column('dashboards', 'slug')