kopia lustrzana https://github.com/simonw/datasette
Rename RequestParameters to MultiParams, refs #799
rodzic
0c064c5fe2
commit
0da7f49b24
|
@ -753,15 +753,24 @@ def escape_fts(query):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class RequestParameters:
|
class MultiParams:
|
||||||
def __init__(self, data):
|
def __init__(self, data):
|
||||||
# data is a dictionary of key => [list, of, values]
|
# data is a dictionary of key => [list, of, values] or a list of [["key", "value"]] pairs
|
||||||
assert isinstance(data, dict), "data should be a dictionary of key => [list]"
|
if isinstance(data, dict):
|
||||||
for key in data:
|
for key in data:
|
||||||
assert isinstance(
|
assert isinstance(
|
||||||
data[key], list
|
data[key], list
|
||||||
), "data should be a dictionary of key => [list]"
|
), "dictionary data should be a dictionary of key => [list]"
|
||||||
self._data = data
|
self._data = data
|
||||||
|
elif isinstance(data, list):
|
||||||
|
new_data = {}
|
||||||
|
for item in data:
|
||||||
|
assert (
|
||||||
|
isinstance(item, list) and len(item) == 2
|
||||||
|
), "list data should be a list of [key, value] pairs"
|
||||||
|
key, value = item
|
||||||
|
new_data.setdefault(key, []).append(value)
|
||||||
|
self._data = new_data
|
||||||
|
|
||||||
def __contains__(self, key):
|
def __contains__(self, key):
|
||||||
return key in self._data
|
return key in self._data
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import json
|
import json
|
||||||
from datasette.utils import RequestParameters
|
from datasette.utils import MultiParams
|
||||||
from mimetypes import guess_type
|
from mimetypes import guess_type
|
||||||
from urllib.parse import parse_qs, urlunparse, parse_qsl
|
from urllib.parse import parse_qs, urlunparse, parse_qsl
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
@ -68,7 +68,7 @@ class Request:
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def args(self):
|
def args(self):
|
||||||
return RequestParameters(parse_qs(qs=self.query_string))
|
return MultiParams(parse_qs(qs=self.query_string))
|
||||||
|
|
||||||
async def post_vars(self):
|
async def post_vars(self):
|
||||||
body = []
|
body = []
|
||||||
|
|
|
@ -8,7 +8,7 @@ from datasette.plugins import pm
|
||||||
from datasette.database import QueryInterrupted
|
from datasette.database import QueryInterrupted
|
||||||
from datasette.utils import (
|
from datasette.utils import (
|
||||||
CustomRow,
|
CustomRow,
|
||||||
RequestParameters,
|
MultiParams,
|
||||||
append_querystring,
|
append_querystring,
|
||||||
compound_keys_after_sql,
|
compound_keys_after_sql,
|
||||||
escape_sqlite,
|
escape_sqlite,
|
||||||
|
@ -286,7 +286,7 @@ class TableView(RowTableShared):
|
||||||
order_by = ""
|
order_by = ""
|
||||||
|
|
||||||
# Ensure we don't drop anything with an empty value e.g. ?name__exact=
|
# Ensure we don't drop anything with an empty value e.g. ?name__exact=
|
||||||
args = RequestParameters(
|
args = MultiParams(
|
||||||
urllib.parse.parse_qs(request.query_string, keep_blank_values=True)
|
urllib.parse.parse_qs(request.query_string, keep_blank_values=True)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -39,7 +39,7 @@ The request object is passed to various plugin hooks. It represents an incoming
|
||||||
``.query_string`` - string
|
``.query_string`` - string
|
||||||
The querystring component of the request, without the ``?`` - e.g. ``name__contains=sam&age__gt=10``.
|
The querystring component of the request, without the ``?`` - e.g. ``name__contains=sam&age__gt=10``.
|
||||||
|
|
||||||
``.args`` - RequestParameters
|
``.args`` - MultiParams
|
||||||
An object representing the parsed querystring parameters, see below.
|
An object representing the parsed querystring parameters, see below.
|
||||||
|
|
||||||
The object also has one awaitable method:
|
The object also has one awaitable method:
|
||||||
|
@ -47,10 +47,12 @@ The object also has one awaitable method:
|
||||||
``await request.post_vars()`` - dictionary
|
``await request.post_vars()`` - dictionary
|
||||||
Returns a dictionary of form variables that were submitted in the request body via ``POST``.
|
Returns a dictionary of form variables that were submitted in the request body via ``POST``.
|
||||||
|
|
||||||
The RequestParameters class
|
.. _internals_multiparams:
|
||||||
---------------------------
|
|
||||||
|
|
||||||
``request.args`` is a ``RequestParameters`` object - a dictionary-like object which provides access to querystring parameters that may have multiple values.
|
The MultiParams class
|
||||||
|
---------------------
|
||||||
|
|
||||||
|
``request.args`` is a ``MultiParams`` object - a dictionary-like object which provides access to querystring 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 querystring ``?foo=1&foo=2&bar=3`` - with two values for ``foo`` and one value for ``bar``.
|
||||||
|
|
||||||
|
|
|
@ -437,3 +437,12 @@ def test_call_with_supported_arguments():
|
||||||
|
|
||||||
with pytest.raises(TypeError):
|
with pytest.raises(TypeError):
|
||||||
utils.call_with_supported_arguments(foo, a=1)
|
utils.call_with_supported_arguments(foo, a=1)
|
||||||
|
|
||||||
|
|
||||||
|
def test_multi_params_list():
|
||||||
|
p1 = utils.MultiParams([["foo", "bar"], ["foo", "baz"]])
|
||||||
|
assert "bar" == p1["foo"]
|
||||||
|
assert ["bar", "baz"] == p1.getlist("foo")
|
||||||
|
# Should raise an error if list isn't pairs
|
||||||
|
with pytest.raises(AssertionError):
|
||||||
|
utils.MultiParams([["foo", "bar"], ["foo", "baz", "bar"]])
|
||||||
|
|
Ładowanie…
Reference in New Issue