Skip to content

Add title, description, settings and custom template to dashboard queries #156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
inital changes for dashboard queries
  • Loading branch information
drkane committed Oct 13, 2021
commit 65c1416021469132e2d2c66ce503d2c313ec050c
7 changes: 6 additions & 1 deletion django_sql_dashboard/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ def has_change_permission(self, request, obj=None):

def get_readonly_fields(self, request, obj=None):
if not request.user.has_perm("django_sql_dashboard.execute_sql"):
return ("sql", "title", "description", "settings", )
return (
"sql",
"title",
"description",
"settings",
)
else:
return tuple()

Expand Down
22 changes: 12 additions & 10 deletions django_sql_dashboard/migrations/0005_auto_20211013_0953.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,30 @@
class Migration(migrations.Migration):

dependencies = [
('django_sql_dashboard', '0004_add_description_help_text'),
("django_sql_dashboard", "0004_add_description_help_text"),
]

operations = [
migrations.AddField(
model_name='dashboardquery',
name='created_at',
model_name="dashboardquery",
name="created_at",
field=models.DateTimeField(default=django.utils.timezone.now),
),
migrations.AddField(
model_name='dashboardquery',
name='description',
field=models.TextField(blank=True, help_text='Optional description (Markdown allowed)'),
model_name="dashboardquery",
name="description",
field=models.TextField(
blank=True, help_text="Optional description (Markdown allowed)"
),
),
migrations.AddField(
model_name='dashboardquery',
name='settings',
model_name="dashboardquery",
name="settings",
field=models.JSONField(blank=True, null=True),
),
migrations.AddField(
model_name='dashboardquery',
name='title',
model_name="dashboardquery",
name="title",
field=models.CharField(blank=True, max_length=128),
),
]
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<div class="query-results">
{% if result.title %}<h2>{{ result.title }}</h2>{% endif %}
{% if result.description %}
{{ result.description|sql_dashboard_markdown }}
{% endif %}
{% block widget_results %}{% endblock %}
<details><summary style="font-size: 0.7em; margin-bottom: 0.5em; cursor: pointer;">SQL query</summary>
{% if saved_dashboard %}<pre class="sql">{{ result.sql }}</pre>{% else %}<textarea
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
{% load django_sql_dashboard %}<div class="query-results" id="query-results-{{ result.index }}">
{% if saved_dashboard %}<details><summary style="cursor: pointer;">SQL query</summary><pre class="sql">{{ result.sql }}</pre>{% else %}<textarea
{% if saved_dashboard %}
{% if result.title %}<h2>{{ result.title }}</h2>{% endif %}
{% if result.description %}
{{ result.description|sql_dashboard_markdown }}
{% endif %}
<details>
<summary style="cursor: pointer;">SQL query</summary>
<pre class="sql">{{ result.sql }}</pre>
{% else %}
{{ save_query_form.as_p }}
<textarea
name="sql"
rows="{{ result.textarea_rows }}"
>{{ result.sql|default:"" }}</textarea>{% endif %}
>{{ result.sql|default:"" }}</textarea>
{% endif %}
{% if not saved_dashboard %}<p>
<input
class="btn"
type="submit"
value="Run quer{% if query_results|length > 1 %}ies{% else %}y{% endif %}"
/>
</p>{% else %}</details>{% endif %}
</p>
{% else %}
</details>
{% endif %}
{% if result.truncated %}
<p class="results-truncated">
Results were truncated
Expand Down
36 changes: 33 additions & 3 deletions django_sql_dashboard/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.db import connections
from django.db.models import query
from django.db.utils import ProgrammingError
from django.forms import CharField, ModelForm, Textarea
from django.forms import CharField, ModelForm, Textarea, inlineformset_factory
from django.http.response import (
HttpResponseForbidden,
HttpResponseRedirect,
Expand All @@ -20,7 +21,7 @@

from psycopg2.extensions import quote_ident

from .models import Dashboard
from .models import Dashboard, DashboardQuery
from .utils import (
apply_sort,
check_for_base64_upgrade,
Expand Down Expand Up @@ -58,13 +59,34 @@ class Meta:
}


DashboardQueryFormSet = inlineformset_factory(
Dashboard,
DashboardQuery,
fields=(
"title",
"description",
"sql",
),
widgets={
"description": Textarea(
attrs={
"placeholder": "Optional description, markdown allowed",
"rows": 4,
}
)
},
# extra=1,
)


@login_required
def dashboard_index(request):
if not request.user.has_perm("django_sql_dashboard.execute_sql"):
return HttpResponseForbidden("You do not have permission to execute SQL")
sql_queries = []
too_long_so_use_post = False
save_form = SaveDashboardForm(prefix="_save")
save_query_form = DashboardQueryFormSet(prefix="_save_query")
if request.method == "POST":
# Is this an export?
if any(
Expand Down Expand Up @@ -114,6 +136,14 @@ def dashboard_index(request):
sql_queries.append(sql)
else:
unverified_sql_queries.append(sql)
save_query_form_data = {
'_save_query-TOTAL_FORMS': str(len(sql_queries) + 1),
'_save_query-INITIAL_FORMS': str(0),
}
# for k, sql in enumerate(sql_queries):
# save_query_form_data[f"_save_query-{k}-sql"] = sql
save_query_form = DashboardQueryFormSet(initial=[{"sql": sql} for sql in sql_queries], prefix="_save_query")
print(save_query_form.as_table())
if getattr(settings, "DASHBOARD_UPGRADE_OLD_BASE64_LINKS", None):
redirect_querystring = check_for_base64_upgrade(sql_queries)
if redirect_querystring:
Expand All @@ -123,7 +153,7 @@ def dashboard_index(request):
sql_queries,
unverified_sql_queries=unverified_sql_queries,
too_long_so_use_post=too_long_so_use_post,
extra_context={"save_form": save_form},
extra_context={"save_form": save_form, "save_query_form": save_query_form},
)


Expand Down