Commit 9739fc30 authored by Romain Clement's avatar Romain Clement
Browse files

Enforce dashboard filters types (only text, date and number for now)

parent 1d55425e
......@@ -88,14 +88,14 @@ Dashboard properties:
Dashboard filters:
| Property | Type | Description |
| --------- | ------------------ | -------------------------------------------- |
| `name` | `string` | Filter display name |
| `type` | `string` | Filter type (mapped to HTML form input type) |
| `default` | `string`, `number` | (optional) Filter default value |
| `min` | `number` | (optional) Filter minimum value |
| `max` | `number` | (optional) Filter maximum value |
| `step` | `number` | (optional) Filter stepping value |
| Property | Type | Description |
| --------- | ------------------ | -------------------------------------- |
| `name` | `string` | Filter display name |
| `type` | `string` | Filter type (`text`, `date`, `number`) |
| `default` | `string`, `number` | (optional) Filter default value |
| `min` | `number` | (optional) Filter minimum value |
| `max` | `number` | (optional) Filter maximum value |
| `step` | `number` | (optional) Filter stepping value |
Common chart properties for all chart types:
......
......@@ -50,9 +50,10 @@
<form method="GET">
{% for key, dfilter in dashboard.filters.items() %}
{% set dfilter_type = dfilter.type if dfilter.type in ['text', 'date', 'number'] else 'text' %}
<p>
<label for="{{ key }}">{{ dfilter.name }}</label>
<input id="{{ key }}" name="{{ key }}" type="{{ dfilter.type }}"{% if dfilter.min is defined %} min="{{ dfilter.min }}"{% endif %}{% if dfilter.max is defined %} max="{{ dfilter.max }}"{% endif %}{% if dfilter.step is defined %} step="{{ dfilter.step }}"{% endif %} value="{% if key in query_parameters.keys() %}{{ query_parameters[key] }}{% else %}{{ dfilter.default }}{% endif %}">
<input id="{{ key }}" name="{{ key }}" type="{{ dfilter_type }}"{% if dfilter.min is defined %} min="{{ dfilter.min }}"{% endif %}{% if dfilter.max is defined %} max="{{ dfilter.max }}"{% endif %}{% if dfilter.step is defined %} step="{{ dfilter.step }}"{% endif %} value="{% if key in query_parameters.keys() %}{{ query_parameters[key] }}{% else %}{{ dfilter.default }}{% endif %}">
</p>
{% endfor %}
<input type="submit" value="Apply">
......
......@@ -46,6 +46,21 @@ def datasette_metadata():
"type": "date",
"default": "2021-12-31",
},
"text_filter": {
"name": "Text Filter",
"type": "text",
},
"number_filter": {
"name": "Number Filter",
"type": "number",
"min": 12,
"max": 42,
"step": 2,
},
"unknown_filter": {
"name": "Unknown Filter",
"type": "unknown",
},
},
"charts": {
"analysis-note": {
......
......@@ -16,9 +16,13 @@ async def test_dashboard_views(datasette):
assert '<details class="dashboard-filters">' in response.text
for key, flt in dashboard["filters"].items():
expected_type = (
flt["type"] if flt["type"] in ["text", "date", "number"] else "text"
)
assert f'<label for="{key}">{flt["name"]}</label>' in response.text
assert (
f'<input id="{key}" name="{key}" type="{flt["type"]}"' in response.text
f'<input id="{key}" name="{key}" type="{expected_type}"'
in response.text
)
for chart_slug, chart in dashboard["charts"].items():
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment