Skip to content

Commit

Permalink
Use table_actions plugin hook and Datasette 0.51a1, closes #26
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Oct 30, 2020
1 parent d08c321 commit 6284b73
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
17 changes: 17 additions & 0 deletions datasette_edit_schema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@ def permission_allowed(actor, action):
return True


@hookimpl
def table_actions(datasette, actor, database, table):
async def inner():
if not await datasette.permission_allowed(actor, "edit-schema", default=False):
return []
return [
{
"href": datasette.urls.path(
"/-/edit-schema/{}/{}".format(database, quote_plus(table))
),
"label": "Edit table schema",
}
]

return inner


@hookimpl
def register_routes():
return [
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from setuptools import setup
import os

VERSION = "0.3"
VERSION = "0.3a"


def get_long_description():
Expand All @@ -24,7 +24,7 @@ def get_long_description():
packages=["datasette_edit_schema"],
entry_points={"datasette": ["edit_schema = datasette_edit_schema"]},
install_requires=[
"datasette>=0.44",
"datasette>=0.51a1",
"sqlite-utils>=2.21",
],
extras_require={"test": ["pytest", "pytest-asyncio", "httpx"]},
Expand Down
20 changes: 20 additions & 0 deletions tests/test_edit_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,26 @@ async def test_csrf_required(db_path):
assert 403 == response.status_code


@pytest.mark.parametrize("authenticate", [True, False])
@pytest.mark.asyncio
async def test_table_actions(db_path, authenticate):
ds = Datasette([db_path])
async with httpx.AsyncClient(app=ds.app()) as client:
cookies = None
if authenticate:
cookies = {"ds_actor": ds.sign({"a": {"id": "root"}}, "actor")}
response = await client.get("http://localhost/data/creatures", cookies=cookies)
assert response.status_code == 200
fragment = (
'<li><a href="/-/edit-schema/data/creatures">Edit table schema</a></li>'
)
if authenticate:
# Should have column actions
assert fragment in response.text
else:
assert fragment not in response.text


@pytest.mark.asyncio
async def test_post_without_operation_raises_error(db_path):
ds = Datasette([db_path])
Expand Down

0 comments on commit 6284b73

Please sign in to comment.