Tests should now pass for facets branch

This commit is contained in:
Simon Willison 2019-04-16 21:08:01 -07:00
commit f6cdca3f6e
4 changed files with 23 additions and 8 deletions

View file

@ -605,7 +605,7 @@ class Datasette:
truncated = False truncated = False
except sqlite3.OperationalError as e: except sqlite3.OperationalError as e:
if e.args == ('interrupted',): if e.args == ('interrupted',):
raise InterruptedError(e) raise InterruptedError(e, sql, params)
if log_sql_errors: if log_sql_errors:
print( print(
"ERROR: conn={}, sql = {}, params = {}: {}".format( "ERROR: conn={}, sql = {}, params = {}: {}".format(

View file

@ -8,6 +8,7 @@ from datasette.utils import (
path_with_added_args, path_with_added_args,
path_with_removed_args, path_with_removed_args,
detect_json1, detect_json1,
InterruptedError,
InvalidSql, InvalidSql,
sqlite3, sqlite3,
) )
@ -142,7 +143,7 @@ class ColumnFacet(Facet):
), ),
}) })
except InterruptedError: except InterruptedError:
pass continue
return suggested_facets return suggested_facets
async def facet_results(self, sql, params): async def facet_results(self, sql, params):
@ -238,6 +239,9 @@ class ManyToManyFacet(Facet):
all_foreign_keys = await self.ds.execute_against_connection_in_thread( all_foreign_keys = await self.ds.execute_against_connection_in_thread(
self.database, get_all_foreign_keys self.database, get_all_foreign_keys
) )
if not all_foreign_keys.get(self.table):
# It's probably a view
return []
incoming = all_foreign_keys[self.table]["incoming"] incoming = all_foreign_keys[self.table]["incoming"]
# Do any of these incoming tables have exactly two outgoing keys? # Do any of these incoming tables have exactly two outgoing keys?
for fk in incoming: for fk in incoming:
@ -377,7 +381,7 @@ class DateFacet(Facet):
suggested_facet_sql = """ suggested_facet_sql = """
select date({column}) from ( select date({column}) from (
{sql} {sql}
) limit 100; ) where {column} glob "????-??-??" limit 100;
""".format( """.format(
column=escape_sqlite(column), column=escape_sqlite(column),
sql=sql, sql=sql,
@ -429,7 +433,7 @@ class DateFacet(Facet):
{sql} {sql}
) )
where date({col}) is not null where date({col}) is not null
group by date({col}) order by date({col}) desc limit {limit} group by date({col}) order by count desc limit {limit}
""".format( """.format(
col=escape_sqlite(column), col=escape_sqlite(column),
sql=sql, sql=sql,
@ -577,7 +581,6 @@ class EmojiFacet(Facet):
), ),
}) })
except (InterruptedError, sqlite3.OperationalError) as e: except (InterruptedError, sqlite3.OperationalError) as e:
print(" oh no ", e)
continue continue
return suggested_facets return suggested_facets

View file

@ -464,11 +464,14 @@ class TableView(RowTableShared):
else: else:
page_size = self.ds.page_size page_size = self.ds.page_size
sql = "select {select} from {table_name} {where}{order_by}limit {limit}{offset}".format( sql_no_limit = "select {select} from {table_name} {where}{order_by}".format(
select=select, select=select,
table_name=escape_sqlite(table), table_name=escape_sqlite(table),
where=where_clause, where=where_clause,
order_by=order_by, order_by=order_by,
)
sql = "{sql_no_limit} limit {limit}{offset}".format(
sql_no_limit=sql_no_limit.rstrip(),
limit=page_size + 1, limit=page_size + 1,
offset=offset, offset=offset,
) )
@ -498,7 +501,7 @@ class TableView(RowTableShared):
for facet in facet_instances: for facet in facet_instances:
instance_facet_results, instance_facets_timed_out = await facet.facet_results( instance_facet_results, instance_facets_timed_out = await facet.facet_results(
sql, params, sql_no_limit, params,
) )
facet_results.update(instance_facet_results) facet_results.update(instance_facet_results)
facets_timed_out.extend(instance_facets_timed_out) facets_timed_out.extend(instance_facets_timed_out)
@ -605,7 +608,7 @@ class TableView(RowTableShared):
for facet in facet_instances: for facet in facet_instances:
# TODO: ensure facet is not suggested if it is already active # TODO: ensure facet is not suggested if it is already active
# used to use 'if facet_column in facets' for this # used to use 'if facet_column in facets' for this
suggested_facets.extend(await facet.suggest(sql, params, filtered_table_rows_count)) suggested_facets.extend(await facet.suggest(sql_no_limit, params, filtered_table_rows_count))
# human_description_en combines filters AND search, if provided # human_description_en combines filters AND search, if provided
human_description_en = filters.human_description_en(extra=search_descriptions) human_description_en = filters.human_description_en(extra=search_descriptions)

View file

@ -551,3 +551,12 @@ The ``template``, ``database`` and ``table`` options can be used to return diffe
The ``datasette`` instance is provided primarily so that you can consult any plugin configuration options that may have been set, using the ``datasette.plugin_config(plugin_name)`` method documented above. The ``datasette`` instance is provided primarily so that you can consult any plugin configuration options that may have been set, using the ``datasette.plugin_config(plugin_name)`` method documented above.
The string that you return from this function will be treated as "safe" for inclusion in a ``<script>`` block directly in the page, so it is up to you to apply any necessary escaping. The string that you return from this function will be treated as "safe" for inclusion in a ``<script>`` block directly in the page, so it is up to you to apply any necessary escaping.
.. _plugin_hook_register_facet_classes:
register_facet_classes()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Return a list of additional ``Facet`` subclasses that can be used to suggest and render facets.
For examples of how this can be used, see `datasette/facets.py <https://github.com/simonw/datasette/blob/master/datasette/facets.py>`__.