From 69b18daf80e3a292d1736f074e82d52fb3d871b5 Mon Sep 17 00:00:00 2001 From: atomi <1456417373@qq.com> Date: Mon, 25 Dec 2023 15:55:15 +0800 Subject: [PATCH] perf: Update the Paginator class --- fastapi_amis_admin/crud/_sqlalchemy.py | 4 ++-- fastapi_amis_admin/crud/schema.py | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/fastapi_amis_admin/crud/_sqlalchemy.py b/fastapi_amis_admin/crud/_sqlalchemy.py index 0a17ed0..6af162d 100644 --- a/fastapi_amis_admin/crud/_sqlalchemy.py +++ b/fastapi_amis_admin/crud/_sqlalchemy.py @@ -512,14 +512,14 @@ async def route( data.filters = await self.on_filter_pre(request, filters) if data.filters: sel = sel.filter(*self.calc_filter_clause(data.filters)) - if paginator.show_total: + if paginator.showTotal: data.total = await self.db.async_scalar(sel.with_only_columns(func.count("*"))) if data.total == 0: return BaseApiOut(data=data) orderBy = self._calc_ordering(paginator.orderBy, paginator.orderDir) if orderBy: sel = sel.order_by(*orderBy) - sel = sel.limit(paginator.perPage).offset((paginator.page - 1) * paginator.perPage) + sel = sel.limit(paginator.perPage).offset(paginator.offset) result = await self.db.async_execute(sel) return BaseApiOut(data=await self.on_list_after(request, result, data)) diff --git a/fastapi_amis_admin/crud/schema.py b/fastapi_amis_admin/crud/schema.py index c0fe555..41cc776 100644 --- a/fastapi_amis_admin/crud/schema.py +++ b/fastapi_amis_admin/crud/schema.py @@ -1,5 +1,6 @@ from enum import Enum from typing import Any, Dict, Generic, List, Optional, TypeVar, Union +from warnings import warn from fastapi_amis_admin.utils.pydantic import AllowExtraModelMixin, GenericModel @@ -46,7 +47,7 @@ def __call__( self, page: Union[int, str] = 1, perPage: Union[int, str] = None, - show_total: int = 1, + showTotal: bool = True, orderBy: str = None, orderDir: str = "asc", ): @@ -56,7 +57,20 @@ def __call__( self.perPage = perPage if perPage > 0 else self.perPageDefault if self.perPageMax: self.perPage = min(self.perPage, self.perPageMax) - self.show_total = show_total + self.showTotal = showTotal self.orderBy = orderBy self.orderDir = orderDir return self + + @property + def offset(self): + return (self.page - 1) * self.perPage + + @property + def limit(self): + return self.perPage + + @property + def show_total(self): + warn("show_total is deprecated, use showTotal instead", DeprecationWarning, stacklevel=1) + return self.showTotal