Skip to content

Commit

Permalink
?_extra=request and ?_extra=extras, refs #262
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Feb 8, 2023
1 parent 999c1bc commit 732285c
Showing 1 changed file with 43 additions and 2 deletions.
45 changes: 43 additions & 2 deletions datasette/views/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1483,6 +1483,11 @@ async def _sort_order(table_metadata, sortable_columns, request, order_by):


async def table_view(datasette, request):
with tracer.trace_child_tasks():
return await table_view_traced(datasette, request)


async def table_view_traced(datasette, request):
from datasette.app import TableNotFound

try:
Expand Down Expand Up @@ -1699,6 +1704,7 @@ async def table_view(datasette, request):
extras.add("facet_results")

async def extra_count():
"Total count of rows matching these filters"
# Calculate the total count for this query
count = None
if (
Expand Down Expand Up @@ -1744,6 +1750,7 @@ async def facet_instances(extra_count):
return facet_instances

async def extra_facet_results(facet_instances):
"Results of facets calculated against this data"
facet_results = {}
facets_timed_out = []

Expand Down Expand Up @@ -1771,6 +1778,7 @@ async def extra_facet_results(facet_instances):
}

async def extra_suggested_facets(facet_instances):
"Suggestions for facets that might return interesting results"
suggested_facets = []
# Calculate suggested facets
if (
Expand All @@ -1794,6 +1802,7 @@ async def extra_suggested_facets(facet_instances):

# human_description_en combines filters AND search, if provided
async def extra_human_description_en():
"Human-readable description of the filters"
human_description_en = filters.human_description_en(
extra=extra_human_descriptions
)
Expand All @@ -1809,14 +1818,44 @@ async def extra_human_description_en():
)

async def extra_next_url():
"Full URL for the next page of results"
return next_url

async def extra_columns():
"Column names returned by this query"
return columns

async def extra_primary_keys():
"Primary keys for this table"
return pks

async def extra_debug():
"Extra debug information"
return {
"resolved": repr(resolved),
"url_vars": request.url_vars,
"nofacet": nofacet,
"nosuggest": nosuggest,
}

async def extra_request():
"Full information about the request"
return {
"url": request.url,
"path": request.path,
"full_path": request.full_path,
"host": request.host,
"args": request.args._data,
}

async def extra_extras():
"Available ?_extra= blocks"
return [{
"name": key[len("extra_"):],
"doc": fn.__doc__,
} for key, fn in registry._registry.items()
if key.startswith("extra_")]

registry = Registry(
extra_count,
extra_facet_results,
Expand All @@ -1826,6 +1865,9 @@ async def extra_primary_keys():
extra_next_url,
extra_columns,
extra_primary_keys,
extra_debug,
extra_request,
extra_extras,
)

results = await registry.resolve_multi(
Expand All @@ -1849,8 +1891,7 @@ async def extra_primary_keys():
return Response.json(
{
"debug": {
"resolved": repr(resolved),
"url_vars": request.url_vars,

},
"sql": sql,
"next": next_value,
Expand Down

0 comments on commit 732285c

Please sign in to comment.