generateur_v3/backend/api/services/models.py
2023-02-28 10:21:08 +01:00

44 lines
1.0 KiB
Python

from __future__ import annotations
from math import ceil
from typing import TypeVar, Generic, Sequence
from fastapi_pagination import Params
from fastapi_pagination.bases import AbstractParams, BasePage
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]:
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)