Skip to content

Commit

Permalink
Rename metadata= to table_config= in facet code, refs #2247
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Feb 6, 2024
1 parent 1e901aa commit 5a63ecc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
41 changes: 20 additions & 21 deletions datasette/facets.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,30 @@
)


def load_facet_configs(request, table_metadata):
# Given a request and the metadata configuration for a table, return
def load_facet_configs(request, table_config):
# Given a request and the configuration for a table, return
# a dictionary of selected facets, their lists of configs and for each
# config whether it came from the request or the metadata.
#
# return {type: [
# {"source": "metadata", "config": config1},
# {"source": "request", "config": config2}]}
facet_configs = {}
table_metadata = table_metadata or {}
metadata_facets = table_metadata.get("facets", [])
for metadata_config in metadata_facets:
if isinstance(metadata_config, str):
table_config = table_config or {}
table_facet_configs = table_config.get("facets", [])
for facet_config in table_facet_configs:
if isinstance(facet_config, str):
type = "column"
metadata_config = {"simple": metadata_config}
facet_config = {"simple": facet_config}
else:
assert (
len(metadata_config.values()) == 1
len(facet_config.values()) == 1
), "Metadata config dicts should be {type: config}"
type, metadata_config = list(metadata_config.items())[0]
if isinstance(metadata_config, str):
metadata_config = {"simple": metadata_config}
type, facet_config = list(facet_config.items())[0]
if isinstance(facet_config, str):
facet_config = {"simple": facet_config}
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)
for key, values in qs_pairs.items():
Expand All @@ -45,13 +45,12 @@ def load_facet_configs(request, table_metadata):
elif key.startswith("_facet_"):
type = key[len("_facet_") :]
for value in values:
# The value is the config - either JSON or not
if value.startswith("{"):
config = json.loads(value)
else:
config = {"simple": value}
# The value is the facet_config - either JSON or not
facet_config = (
json.loads(value) if value.startswith("{") else {"simple": value}
)
facet_configs.setdefault(type, []).append(
{"source": "request", "config": config}
{"source": "request", "config": facet_config}
)
return facet_configs

Expand All @@ -75,7 +74,7 @@ def __init__(
sql=None,
table=None,
params=None,
metadata=None,
table_config=None,
row_count=None,
):
assert table or sql, "Must provide either table= or sql="
Expand All @@ -86,12 +85,12 @@ def __init__(
self.table = table
self.sql = sql or f"select * from [{table}]"
self.params = params or []
self.metadata = metadata
self.table_config = table_config
# row_count can be None, in which case we calculate it ourselves:
self.row_count = row_count

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 []

def get_querystring_pairs(self):
Expand Down
2 changes: 1 addition & 1 deletion datasette/views/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1275,7 +1275,7 @@ async def facet_instances(extra_count):
sql=sql_no_order_no_limit,
params=params,
table=table_name,
metadata=table_metadata,
table_config=table_metadata,
row_count=extra_count,
)
)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_facets.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ async def test_column_facet_suggest_skip_if_enabled_by_metadata(ds_client):
database="fixtures",
sql="select * from facetable",
table="facetable",
metadata={"facets": ["_city_id"]},
table_config={"facets": ["_city_id"]},
)
suggestions = [s["name"] for s in await facet.suggest()]
assert [
Expand Down Expand Up @@ -278,7 +278,7 @@ async def test_column_facet_from_metadata_cannot_be_hidden(ds_client):
database="fixtures",
sql="select * from facetable",
table="facetable",
metadata={"facets": ["_city_id"]},
table_config={"facets": ["_city_id"]},
)
buckets, timed_out = await facet.facet_results()
assert [] == timed_out
Expand Down

0 comments on commit 5a63ecc

Please sign in to comment.