Extract facet code out into a new plugin hook, closes #427 (#445)

Datasette previously only supported one type of faceting: exact column value counting.

With this change, faceting logic is extracted out into one or more separate classes which can implement other patterns of faceting - this is discussed in #427, but potential upcoming facet types include facet-by-date, facet-by-JSON-array, facet-by-many-2-many and more.

A new plugin hook, register_facet_classes, can be used by plugins to add in additional facet classes.

Each class must implement two methods: suggest(), which scans columns in the table to decide if they might be worth suggesting for faceting, and facet_results(), which executes the facet operation and returns results ready to be displayed in the UI.
This commit is contained in:
Simon Willison 2019-05-02 17:11:26 -07:00 committed by GitHub
commit ea66c45df9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 600 additions and 132 deletions

8
tests/utils.py Normal file
View file

@ -0,0 +1,8 @@
class MockRequest:
def __init__(self, url):
self.url = url
self.path = "/" + url.split("://")[1].split("/", 1)[1]
self.query_string = ""
if "?" in url:
self.query_string = url.split("?", 1)[1]
self.path = self.path.split("?")[0]