How to use the awokado.custom_fields function in awokado

To help you get started, we’ve selected a few awokado examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github 5783354 / awokado / tests / test_app / resources / tag.py View on Github external
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(
github 5783354 / awokado / tests / test_app / resources / store.py View on Github external
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"),
github 5783354 / awokado / tests / test_app / resources / author.py View on Github external
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
github 5783354 / awokado / tests / test_app / resources / store.py View on Github external
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"),
                    m.Store.name.label("name"),
                    sa.func.array_agg(m.Book.id).label("book_ids"),
                ]
            )