Skip to content

Commit

Permalink
Half working JSON mode, refs #157
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Dec 16, 2023
1 parent 59b71ea commit ac5ac0c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
3 changes: 2 additions & 1 deletion django_sql_dashboard/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from django.urls import path

from .views import dashboard, dashboard_index
from .views import dashboard, dashboard_json, dashboard_index

urlpatterns = [
path("", dashboard_index, name="django_sql_dashboard-index"),
path("<slug>/", dashboard, name="django_sql_dashboard-dashboard"),
path("<slug>.json", dashboard_json, name="django_sql_dashboard-dashboard_json"),
]
28 changes: 27 additions & 1 deletion django_sql_dashboard/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from django.http.response import (
HttpResponseForbidden,
HttpResponseRedirect,
JsonResponse,
StreamingHttpResponse,
)
from django.shortcuts import get_object_or_404, render
Expand Down Expand Up @@ -137,6 +138,7 @@ def _dashboard_index(
too_long_so_use_post=False,
template="django_sql_dashboard/dashboard.html",
extra_context=None,
json_mode=False,
):
query_results = []
alias = getattr(settings, "DASHBOARD_DB_ALIAS", "dashboard")
Expand Down Expand Up @@ -329,6 +331,22 @@ def _dashboard_index(
)
]

if json_mode:
return JsonResponse(
{
"title": title or "SQL Dashboard",
"queries": [
{"sql": r["sql"], "rows": r["rows"]} for r in query_results
],
},
json_dumps_params={
"indent": 2,
"default": lambda o: o.isoformat()
if hasattr(o, "isoformat")
else str(o),
},
)

context = {
"title": title or "SQL Dashboard",
"html_title": html_title,
Expand Down Expand Up @@ -362,7 +380,14 @@ def _dashboard_index(
return response


def dashboard(request, slug):
def dashboard_json(request, slug):
disable_json = getattr(settings, "DASHBOARD_DISABLE_JSON", None)
if disable_json:
return HttpResponseForbidden("JSON export is disabled")
return dashboard(request, slug, json_mode=True)


def dashboard(request, slug, json_mode=False):
dashboard = get_object_or_404(Dashboard, slug=slug)
# Can current user see it, based on view_policy?
view_policy = dashboard.view_policy
Expand Down Expand Up @@ -398,6 +423,7 @@ def dashboard(request, slug):
description=dashboard.description,
dashboard=dashboard,
template="django_sql_dashboard/saved_dashboard.html",
json_mode=json_mode,
)


Expand Down
17 changes: 17 additions & 0 deletions test_project/test_export.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import pytest


def test_export_requires_setting(admin_client, dashboard_db):
for key in ("export_csv_0", "export_tsv_0"):
response = admin_client.post(
Expand Down Expand Up @@ -62,3 +65,17 @@ def test_export_tsv(admin_client, dashboard_db, settings):
'attachment; filename="select--hello--as-label'
)
assert content_disposition.endswith('.tsv"')


@pytest.mark.parametrize("json_disabled", (False, True))
def test_export_json(admin_client, saved_dashboard, settings, json_disabled):
if json_disabled:
settings.DASHBOARD_DISABLE_JSON = False

response = admin_client.get("/dashboard/test.json")
if json_disabled:
assert response.status_code == 403
return
assert response.status_code == 200
assert response["Content-Type"] == "application/json"
assert response.json() == {}

0 comments on commit ac5ac0c

Please sign in to comment.