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

Writable canned queries fail to load custom templates #1547

Closed
wragge opened this issue Dec 10, 2021 · 6 comments
Closed

Writable canned queries fail to load custom templates #1547

wragge opened this issue Dec 10, 2021 · 6 comments
Labels

Comments

@wragge
Copy link
Sponsor Contributor

wragge commented Dec 10, 2021

I've created a canned query with "write": true set. I've also created a custom template for it, but the template doesn't seem to be found. If I look in the HTML I see (stock_exchange is the db name):

<!-- Templates considered: query-stock_exchange.html, *query.html -->

My non-writeable canned queries pick up custom templates as expected, and if I look at their HTML I see the canned query name added to the templates considered (the canned query here is date_search):

<!-- Templates considered: query-stock_exchange-date_search.html, query-stock_exchange.html, *query.html -->

So it seems like the writeable canned query is behaving differently for some reason. Is it an authentication thing? I'm using the built in --root authentication.

Thanks!

@simonw simonw added the bug label Dec 19, 2021
@simonw
Copy link
Owner

simonw commented Dec 19, 2021

Yes, this is a bug. It looks like the problem is with the if write: branch in this code here:

# Execute query - as write or as read
if write:
if request.method == "POST":
body = await request.post_body()
body = body.decode("utf-8").strip()
if body.startswith("{") and body.endswith("}"):
params = json.loads(body)
# But we want key=value strings
for key, value in params.items():
params[key] = str(value)
else:
params = dict(parse_qsl(body, keep_blank_values=True))
# Should we return JSON?
should_return_json = (
request.headers.get("accept") == "application/json"
or request.args.get("_json")
or params.get("_json")
)
if canned_query:
params_for_query = MagicParameters(params, request, self.ds)
else:
params_for_query = params
ok = None
try:
cursor = await self.ds.databases[database].execute_write(
sql, params_for_query, block=True
)
message = metadata.get(
"on_success_message"
) or "Query executed, {} row{} affected".format(
cursor.rowcount, "" if cursor.rowcount == 1 else "s"
)
message_type = self.ds.INFO
redirect_url = metadata.get("on_success_redirect")
ok = True
except Exception as e:
message = metadata.get("on_error_message") or str(e)
message_type = self.ds.ERROR
redirect_url = metadata.get("on_error_redirect")
ok = False
if should_return_json:
return Response.json(
{
"ok": ok,
"message": message,
"redirect": redirect_url,
}
)
else:
self.ds.add_message(request, message, message_type)
return self.redirect(request, redirect_url or request.path)
else:
async def extra_template():
return {
"request": request,
"path_with_added_args": path_with_added_args,
"path_with_removed_args": path_with_removed_args,
"named_parameter_values": named_parameter_values,
"canned_query": canned_query,
"success_message": request.args.get("_success") or "",
"canned_write": True,
}
return (
{
"database": database,
"rows": [],
"truncated": False,
"columns": [],
"query": {"sql": sql, "params": params},
"private": private,
},
extra_template,
templates,
)

Is missing this bit of code:

if canned_query:
templates.insert(
0,
f"query-{to_css_class(database)}-{to_css_class(canned_query)}.html",
)

@simonw simonw changed the title Custom templates with writable canned queries? Writable canned queries fail to load custom templates Dec 19, 2021
@simonw simonw closed this as completed in 4094741 Dec 19, 2021
simonw added a commit that referenced this issue Dec 19, 2021
@simonw
Copy link
Owner

simonw commented Dec 19, 2021

I released this in an alpha, so you can try out this fix using:

pip install datasette==0.60a1

@wragge
Copy link
Sponsor Contributor Author

wragge commented Dec 20, 2021

I've installed the alpha version but get an error when starting up Datasette:

Traceback (most recent call last):
  File "/Users/tim/.pyenv/versions/stock-exchange/bin/datasette", line 5, in <module>
    from datasette.cli import cli
  File "/Users/tim/.pyenv/versions/3.8.5/envs/stock-exchange/lib/python3.8/site-packages/datasette/cli.py", line 15, in <module>
    from .app import Datasette, DEFAULT_SETTINGS, SETTINGS, SQLITE_LIMIT_ATTACHED, pm
  File "/Users/tim/.pyenv/versions/3.8.5/envs/stock-exchange/lib/python3.8/site-packages/datasette/app.py", line 31, in <module>
    from .views.database import DatabaseDownload, DatabaseView
  File "/Users/tim/.pyenv/versions/3.8.5/envs/stock-exchange/lib/python3.8/site-packages/datasette/views/database.py", line 25, in <module>
    from datasette.plugins import pm
  File "/Users/tim/.pyenv/versions/3.8.5/envs/stock-exchange/lib/python3.8/site-packages/datasette/plugins.py", line 29, in <module>
    mod = importlib.import_module(plugin)
  File "/Users/tim/.pyenv/versions/3.8.5/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/Users/tim/.pyenv/versions/3.8.5/envs/stock-exchange/lib/python3.8/site-packages/datasette/filters.py", line 9, in <module>
    @hookimpl(specname="filters_from_request")
TypeError: __call__() got an unexpected keyword argument 'specname'

@simonw
Copy link
Owner

simonw commented Dec 20, 2021

@wragge thanks, that's a bug! Working on that in #1575.

@simonw
Copy link
Owner

simonw commented Dec 20, 2021

OK, this should hopefully fix that for you:

pip install https://github.com/simonw/datasette/archive/f36e010b3b69ada104b79d83c7685caf9359049e.zip

@wragge
Copy link
Sponsor Contributor Author

wragge commented Dec 20, 2021

Yep, that works -- thanks!

@simonw simonw added this to the Datasette 0.60 milestone Jan 13, 2022
simonw added a commit that referenced this issue Jan 14, 2022
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

2 participants