From 6284b7329c104a2ca8a2cfaff7b3421a4da95c59 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Thu, 29 Oct 2020 22:50:12 -0700 Subject: [PATCH] Use table_actions plugin hook and Datasette 0.51a1, closes #26 --- datasette_edit_schema/__init__.py | 17 +++++++++++++++++ setup.py | 4 ++-- tests/test_edit_schema.py | 20 ++++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/datasette_edit_schema/__init__.py b/datasette_edit_schema/__init__.py index 35b3c23..0151020 100644 --- a/datasette_edit_schema/__init__.py +++ b/datasette_edit_schema/__init__.py @@ -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 [ diff --git a/setup.py b/setup.py index d61f126..f5576d6 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ from setuptools import setup import os -VERSION = "0.3" +VERSION = "0.3a" def get_long_description(): @@ -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"]}, diff --git a/tests/test_edit_schema.py b/tests/test_edit_schema.py index 3625395..3315950 100644 --- a/tests/test_edit_schema.py +++ b/tests/test_edit_schema.py @@ -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 = ( + '
  • Edit table schema
  • ' + ) + 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])