Correctly persist selected facets in hidden fields

Closes #963
pull/977/head
Simon Willison 2020-09-12 14:54:01 -07:00
rodzic 20b1de86a1
commit ea340cf320
3 zmienionych plików z 13 dodań i 6 usunięć

Wyświetl plik

@ -88,9 +88,6 @@
</div>
<label class="sort_by_desc small-screen-only"><input type="checkbox" name="_sort_by_desc"{% if sort_desc %} checked{% endif %}> descending</label>
{% endif %}
{% for facet in sorted_facet_results %}
<input type="hidden" name="_facet" value="{{ facet.name }}">
{% endfor %}
{% for key, value in form_hidden_args %}
<input type="hidden" name="{{ key }}" value="{{ value }}">
{% endfor %}

Wyświetl plik

@ -778,6 +778,12 @@ class TableView(RowTableShared):
)
self.ds.update_with_inherited_metadata(metadata)
form_hidden_args = []
# Add currently selected facets
for arg in special_args:
if arg == "_facet" or arg.startswith("_facet_"):
form_hidden_args.extend(
(arg, item) for item in request.args.getlist(arg)
)
for arg in ("_fts_table", "_fts_pk"):
if arg in special_args:
form_hidden_args.append((arg, special_args[arg]))

Wyświetl plik

@ -448,12 +448,16 @@ def test_facet_display(app_client):
def test_facets_persist_through_filter_form(app_client):
response = app_client.get("/fixtures/facetable?_facet=planet_int&_facet=city_id")
response = app_client.get(
"/fixtures/facetable?_facet=planet_int&_facet=city_id&_facet_array=tags"
)
assert response.status == 200
inputs = Soup(response.body, "html.parser").find("form").findAll("input")
hiddens = [i for i in inputs if i["type"] == "hidden"]
assert [("_facet", "city_id"), ("_facet", "planet_int")] == [
(hidden["name"], hidden["value"]) for hidden in hiddens
assert [(hidden["name"], hidden["value"]) for hidden in hiddens] == [
("_facet", "planet_int"),
("_facet", "city_id"),
("_facet_array", "tags"),
]