Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove obsolete table POST code #2041

Closed
simonw opened this issue Mar 21, 2023 · 2 comments
Closed

Remove obsolete table POST code #2041

simonw opened this issue Mar 21, 2023 · 2 comments

Comments

@simonw
Copy link
Owner

simonw commented Mar 21, 2023

Spotted this in:

POST /db/table currently executes obsolete code for inserting a row - I replaced that with /db/table/-/insert in
6e788b4 but forgot to remove the old code.

@simonw simonw added this to the Datasette 1.0a3 milestone Mar 21, 2023
@simonw
Copy link
Owner Author

simonw commented Mar 21, 2023

This code here:

# Handle POST to a table
return await self.table_post(
request, resolved.db, resolved.db.name, resolved.table
)
async def table_post(self, request, db, database_name, table_name):
# Must have insert-row permission
if not await self.ds.permission_allowed(
request.actor, "insert-row", resource=(database_name, table_name)
):
raise Forbidden("Permission denied")
if request.headers.get("content-type") != "application/json":
# TODO: handle form-encoded data
raise BadRequest("Must send JSON data")
data = json.loads(await request.post_body())
if "insert" not in data:
raise BadRequest('Must send a "insert" key containing a dictionary')
row = data["insert"]
if not isinstance(row, dict):
raise BadRequest("insert must be a dictionary")
# Verify all columns exist
columns = await db.table_columns(table_name)
pks = await db.primary_keys(table_name)
for key in row:
if key not in columns:
raise BadRequest("Column not found: {}".format(key))
if key in pks:
raise BadRequest(
"Cannot insert into primary key column: {}".format(key)
)
# Perform the insert
sql = "INSERT INTO [{table}] ({columns}) VALUES ({values})".format(
table=escape_sqlite(table_name),
columns=", ".join(escape_sqlite(c) for c in row),
values=", ".join("?" for c in row),
)
cursor = await db.execute_write(sql, list(row.values()))
# Return the new row
rowid = cursor.lastrowid
new_row = (
await db.execute(
"SELECT * FROM [{table}] WHERE rowid = ?".format(
table=escape_sqlite(table_name)
),
[rowid],
)
).first()
return Response.json(
{
"inserted_row": dict(new_row),
},
status=201,
)

@simonw
Copy link
Owner Author

simonw commented Mar 21, 2023

Removed code in 538ca9d - it will merge when I land:

@simonw simonw closed this as completed Mar 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant