Skip to content

Commit

Permalink
Adding an URL shortner
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch committed Jan 14, 2016
1 parent d726bd0 commit f4ef1c0
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 1 deletion.
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
List of TODO items for Panoramix

## Features
* **URL shortner**
* **Dashboard URL filters:** `{dash_url}#fltin__fieldname__value1,value2`
* **Default slice:** choose a default slice for the dataset instead of default endpoint
* **refresh freq**: specifying the refresh frequency of a dashboard and specific slices within it, some randomization would be nice
Expand Down
32 changes: 32 additions & 0 deletions panoramix/migrations/versions/8e80a26a31db_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""empty message
Revision ID: 8e80a26a31db
Revises: 2591d77e9831
Create Date: 2016-01-13 20:24:45.256437
"""

# revision identifiers, used by Alembic.
revision = '8e80a26a31db'
down_revision = '2591d77e9831'

from alembic import op
import sqlalchemy as sa


def upgrade():
op.create_table('url',
sa.Column('created_on', sa.DateTime(), nullable=False),
sa.Column('changed_on', sa.DateTime(), nullable=False),
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('url', sa.Text(), nullable=True),
sa.Column('created_by_fk', sa.Integer(), nullable=True),
sa.Column('changed_by_fk', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['changed_by_fk'], ['ab_user.id'], ),
sa.ForeignKeyConstraint(['created_by_fk'], ['ab_user.id'], ),
sa.PrimaryKeyConstraint('id')
)


def downgrade():
op.drop_table('url')
7 changes: 7 additions & 0 deletions panoramix/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ def changed_on_(cls):
return utils.datetime_f(cls.changed_on)


class Url(Model, AuditMixinNullable):
"""Used for the short url feature"""
__tablename__ = 'url'
id = Column(Integer, primary_key=True)
url = Column(Text)


class Slice(Model, AuditMixinNullable):
"""A slice is essentially a report or a view on data"""
__tablename__ = 'slices'
Expand Down
18 changes: 18 additions & 0 deletions panoramix/static/panoramix.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,24 @@ var px = (function() {
$('legend').click(function () {
toggle_fieldset($(this), true);
});
$('#shortner').click(function () {
$.ajax({
type: "POST",
url: '/r/shortner/',
data: {'data': '/' + window.location.pathname + slice.querystring()},
success: function(data) {
console.log(data);
data += '&nbsp;&nbsp;&nbsp;<a style="cursor: pointer;"><i class="fa fa-close" id="close_shortner"></a>';
$('#shortner').popover({content: data, placement: 'left', html: true, trigger: 'manual'});
$('#shortner').popover('show');
$('#close_shortner').click(function(){
$('#shortner').popover('destroy');
});

},
error: function() {alert("Error :(");},
});
});
$("#viz_type").change(function() {$("#query").submit();});
collapsed_fieldsets = get_collapsed_fieldsets();
for(var i=0; i < collapsed_fieldsets.length; i++){
Expand Down
3 changes: 3 additions & 0 deletions panoramix/templates/panoramix/explore.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
</span>
<span>{{ form.get_field("viz_type")(class_="select2") }}</span>
<div class="btn-group results pull-right" role="group">
<a role="button" tabindex="0" class="btn btn-default" id="shortner" title="Short URL" data-toggle="popover" data-trigger="focus">
<i class="fa fa-link"></i>
</a>
<span class="btn btn-default" id="standalone" title="Standalone version, use to embed anywhere" data-toggle="tooltip">
<i class="fa fa-code"></i>
</span>
Expand Down
26 changes: 26 additions & 0 deletions panoramix/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,32 @@ def ping():
return "OK"


class R(BaseView):

@utils.log_this
@expose("/<url_id>")
def index(self, url_id):
url = db.session.query(models.Url).filter_by(id=url_id).first()
if url:
print(url.url)
return redirect('/' + url.url)
else:
flash("URL to nowhere...", "danger")
return redirect('/')

@utils.log_this
@expose("/shortner/", methods=['POST', 'GET'])
def shortner(self):
url = request.form.get('data')
obj = models.Url(url=url)
db.session.add(obj)
db.session.commit()
return("{request.headers[Host]}/r/{obj.id}".format(
request=request, obj=obj))

appbuilder.add_view_no_menu(R)


class Panoramix(BaseView):

@has_access
Expand Down

0 comments on commit f4ef1c0

Please sign in to comment.