mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
csrftoken() now works with .render_template(), closes #863
This commit is contained in:
parent
eed116ac05
commit
28bb1c5189
4 changed files with 19 additions and 1 deletions
|
|
@ -739,6 +739,7 @@ class Datasette:
|
||||||
"extra_css_urls": self._asset_urls("extra_css_urls", template, context),
|
"extra_css_urls": self._asset_urls("extra_css_urls", template, context),
|
||||||
"extra_js_urls": self._asset_urls("extra_js_urls", template, context),
|
"extra_js_urls": self._asset_urls("extra_js_urls", template, context),
|
||||||
"base_url": self.config("base_url"),
|
"base_url": self.config("base_url"),
|
||||||
|
"csrftoken": request.scope["csrftoken"] if request else lambda: "",
|
||||||
},
|
},
|
||||||
**extra_template_vars,
|
**extra_template_vars,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -103,7 +103,6 @@ class BaseView(AsgiView):
|
||||||
**context,
|
**context,
|
||||||
**{
|
**{
|
||||||
"database_url": self.database_url,
|
"database_url": self.database_url,
|
||||||
"csrftoken": request.scope["csrftoken"],
|
|
||||||
"database_color": self.database_color,
|
"database_color": self.database_color,
|
||||||
"show_messages": lambda: self.ds._show_messages(request),
|
"show_messages": lambda: self.ds._show_messages(request),
|
||||||
"select_templates": [
|
"select_templates": [
|
||||||
|
|
|
||||||
|
|
@ -182,11 +182,17 @@ def register_routes():
|
||||||
else:
|
else:
|
||||||
return Response.json(await request.post_vars())
|
return Response.json(await request.post_vars())
|
||||||
|
|
||||||
|
async def csrftoken_form(request, datasette):
|
||||||
|
return Response.html(
|
||||||
|
await datasette.render_template("csrftoken_form.html", request=request)
|
||||||
|
)
|
||||||
|
|
||||||
return [
|
return [
|
||||||
(r"/one/$", one),
|
(r"/one/$", one),
|
||||||
(r"/two/(?P<name>.*)$", two),
|
(r"/two/(?P<name>.*)$", two),
|
||||||
(r"/three/$", three),
|
(r"/three/$", three),
|
||||||
(r"/post/$", post),
|
(r"/post/$", post),
|
||||||
|
(r"/csrftoken-form/$", csrftoken_form),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -580,6 +580,18 @@ def test_register_routes_post(app_client):
|
||||||
assert "post data" == response.json["this is"]
|
assert "post data" == response.json["this is"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_register_routes_csrftoken(tmpdir):
|
||||||
|
templates = tmpdir / "templates"
|
||||||
|
templates.mkdir()
|
||||||
|
(templates / "csrftoken_form.html").write_text(
|
||||||
|
"CSRFTOKEN: {{ csrftoken() }}", "utf-8"
|
||||||
|
)
|
||||||
|
with make_app_client(template_dir=templates) as client:
|
||||||
|
response = client.get("/csrftoken-form/")
|
||||||
|
expected_token = client.ds._last_request.scope["csrftoken"]()
|
||||||
|
assert "CSRFTOKEN: {}".format(expected_token) == response.text
|
||||||
|
|
||||||
|
|
||||||
def test_register_routes_asgi(app_client):
|
def test_register_routes_asgi(app_client):
|
||||||
response = app_client.get("/three/")
|
response = app_client.get("/three/")
|
||||||
assert {"hello": "world"} == response.json
|
assert {"hello": "world"} == response.json
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue