Skip to content

Commit

Permalink
Use new homepage_actions() plugin hook
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Mar 13, 2024
1 parent 58d9bd2 commit d870d29
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 42 deletions.
41 changes: 12 additions & 29 deletions datasette_metadata_editable/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,38 +255,21 @@ def register_routes():


@hookimpl
async def extra_body_script(
template, database, table, columns, view_name, request, datasette
):
if not request or not await datasette.permission_allowed(
request.actor, PERMISSION_EDIT_METADATA, default=False
):
return ""
if view_name == "index":
url = "/-/datasette-metadata-editable/edit?"
return f"""
const editMetadata = document.createElement("a")
editMetadata.textContent = "Edit metadata"
editMetadata.setAttribute('href', {json.dumps(url)})
const metadataElement = document.querySelector('.metadata-description');
if(metadataElement)
metadataElement.appendChild(editMetadata)
else
(document.querySelector('.page-header') || document.querySelector('h1')).after(editMetadata)
"""
return ""


@hookimpl
def extra_js_urls(template, database, table, columns, view_name, request, datasette):
if view_name in {"table", "query", "database"}:
def homepage_actions(datasette, actor):
async def inner():
if not await datasette.permission_allowed(
actor, PERMISSION_EDIT_METADATA, default=False
):
return []
return [
datasette.urls.path(
"/-/static-plugins/datasette-metadata-editable/plugin.js"
)
{
"href": datasette.urls.path("/-/datasette-metadata-editable/edit"),
"label": "Edit instance metadata",
}
]

return inner


@hookimpl
def database_actions(datasette, actor, database):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ classifiers=[
]
requires-python = ">=3.8"
dependencies = [
"datasette>=1.0a7",
"datasette>=1.0a13",
"markdown2>=2.4.10",
"nh3==0.2.14",
"sqlite-migrate>=0.1b0",
Expand Down
53 changes: 41 additions & 12 deletions tests/test_metadata_editable.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,48 @@ async def test_plugin_is_installed():
assert "datasette-metadata-editable" in installed_plugins


@pytest.mark.asyncio
@pytest.mark.parametrize(
"path,expected_url",
(
("/", "/-/datasette-metadata-editable/edit"),
(
"/test_action_menus",
"/-/datasette-metadata-editable/edit?db=test_action_menus",
),
(
"/test_action_menus/foo",
"/-/datasette-metadata-editable/edit?db=test_action_menus&table=foo",
),
),
)
async def test_action_menus(path, expected_url):
datasette = Datasette(
config={"permissions": {"datasette-metadata-editable-edit": {"id": ["root"]}}},
)
db = datasette.add_memory_database("test_action_menus")
await db.execute_write("create table if not exists foo (id integer primary key)")
fragment = '"{}"'.format(expected_url)
# Anonymous
anon_response = await datasette.client.get(path)
assert fragment not in anon_response.text
# User but not the root user
user_response = await datasette.client.get(
path, cookies={"ds_actor": datasette.client.actor_cookie({"id": "user"})}
)
assert fragment not in user_response.text
# root user
root_response = await datasette.client.get(
path, cookies={"ds_actor": datasette.client.actor_cookie({"id": "root"})}
)
assert fragment in root_response.text


@pytest.mark.asyncio
async def test_basic(snapshot):
datasette = Datasette(
memory=True,
metadata={
"permissions": {"datasette-metadata-editable-edit": {"id": ["root"]}}
},
config={"permissions": {"datasette-metadata-editable-edit": {"id": ["root"]}}},
)
assert datasette.metadata("title") is None

Expand Down Expand Up @@ -62,9 +97,7 @@ async def all_entries():
async def test_edit_table():
datasette = Datasette(
memory=True,
metadata={
"permissions": {"datasette-metadata-editable-edit": {"id": ["root"]}}
},
config={"permissions": {"datasette-metadata-editable-edit": {"id": ["root"]}}},
)
db = datasette.add_memory_database("test-db-with-hyphens")
await db.execute_write(
Expand Down Expand Up @@ -172,9 +205,7 @@ async def test_metadata_survives_server_restart(tmpdir):
sqlite_utils.Database(path).vacuum()
datasette = Datasette(
[one, two],
metadata={
"permissions": {"datasette-metadata-editable-edit": {"id": ["root"]}}
},
config={"permissions": {"datasette-metadata-editable-edit": {"id": ["root"]}}},
internal=internal,
)
db = datasette.get_database("one")
Expand Down Expand Up @@ -218,9 +249,7 @@ async def test_metadata_survives_server_restart(tmpdir):
# Now restart the server
datasette2 = Datasette(
[one, two],
metadata={
"permissions": {"datasette-metadata-editable-edit": {"id": ["root"]}}
},
config={"permissions": {"datasette-metadata-editable-edit": {"id": ["root"]}}},
internal=internal,
pdb=True,
)
Expand Down

0 comments on commit d870d29

Please sign in to comment.