Rename metadata= to table_config= in facet code, refs #2247

pull/2257/head
Simon Willison 2024-02-06 15:03:19 -08:00
rodzic 1e901aa690
commit 5a63ecc557
3 zmienionych plików z 23 dodań i 24 usunięć

Wyświetl plik

@ -11,8 +11,8 @@ from datasette.utils import (
) )
def load_facet_configs(request, table_metadata): def load_facet_configs(request, table_config):
# Given a request and the metadata configuration for a table, return # Given a request and the configuration for a table, return
# a dictionary of selected facets, their lists of configs and for each # a dictionary of selected facets, their lists of configs and for each
# config whether it came from the request or the metadata. # config whether it came from the request or the metadata.
# #
@ -20,21 +20,21 @@ def load_facet_configs(request, table_metadata):
# {"source": "metadata", "config": config1}, # {"source": "metadata", "config": config1},
# {"source": "request", "config": config2}]} # {"source": "request", "config": config2}]}
facet_configs = {} facet_configs = {}
table_metadata = table_metadata or {} table_config = table_config or {}
metadata_facets = table_metadata.get("facets", []) table_facet_configs = table_config.get("facets", [])
for metadata_config in metadata_facets: for facet_config in table_facet_configs:
if isinstance(metadata_config, str): if isinstance(facet_config, str):
type = "column" type = "column"
metadata_config = {"simple": metadata_config} facet_config = {"simple": facet_config}
else: else:
assert ( assert (
len(metadata_config.values()) == 1 len(facet_config.values()) == 1
), "Metadata config dicts should be {type: config}" ), "Metadata config dicts should be {type: config}"
type, metadata_config = list(metadata_config.items())[0] type, facet_config = list(facet_config.items())[0]
if isinstance(metadata_config, str): if isinstance(facet_config, str):
metadata_config = {"simple": metadata_config} facet_config = {"simple": facet_config}
facet_configs.setdefault(type, []).append( facet_configs.setdefault(type, []).append(
{"source": "metadata", "config": metadata_config} {"source": "metadata", "config": facet_config}
) )
qs_pairs = urllib.parse.parse_qs(request.query_string, keep_blank_values=True) qs_pairs = urllib.parse.parse_qs(request.query_string, keep_blank_values=True)
for key, values in qs_pairs.items(): for key, values in qs_pairs.items():
@ -45,13 +45,12 @@ def load_facet_configs(request, table_metadata):
elif key.startswith("_facet_"): elif key.startswith("_facet_"):
type = key[len("_facet_") :] type = key[len("_facet_") :]
for value in values: for value in values:
# The value is the config - either JSON or not # The value is the facet_config - either JSON or not
if value.startswith("{"): facet_config = (
config = json.loads(value) json.loads(value) if value.startswith("{") else {"simple": value}
else: )
config = {"simple": value}
facet_configs.setdefault(type, []).append( facet_configs.setdefault(type, []).append(
{"source": "request", "config": config} {"source": "request", "config": facet_config}
) )
return facet_configs return facet_configs
@ -75,7 +74,7 @@ class Facet:
sql=None, sql=None,
table=None, table=None,
params=None, params=None,
metadata=None, table_config=None,
row_count=None, row_count=None,
): ):
assert table or sql, "Must provide either table= or sql=" assert table or sql, "Must provide either table= or sql="
@ -86,12 +85,12 @@ class Facet:
self.table = table self.table = table
self.sql = sql or f"select * from [{table}]" self.sql = sql or f"select * from [{table}]"
self.params = params or [] self.params = params or []
self.metadata = metadata self.table_config = table_config
# row_count can be None, in which case we calculate it ourselves: # row_count can be None, in which case we calculate it ourselves:
self.row_count = row_count self.row_count = row_count
def get_configs(self): def get_configs(self):
configs = load_facet_configs(self.request, self.metadata) configs = load_facet_configs(self.request, self.table_config)
return configs.get(self.type) or [] return configs.get(self.type) or []
def get_querystring_pairs(self): def get_querystring_pairs(self):

Wyświetl plik

@ -1275,7 +1275,7 @@ async def table_view_data(
sql=sql_no_order_no_limit, sql=sql_no_order_no_limit,
params=params, params=params,
table=table_name, table=table_name,
metadata=table_metadata, table_config=table_metadata,
row_count=extra_count, row_count=extra_count,
) )
) )

Wyświetl plik

@ -82,7 +82,7 @@ async def test_column_facet_suggest_skip_if_enabled_by_metadata(ds_client):
database="fixtures", database="fixtures",
sql="select * from facetable", sql="select * from facetable",
table="facetable", table="facetable",
metadata={"facets": ["_city_id"]}, table_config={"facets": ["_city_id"]},
) )
suggestions = [s["name"] for s in await facet.suggest()] suggestions = [s["name"] for s in await facet.suggest()]
assert [ assert [
@ -278,7 +278,7 @@ async def test_column_facet_from_metadata_cannot_be_hidden(ds_client):
database="fixtures", database="fixtures",
sql="select * from facetable", sql="select * from facetable",
table="facetable", table="facetable",
metadata={"facets": ["_city_id"]}, table_config={"facets": ["_city_id"]},
) )
buckets, timed_out = await facet.facet_results() buckets, timed_out = await facet.facet_results()
assert [] == timed_out assert [] == timed_out