From a2526cb2dfbc9dc690cc40225a0d7c7164bab7e3 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Mon, 27 Nov 2023 21:35:02 -0800 Subject: [PATCH] Fixes for Datasette < 1.0 Closes #29 --- .github/workflows/test.yml | 4 +++- datasette_enrichments/__init__.py | 8 ++++++-- datasette_enrichments/views.py | 14 +++++++++++++- setup.py | 2 +- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f8f4f78..1423766 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -11,6 +11,7 @@ jobs: strategy: matrix: python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"] + datasette-version: ["<1.0", ">=1.0a7"] steps: - uses: actions/checkout@v3 - name: Set up Python ${{ matrix.python-version }} @@ -22,7 +23,8 @@ jobs: - name: Install dependencies run: | pip install '.[test]' + - name: Install specific version of Datasette + run: pip install "datasette${{ matrix.datasette-version }}" - name: Run tests run: | pytest - diff --git a/datasette_enrichments/__init__.py b/datasette_enrichments/__init__.py index 4ce467f..5c2c481 100644 --- a/datasette_enrichments/__init__.py +++ b/datasette_enrichments/__init__.py @@ -127,7 +127,11 @@ async def enqueue( table_path = datasette.urls.table(db.name, table) response = await get_with_auth(datasette, table_path + ".json" + "?" + qs) - row_count = response.json()["count"] + filtered_data = response.json() + if "count" in filtered_data: + row_count = filtered_data["count"] + else: + row_count = filtered_data["filtered_table_rows_count"] await db.execute_write(CREATE_JOB_TABLE_SQL) def _insert(conn): @@ -189,7 +193,7 @@ async def run_enrichment(): qs = job["filter_querystring"] if next_cursor: qs += "&_next={}".format(next_cursor) - qs += "&_size={}".format(self.batch_size) + qs += "&_size={}&_shape=objects".format(self.batch_size) response = await get_with_auth(datasette, table_path + "?" + qs) rows = response.json()["rows"] if not rows: diff --git a/datasette_enrichments/views.py b/datasette_enrichments/views.py index b24233f..c74423f 100644 --- a/datasette_enrichments/views.py +++ b/datasette_enrichments/views.py @@ -36,7 +36,13 @@ async def enrichment_view(datasette, request): bits = [bit for bit in bits if bit[0] != "_sort"] # Add extras bits.extend( - [("_extra", "human_description_en"), ("_extra", "count"), ("_extra", "columns")] + [ + ("_extra", "human_description_en"), + ("_extra", "count"), + ("_extra", "columns"), + # This one makes it work for Datasette < 1.0: + ("_shape", "objects"), + ] ) # re-encode query_string = urllib.parse.urlencode(bits) @@ -46,6 +52,9 @@ async def enrichment_view(datasette, request): datasette.urls.table(database, table, "json") + "?" + query_string, ) ).json() + if "count" not in filtered_data: + # Fix for Datasette < 1.0 + filtered_data["count"] = filtered_data["filtered_table_rows_count"] # If an enrichment is selected, use that UI @@ -102,6 +111,9 @@ async def enrichment_picker(datasette, request): datasette.urls.table(database, table, "json") + "?" + query_string, ) filtered_data = response.json() + if "count" not in filtered_data: + # Fix for Datasette < 1.0 + filtered_data["count"] = filtered_data["filtered_table_rows_count"] enrichments_and_paths = [] for enrichment in enrichments.values(): diff --git a/setup.py b/setup.py index 9a578fa..09e355e 100644 --- a/setup.py +++ b/setup.py @@ -32,7 +32,7 @@ def get_long_description(): version=VERSION, packages=["datasette_enrichments"], entry_points={"datasette": ["enrichments = datasette_enrichments"]}, - install_requires=["datasette>=1.0a7", "WTForms"], + install_requires=["datasette", "WTForms"], extras_require={ "test": ["pytest", "pytest-asyncio", "black", "ruff"], "docs": [