How to use the awokado.meta.ResourceMeta 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 / store.py View on Github external
import sqlalchemy as sa
from marshmallow import fields

import tests.test_app.models as m
from awokado import custom_fields
from awokado.consts import CREATE, READ, BULK_CREATE
from awokado.meta import ResourceMeta
from awokado.request import ReadContext
from awokado.resource import BaseResource

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],
github 5783354 / awokado / tests / test_app / resources / forbidden_book.py View on Github external
from marshmallow import fields

import tests.test_app.models as m
from awokado.auth import BaseAuth
from awokado.consts import CREATE, READ, UPDATE, BULK_UPDATE, DELETE
from awokado.meta import ResourceMeta
from awokado.resource import BaseResource


class ForbiddenBookResource(BaseResource):
    Meta = ResourceMeta(
        model=m.Book,
        name="forbidden_book",
        methods=(CREATE, READ, UPDATE, BULK_UPDATE, DELETE),
        auth=BaseAuth,
    )

    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)
github 5783354 / awokado / tests / test_app / resources / tag_stats.py View on Github external
import sqlalchemy as sa
from marshmallow import fields

import tests.test_app.models as m
from awokado.consts import READ
from awokado.meta import ResourceMeta
from awokado.resource import BaseResource


class TagStatsResource(BaseResource):
    Meta = ResourceMeta(
        model=m.Tag,
        name="tag_stats",
        methods=(READ,),
        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_field="name",
    )

    name = fields.String(model_field=m.Tag.name)
    books_count = fields.Int(
        dump_only=True, model_field=sa.func.count(m.Book.id)
    )
github 5783354 / awokado / tests / test_app / resources / store_stats.py View on Github external
import sqlalchemy as sa
from marshmallow import fields

import tests.test_app.models as m
from awokado.consts import READ
from awokado.meta import ResourceMeta
from awokado.resource import BaseResource


class StoreStatsResource(BaseResource):
    Meta = ResourceMeta(
        model=m.Store,
        name="store_stats",
        methods=(READ,),
        select_from=sa.outerjoin(
            m.Store, m.Book, m.Store.id == m.Book.store_id
        ),
    )

    name = fields.String(model_field=m.Store.name)
    books_count = fields.Int(
        dump_only=True, model_field=sa.func.count(m.Book.id)
    )
github 5783354 / awokado / tests / test_app / resources / tag.py View on Github external
import sqlalchemy as sa
from marshmallow import fields

import tests.test_app.models as m
from awokado import custom_fields
from awokado.consts import CREATE, READ, UPDATE, BULK_UPDATE, DELETE
from awokado.meta import ResourceMeta
from awokado.request import ReadContext
from awokado.resource import BaseResource


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(),
github 5783354 / awokado / tests / test_app / resources / author.py View on Github external
    @classmethod
    def can_update(cls, session, user_id: int, obj_ids: list, skip_exc=False):
        return True

    @classmethod
    def can_delete(cls, session, user_id: int, obj_ids: list, skip_exc=False):
        return True

    @classmethod
    def _get_read_query(cls, ctx, query: Selectable):
        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",
    )
github 5783354 / awokado / tests / test_app / resources / book.py View on Github external
import sqlalchemy as sa
from marshmallow import fields

import tests.test_app.models as m
from awokado import custom_fields
from awokado.consts import CREATE, READ, UPDATE, BULK_UPDATE, DELETE
from awokado.meta import ResourceMeta
from awokado.request import ReadContext
from awokado.resource import BaseResource


class BookResource(BaseResource):
    Meta = ResourceMeta(
        model=m.Book,
        name="book",
        methods=(CREATE, READ, UPDATE, BULK_UPDATE, DELETE),
        select_from=sa.outerjoin(
            m.Book, m.M2M_Book_Tag, m.Book.id == m.M2M_Book_Tag.c.book_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(
github 5783354 / awokado / awokado / resource.py View on Github external
def __init__(self):
        super().__init__()
        cls_name = self.__class__.__name__

        class_meta = getattr(self, "Meta", None)
        if isinstance(class_meta, type):
            print(
                "resourse.Meta as class will be deprecated soon",
                file=sys.stderr,
            )
            self.Meta = ResourceMeta.from_class(class_meta)

        if not isinstance(self.Meta, ResourceMeta):
            raise Exception(
                f"{cls_name}.Meta must inherit from ResourceMeta class"
            )

        if not self.Meta.name or self.Meta.name in (
            "base_resource",
            "_resource",
        ):
            raise Exception(f"{cls_name} must have Meta.name")

        resource_id_name = get_id_field(self, name_only=True, skip_exc=True)
        if resource_id_name:
            resource_id_field = self.fields.get(resource_id_name)
            resource_id_field = resource_id_field.metadata.get("model_field")
github 5783354 / awokado / awokado / resource.py View on Github external
from awokado.meta import ResourceMeta
from awokado.request import ReadContext
from awokado.response import Response
from awokado.utils import (
    get_ids_from_payload,
    get_read_params,
    get_id_field,
    M2MMapping,
    AuthBundle,
)


class BaseResource(Schema):
    RESOURCES: Dict[str, Type["BaseResource"]] = {}
    Response = Response
    Meta: ResourceMeta

    def __new__(cls: Type["BaseResource"]):
        if cls.Meta.name not in ("base_resource", "_resource"):
            cls.RESOURCES[cls.Meta.name] = cls

        return super().__new__(cls)

    def __init__(self):
        super().__init__()
        cls_name = self.__class__.__name__

        class_meta = getattr(self, "Meta", None)
        if isinstance(class_meta, type):
            print(
                "resourse.Meta as class will be deprecated soon",
                file=sys.stderr,