arraynotcontains filter, closes #1132

pull/1145/head
Simon Willison 2020-12-07 14:41:03 -08:00
rodzic 8ae0f9f7f0
commit 4c25b035b2
3 zmienionych plików z 39 dodań i 4 usunięć

Wyświetl plik

@ -154,7 +154,16 @@ class Filters:
where j.value = :{p}
)""",
'{c} contains "{v}"',
)
),
TemplatedFilter(
"arraynotcontains",
"array does not contain",
"""rowid not in (
select {t}.rowid from {t}, json_each({t}.{c}) j
where j.value = :{p}
)""",
'{c} does not contain "{v}"',
),
]
if detect_json1()
else []

Wyświetl plik

@ -267,7 +267,12 @@ You can filter the data returned by the table based on column values using a que
Rows where column does not match any of the provided values. The inverse of ``__in=``. Also supports JSON arrays.
``?column__arraycontains=value``
Works against columns that contain JSON arrays - matches if any of the values in that array match.
Works against columns that contain JSON arrays - matches if any of the values in that array match the provided value.
This is only available if the ``json1`` SQLite extension is enabled.
``?column__arraynotcontains=value``
Works against columns that contain JSON arrays - matches if none of the values in that array match the provided value.
This is only available if the ``json1`` SQLite extension is enabled.

Wyświetl plik

@ -1121,7 +1121,7 @@ def test_table_filter_queries_multiple_of_same_type(app_client):
@pytest.mark.skipif(not detect_json1(), reason="Requires the SQLite json1 module")
def test_table_filter_json_arraycontains(app_client):
response = app_client.get("/fixtures/facetable.json?tags__arraycontains=tag1")
assert [
assert response.json["rows"] == [
[
1,
"2019-01-14 08:00:00",
@ -1146,7 +1146,28 @@ def test_table_filter_json_arraycontains(app_client):
"[]",
"two",
],
] == response.json["rows"]
]
@pytest.mark.skipif(not detect_json1(), reason="Requires the SQLite json1 module")
def test_table_filter_json_arraynotcontains(app_client):
response = app_client.get(
"/fixtures/facetable.json?tags__arraynotcontains=tag3&tags__not=[]"
)
assert response.json["rows"] == [
[
1,
"2019-01-14 08:00:00",
1,
1,
"CA",
1,
"Mission",
'["tag1", "tag2"]',
'[{"foo": "bar"}]',
"one",
]
]
def test_table_filter_extra_where(app_client):