Skip to content

Commit

Permalink
Facets can now be toggled off again, refs #255
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed May 15, 2018
1 parent 89af6f0 commit 5b2afb7
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
4 changes: 2 additions & 2 deletions datasette/templates/table.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ <h3>{% if filtered_table_rows_count or filtered_table_rows_count == 0 %}{{ "{:,}
<p>This data as <a href="{{ url_json }}">.json</a></p>

{% 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 %}

{% 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>
{% for facet_value in facet_info.results %}
<li><a href="{{ facet_value.toggle_url }}">{{ facet_value.value }}</a> ({{ facet_value.count }})</li>
Expand Down
10 changes: 9 additions & 1 deletion datasette/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,18 @@ def path_with_added_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
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):
if key not in args:
if not should_remove(key, value):
current.append((key, value))
query_string = urllib.parse.urlencode(current)
if query_string:
Expand Down
1 change: 1 addition & 0 deletions datasette/views/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,7 @@ async def extra_template():
"display_rows": display_rows,
"is_sortable": any(c["sortable"] for c in display_columns),
"path_with_replaced_args": path_with_replaced_args,
"path_with_removed_args": path_with_removed_args,
"request": request,
"sort": sort,
"sort_desc": sort_desc,
Expand Down
1 change: 1 addition & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def test_path_with_added_args(path, added_args, expected):
@pytest.mark.parametrize('path,args,expected', [
('/foo?bar=1', {'bar'}, '/foo'),
('/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):
request = Request(
Expand Down

0 comments on commit 5b2afb7

Please sign in to comment.