Skip to content

Commit

Permalink
row_actions() for enriching rows, closes #41
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Mar 17, 2024
1 parent eeded41 commit c0deca1
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
33 changes: 33 additions & 0 deletions datasette_enrichments/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import json
import secrets
import traceback
import urllib
from datasette.plugins import pm
from .views import enrichment_picker, enrichment_view
from .utils import get_with_auth, mark_job_complete, pks_for_rows
Expand Down Expand Up @@ -326,6 +327,38 @@ async def inner():
return inner


@hookimpl
def row_actions(datasette, database, table, actor, row):
async def inner():
if await datasette.permission_allowed(
actor, "enrichments", resource=database, default=False
):
# query_string to select row based on its primary keys
db = datasette.get_database(database)
pks = await db.primary_keys(table)
if not pks:
pks = ["rowid"]
# Build the querystring to select this row
bits = []
for pk in pks:
bits.append((pk, row[pk]))
query_string = urllib.parse.urlencode(bits)
return [
{
"href": datasette.urls.path(
"/-/enrich/{}/{}?{}".format(
database,
tilde_encode(table),
query_string,
)
),
"label": "Enrich this row",
}
]

return inner


@hookimpl
def permission_allowed(actor, action):
# Special actor used for internal datasette.client.get() calls
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def get_long_description():
entry_points={"datasette": ["enrichments = datasette_enrichments"]},
install_requires=["datasette", "WTForms"],
extras_require={
"test": ["pytest", "pytest-asyncio", "black", "ruff"],
"test": ["pytest", "pytest-asyncio", "black", "ruff", "packaging"],
"docs": [
"sphinx==7.2.6",
"furo==2023.9.10",
Expand Down
39 changes: 39 additions & 0 deletions tests/test_enrichments.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from datasette_enrichments.utils import wait_for_job
from datasette.app import Datasette
from datasette.utils import tilde_encode
from datasette import version
from packaging.version import parse
import pytest, pytest_asyncio
import sqlite3

Expand All @@ -20,6 +22,13 @@ async def datasette(tmpdir):
db.execute("create table [foo/bar] (s text)")
db.execute("insert into [foo/bar] (s) values ('one')")
db.execute("insert into [foo/bar] (s) values ('two')")
db.execute(
"create table compound_pk_table (category text, name text, value integer, primary key (category, name))"
)
db.execute(
"insert into compound_pk_table (category, name, value) values ('dog', 'a', 34)"
)

datasette = Datasette(
[data],
metadata={
Expand Down Expand Up @@ -139,3 +148,33 @@ async def test_error_log(datasette):
"select error_count, done_count from _enrichment_jobs where id = ?", (job_id,)
).fetchone()
assert job_details == (2, 2)


@pytest.mark.asyncio
@pytest.mark.skipif(
parse(version.__version__) < parse("1.0a13"),
reason="uses row_actions() plugin hook",
)
@pytest.mark.parametrize(
"path,expected_path",
(
("/data/t/1", "t?id=1"),
("/data/rowid_table/1", "rowid_table?rowid=1"),
("/data/compound_pk_table/dog,a", "compound_pk_table?category=dog&amp;name=a"),
),
)
async def test_row_actions(datasette, path, expected_path):
cookies = {"ds_actor": datasette.sign({"a": {"id": "root"}}, "actor")}
response = await datasette.client.get(path, cookies=cookies)
assert response.status_code == 200
assert (
'<a href="/-/enrich/data/{}">Enrich this row'.format(expected_path)
in response.text
)
# And check that page offers to enrich just one row
enrich_path = "/-/enrich/data/" + response.text.split('<a href="/-/enrich/data/')[
1
].split('">')[0].replace("&amp;", "&")
enrich_page_response = await datasette.client.get(enrich_path, cookies=cookies)
assert enrich_page_response.status_code == 200
assert "1 row selected" in enrich_page_response.text

0 comments on commit c0deca1

Please sign in to comment.