Fixed bug with metadata config of array/date facets, closes #1552

Thanks @davidbgk for spotting the fix for the bug.
pull/1562/head
Simon Willison 2021-12-16 10:47:22 -08:00
rodzic 40e5b0a5b5
commit 20a2ed6bec
3 zmienionych plików z 43 dodań i 4 usunięć

Wyświetl plik

@ -30,7 +30,7 @@ def load_facet_configs(request, table_metadata):
assert (
len(metadata_config.values()) == 1
), "Metadata config dicts should be {type: config}"
type, metadata_config = metadata_config.items()[0]
type, metadata_config = list(metadata_config.items())[0]
if isinstance(metadata_config, str):
metadata_config = {"simple": metadata_config}
facet_configs.setdefault(type, []).append(

Wyświetl plik

@ -16,7 +16,9 @@ To turn on faceting for specific columns on a Datasette table view, add one or m
/dbname/tablename?_facet=state&_facet=city_id
This works for both the HTML interface and the ``.json`` view. When enabled, facets will cause a ``facet_results`` block to be added to the JSON output, looking something like this::
This works for both the HTML interface and the ``.json`` view. When enabled, facets will cause a ``facet_results`` block to be added to the JSON output, looking something like this:
.. code-block:: json
{
"state": {
@ -93,7 +95,9 @@ Facets in metadata.json
You can turn facets on by default for specific tables by adding them to a ``"facets"`` key in a Datasette :ref:`metadata` file.
Here's an example that turns on faceting by default for the ``qLegalStatus`` column in the ``Street_Tree_List`` table in the ``sf-trees`` database::
Here's an example that turns on faceting by default for the ``qLegalStatus`` column in the ``Street_Tree_List`` table in the ``sf-trees`` database:
.. code-block:: json
{
"databases": {
@ -109,6 +113,18 @@ Here's an example that turns on faceting by default for the ``qLegalStatus`` col
Facets defined in this way will always be shown in the interface and returned in the API, regardless of the ``_facet`` arguments passed to the view.
You can specify :ref:`array <facet_by_json_array>` or :ref:`date <facet_by_date>` facets in metadata using JSON objects with a single key of ``array`` or ``date`` and a value specifying the column, like this:
.. code-block:: json
{
"facets": [
{"array": "tags"},
{"date": "created"}
]
}
Suggested facets
----------------

Wyświetl plik

@ -3,7 +3,7 @@ from datasette.database import Database
from datasette.facets import ColumnFacet, ArrayFacet, DateFacet
from datasette.utils.asgi import Request
from datasette.utils import detect_json1
from .fixtures import app_client # noqa
from .fixtures import app_client, make_app_client # noqa
import json
import pytest
@ -588,3 +588,26 @@ async def test_facet_size():
)
data5 = response5.json()
assert len(data5["facet_results"]["city"]["results"]) == 20
def test_other_types_of_facet_in_metadata():
with make_app_client(
metadata={
"databases": {
"fixtures": {
"tables": {
"facetable": {
"facets": ["state", {"array": "tags"}, {"date": "created"}]
}
}
}
}
}
) as client:
response = client.get("/fixtures/facetable")
for fragment in (
"<strong>created (date)\n",
"<strong>tags (array)\n",
"<strong>state\n",
):
assert fragment in response.text