Skip to content

Commit

Permalink
Use datasette.client, closes #28
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Oct 17, 2020
1 parent bed9df2 commit 9a759e9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
25 changes: 11 additions & 14 deletions dogsheep_beta/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
},
Expand Down

0 comments on commit 9a759e9

Please sign in to comment.