From 5bc7680f5aeba3587df188b26dd893c219fef974 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Tue, 23 Apr 2024 18:32:23 -0700 Subject: [PATCH] Switch to tilde-encoded table names, refs #62 --- datasette_edit_schema/__init__.py | 7 ++-- .../templates/edit_schema_table.html | 16 ++++---- tests/conftest.py | 15 ++++++++ tests/test_edit_schema.py | 37 +++++++++++++++---- 4 files changed, 57 insertions(+), 18 deletions(-) diff --git a/datasette_edit_schema/__init__.py b/datasette_edit_schema/__init__.py index d92fb8d..b71726e 100644 --- a/datasette_edit_schema/__init__.py +++ b/datasette_edit_schema/__init__.py @@ -1,7 +1,7 @@ from datasette import hookimpl from datasette.events import CreateTableEvent, AlterTableEvent, DropTableEvent from datasette.utils.asgi import Response, NotFound, Forbidden -from datasette.utils import sqlite3 +from datasette.utils import sqlite3, tilde_decode, tilde_encode from urllib.parse import quote_plus, unquote_plus import sqlite_utils import textwrap @@ -40,7 +40,7 @@ async def inner(): return [ { "href": datasette.urls.path( - "/-/edit-schema/{}/{}".format(database, quote_plus(table)) + "/-/edit-schema/{}/{}".format(database, tilde_encode(table)) ), "label": "Edit table schema", "description": "Rename the table, add and remove columns...", @@ -307,7 +307,7 @@ def create_the_table(conn): async def edit_schema_table(request, datasette): - table = unquote_plus(request.url_vars["table"]) + table = tilde_decode(request.url_vars["table"]) databases = get_databases(datasette) database_name = request.url_vars["database"] @@ -619,6 +619,7 @@ def get_columns_and_schema_and_fks_and_pks_and_indexes(conn): "can_rename_table": await can_rename_table( datasette, request.actor, database_name, table ), + "tilde_encode": tilde_encode, }, request=request, ) diff --git a/datasette_edit_schema/templates/edit_schema_table.html b/datasette_edit_schema/templates/edit_schema_table.html index 5d2b704..0dda850 100644 --- a/datasette_edit_schema/templates/edit_schema_table.html +++ b/datasette_edit_schema/templates/edit_schema_table.html @@ -86,12 +86,12 @@ {% endblock %} {% block content %} -

Edit table {{ database.name }}/{{ table }}

+

Edit table {{ database.name }}/{{ table }}

{% if can_rename_table %}

Rename table

-
+

@@ -99,7 +99,7 @@

Rename table

{% endif %} -
+

Change existing columns