From 9a759e9ddf36dc834f6a91158910fef041be5679 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sat, 17 Oct 2020 16:00:41 -0700 Subject: [PATCH] Use datasette.client, closes #28 Also refs https://github.com/simonw/datasette/issues/1020 --- dogsheep_beta/__init__.py | 25 +++++++++++-------------- setup.py | 2 +- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/dogsheep_beta/__init__.py b/dogsheep_beta/__init__.py index 8a1c5b1..9508ca8 100644 --- a/dogsheep_beta/__init__.py +++ b/dogsheep_beta/__init__.py @@ -48,6 +48,10 @@ } +class InnerResponseError(Exception): + pass + + async def beta(request, datasette): from datasette.utils.asgi import Response from datasette.utils import path_with_removed_args, path_with_replaced_args @@ -190,7 +194,6 @@ async def process_results(datasette, results, rules, template_debug=False): async def get_count_and_facets(datasette, database_name, request): - from datasette.views.table import TableView from datasette.utils.asgi import Request, Response from datasette.utils import sqlite3, escape_fts @@ -220,25 +223,19 @@ async def execute_search(searchmode_raw): doseq=True, ), ) - inner_request = Request.fake(path_with_query_string) - view = TableView(datasette) - data, _, _ = await view.data( - inner_request, - database=database_name, - hash=None, - table="search_index", - _next=None, + inner_response = await datasette.client.get( + path_with_query_string, cookies=request.cookies ) + if inner_response.status_code != 200: + raise InnerResponseError(inner_response.status_code) + data = inner_response.json() count, facets = data["filtered_table_rows_count"], data["facet_results"] return count, facets try: count, facets = await execute_search(True) - except sqlite3.OperationalError as e: - if "fts5" in str(e): - count, facets = await execute_search(False) - else: - raise + except InnerResponseError as e: + count, facets = await execute_search(False) facets = facets.values() # Rewrite toggle_url on facet_results diff --git a/setup.py b/setup.py index 34780e0..02d6b63 100644 --- a/setup.py +++ b/setup.py @@ -32,7 +32,7 @@ def get_long_description(): "datasette": ["beta = dogsheep_beta"], "console_scripts": ["dogsheep-beta = dogsheep_beta.cli:cli"], }, - install_requires=["datasette", "click", "PyYAML", "sqlite-utils"], + install_requires=["datasette>=0.50.2", "click", "PyYAML", "sqlite-utils"], extras_require={ "test": ["pytest", "pytest-asyncio", "httpx", "beautifulsoup4", "html5lib"] },