Skip to content

Commit

Permalink
Run queries with no cached result in public dashboards (re getredash#220
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Allen Short committed Jul 30, 2018
1 parent 9ea7d74 commit f708df0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
17 changes: 13 additions & 4 deletions redash/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from flask_login import current_user
from redash import models
from redash.permissions import has_access, view_only
from redash.handlers.query_results import run_query_sync


def public_widget(widget):
Expand All @@ -21,8 +22,15 @@ def public_widget(widget):
'created_at': widget.created_at
}

if widget.visualization and widget.visualization.id:
query_data = models.QueryResult.query.get(widget.visualization.query_rel.latest_query_data_id).to_dict()
if (widget.visualization and widget.visualization.id and
widget.visualization.query_rel is not None):
q = widget.visualization.query_rel
# make sure the widget's query has a latest_query_data_id that is
# not null so public dashboards work
if (q.latest_query_data_id is None):
run_query_sync(q.data_source, {}, q.query_text)

query_data = q.latest_query_data.to_dict()
res['visualization'] = {
'type': widget.visualization.type,
'name': widget.visualization.name,
Expand All @@ -31,9 +39,10 @@ def public_widget(widget):
'updated_at': widget.visualization.updated_at,
'created_at': widget.visualization.created_at,
'query': {
'id': q.id,
'query': ' ', # workaround, as otherwise the query data won't be loaded.
'name': widget.visualization.query_rel.name,
'description': widget.visualization.query_rel.description,
'name': q.name,
'description': q.description,
'options': {},
'latest_query_data': query_data
}
Expand Down
12 changes: 12 additions & 0 deletions tests/handlers/test_embed.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import mock

from tests import BaseTestCase
from redash.models import db
from redash.query_runner.pg import PostgreSQL


class TestEmbedVisualization(BaseTestCase):
Expand Down Expand Up @@ -97,6 +100,15 @@ def test_inactive_token(self):
res = self.make_request('get', '/api/dashboards/public/{}'.format(api_key.api_key), user=False, is_json=False)
self.assertEqual(res.status_code, 404)

def test_dashboard_widgets(self):
dashboard = self.factory.create_dashboard()
w1 = self.factory.create_widget(dashboard=dashboard)
w2 = self.factory.create_widget(dashboard=dashboard, visualization=None, text="a text box")
api_key = self.factory.create_api_key(object=dashboard)
with mock.patch.object(PostgreSQL, "run_query") as qr:
qr.return_value = ("[1, 2]", None)
res = self.make_request('get', '/api/dashboards/public/{}'.format(api_key.api_key), user=False, is_json=False)
self.assertEqual(res.status_code, 200)
# Not relevant for now, as tokens in api_keys table are only created for dashboards. Once this changes, we should
# add this test.
# def test_token_doesnt_belong_to_dashboard(self):
Expand Down

0 comments on commit f708df0

Please sign in to comment.