kopia lustrzana https://github.com/simonw/datasette
request.url_vars property, closes #822
rodzic
db660db463
commit
fac8e93815
|
@ -32,6 +32,10 @@ class Request:
|
||||||
(self.scheme, self.host, self.path, None, self.query_string, None)
|
(self.scheme, self.host, self.path, None, self.query_string, None)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def url_vars(self):
|
||||||
|
return (self.scope.get("url_route") or {}).get("kwargs") or {}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def scheme(self):
|
def scheme(self):
|
||||||
return self.scope.get("scheme") or "http"
|
return self.scope.get("scheme") or "http"
|
||||||
|
|
|
@ -42,6 +42,9 @@ The request object is passed to various plugin hooks. It represents an incoming
|
||||||
``.args`` - MultiParams
|
``.args`` - MultiParams
|
||||||
An object representing the parsed querystring parameters, see below.
|
An object representing the parsed querystring parameters, see below.
|
||||||
|
|
||||||
|
``.url_vars`` - dictionary (str -> str)
|
||||||
|
Variables extracted from the URL path, if that path was defined using a regular expression. See :ref:`plugin_register_routes`.
|
||||||
|
|
||||||
``.actor`` - dictionary (str -> Any) or None
|
``.actor`` - dictionary (str -> Any) or None
|
||||||
The currently authenticated actor (see :ref:`actors <authentication_actor>`), or ``None`` if the request is unauthenticated.
|
The currently authenticated actor (see :ref:`actors <authentication_actor>`), or ``None`` if the request is unauthenticated.
|
||||||
|
|
||||||
|
|
|
@ -850,8 +850,8 @@ Return a list of ``(regex, async_view_function)`` pairs, something like this:
|
||||||
import html
|
import html
|
||||||
|
|
||||||
|
|
||||||
async def hello_from(scope):
|
async def hello_from(request):
|
||||||
name = scope["url_route"]["kwargs"]["name"]
|
name = request.url_vars["name"]
|
||||||
return Response.html("Hello from {}".format(
|
return Response.html("Hello from {}".format(
|
||||||
html.escape(name)
|
html.escape(name)
|
||||||
))
|
))
|
||||||
|
|
|
@ -152,8 +152,8 @@ def register_routes():
|
||||||
(await datasette.get_database().execute("select 1 + 1")).first()[0]
|
(await datasette.get_database().execute("select 1 + 1")).first()[0]
|
||||||
)
|
)
|
||||||
|
|
||||||
async def two(request, scope):
|
async def two(request):
|
||||||
name = scope["url_route"]["kwargs"]["name"]
|
name = request.url_vars["name"]
|
||||||
greeting = request.args.get("greeting")
|
greeting = request.args.get("greeting")
|
||||||
return Response.text("{} {}".format(greeting, name))
|
return Response.text("{} {}".format(greeting, name))
|
||||||
|
|
||||||
|
|
|
@ -44,3 +44,20 @@ def test_request_args():
|
||||||
assert 2 == len(request.args)
|
assert 2 == len(request.args)
|
||||||
with pytest.raises(KeyError):
|
with pytest.raises(KeyError):
|
||||||
request.args["missing"]
|
request.args["missing"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_request_url_vars():
|
||||||
|
scope = {
|
||||||
|
"http_version": "1.1",
|
||||||
|
"method": "POST",
|
||||||
|
"path": "/",
|
||||||
|
"raw_path": b"/",
|
||||||
|
"query_string": b"",
|
||||||
|
"scheme": "http",
|
||||||
|
"type": "http",
|
||||||
|
"headers": [[b"content-type", b"application/x-www-form-urlencoded"]],
|
||||||
|
}
|
||||||
|
assert {} == Request(scope, None).url_vars
|
||||||
|
assert {"name": "cleo"} == Request(
|
||||||
|
dict(scope, url_route={"kwargs": {"name": "cleo"}}), None
|
||||||
|
).url_vars
|
||||||
|
|
Ładowanie…
Reference in New Issue