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,
),
class TagResource(BaseResource):
Meta = ResourceMeta(
model=m.Tag,
name="tag",
methods=(CREATE, READ, UPDATE, BULK_UPDATE, DELETE),
disable_total=True,
select_from=sa.outerjoin(
m.Tag, m.M2M_Book_Tag, m.Tag.id == m.M2M_Book_Tag.c.tag_id
).outerjoin(m.Book, m.M2M_Book_Tag.c.book_id == m.Book.id),
)
id = fields.Int(model_field=m.Tag.id)
name = fields.String(model_field=m.Tag.name)
books = custom_fields.ToMany(
fields.Int(), resource="book", model_field=m.M2M_Book_Tag.c.book_id
)
book_titles = fields.List(
fields.Str(),
resource="author",
model_field=sa.func.array_remove(sa.func.array_agg(m.Book.title), None),
)
def get_by_book_ids(self, session, ctx: ReadContext, field: str = None):
"""
:param user_id: User id
:return: serialized JSON response
"""
q = (
sa.select([m.Tag.id.label("id"), m.Tag.name.label("name")])
.select_from(
STORE_OPEN = "open"
STORE_CLOSED = "closed"
class StoreResource(BaseResource):
Meta = ResourceMeta(
model=m.Store,
name="store",
methods=(CREATE, BULK_CREATE, READ),
select_from=sa.outerjoin(
m.Store, m.Book, m.Store.id == m.Book.store_id
),
)
id = fields.Int(model_field=m.Store.id)
book_ids = custom_fields.ToMany(
fields.Int(), resource="book", model_field=m.Book.id
)
name = fields.String(model_field=m.Store.name, required=True)
status = custom_fields.Choice(
model_field=m.Store.status,
allowed_values=[STORE_OPEN, STORE_CLOSED],
allow_none=True,
)
def get_by_book_ids(
self, session, ctx: ReadContext, field: sa.Column = None
):
q = (
sa.select(
[
m.Store.id.label("id"),
).outerjoin(m.Author, m.Book.author_id == m.Author.id),
)
id = fields.Int(model_field=m.Book.id)
title = fields.String(model_field=m.Book.title, required=True)
description = fields.String(model_field=m.Book.description)
author = custom_fields.ToOne(
resource="author", model_field=m.Book.author_id
)
author_name = fields.Str(model_field=m.Author.first_name, dump_only=True)
store = custom_fields.ToOne(
resource="store",
model_field=m.Book.store_id,
description="Store selling book",
)
tags = custom_fields.ToMany(
fields.Int(), resource="tag", model_field=m.M2M_Book_Tag.c.tag_id
)
def get_by_author_ids(
self, session, ctx: ReadContext, field: sa.Column = None
):
authors = sa.func.array_remove(
sa.func.array_agg(m.Author.id), None
).label("authors")
q = (
sa.select(
[
m.Book.id.label("id"),
m.Book.title.label("title"),
m.Book.description.label("description"),
m.Book.store_id.label("store"),
return query
class AuthorResource(BaseResource):
Meta = ResourceMeta(
model=m.Author,
name="author",
methods=(CREATE, READ, UPDATE, BULK_UPDATE, DELETE),
auth=AuthorAuth,
select_from=sa.outerjoin(
m.Author, m.Book, m.Author.id == m.Book.author_id
),
)
id = fields.Int(model_field=m.Author.id)
books = custom_fields.ToMany(
fields.Int(),
resource="book",
model_field=m.Book.id,
description="Authors Books",
)
books_count = fields.Int(
dump_only=True, model_field=sa.func.count(m.Book.id)
)
name = fields.String(
model_field=sa.func.concat(
m.Author.first_name, " ", m.Author.last_name
),
dump_only=True,
)
last_name = fields.String(
model_field=m.Author.last_name, required=True, load_only=True