Skip to content

Commit

Permalink
Fix for row actions on _id tables, closes #42
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Mar 19, 2024
1 parent addb524 commit 6f1ee82
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
13 changes: 9 additions & 4 deletions datasette_enrichments/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,11 @@ async def inner():
"/-/enrich/{}/{}{}".format(
database,
tilde_encode(table),
"?{}".format(request.query_string)
if request.query_string
else "",
(
"?{}".format(request.query_string)
if request.query_string
else ""
),
)
),
"label": "Enrich selected data",
Expand All @@ -341,7 +343,10 @@ async def inner():
# Build the querystring to select this row
bits = []
for pk in pks:
bits.append((pk, row[pk]))
if pk.startswith("_"):
bits.append((pk + "__exact", row[pk]))
else:
bits.append((pk, row[pk]))
query_string = urllib.parse.urlencode(bits)
return [
{
Expand Down
7 changes: 4 additions & 3 deletions tests/test_enrichments.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ async def datasette(tmpdir):
db.execute("create table rowid_table (s text)")
db.execute("insert into rowid_table (s) values ('one')")
db.execute("insert into rowid_table (s) values ('two')")
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 [foo/bar] (_id integer primary key, s text)")
db.execute("insert into [foo/bar] (_id, s) values (1, 'one')")
db.execute("insert into [foo/bar] (_id, s) values (2, 'two')")
db.execute(
"create table compound_pk_table (category text, name text, value integer, primary key (category, name))"
)
Expand Down Expand Up @@ -161,6 +161,7 @@ async def test_error_log(datasette):
("/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&name=a"),
("/data/foo~2Fbar/1", "foo~2Fbar?_id__exact=1"),
),
)
async def test_row_actions(datasette, path, expected_path):
Expand Down

0 comments on commit 6f1ee82

Please sign in to comment.