mirror of
https://github.com/flibusta-apps/book_library_server.git
synced 2025-12-06 07:05:36 +01:00
Fix
This commit is contained in:
@@ -1,8 +1,18 @@
|
||||
from typing import Any, Generic, Protocol, Sequence, TypeVar, runtime_checkable
|
||||
from math import ceil
|
||||
from typing import (
|
||||
Any,
|
||||
Generic,
|
||||
Protocol,
|
||||
Sequence,
|
||||
TypeVar,
|
||||
runtime_checkable,
|
||||
)
|
||||
|
||||
from fastapi_pagination import Page, Params
|
||||
from fastapi_pagination.bases import AbstractParams
|
||||
from pydantic import conint
|
||||
from fastapi_pagination import Params
|
||||
from fastapi_pagination.bases import AbstractParams, BasePage
|
||||
from fastapi_pagination.types import GreaterEqualOne, GreaterEqualZero
|
||||
import orjson
|
||||
from utils.orjson_default import orjson_dumps
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
@@ -14,23 +24,36 @@ class ToDict(Protocol):
|
||||
T = TypeVar("T", ToDict, Any)
|
||||
|
||||
|
||||
class CustomPage(Page[T], Generic[T]):
|
||||
total_pages: conint(ge=0) # type: ignore
|
||||
class Page(BasePage[T], Generic[T]):
|
||||
page: GreaterEqualOne
|
||||
size: GreaterEqualOne
|
||||
total_pages: GreaterEqualZero
|
||||
|
||||
__params_type__ = Params
|
||||
|
||||
class Config:
|
||||
json_loads = orjson.loads
|
||||
json_dumps = orjson_dumps
|
||||
|
||||
@classmethod
|
||||
def create(
|
||||
cls,
|
||||
items: Sequence[T],
|
||||
total: int,
|
||||
params: AbstractParams,
|
||||
) -> Page[T]:
|
||||
*,
|
||||
total: int,
|
||||
**kwargs: Any,
|
||||
) -> "Page[T]":
|
||||
if not isinstance(params, Params):
|
||||
raise ValueError("Page should be used with Params")
|
||||
|
||||
pages = ceil(total / params.size)
|
||||
|
||||
return cls(
|
||||
total=total,
|
||||
items=[item.dict() for item in items],
|
||||
items=items,
|
||||
page=params.page,
|
||||
size=params.size,
|
||||
total_pages=(total + params.size - 1) // params.size,
|
||||
total_pages=pages,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user