Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding an URL shortner #112

Merged
merged 3 commits into from
Jan 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion panoramix/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ def __init__(self, viz):
"Legend", default=True,
description="Whether to display the legend (toggles)"),
'x_axis_showminmax': BetterBooleanField(
"X axis show min/max", default=True,
"X show min/max", default=True,
description=(
"Whether to display the min and max values of the axis")),
'rich_tooltip': BetterBooleanField(
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
28 changes: 28 additions & 0 deletions panoramix/static/lib/bootstrap-toggle.min.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*! ========================================================================
* Bootstrap Toggle: bootstrap-toggle.css v2.2.0
* http://www.bootstraptoggle.com
* ========================================================================
* Copyright 2014 Min Hur, The New York Times Company
* Licensed under MIT
* ======================================================================== */
.checkbox label .toggle,.checkbox-inline .toggle{margin-left:-20px;margin-right:5px}
.toggle{position:relative;overflow:hidden}
.toggle input[type=checkbox]{display:none}
.toggle-group{position:absolute;width:200%;top:0;bottom:0;left:0;transition:left .35s;-webkit-transition:left .35s;-moz-user-select:none;-webkit-user-select:none}
.toggle.off .toggle-group{left:-100%}
.toggle-on{position:absolute;top:0;bottom:0;left:0;right:50%;margin:0;border:0;border-radius:0}
.toggle-off{position:absolute;top:0;bottom:0;left:50%;right:0;margin:0;border:0;border-radius:0}
.toggle-handle{position:relative;margin:0 auto;padding-top:0;padding-bottom:0;height:100%;width:0;border-width:0 1px}
.toggle.btn{min-width:59px;min-height:34px}
.toggle-on.btn{padding-right:24px}
.toggle-off.btn{padding-left:24px}
.toggle.btn-lg{min-width:79px;min-height:45px}
.toggle-on.btn-lg{padding-right:31px}
.toggle-off.btn-lg{padding-left:31px}
.toggle-handle.btn-lg{width:40px}
.toggle.btn-sm{min-width:50px;min-height:30px}
.toggle-on.btn-sm{padding-right:20px}
.toggle-off.btn-sm{padding-left:20px}
.toggle.btn-xs{min-width:35px;min-height:22px}
.toggle-on.btn-xs{padding-right:12px}
.toggle-off.btn-xs{padding-left:12px}
9 changes: 9 additions & 0 deletions panoramix/static/lib/bootstrap-toggle.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
10 changes: 10 additions & 0 deletions panoramix/templates/panoramix/explore.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{% block head_css %}
{{super()}}
<link rel="stylesheet" type="text/css" href="/static/lib/pygments.css" />
<link href="/static/lib/bootstrap-toggle.min.css" rel="stylesheet">
{% endblock %}

{% block content_fluid %}
Expand Down Expand Up @@ -42,6 +43,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 Expand Up @@ -200,6 +204,7 @@ <h4 class="modal-title">Datasource Description</h4>

{% block tail_js %}
{{ super() }}
<script src="/static/lib/bootstrap-toggle.min.js"></script>
<script>
$(document).ready(px.initExploreView);
$(document).ready(function() {
Expand All @@ -208,6 +213,11 @@ <h4 class="modal-title">Datasource Description</h4>
$('.slice').data('slice', slice);
slice.render();
});
$(':checkbox')
.addClass('pull-right')
.attr("data-onstyle", "default")
.bootstrapToggle({size: 'mini'});
$('div.toggle').addClass('pull-right');

$(window).on('hashchange', function() {
console.log("change occurred");
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