mirror of
https://github.com/getredash/redash.git
synced 2026-05-12 12:00:15 -04:00
61 lines
2.2 KiB
Python
61 lines
2.2 KiB
Python
"""
|
|
This will eventually replace all the `to_dict` methods of the different model classes we have. This will ensure cleaner
|
|
code and better separation of concerns.
|
|
"""
|
|
|
|
import json
|
|
from funcy import project
|
|
from redash import models
|
|
|
|
|
|
def public_widget(widget):
|
|
res = {
|
|
'id': widget.id,
|
|
'width': widget.width,
|
|
'options': json.loads(widget.options),
|
|
'text': widget.text,
|
|
'updated_at': widget.updated_at,
|
|
'created_at': widget.created_at
|
|
}
|
|
|
|
if widget.visualization and widget.visualization.id:
|
|
query_data = models.QueryResult.get_by_id(widget.visualization.query.latest_query_data_id).to_dict()
|
|
res['visualization'] = {
|
|
'type': widget.visualization.type,
|
|
'name': widget.visualization.name,
|
|
'description': widget.visualization.description,
|
|
'options': json.loads(widget.visualization.options),
|
|
'updated_at': widget.visualization.updated_at,
|
|
'created_at': widget.visualization.created_at,
|
|
'query': {
|
|
'query': ' ', # workaround, as otherwise the query data won't be loaded.
|
|
'name': widget.visualization.query.name,
|
|
'description': widget.visualization.query.description,
|
|
'latest_query_data': query_data
|
|
}
|
|
}
|
|
|
|
return res
|
|
|
|
|
|
def public_dashboard(dashboard):
|
|
dashboard_dict = project(dashboard.to_dict(), ('name', 'layout', 'dashboard_filters_enabled', 'updated_at', 'created_at'))
|
|
|
|
widget_list = models.Widget.select(models.Widget, models.Visualization, models.Query) \
|
|
.where(models.Widget.dashboard == dashboard.id) \
|
|
.join(models.Visualization, join_type=models.peewee.JOIN_LEFT_OUTER) \
|
|
.join(models.Query, join_type=models.peewee.JOIN_LEFT_OUTER)
|
|
widgets = {w.id: public_widget(w) for w in widget_list}
|
|
|
|
widgets_layout = []
|
|
for row in dashboard_dict['layout']:
|
|
new_row = []
|
|
for widget_id in row:
|
|
widget = widgets.get(widget_id, None)
|
|
if widget:
|
|
new_row.append(widget)
|
|
widgets_layout.append(new_row)
|
|
|
|
dashboard_dict['widgets'] = widgets_layout
|
|
return dashboard_dict
|