Skip to content

Commit

Permalink
Merge pull request #70 from mistercrunch/dash_slug
Browse files Browse the repository at this point in the history
Adding url slug support for dashboard model
  • Loading branch information
mistercrunch committed Dec 4, 2015
2 parents a0e967e + 66089a8 commit aed2f46
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
23 changes: 23 additions & 0 deletions panoramix/migrations/versions/1a48a5411020_adding_slug_to_dash.py
Original file line number Diff line number Diff line change
@@ -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')
3 changes: 2 additions & 1 deletion panoramix/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,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')

Expand All @@ -142,7 +143,7 @@ def __repr__(self):

@property
def url(self):
return "/panoramix/dashboard/{}/".format(self.id)
return "/panoramix/dashboard/{}/".format(self.slug or self.id)

def dashboard_link(self):
return '<a href="{self.url}">{self.dashboard_title}</a>'.format(self=self)
Expand Down
26 changes: 17 additions & 9 deletions panoramix/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ class SliceModelView(PanoramixModelView, DeleteMixin):
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 = {
Expand All @@ -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(
Expand Down Expand Up @@ -424,15 +431,16 @@ def testconn(self):
mimetype="application/json")

@has_access
@expose("/dashboard/<id_>/")
def dashboard(self, id_):
@expose("/dashboard/<identifier>/")
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 = {
Expand Down

0 comments on commit aed2f46

Please sign in to comment.