From d7dea82b3733ee27ad5cd5e444a74d1069c98049 Mon Sep 17 00:00:00 2001 From: Fa2y Date: Wed, 22 Feb 2023 11:58:13 +0100 Subject: [PATCH] Fixing issues #153 & #138 --- fastapi_crudrouter/core/_base.py | 4 ++-- fastapi_crudrouter/core/_types.py | 6 +++++- fastapi_crudrouter/core/tortoise.py | 14 ++++++++++---- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/fastapi_crudrouter/core/_base.py b/fastapi_crudrouter/core/_base.py index e45d33fe..c8581258 100644 --- a/fastapi_crudrouter/core/_base.py +++ b/fastapi_crudrouter/core/_base.py @@ -4,7 +4,7 @@ from fastapi import APIRouter, HTTPException from fastapi.types import DecoratedCallable -from ._types import T, DEPENDENCIES +from ._types import T, DEPENDENCIES, PAGINATIONEXTRADATA from ._utils import pagination_factory, schema_factory NOT_FOUND = HTTPException(404, "Item not found") @@ -58,7 +58,7 @@ def __init__( "", self._get_all(), methods=["GET"], - response_model=Optional[List[self.schema]], # type: ignore + response_model=Optional[List[self.schema] | PAGINATIONEXTRADATA], # type: ignore summary="Get All", dependencies=get_all_route, ) diff --git a/fastapi_crudrouter/core/_types.py b/fastapi_crudrouter/core/_types.py index 959d5feb..b8e5ab0c 100644 --- a/fastapi_crudrouter/core/_types.py +++ b/fastapi_crudrouter/core/_types.py @@ -1,4 +1,4 @@ -from typing import Dict, TypeVar, Optional, Sequence +from typing import Dict, TypeVar, Optional, Sequence, List from fastapi.params import Depends from pydantic import BaseModel @@ -8,3 +8,7 @@ T = TypeVar("T", bound=BaseModel) DEPENDENCIES = Optional[Sequence[Depends]] + +class PAGINATIONEXTRADATA(BaseModel): + count: int + results: List \ No newline at end of file diff --git a/fastapi_crudrouter/core/tortoise.py b/fastapi_crudrouter/core/tortoise.py index 52972a48..3a60a5e6 100644 --- a/fastapi_crudrouter/core/tortoise.py +++ b/fastapi_crudrouter/core/tortoise.py @@ -1,7 +1,7 @@ from typing import Any, Callable, List, Type, cast, Coroutine, Optional, Union from . import CRUDGenerator, NOT_FOUND -from ._types import DEPENDENCIES, PAGINATION, PYDANTIC_SCHEMA as SCHEMA +from ._types import PAGINATIONEXTRADATA, DEPENDENCIES, PAGINATION, PYDANTIC_SCHEMA as SCHEMA try: from tortoise.models import Model @@ -13,7 +13,7 @@ CALLABLE = Callable[..., Coroutine[Any, Any, Model]] -CALLABLE_LIST = Callable[..., Coroutine[Any, Any, List[Model]]] +CALLABLE_LIST = Callable[..., Coroutine[Any, Any, PAGINATIONEXTRADATA | List[Model]]] class TortoiseCRUDRouter(CRUDGenerator[SCHEMA]): @@ -32,6 +32,7 @@ def __init__( update_route: Union[bool, DEPENDENCIES] = True, delete_one_route: Union[bool, DEPENDENCIES] = True, delete_all_route: Union[bool, DEPENDENCIES] = True, + paginationextradata: Union[bool, DEPENDENCIES] = False, **kwargs: Any ) -> None: assert ( @@ -58,11 +59,16 @@ def __init__( ) def _get_all(self, *args: Any, **kwargs: Any) -> CALLABLE_LIST: - async def route(pagination: PAGINATION = self.pagination) -> List[Model]: + async def route(pagination: PAGINATION = self.pagination) -> PAGINATIONEXTRADATA | List[Model]: skip, limit = pagination.get("skip"), pagination.get("limit") query = self.db_model.all().offset(cast(int, skip)) + if self.paginationextradata: + count = self.db_model.all().count() # added for issue #138 if limit: query = query.limit(limit) + query = self.schema.from_queryset(query) # added from issue #153 + if self.paginationextradata: + return {"results": await query, "count": await count} return await query return route @@ -70,7 +76,7 @@ async def route(pagination: PAGINATION = self.pagination) -> List[Model]: def _get_one(self, *args: Any, **kwargs: Any) -> CALLABLE: async def route(item_id: int) -> Model: model = await self.db_model.filter(id=item_id).first() - + model = await self.schema.from_tortoise_orm(model) # added from issue #153 if model: return model else: