Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update query parameters to pydantic model #453

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions backend/app/admin/schema/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ class SaveConfigParam(SchemaBase):

class AnyConfigSchemaBase(SchemaBase):
name: str
type: str | None
type: str | None = None
key: str
value: str
is_frontend: bool
remark: str | None
remark: str | None = None


class CreateAnyConfigParam(AnyConfigSchemaBase):
Expand Down
21 changes: 12 additions & 9 deletions backend/common/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,36 @@
SchemaT = TypeVar('SchemaT')


class _Params(BaseModel, AbstractParams):
class PageParams(BaseModel):
page: int = Query(1, ge=1, description='Page number')
size: int = Query(20, gt=0, le=100, description='Page size') # 默认 20 条记录


class _PageParams(PageParams, AbstractParams):
def to_raw_params(self) -> RawParams:
return RawParams(
limit=self.size,
offset=self.size * (self.page - 1),
)


class _Page(AbstractPage[T], Generic[T]):
class _CustomPage(AbstractPage[T], Generic[T]):
items: Sequence[T] # 数据
total: int # 总数据数
page: int # 第n页
size: int # 每页数量
total_pages: int # 总页数
links: Dict[str, str | None] # 跳转链接

__params_type__ = _Params # 使用自定义的Params
__params_type__ = _PageParams

@classmethod
def create(
cls,
items: Sequence[T],
params: _PageParams,
total: int,
params: _Params,
) -> _Page[T]:
) -> _CustomPage[T]:
page = params.page
size = params.size
total_pages = math.ceil(total / params.size)
Expand All @@ -64,7 +66,7 @@ def create(


class _PageData(BaseModel, Generic[DataT]):
page_data: DataT | None = None
data: DataT | None = None


async def paging_data(db: AsyncSession, select: Select, page_data_schema: SchemaT) -> dict:
Expand All @@ -76,10 +78,11 @@ async def paging_data(db: AsyncSession, select: Select, page_data_schema: Schema
:param page_data_schema:
:return:
"""
_paginate = await paginate(db, select)
page_data = _PageData[_Page[page_data_schema]](page_data=_paginate).model_dump()['page_data']
paginated_data = await paginate(db, select)
# 参考:https://docs.pydantic.dev/dev/concepts/models/#generic-models
page_data = _PageData[_CustomPage[page_data_schema]](page_data=paginated_data).model_dump()['data']
return page_data


# 分页依赖注入
DependsPagination = Depends(pagination_ctx(_Page))
DependsPagination = Depends(pagination_ctx(_CustomPage))
2 changes: 1 addition & 1 deletion backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dependencies = [
"celery==5.3.6",
"cryptography==41.0.7",
"fast-captcha==0.3.2",
"fastapi[all]==0.111.0",
"fastapi[all]==0.115.0",
"fastapi-limiter==0.1.6",
"fastapi-pagination==0.12.13",
"gunicorn==21.2.0",
Expand Down
2 changes: 1 addition & 1 deletion backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ ecdsa==0.19.0
email-validator==2.2.0
exceptiongroup==1.2.2 ; python_full_version < '3.11'
fast-captcha==0.3.2
fastapi==0.111.0
fastapi==0.115.0
fastapi-cli==0.0.5
fastapi-limiter==0.1.6
fastapi-oauth20==0.0.1a2
Expand Down
22 changes: 10 additions & 12 deletions backend/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.