Don't show facet in cog menu if not allow_facet, closes #1683

pull/1685/head
Simon Willison 2022-03-24 12:16:19 -07:00
rodzic d431a9055e
commit c496f2b663
4 zmienionych plików z 26 dodań i 2 usunięć

Wyświetl plik

@ -128,7 +128,8 @@ var DROPDOWN_ICON_SVG = `<svg xmlns="http://www.w3.org/2000/svg" width="14" heig
} else {
hideColumn.parentNode.style.display = "none";
}
/* Only show facet if it's not the first column, not selected, not a single PK */
/* Only show "Facet by this" if it's not the first column, not selected,
not a single PK and the Datasette allow_facet setting is True */
var displayedFacets = Array.from(
document.querySelectorAll(".facet-info")
).map((el) => el.dataset.column);
@ -137,7 +138,12 @@ var DROPDOWN_ICON_SVG = `<svg xmlns="http://www.w3.org/2000/svg" width="14" heig
var isSinglePk =
th.getAttribute("data-is-pk") == "1" &&
document.querySelectorAll('th[data-is-pk="1"]').length == 1;
if (isFirstColumn || displayedFacets.includes(column) || isSinglePk) {
if (
!DATASETTE_ALLOW_FACET ||
isFirstColumn ||
displayedFacets.includes(column) ||
isSinglePk
) {
facetItem.parentNode.style.display = "none";
} else {
facetItem.parentNode.style.display = "block";

Wyświetl plik

@ -5,6 +5,7 @@
{% block extra_head %}
{{- super() -}}
<script src="{{ urls.static('table.js') }}" defer></script>
<script>DATASETTE_ALLOW_FACET = {{ datasette_allow_facet }};</script>
<style>
@media only screen and (max-width: 576px) {
{% for column in display_columns -%}

Wyświetl plik

@ -888,6 +888,9 @@ class TableView(RowTableShared):
"metadata": metadata,
"view_definition": await db.get_view_definition(table),
"table_definition": await db.get_table_definition(table),
"datasette_allow_facet": "true"
if self.ds.setting("allow_facet")
else "false",
}
d.update(extra_context_from_filters)
return d

Wyświetl plik

@ -1075,3 +1075,17 @@ def test_table_page_title(app_client, path, expected):
response = app_client.get(path)
title = Soup(response.text, "html.parser").find("title").text
assert title == expected
@pytest.mark.parametrize("allow_facet", (True, False))
def test_allow_facet_off(allow_facet):
with make_app_client(settings={"allow_facet": allow_facet}) as client:
response = client.get("/fixtures/facetable")
expected = "DATASETTE_ALLOW_FACET = {};".format(
"true" if allow_facet else "false"
)
assert expected in response.text
if allow_facet:
assert "Suggested facets" in response.text
else:
assert "Suggested facets" not in response.text