generateur_v3/backend/api/services/models.py
2023-01-27 21:41:08 +01:00

43 lines
1.1 KiB
Python

from __future__ import annotations
from typing import TypeVar, Generic, Sequence
from fastapi_pagination import Params
from fastapi_pagination.bases import AbstractPage, AbstractParams, BasePage
from math import ceil
from pydantic import conint
T = TypeVar("T")
class Page(BasePage[T], Generic[T]):
page: conint(ge=1) # type: ignore
size: conint(ge=1)
totalPage: conint(ge=0)
hasMore: bool
__params_type__ = Params # Set params related to Page
@classmethod
def create(
cls,
items: Sequence[T],
total: int,
params: AbstractParams,
) -> Page[T]:
print("PARAMS", params)
totalPage = ceil(total/params.size)
return cls(
total=total,
items=items,
page=params.page,
size=params.size,
totalPage = totalPage,
hasMore= params.page < totalPage
)
return {
"items": items,
"total": total,
"size": params,
}
return cls(results=items)