Skip to main content

Fastapi view mixins

Project description

Package of mixins that helps build API.

List of available mixins:

  1. BaseAPIListMixin
  2. BaseAPIDetailMixin
  3. BaseAPIUpdateMixin
  4. BaseAPICreateMixin
  5. BaseAPIDestroyMixin

List of available pagination's:

  1. LimitOffsetPagination - default
  2. CursorPagination

Built on:

  1. fastapi==0.79.0
  2. pytest==7.1.2
  3. pytest-asyncio==0.19.0
  4. SQLAlchemy==1.4.39
  5. aiosqlite==0.17.0 # testing purpose

Base usage:

List view:

# views.py
from fastapi_views.views.mixins import BaseAPIListMixin


class FooListView(BaseAPIListMixin):
    model = Foo


# urls.py
from fastapi import (APIRouter, Depends)
from fastapi_views import utils

foo_router = APIRouter(
    prefix='/foo',
    tags=['Foo']
)


@foo_router.get('/all', response_model=list[SomePydanticModel])
async def get_articles(
        session: Session = Depends(db_session),
):
    return utils.scalars(FooApiListView().get_all(session=session))

You can also override mixin attrs to do it a bit shorter

# urls.py
from fastapi import (APIRouter, Depends)
from fastapi_views import utils
from fastapi_views.views.mixins import BaseAPIListMixin

foo_router = APIRouter(
    prefix='/foo',
    tags=['Foo']
)


@foo_router.get('/all', response_model=list[SomePydanticModel])
async def get_articles(
        session: Session = Depends(db_session),
):
    return utils.scalars(BaseAPIListMixin(attrs={'model': Foo}).get_all(session=session))

List view using pagination:

# views.py
from fastapi_views.views.mixins import BaseAPIListMixin


class FooListView(BaseAPIListMixin):
    model = Foo
    paginate_by = 15


# urls.py
from fastapi import (APIRouter, Depends)
from fastapi_views import utils
from fastapi_views.pagination.annotations import PaginationParams
from fastapi_views.pagination.schema import LimitOffsetPage

foo_router = APIRouter(
    prefix='/foo',
    tags=['Foo']
)


@foo_router.get('/all', response_model=LimitOffsetPage.include(SomePydanticModel))
async def get_articles(
        params: PaginationParams = Depends(),  # put inside fun to return Session and Request
):
    return utils.scalars(FooApiListView().get_all_with_pagination(**params))  # session=, request=

List view cursor pagination example:

# views.py
from fastapi_views.views.mixins import BaseAPIListMixin
from fastapi_views.pagination.core import PaginationCursor
from sqlalchemy import select, and_
from sqlalchemy.orm import joinedload, Load
from sqlalchemy.sql import Select


class FooListView(BaseAPIListMixin):
    model = Foo
    paginate_by = 10
    pagination_strategy = PaginationCursor

    def get_statement(self) -> Select:
        """As default returns select(self.model)."""

        statement = select(self.model).options(
            Load(self.model).load_only(self.model.title, self.model.published),
        ).options(
            joinedload(self.model.fk_related, innerjoin=True).
            load_only(Fk_related.username, Fk_related.email)
        ).where(and_(self.model.id > 0, self.model.id < 100))

        return statement

    def get_pagination_kwargs(self):
        """
        As default returns {
            'model': self.model,
            'ordering': ['id'],
            'cursor_prefixes': ['next__', 'prev__']
        }
        """
        kw = super().get_pagination_kwargs()
        kw['ordering'] = ['-created']
        return kw


# urls.py
from fastapi import (APIRouter, Depends)
from fastapi_views import utils
from fastapi_views.pagination.annotations import PaginationParams
from fastapi_views.pagination.schema import CursorPage

foo_router = APIRouter(
    prefix='/foo',
    tags=['Foo']
)


@foo_router.get('/all', response_model=CursorPage.include(SomePydanticModel))
async def get_articles(
        params: PaginationParams = Depends(),  # put inside fun to return Session and Request
):
    return utils.scalars(FooApiListView().get_all_with_pagination(**params))  # session=, request=

Detail/Update/Delete view:

Default field_name for detail/update/delete view is set to id. To override do as below

# views.py
class FooDetailUpdateDeleteView:
    ...
    pk_field = 'field_name'

Detail view:

# views.py
from fastapi_views.views.mixins import BaseAPIDetailMixin


class FooDetailView(BaseAPIDetailMixin):
    model = Foo


# urls.py
from fastapi import (APIRouter, Depends)

foo_router = APIRouter(
    prefix='/foo',
    tags=['Foo']
)


@foo_router.get('/{pk}')
async def get_foo(pk: int, session: Session = Depends(db_session)):
    return FooDetailView().get_detail(field_value=pk, session=session)

Update view:

# views.py
from fastapi_views.views.mixins import BaseAPIUpdateMixin


class FooUpdateView(BaseAPIUpdateMixin):
    model = Foo


# urls.py
from fastapi import (APIRouter, Depends)

foo_router = APIRouter(
    prefix='/foo',
    tags=['Foo']
)


@foo_router.get('/{pk}')
async def get_foo(pk: int, session: Session = Depends(db_session)):
    return FooUpdateView().update_single(field_value=pk, session=session, data={})

Delete view:

# views.py
from fastapi_views.views.mixins import BaseAPIDestroyMixin


class FooDeleteView(BaseAPIDestroyMixin):
    model = Foo


# urls.py
from fastapi import (APIRouter, Depends)

foo_router = APIRouter(
    prefix='/foo',
    tags=['Foo']
)


@foo_router.get('/{slug}')
async def get_foo(slug: str, session: Session = Depends(db_session)):
    return FooDeleteView().delete(session=session, field_value=slug)

To run tests install requirements and run pytest command

Download on https://pypi.org/project/fastapi-view-mixins/

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi-view-mixins-0.0.4.tar.gz (58.6 kB view details)

Uploaded Source

Built Distribution

fastapi_view_mixins-0.0.4-py3-none-any.whl (74.2 kB view details)

Uploaded Python 3

File details

Details for the file fastapi-view-mixins-0.0.4.tar.gz.

File metadata

  • Download URL: fastapi-view-mixins-0.0.4.tar.gz
  • Upload date:
  • Size: 58.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.5

File hashes

Hashes for fastapi-view-mixins-0.0.4.tar.gz
Algorithm Hash digest
SHA256 bb6afb276d1df218bf8eabbe76a49a01782a6d9006f58c78ab2232699e8e299e
MD5 83020ea7f041b8a9915d7fc9d563a657
BLAKE2b-256 dc754a7cf408ac74740efb25814398dae6217b144420ae5dca1d659731baedfe

See more details on using hashes here.

File details

Details for the file fastapi_view_mixins-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_view_mixins-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 3f57d4edd60fdbd0f277df2840237f3247eaef9c26575a34cb11d365c86cb38f
MD5 061ed32deb07b71d86c42ce3173634ad
BLAKE2b-256 f38d9d2015221945c22612f390ea0bb8439b6bb8602cca0ce937cd26bbbd483c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page