mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-22 13:05:18 +00:00
这是一次综合性的项目迭代,包含以下核心变更:
1. **目录与模块重构**
- 调整工作流节点类型模块目录结构,迁移节点类型相关代码
- 重命名platform模块为system模块,更新插件配置信息
- 重构代码生成模块导入路径
2. **数据库与CRUD优化**
- 统一所有CRUD类构造函数,新增数据库会话参数
- 修复权限过滤器数据库会话使用问题
- 更新模板生成器的CRUD代码模板
3. **认证与安全改进**
- 重构JWT密钥配置,移除默认密钥强制要求环境变量
- 重命名密码工具类,统一密码加密校验逻辑
- 优化OAuth认证流程,修复匿名认证使用问题
4. **前端与静态资源**
- 重构前端挂载逻辑,增加目录存在性校验
- 使用标准StaticFiles替换自定义前端挂载实现
5. **工具类与依赖更新**
- 修复导入工具的表名重复检测逻辑
- 优化限流回调代码,移除冗余依赖
- 更新用户、租户等模块的响应模型字段
6. **数据与配置修正**
- 修复系统版本数据字段命名不统一问题
- 简化枚举类校验逻辑,移除冗余注释
- 修复测试用例中的密码工具类导入错误
262 lines
9.4 KiB
Django/Jinja
262 lines
9.4 KiB
Django/Jinja
# -*- coding: utf-8 -*-
|
|
import urllib.parse
|
|
from typing import Annotated
|
|
|
|
from fastapi import APIRouter, Body, Depends, File, Path, Query, Security, UploadFile
|
|
from fastapi.responses import JSONResponse, StreamingResponse
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.base_schema import AuthSchema
|
|
from app.common.response import ResponseSchema, StreamResponse, SuccessResponse
|
|
from app.core.base_schema import BatchSetAvailable, PageResultSchema, PaginationQueryParam
|
|
from app.core.dependencies import AuthPermission, db_getter
|
|
from app.core.router_class import OperationLogRoute
|
|
from app.utils.common_util import bytes2file_response
|
|
|
|
from .schema import {{ class_name }}CreateSchema, {{ class_name }}OutSchema, {{ class_name }}QueryParam, {{ class_name }}UpdateSchema
|
|
from .service import {{ class_name }}Service
|
|
|
|
{{ class_name }}Router = APIRouter(route_class=OperationLogRoute, prefix="/{{ module_name }}", tags=["{{ function_name }}模块"])
|
|
|
|
|
|
@{{ class_name }}Router.get(
|
|
"/detail/{id}",
|
|
summary="获取{{ function_name }}详情",
|
|
response_model=ResponseSchema[{{ class_name }}OutSchema],
|
|
)
|
|
async def get_obj_detail_controller(
|
|
id: Annotated[int, Path(description="{{ function_name }}ID")],
|
|
auth: Annotated[AuthSchema, Security(AuthPermission(["{{ permission_prefix }}:detail"]))],
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
|
) -> JSONResponse:
|
|
"""
|
|
获取{{ function_name }}详情
|
|
|
|
参数:
|
|
- id (int): {{ function_name }}ID
|
|
- auth (AuthSchema): 认证信息模型
|
|
- db (AsyncSession): 数据库会话
|
|
|
|
返回:
|
|
- JSONResponse: 包含{{ function_name }}详情的JSON响应
|
|
"""
|
|
result_dict = await {{ class_name }}Service.detail_service(id=id, auth=auth, db=db)
|
|
return SuccessResponse(data=result_dict, msg="获取{{ function_name }}详情成功")
|
|
|
|
|
|
@{{ class_name }}Router.get(
|
|
"/list",
|
|
summary="分页查询{{ function_name }}",
|
|
response_model=ResponseSchema[PageResultSchema[{{ class_name }}OutSchema]],
|
|
)
|
|
async def get_obj_list_controller(
|
|
page: Annotated[PaginationQueryParam, Depends()],
|
|
search: Annotated[{{ class_name }}QueryParam, Depends()],
|
|
auth: Annotated[AuthSchema, Security(AuthPermission(["{{ permission_prefix }}:query"]))],
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
|
) -> JSONResponse:
|
|
"""
|
|
查询{{ function_name }}列表
|
|
|
|
参数:
|
|
- page (PaginationQueryParam): 分页查询参数
|
|
- search ({{ class_name }}QueryParam): 查询参数
|
|
- auth (AuthSchema): 认证信息模型
|
|
- db (AsyncSession): 数据库会话
|
|
|
|
返回:
|
|
- JSONResponse: 包含{{ function_name }}列表分页信息的JSON响应
|
|
"""
|
|
result_dict = await {{ class_name }}Service.page_service(
|
|
auth=auth,
|
|
db=db,
|
|
page_no=page.page_no,
|
|
page_size=page.page_size,
|
|
search=search,
|
|
order_by=page.order_by,
|
|
)
|
|
return SuccessResponse(data=result_dict, msg="查询{{ function_name }}列表成功")
|
|
|
|
|
|
@{{ class_name }}Router.post(
|
|
"/create",
|
|
summary="创建{{ function_name }}",
|
|
response_model=ResponseSchema[{{ class_name }}OutSchema],
|
|
)
|
|
async def create_obj_controller(
|
|
data: Annotated[{{ class_name }}CreateSchema, Body(description="创建参数")],
|
|
auth: Annotated[AuthSchema, Security(AuthPermission(["{{ permission_prefix }}:create"]))],
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
|
) -> JSONResponse:
|
|
"""
|
|
创建{{ function_name }}
|
|
|
|
参数:
|
|
- data ({{ class_name }}CreateSchema): {{ function_name }}创建模型
|
|
- auth (AuthSchema): 认证信息模型
|
|
- db (AsyncSession): 数据库会话
|
|
|
|
返回:
|
|
- JSONResponse: 包含创建{{ function_name }}详情的JSON响应
|
|
"""
|
|
result_dict = await {{ class_name }}Service.create_service(auth=auth, db=db, data=data)
|
|
return SuccessResponse(data=result_dict, msg="创建{{ function_name }}成功")
|
|
|
|
|
|
@{{ class_name }}Router.put(
|
|
"/update/{id}",
|
|
summary="修改{{ function_name }}",
|
|
response_model=ResponseSchema[{{ class_name }}OutSchema],
|
|
)
|
|
async def update_obj_controller(
|
|
data: Annotated[{{ class_name }}UpdateSchema, Body(description="更新参数")],
|
|
id: Annotated[int, Path(description="{{ function_name }}ID")],
|
|
auth: Annotated[AuthSchema, Security(AuthPermission(["{{ permission_prefix }}:update"]))],
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
|
) -> JSONResponse:
|
|
"""
|
|
修改{{ function_name }}
|
|
|
|
参数:
|
|
- data ({{ class_name }}UpdateSchema): {{ function_name }}更新模型
|
|
- id (int): {{ function_name }}ID
|
|
- auth (AuthSchema): 认证信息模型
|
|
- db (AsyncSession): 数据库会话
|
|
|
|
返回:
|
|
- JSONResponse: 包含修改{{ function_name }}详情的JSON响应
|
|
"""
|
|
result_dict = await {{ class_name }}Service.update_service(auth=auth, db=db, id=id, data=data)
|
|
return SuccessResponse(data=result_dict, msg="修改{{ function_name }}成功")
|
|
|
|
|
|
@{{ class_name }}Router.delete(
|
|
"/delete",
|
|
summary="删除{{ function_name }}",
|
|
response_model=ResponseSchema[None],
|
|
)
|
|
async def delete_obj_controller(
|
|
ids: Annotated[list[int], Body(description="ID列表")],
|
|
auth: Annotated[AuthSchema, Security(AuthPermission(["{{ permission_prefix }}:delete"]))],
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
|
) -> JSONResponse:
|
|
"""
|
|
删除{{ function_name }}
|
|
|
|
参数:
|
|
- ids (list[int]): {{ function_name }}ID列表
|
|
- auth (AuthSchema): 认证信息模型
|
|
- db (AsyncSession): 数据库会话
|
|
|
|
返回:
|
|
- JSONResponse: 包含删除{{ function_name }}详情的JSON响应
|
|
"""
|
|
await {{ class_name }}Service.delete_service(auth=auth, db=db, ids=ids)
|
|
return SuccessResponse(msg="删除{{ function_name }}成功")
|
|
|
|
|
|
@{{ class_name }}Router.patch(
|
|
"/status/batch",
|
|
summary="批量修改{{ function_name }}状态",
|
|
response_model=ResponseSchema[None],
|
|
)
|
|
async def batch_set_available_obj_controller(
|
|
data: Annotated[BatchSetAvailable, Body(description="状态设置")],
|
|
auth: Annotated[AuthSchema, Security(AuthPermission(["{{ permission_prefix }}:patch"]))],
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
|
) -> JSONResponse:
|
|
"""
|
|
批量修改{{ function_name }}状态
|
|
|
|
参数:
|
|
- data (BatchSetAvailable): 批量修改{{ function_name }}状态模型
|
|
- auth (AuthSchema): 认证信息模型
|
|
- db (AsyncSession): 数据库会话
|
|
|
|
返回:
|
|
- JSONResponse: 包含批量修改{{ function_name }}状态详情的JSON响应
|
|
"""
|
|
await {{ class_name }}Service.set_available_service(auth=auth, db=db, data=data)
|
|
return SuccessResponse(msg="批量修改{{ function_name }}状态成功")
|
|
|
|
|
|
@{{ class_name }}Router.post(
|
|
"/export",
|
|
summary="导出{{ function_name }}",
|
|
)
|
|
async def export_obj_list_controller(
|
|
search: Annotated[{{ class_name }}QueryParam, Query(description="查询参数")],
|
|
auth: Annotated[AuthSchema, Security(AuthPermission(["{{ permission_prefix }}:export"]))],
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
|
) -> StreamingResponse:
|
|
"""
|
|
导出{{ function_name }}
|
|
|
|
参数:
|
|
- search ({{ class_name }}QueryParam): 查询参数
|
|
- auth (AuthSchema): 认证信息模型
|
|
- db (AsyncSession): 数据库会话
|
|
|
|
返回:
|
|
- StreamingResponse: 包含{{ function_name }}列表的Excel文件流响应
|
|
"""
|
|
result_dict_list = await {{ class_name }}Service.list_service(search=search, auth=auth, db=db)
|
|
export_result = {{ class_name }}Service.batch_export_service(obj_list=result_dict_list)
|
|
|
|
return StreamResponse(
|
|
data=bytes2file_response(export_result),
|
|
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
headers={"Content-Disposition": "attachment; filename={{ table_name }}.xlsx"},
|
|
)
|
|
|
|
|
|
@{{ class_name }}Router.post(
|
|
"/import",
|
|
summary="导入{{ function_name }}",
|
|
response_model=ResponseSchema[str],
|
|
)
|
|
async def import_obj_list_controller(
|
|
file: Annotated[UploadFile, File(description="导入的Excel文件")],
|
|
auth: Annotated[AuthSchema, Security(AuthPermission(["{{ permission_prefix }}:import"]))],
|
|
db: Annotated[AsyncSession, Depends(db_getter)],
|
|
) -> JSONResponse:
|
|
"""
|
|
导入{{ function_name }}
|
|
|
|
参数:
|
|
- file (UploadFile): 导入的Excel文件
|
|
- auth (AuthSchema): 认证信息模型
|
|
- db (AsyncSession): 数据库会话
|
|
|
|
返回:
|
|
- JSONResponse: 包含导入{{ function_name }}详情的JSON响应
|
|
"""
|
|
batch_import_result = await {{ class_name }}Service.batch_import_service(
|
|
file=file, auth=auth, db=db, update_support=True
|
|
)
|
|
return SuccessResponse(data=batch_import_result, msg="导入{{ function_name }}成功")
|
|
|
|
|
|
@{{ class_name }}Router.post(
|
|
"/download/template",
|
|
summary="获取{{ function_name }}导入模板",
|
|
dependencies=[Security(AuthPermission(["{{ permission_prefix }}:download"]))],
|
|
)
|
|
async def export_obj_template_controller() -> StreamingResponse:
|
|
"""
|
|
获取{{ function_name }}导入模板
|
|
|
|
返回:
|
|
- StreamingResponse: 包含{{ function_name }}导入模板的Excel文件流响应
|
|
"""
|
|
import_template_result = {{ class_name }}Service.import_template_download_service()
|
|
|
|
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('{{ function_name }}导入模板.xlsx')}",
|
|
"Access-Control-Expose-Headers": "Content-Disposition",
|
|
},
|
|
)
|