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 authored and jezdez committed Mar 25, 2019
1 parent f47e23f commit 0cbbdbb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
18 changes: 14 additions & 4 deletions redash/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from flask_login import current_user

from redash import models
from redash.handlers.query_results import run_query_sync
from redash.permissions import has_access, view_only
from redash.utils import json_loads
from redash.models.parameterized_query import ParameterizedQuery
Expand All @@ -23,8 +24,16 @@ 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 @@ -33,9 +42,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 0cbbdbb

Please sign in to comment.