From 668ede1133e61edaf12157e46b42e817a1ab8932 Mon Sep 17 00:00:00 2001 From: Chris Williams Date: Mon, 20 Jun 2016 09:18:03 -0700 Subject: [PATCH] expose /slice// endpoint to redirect to a slice's url (#633) * expose /slice// endpoint to redirect to a slice's url * remove residual print statement * add unit test for caravel/slices/id endpoint --- caravel/views.py | 13 +++++++++++++ tests/core_tests.py | 5 +++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/caravel/views.py b/caravel/views.py index 9b5c87e80f296..7679c87577330 100644 --- a/caravel/views.py +++ b/caravel/views.py @@ -953,6 +953,19 @@ def favstar(self, class_name, obj_id, action): json.dumps({'count': count}), mimetype="application/json") + @has_access + @expose("/slice//") + def slice(self, slice_id): + """Redirects a request for a slice id to its corresponding URL""" + session = db.session() + qry = session.query(models.Slice).filter_by(id=int(slice_id)) + slc = qry.first() + if slc: + return redirect(slc.slice_url) + else: + flash("The specified slice could not be found", "danger") + return redirect('/slicemodelview/list/') + @has_access @expose("/dashboard//") def dashboard(self, dashboard_id): diff --git a/tests/core_tests.py b/tests/core_tests.py index c6f1dc02971dd..6470fbcaf42f1 100644 --- a/tests/core_tests.py +++ b/tests/core_tests.py @@ -140,13 +140,14 @@ def test_save_slice(self): assert 'Energy' in resp.data.decode('utf-8') def test_slices(self): - # Testing by running all the examples + # Testing by hitting the two supported end points for all slices self.login(username='admin') Slc = models.Slice urls = [] for slc in db.session.query(Slc).all(): urls += [ - (slc.slice_name, 'slice_url', slc.slice_url), + (slc.slice_name, 'slice_url', slc.slice_url), + (slc.slice_name, 'slice_id_endpoint', '/caravel/slices/{}'.format(slc.id)), (slc.slice_name, 'json_endpoint', slc.viz.json_endpoint), (slc.slice_name, 'csv_endpoint', slc.viz.csv_endpoint), ]