Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
parse_filters(
{
f"id[{OP_IN}]": "1",
f"last_name[{OP_EQ}]": "",
f"non_existent_field[{OP_EQ}]": "val",
},
AuthorResource,
),
)
with self.assertRaises(BadFilter):
parse_filters({"id[nonexistentop]": "1"}, AuthorResource),
self.assertFilterItems(
[
FilterItem("id", "__ge__", lambda v: v, "2"),
FilterItem("last_name", OP_ILIKE, lambda v: f"%{v}%", "name"),
],
parse_filters(
{f"id[{OP_GTE}]": "2", f"last_name[{OP_ILIKE}]": "name"},
AuthorResource,
),
)
self.assertFilterItems(
[
FilterItem("first_name", "is_", lambda v: None, "true"),
FilterItem("id", "__le__", lambda v: v, "2"),
FilterItem("last_name", OP_CONTAINS, lambda v: v, ["name"]),
],
parse_filters(
{
parse_filters({"id[nonexistentop]": "1"}, AuthorResource),
self.assertFilterItems(
[
FilterItem("id", "__ge__", lambda v: v, "2"),
FilterItem("last_name", OP_ILIKE, lambda v: f"%{v}%", "name"),
],
parse_filters(
{f"id[{OP_GTE}]": "2", f"last_name[{OP_ILIKE}]": "name"},
AuthorResource,
),
)
self.assertFilterItems(
[
FilterItem("first_name", "is_", lambda v: None, "true"),
FilterItem("id", "__le__", lambda v: v, "2"),
FilterItem("last_name", OP_CONTAINS, lambda v: v, ["name"]),
],
parse_filters(
{
f"id[{OP_LTE}]": "2",
f"last_name[{OP_CONTAINS}]": "name",
f"first_name[{OP_EMPTY}]": "true",
},
AuthorResource,
),
)
self.assertFilterItems(
[
FilterItem("first_name", "isnot", lambda v: None, "false"),
],
parse_filters(
{
f"id[{OP_LTE}]": "2",
f"last_name[{OP_CONTAINS}]": "name",
f"first_name[{OP_EMPTY}]": "true",
},
AuthorResource,
),
)
self.assertFilterItems(
[
FilterItem("first_name", "isnot", lambda v: None, "false"),
FilterItem("id", "in_", lambda v: v, ["2", "3", "4"]),
FilterItem("last_name", "in_", lambda v: v, ["name1", "name2"]),
],
parse_filters(
{
f"id[{OP_IN}]": "2,3,4",
f"last_name[{OP_IN}]": ["name1", "name2"],
f"first_name[{OP_EMPTY}]": "false",
},
AuthorResource,
),
)
self.assertFilterItems(
[
FilterItem("id", "__gt__", lambda v: v, "2"),
FilterItem("last_name", OP_CONTAINS, lambda v: v, ["name"]),
],
def test_parse_filters(self):
self.assertFilterItems(
[FilterItem("id", "__eq__", lambda v: v, "1")],
parse_filters({f"id[{OP_EQ}]": "1"}, AuthorResource),
)
self.assertFilterItems(
[FilterItem("id", "in_", lambda v: v, ["1"])],
parse_filters(
{
f"id[{OP_IN}]": "1",
f"last_name[{OP_EQ}]": "",
f"non_existent_field[{OP_EQ}]": "val",
},
AuthorResource,
),
)
with self.assertRaises(BadFilter):
parse_filters({"id[nonexistentop]": "1"}, AuthorResource),
self.assertFilterItems(
[
FilterItem("id", "__ge__", lambda v: v, "2"),
self.assertFilterItems(
[
FilterItem("id", "__ge__", lambda v: v, "2"),
FilterItem("last_name", OP_ILIKE, lambda v: f"%{v}%", "name"),
],
parse_filters(
{f"id[{OP_GTE}]": "2", f"last_name[{OP_ILIKE}]": "name"},
AuthorResource,
),
)
self.assertFilterItems(
[
FilterItem("first_name", "is_", lambda v: None, "true"),
FilterItem("id", "__le__", lambda v: v, "2"),
FilterItem("last_name", OP_CONTAINS, lambda v: v, ["name"]),
],
parse_filters(
{
f"id[{OP_LTE}]": "2",
f"last_name[{OP_CONTAINS}]": "name",
f"first_name[{OP_EMPTY}]": "true",
},
AuthorResource,
),
)
self.assertFilterItems(
[
FilterItem("first_name", "isnot", lambda v: None, "false"),
FilterItem("id", "in_", lambda v: v, ["2", "3", "4"]),
)
self.assertFilterItems(
[
FilterItem("id", "__gt__", lambda v: v, "2"),
FilterItem("last_name", OP_CONTAINS, lambda v: v, ["name"]),
],
parse_filters(
{f"id[{OP_GT}]": "2", f"last_name[{OP_CONTAINS}]": "name"},
AuthorResource,
),
)
self.assertFilterItems(
[
FilterItem("id", "__lt__", lambda v: v, "2"),
FilterItem("last_name", OP_ILIKE, lambda v: f"%{v}%", "name"),
],
parse_filters(
{f"id[{OP_LT}]": "2", f"last_name[{OP_ILIKE}]": "name"},
AuthorResource,
),
from awokado.filter_parser import filter_value_to_python, FilterItem
from awokado.utils import get_id_field, get_sort_way
if False:
from awokado.resource import BaseResource
@dataclass
class ReadContext:
session: Session
resource: "BaseResource"
uid: int
# request vars
include: Optional[List]
query: Optional[List[FilterItem]]
sort: Optional[list]
resource_id: Optional[int]
limit: Optional[int]
offset: Optional[int]
# runtime vars
q: Select = field(default_factory=Select)
obj_ids: List[int] = field(default_factory=list)
parent_payload: List = field(default_factory=list)
related_payload: Dict = field(default_factory=dict)
total: int = 0
# for Aggregation Resources
time_scale = None
@property
return value
if isinstance(value, int):
return value
# Simple values
if value in ["true", "True", True]:
value = True
elif value in ["false", "False", False]:
value = False
elif value in ("null", "none", "None", None):
value = None
return value
parse_filters = FilterItem.parse