kopia lustrzana https://github.com/simonw/datasette
request.full_path property, closes #1184
rodzic
ed15c9908e
commit
649f48cd70
|
@ -89,6 +89,14 @@ class Request:
|
||||||
def query_string(self):
|
def query_string(self):
|
||||||
return (self.scope.get("query_string") or b"").decode("latin-1")
|
return (self.scope.get("query_string") or b"").decode("latin-1")
|
||||||
|
|
||||||
|
@property
|
||||||
|
def full_path(self):
|
||||||
|
qs = self.query_string
|
||||||
|
return "{}{}".format(
|
||||||
|
self.path,
|
||||||
|
('?' + qs) if qs else ''
|
||||||
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def args(self):
|
def args(self):
|
||||||
return MultiParams(parse_qs(qs=self.query_string))
|
return MultiParams(parse_qs(qs=self.query_string))
|
||||||
|
|
|
@ -35,13 +35,16 @@ The request object is passed to various plugin hooks. It represents an incoming
|
||||||
The host header from the incoming request, e.g. ``latest.datasette.io`` or ``localhost``.
|
The host header from the incoming request, e.g. ``latest.datasette.io`` or ``localhost``.
|
||||||
|
|
||||||
``.path`` - string
|
``.path`` - string
|
||||||
The path of the request, e.g. ``/fixtures``.
|
The path of the request excluding the query string, e.g. ``/fixtures``.
|
||||||
|
|
||||||
|
``.full_path`` - string
|
||||||
|
The path of the request including the query string if one is present, e.g. ``/fixtures?sql=select+sqlite_version()``.
|
||||||
|
|
||||||
``.query_string`` - string
|
``.query_string`` - string
|
||||||
The querystring component of the request, without the ``?`` - e.g. ``name__contains=sam&age__gt=10``.
|
The query string component of the request, without the ``?`` - e.g. ``name__contains=sam&age__gt=10``.
|
||||||
|
|
||||||
``.args`` - MultiParams
|
``.args`` - MultiParams
|
||||||
An object representing the parsed querystring parameters, see below.
|
An object representing the parsed query string parameters, see below.
|
||||||
|
|
||||||
``.url_vars`` - dictionary (str -> str)
|
``.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`.
|
Variables extracted from the URL path, if that path was defined using a regular expression. See :ref:`plugin_register_routes`.
|
||||||
|
@ -62,9 +65,9 @@ The object also has two awaitable methods:
|
||||||
The MultiParams class
|
The MultiParams class
|
||||||
=====================
|
=====================
|
||||||
|
|
||||||
``request.args`` is a ``MultiParams`` object - a dictionary-like object which provides access to querystring parameters that may have multiple values.
|
``request.args`` is a ``MultiParams`` object - a dictionary-like object which provides access to query string parameters that may have multiple values.
|
||||||
|
|
||||||
Consider the querystring ``?foo=1&foo=2&bar=3`` - with two values for ``foo`` and one value for ``bar``.
|
Consider the query string ``?foo=1&foo=2&bar=3`` - with two values for ``foo`` and one value for ``bar``.
|
||||||
|
|
||||||
``request.args[key]`` - string
|
``request.args[key]`` - string
|
||||||
Returns the first value for that key, or raises a ``KeyError`` if the key is missing. For the above example ``request.args["foo"]`` would return ``"1"``.
|
Returns the first value for that key, or raises a ``KeyError`` if the key is missing. For the above example ``request.args["foo"]`` would return ``"1"``.
|
||||||
|
|
|
@ -90,3 +90,25 @@ def test_request_url_vars():
|
||||||
assert {"name": "cleo"} == Request(
|
assert {"name": "cleo"} == Request(
|
||||||
dict(scope, url_route={"kwargs": {"name": "cleo"}}), None
|
dict(scope, url_route={"kwargs": {"name": "cleo"}}), None
|
||||||
).url_vars
|
).url_vars
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("path,query_string,expected_full_path", [
|
||||||
|
("/", "", "/"),
|
||||||
|
("/", "foo=bar", "/?foo=bar"),
|
||||||
|
("/foo", "bar", "/foo?bar")
|
||||||
|
])
|
||||||
|
def test_request_properties(path, query_string, expected_full_path):
|
||||||
|
scope = {
|
||||||
|
"http_version": "1.1",
|
||||||
|
"method": "POST",
|
||||||
|
"path": path,
|
||||||
|
"raw_path": path.encode("latin-1"),
|
||||||
|
"query_string": query_string.encode("latin-1"),
|
||||||
|
"scheme": "http",
|
||||||
|
"type": "http",
|
||||||
|
}
|
||||||
|
request = Request(scope, None)
|
||||||
|
assert request.path == path
|
||||||
|
assert request.query_string == query_string
|
||||||
|
assert request.full_path == expected_full_path
|
||||||
|
|
Ładowanie…
Reference in New Issue