Facets can now be toggled off again, refs #255

csv
Simon Willison 2018-05-15 07:11:52 -03:00 zatwierdzone przez Simon Willison
rodzic ba515fc56e
commit 1dc94f6eaa
4 zmienionych plików z 13 dodań i 3 usunięć

Wyświetl plik

@ -92,11 +92,11 @@
<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>

Wyświetl plik

@ -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:

Wyświetl plik

@ -658,6 +658,7 @@ class TableView(RowTableShared):
"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,

Wyświetl plik

@ -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(