kopia lustrzana https://github.com/simonw/datasette
Fixed bug with metadata config of array/date facets, closes #1552
Thanks @davidbgk for spotting the fix for the bug.pull/1562/head
rodzic
40e5b0a5b5
commit
20a2ed6bec
|
@ -30,7 +30,7 @@ def load_facet_configs(request, table_metadata):
|
||||||
assert (
|
assert (
|
||||||
len(metadata_config.values()) == 1
|
len(metadata_config.values()) == 1
|
||||||
), "Metadata config dicts should be {type: config}"
|
), "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):
|
if isinstance(metadata_config, str):
|
||||||
metadata_config = {"simple": metadata_config}
|
metadata_config = {"simple": metadata_config}
|
||||||
facet_configs.setdefault(type, []).append(
|
facet_configs.setdefault(type, []).append(
|
||||||
|
|
|
@ -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
|
/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": {
|
"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.
|
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": {
|
"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.
|
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
|
Suggested facets
|
||||||
----------------
|
----------------
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ from datasette.database import Database
|
||||||
from datasette.facets import ColumnFacet, ArrayFacet, DateFacet
|
from datasette.facets import ColumnFacet, ArrayFacet, DateFacet
|
||||||
from datasette.utils.asgi import Request
|
from datasette.utils.asgi import Request
|
||||||
from datasette.utils import detect_json1
|
from datasette.utils import detect_json1
|
||||||
from .fixtures import app_client # noqa
|
from .fixtures import app_client, make_app_client # noqa
|
||||||
import json
|
import json
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
@ -588,3 +588,26 @@ async def test_facet_size():
|
||||||
)
|
)
|
||||||
data5 = response5.json()
|
data5 = response5.json()
|
||||||
assert len(data5["facet_results"]["city"]["results"]) == 20
|
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
|
||||||
|
|
Ładowanie…
Reference in New Issue