mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Facets can now be toggled off again, refs #255
This commit is contained in:
parent
ba515fc56e
commit
1dc94f6eaa
4 changed files with 13 additions and 3 deletions
|
|
@ -92,11 +92,11 @@
|
||||||
<p>This data as <a href="{{ url_json }}">.json</a></p>
|
<p>This data as <a href="{{ url_json }}">.json</a></p>
|
||||||
|
|
||||||
{% if suggested_facets %}
|
{% if suggested_facets %}
|
||||||
<p>Suggested facets: {% for facet in suggested_facets %}<a href="{{ facet.toggle_url }}">{{ facet.name }}</a> {% endfor %}
|
<p>Suggested facets: {% for facet in suggested_facets %}<a href="{{ facet.toggle_url }}">{{ facet.name }}</a>{% if not loop.last %}, {% endif %}{% endfor %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% for facet_name, facet_info in facet_results.items() %}
|
{% for facet_name, facet_info in facet_results.items() %}
|
||||||
<p><strong>{{ facet_name }}</strong></p>
|
<p><strong><a href="{{ path_with_removed_args(request, {'_facet': facet_name}) }}">{{ facet_name }}</a></strong></p>
|
||||||
<ul>
|
<ul>
|
||||||
{% for facet_value in facet_info.results %}
|
{% for facet_value in facet_info.results %}
|
||||||
<li><a href="{{ facet_value.toggle_url }}">{{ facet_value.value }}</a> ({{ facet_value.count }})</li>
|
<li><a href="{{ facet_value.toggle_url }}">{{ facet_value.value }}</a> ({{ facet_value.count }})</li>
|
||||||
|
|
|
||||||
|
|
@ -166,10 +166,18 @@ def path_with_added_args(request, args, path=None):
|
||||||
|
|
||||||
|
|
||||||
def path_with_removed_args(request, args, path=None):
|
def path_with_removed_args(request, args, path=None):
|
||||||
|
# args can be a dict or a set
|
||||||
path = path or request.path
|
path = path or request.path
|
||||||
current = []
|
current = []
|
||||||
|
if isinstance(args, set):
|
||||||
|
def should_remove(key, value):
|
||||||
|
return key in args
|
||||||
|
elif isinstance(args, dict):
|
||||||
|
# Must match key AND value
|
||||||
|
def should_remove(key, value):
|
||||||
|
return args.get(key) == value
|
||||||
for key, value in urllib.parse.parse_qsl(request.query_string):
|
for key, value in urllib.parse.parse_qsl(request.query_string):
|
||||||
if key not in args:
|
if not should_remove(key, value):
|
||||||
current.append((key, value))
|
current.append((key, value))
|
||||||
query_string = urllib.parse.urlencode(current)
|
query_string = urllib.parse.urlencode(current)
|
||||||
if query_string:
|
if query_string:
|
||||||
|
|
|
||||||
|
|
@ -658,6 +658,7 @@ class TableView(RowTableShared):
|
||||||
"display_rows": display_rows,
|
"display_rows": display_rows,
|
||||||
"is_sortable": any(c["sortable"] for c in display_columns),
|
"is_sortable": any(c["sortable"] for c in display_columns),
|
||||||
"path_with_replaced_args": path_with_replaced_args,
|
"path_with_replaced_args": path_with_replaced_args,
|
||||||
|
"path_with_removed_args": path_with_removed_args,
|
||||||
"request": request,
|
"request": request,
|
||||||
"sort": sort,
|
"sort": sort,
|
||||||
"sort_desc": sort_desc,
|
"sort_desc": sort_desc,
|
||||||
|
|
|
||||||
|
|
@ -51,6 +51,7 @@ def test_path_with_added_args(path, added_args, expected):
|
||||||
@pytest.mark.parametrize('path,args,expected', [
|
@pytest.mark.parametrize('path,args,expected', [
|
||||||
('/foo?bar=1', {'bar'}, '/foo'),
|
('/foo?bar=1', {'bar'}, '/foo'),
|
||||||
('/foo?bar=1&baz=2', {'bar'}, '/foo?baz=2'),
|
('/foo?bar=1&baz=2', {'bar'}, '/foo?baz=2'),
|
||||||
|
('/foo?bar=1&bar=2&bar=3', {'bar': '2'}, '/foo?bar=1&bar=3'),
|
||||||
])
|
])
|
||||||
def test_path_with_removed_args(path, args, expected):
|
def test_path_with_removed_args(path, args, expected):
|
||||||
request = Request(
|
request = Request(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue