Persist show/hide state better, closes #425

This commit is contained in:
Simon Willison 2019-04-11 22:00:47 -07:00
commit e11cb4c664
3 changed files with 13 additions and 2 deletions

View file

@ -1,5 +1,5 @@
<script>
var editor = CodeMirror.fromTextArea(document.getElementsByName("sql")[0], {
var editor = CodeMirror.fromTextArea(document.getElementById("sql-editor"), {
lineNumbers: true,
mode: "text/x-sql",
lineWrapping: true,

View file

@ -29,10 +29,13 @@
<h3>Custom SQL query{% if display_rows %} returning {% if truncated %}more than {% endif %}{{ "{:,}".format(display_rows|length) }} row{% if display_rows|length == 1 %}{% else %}s{% endif %}{% endif %} <span class="show-hide-sql">{% if hide_sql %}(<a href="{{ path_with_removed_args(request, {'_hide_sql': '1'}) }}">show</a>){% else %}(<a href="{{ path_with_added_args(request, {'_hide_sql': '1'}) }}">hide</a>){% endif %}</span></h3>
{% if not hide_sql %}
{% if editable and config.allow_sql %}
<p><textarea name="sql">{% if query and query.sql %}{{ query.sql }}{% else %}select * from {{ tables[0].name|escape_sqlite }}{% endif %}</textarea></p>
<p><textarea id="sql-editor" name="sql">{% if query and query.sql %}{{ query.sql }}{% else %}select * from {{ tables[0].name|escape_sqlite }}{% endif %}</textarea></p>
{% else %}
<pre>{% if query %}{{ query.sql }}{% endif %}</pre>
{% endif %}
{% else %}
<input type="hidden" name="sql" value="{% if query and query.sql %}{{ query.sql }}{% else %}select * from {{ tables[0].name|escape_sqlite }}{% endif %}">
<input type="hidden" name="_hide_sql" value="1">
{% endif %}
{% if named_parameter_values %}
<h3>Query parameters</h3>

View file

@ -861,3 +861,11 @@ def test_show_hide_sql_query(app_client):
assert not span.find("a")["href"].endswith("&_hide_sql=1")
assert "(show)" == span.getText()
assert soup.find("textarea") is None
# The SQL should still be there in a hidden form field
hiddens = soup.find("form").select("input[type=hidden]")
assert [
('sql', "select ('https://twitter.com/' || 'simonw') as user_url;"),
('_hide_sql', '1'),
] == [
(hidden['name'], hidden['value']) for hidden in hiddens
]