Skip to content

Commit 69b18da

Browse files
committed
perf: Update the Paginator class
1 parent b090c10 commit 69b18da

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

fastapi_amis_admin/crud/_sqlalchemy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,14 +512,14 @@ async def route(
512512
data.filters = await self.on_filter_pre(request, filters)
513513
if data.filters:
514514
sel = sel.filter(*self.calc_filter_clause(data.filters))
515-
if paginator.show_total:
515+
if paginator.showTotal:
516516
data.total = await self.db.async_scalar(sel.with_only_columns(func.count("*")))
517517
if data.total == 0:
518518
return BaseApiOut(data=data)
519519
orderBy = self._calc_ordering(paginator.orderBy, paginator.orderDir)
520520
if orderBy:
521521
sel = sel.order_by(*orderBy)
522-
sel = sel.limit(paginator.perPage).offset((paginator.page - 1) * paginator.perPage)
522+
sel = sel.limit(paginator.perPage).offset(paginator.offset)
523523
result = await self.db.async_execute(sel)
524524
return BaseApiOut(data=await self.on_list_after(request, result, data))
525525

fastapi_amis_admin/crud/schema.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from enum import Enum
22
from typing import Any, Dict, Generic, List, Optional, TypeVar, Union
3+
from warnings import warn
34

45
from fastapi_amis_admin.utils.pydantic import AllowExtraModelMixin, GenericModel
56

@@ -46,7 +47,7 @@ def __call__(
4647
self,
4748
page: Union[int, str] = 1,
4849
perPage: Union[int, str] = None,
49-
show_total: int = 1,
50+
showTotal: bool = True,
5051
orderBy: str = None,
5152
orderDir: str = "asc",
5253
):
@@ -56,7 +57,20 @@ def __call__(
5657
self.perPage = perPage if perPage > 0 else self.perPageDefault
5758
if self.perPageMax:
5859
self.perPage = min(self.perPage, self.perPageMax)
59-
self.show_total = show_total
60+
self.showTotal = showTotal
6061
self.orderBy = orderBy
6162
self.orderDir = orderDir
6263
return self
64+
65+
@property
66+
def offset(self):
67+
return (self.page - 1) * self.perPage
68+
69+
@property
70+
def limit(self):
71+
return self.perPage
72+
73+
@property
74+
def show_total(self):
75+
warn("show_total is deprecated, use showTotal instead", DeprecationWarning, stacklevel=1)
76+
return self.showTotal

0 commit comments

Comments
 (0)