mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-21 04:46:26 +00:00
- 重构代码生成模板,统一路由前缀和业务路径规范 - 优化数据库表分页查询性能,支持方言特定实现 - 新增同步数据库差异预览功能 - 调整字段配置界面,支持拖拽排序和批量设置 - 清理旧版示例代码和冗余配置 - 修复权限和路由嵌套路径处理问题 - 优化前端样式和交互细节
264 lines
8.3 KiB
Python
264 lines
8.3 KiB
Python
import urllib.parse
|
|
from typing import Annotated
|
|
|
|
from fastapi import APIRouter, Body, Depends, Path, UploadFile
|
|
from fastapi.responses import JSONResponse, StreamingResponse
|
|
|
|
from app.api.v1.module_system.auth.schema import AuthSchema
|
|
from app.common.response import ResponseSchema, StreamResponse, SuccessResponse
|
|
from app.core.base_params import PaginationQueryParam
|
|
from app.core.base_schema import BatchSetAvailable
|
|
from app.core.dependencies import AuthPermission
|
|
from app.core.logger import log
|
|
from app.core.router_class import OperationLogRoute
|
|
from app.utils.common_util import bytes2file_response
|
|
|
|
from .schema import Demo01CreateSchema, Demo01OutSchema, Demo01QueryParam, Demo01UpdateSchema
|
|
from .service import Demo01Service
|
|
|
|
Demo01Router = APIRouter(route_class=OperationLogRoute, prefix="/demo01", tags=["示例01模块"])
|
|
|
|
|
|
@Demo01Router.get(
|
|
"/detail/{id}",
|
|
summary="获取示例01详情",
|
|
description="获取示例01详情",
|
|
response_model=ResponseSchema[Demo01OutSchema],
|
|
)
|
|
async def get_obj_detail_controller(
|
|
id: Annotated[int, Path(description="示例ID")],
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_example:demo01:detail"]))],
|
|
) -> JSONResponse:
|
|
"""
|
|
获取示例01详情
|
|
|
|
参数:
|
|
- id (int): 示例01ID
|
|
- auth (AuthSchema): 认证信息模型
|
|
|
|
返回:
|
|
- JSONResponse: 包含示例01详情的JSON响应
|
|
"""
|
|
result_dict = await Demo01Service.detail_service(id=id, auth=auth)
|
|
log.info(f"获取示例01详情成功 {id}")
|
|
return SuccessResponse(data=result_dict, msg="获取示例01详情成功")
|
|
|
|
|
|
@Demo01Router.get(
|
|
"/list",
|
|
summary="查询示例01列表",
|
|
description="查询示例01列表",
|
|
response_model=ResponseSchema[list[Demo01OutSchema]],
|
|
)
|
|
async def get_obj_list_controller(
|
|
page: Annotated[PaginationQueryParam, Depends()],
|
|
search: Annotated[Demo01QueryParam, Depends()],
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_example:demo01:query"]))],
|
|
) -> JSONResponse:
|
|
"""
|
|
查询示例01列表
|
|
|
|
参数:
|
|
- page (PaginationQueryParam): 分页查询参数
|
|
- search (Demo01QueryParam): 查询参数
|
|
- auth (AuthSchema): 认证信息模型
|
|
|
|
返回:
|
|
- JSONResponse: 包含示例01列表分页信息的JSON响应
|
|
"""
|
|
# 使用数据库分页而不是应用层分页
|
|
result_dict = await Demo01Service.page_service(
|
|
auth=auth,
|
|
page_no=page.page_no,
|
|
page_size=page.page_size,
|
|
search=search,
|
|
order_by=page.order_by,
|
|
)
|
|
log.info("查询示例01列表成功")
|
|
return SuccessResponse(data=result_dict, msg="查询示例01列表成功")
|
|
|
|
|
|
@Demo01Router.post(
|
|
"/create",
|
|
summary="创建示例01",
|
|
description="创建示例01",
|
|
response_model=ResponseSchema[Demo01OutSchema],
|
|
)
|
|
async def create_obj_controller(
|
|
data: Demo01CreateSchema,
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_example:demo01:create"]))],
|
|
) -> JSONResponse:
|
|
"""
|
|
创建示例01
|
|
|
|
参数:
|
|
- data (DemoCreateSchema): 示例创建模型
|
|
- auth (AuthSchema): 认证信息模型
|
|
|
|
返回:
|
|
- JSONResponse: 包含创建示例01详情的JSON响应
|
|
"""
|
|
result_dict = await Demo01Service.create_service(auth=auth, data=data)
|
|
log.info(f"创建示例01成功: {result_dict.get('name')}")
|
|
return SuccessResponse(data=result_dict, msg="创建示例01成功")
|
|
|
|
|
|
@Demo01Router.put(
|
|
"/update/{id}",
|
|
summary="修改示例01",
|
|
description="修改示例01",
|
|
response_model=ResponseSchema[Demo01OutSchema],
|
|
)
|
|
async def update_obj_controller(
|
|
data: Demo01UpdateSchema,
|
|
id: Annotated[int, Path(description="示例ID")],
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_example:demo01:update"]))],
|
|
) -> JSONResponse:
|
|
"""
|
|
修改示例01
|
|
|
|
参数:
|
|
- data (Demo01UpdateSchema): 示例01更新模型
|
|
- id (int): 示例ID
|
|
- auth (AuthSchema): 认证信息模型
|
|
|
|
返回:
|
|
- JSONResponse: 包含修改示例01详情的JSON响应
|
|
"""
|
|
result_dict = await Demo01Service.update_service(auth=auth, id=id, data=data)
|
|
log.info(f"修改示例01成功: {result_dict.get('name')}")
|
|
return SuccessResponse(data=result_dict, msg="修改示例01成功")
|
|
|
|
|
|
@Demo01Router.delete(
|
|
"/delete",
|
|
summary="删除示例01",
|
|
description="删除示例01",
|
|
response_model=ResponseSchema[None],
|
|
)
|
|
async def delete_obj_controller(
|
|
ids: Annotated[list[int], Body(description="ID列表")],
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_example:demo01:delete"]))],
|
|
) -> JSONResponse:
|
|
"""
|
|
删除示例01
|
|
|
|
参数:
|
|
- ids (list[int]): 示例ID列表
|
|
- auth (AuthSchema): 认证信息模型
|
|
|
|
返回:
|
|
- JSONResponse: 包含删除示例01详情的JSON响应
|
|
"""
|
|
await Demo01Service.delete_service(auth=auth, ids=ids)
|
|
log.info(f"删除示例01成功: {ids}")
|
|
return SuccessResponse(msg="删除示例01成功")
|
|
|
|
|
|
@Demo01Router.patch(
|
|
"/available/setting",
|
|
summary="批量修改示例01状态",
|
|
description="批量修改示例01状态",
|
|
response_model=ResponseSchema[None],
|
|
)
|
|
async def batch_set_available_obj_controller(
|
|
data: BatchSetAvailable,
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_example:demo01:patch"]))],
|
|
) -> JSONResponse:
|
|
"""
|
|
批量修改示例01状态
|
|
|
|
参数:
|
|
- data (BatchSetAvailable): 批量修改示例01状态模型
|
|
- auth (AuthSchema): 认证信息模型
|
|
|
|
返回:
|
|
- JSONResponse: 包含批量修改示例01状态详情的JSON响应
|
|
"""
|
|
await Demo01Service.set_available_service(auth=auth, data=data)
|
|
log.info(f"批量修改示例01状态成功: {data.ids}")
|
|
return SuccessResponse(msg="批量修改示例01状态成功")
|
|
|
|
|
|
@Demo01Router.post(
|
|
"/export",
|
|
summary="导出示例01",
|
|
description="导出示例01",
|
|
)
|
|
async def export_obj_list_controller(
|
|
search: Annotated[Demo01QueryParam, Depends()],
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_example:demo01:export"]))],
|
|
) -> StreamingResponse:
|
|
"""
|
|
导出示例01
|
|
|
|
参数:
|
|
- search (Demo01QueryParam): 查询参数
|
|
- auth (AuthSchema): 认证信息模型
|
|
|
|
返回:
|
|
- StreamingResponse: 包含示例01列表的Excel文件流响应
|
|
"""
|
|
result_dict_list = await Demo01Service.list_service(search=search, auth=auth)
|
|
export_result = await Demo01Service.batch_export_service(obj_list=result_dict_list)
|
|
log.info("导出示例01成功")
|
|
|
|
return StreamResponse(
|
|
data=bytes2file_response(export_result),
|
|
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
headers={"Content-Disposition": "attachment; filename=demo01.xlsx"},
|
|
)
|
|
|
|
|
|
@Demo01Router.post(
|
|
"/import",
|
|
summary="导入示例01",
|
|
description="导入示例01",
|
|
response_model=ResponseSchema[str],
|
|
)
|
|
async def import_obj_list_controller(
|
|
file: UploadFile,
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_example:demo01:import"]))],
|
|
) -> JSONResponse:
|
|
"""
|
|
导入示例01
|
|
|
|
参数:
|
|
- file (UploadFile): 导入的Excel文件
|
|
- auth (AuthSchema): 认证信息模型
|
|
|
|
返回:
|
|
- JSONResponse: 包含导入示例详情的JSON响应
|
|
"""
|
|
batch_import_result = await Demo01Service.batch_import_service(
|
|
file=file, auth=auth, update_support=True
|
|
)
|
|
log.info(f"导入示例01成功: {batch_import_result}")
|
|
return SuccessResponse(data=batch_import_result, msg="导入示例01成功")
|
|
|
|
|
|
@Demo01Router.post(
|
|
"/download/template",
|
|
summary="获取示例01导入模板",
|
|
description="获取示例01导入模板",
|
|
dependencies=[Depends(AuthPermission(["module_example:demo01:download"]))],
|
|
)
|
|
async def export_obj_template_controller() -> StreamingResponse:
|
|
"""
|
|
获取示例01导入模板
|
|
|
|
返回:
|
|
- StreamingResponse: 包含示例01导入模板的Excel文件流响应
|
|
"""
|
|
import_template_result = await Demo01Service.import_template_download_service()
|
|
log.info("获取示例01导入模板成功")
|
|
|
|
return StreamResponse(
|
|
data=bytes2file_response(import_template_result),
|
|
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
headers={
|
|
"Content-Disposition": f"attachment; filename={urllib.parse.quote('示例01导入模板.xlsx')}",
|
|
"Access-Control-Expose-Headers": "Content-Disposition",
|
|
},
|
|
)
|