You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
243 lines
8.3 KiB
Django/Jinja
243 lines
8.3 KiB
Django/Jinja
# -*- coding: utf-8 -*-
|
|
import urllib.parse
|
|
from typing import Annotated
|
|
|
|
from fastapi import APIRouter, Body, Depends, Path, UploadFile
|
|
from fastapi.responses import JSONResponse, StreamingResponse
|
|
|
|
from app.common.response import ResponseSchema, StreamResponse, SuccessResponse
|
|
from app.core.base_params import PaginationQueryParam
|
|
from app.core.base_schema import AuthSchema, BatchSetAvailable, PageResultSchema
|
|
from app.core.dependencies import AuthPermission
|
|
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, Depends(AuthPermission(["{{ permission_prefix }}:detail"]))],
|
|
) -> JSONResponse:
|
|
"""
|
|
获取{{ function_name }}详情
|
|
|
|
参数:
|
|
- id (int): {{ function_name }}ID
|
|
- auth (AuthSchema): 认证信息模型
|
|
|
|
返回:
|
|
- JSONResponse: 包含{{ function_name }}详情的JSON响应
|
|
"""
|
|
result_dict = await {{ class_name }}Service.detail_service(id=id, auth=auth)
|
|
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, Depends(AuthPermission(["{{ permission_prefix }}:query"]))],
|
|
) -> JSONResponse:
|
|
"""
|
|
查询{{ function_name }}列表
|
|
|
|
参数:
|
|
- page (PaginationQueryParam): 分页查询参数
|
|
- search ({{ class_name }}QueryParam): 查询参数
|
|
- auth (AuthSchema): 认证信息模型
|
|
|
|
返回:
|
|
- JSONResponse: 包含{{ function_name }}列表分页信息的JSON响应
|
|
"""
|
|
result_dict = await {{ class_name }}Service.page_service(
|
|
auth=auth,
|
|
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: {{ class_name }}CreateSchema,
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["{{ permission_prefix }}:create"]))],
|
|
) -> JSONResponse:
|
|
"""
|
|
创建{{ function_name }}
|
|
|
|
参数:
|
|
- data ({{ class_name }}CreateSchema): {{ function_name }}创建模型
|
|
- auth (AuthSchema): 认证信息模型
|
|
|
|
返回:
|
|
- JSONResponse: 包含创建{{ function_name }}详情的JSON响应
|
|
"""
|
|
result_dict = await {{ class_name }}Service.create_service(auth=auth, 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: {{ class_name }}UpdateSchema,
|
|
id: Annotated[int, Path(description="{{ function_name }}ID")],
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["{{ permission_prefix }}:update"]))],
|
|
) -> JSONResponse:
|
|
"""
|
|
修改{{ function_name }}
|
|
|
|
参数:
|
|
- data ({{ class_name }}UpdateSchema): {{ function_name }}更新模型
|
|
- id (int): {{ function_name }}ID
|
|
- auth (AuthSchema): 认证信息模型
|
|
|
|
返回:
|
|
- JSONResponse: 包含修改{{ function_name }}详情的JSON响应
|
|
"""
|
|
result_dict = await {{ class_name }}Service.update_service(auth=auth, 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, Depends(AuthPermission(["{{ permission_prefix }}:delete"]))],
|
|
) -> JSONResponse:
|
|
"""
|
|
删除{{ function_name }}
|
|
|
|
参数:
|
|
- ids (list[int]): {{ function_name }}ID列表
|
|
- auth (AuthSchema): 认证信息模型
|
|
|
|
返回:
|
|
- JSONResponse: 包含删除{{ function_name }}详情的JSON响应
|
|
"""
|
|
await {{ class_name }}Service.delete_service(auth=auth, 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: BatchSetAvailable,
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["{{ permission_prefix }}:patch"]))],
|
|
) -> JSONResponse:
|
|
"""
|
|
批量修改{{ function_name }}状态
|
|
|
|
参数:
|
|
- data (BatchSetAvailable): 批量修改{{ function_name }}状态模型
|
|
- auth (AuthSchema): 认证信息模型
|
|
|
|
返回:
|
|
- JSONResponse: 包含批量修改{{ function_name }}状态详情的JSON响应
|
|
"""
|
|
await {{ class_name }}Service.set_available_service(auth=auth, 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, Depends()],
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["{{ permission_prefix }}:export"]))],
|
|
) -> StreamingResponse:
|
|
"""
|
|
导出{{ function_name }}
|
|
|
|
参数:
|
|
- search ({{ class_name }}QueryParam): 查询参数
|
|
- auth (AuthSchema): 认证信息模型
|
|
|
|
返回:
|
|
- StreamingResponse: 包含{{ function_name }}列表的Excel文件流响应
|
|
"""
|
|
result_dict_list = await {{ class_name }}Service.list_service(search=search, auth=auth)
|
|
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: UploadFile,
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["{{ permission_prefix }}:import"]))],
|
|
) -> JSONResponse:
|
|
"""
|
|
导入{{ function_name }}
|
|
|
|
参数:
|
|
- file (UploadFile): 导入的Excel文件
|
|
- auth (AuthSchema): 认证信息模型
|
|
|
|
返回:
|
|
- JSONResponse: 包含导入{{ function_name }}详情的JSON响应
|
|
"""
|
|
batch_import_result = await {{ class_name }}Service.batch_import_service(
|
|
file=file, auth=auth, update_support=True
|
|
)
|
|
return SuccessResponse(data=batch_import_result, msg="导入{{ function_name }}成功")
|
|
|
|
|
|
@{{ class_name }}Router.post(
|
|
"/download/template",
|
|
summary="获取{{ function_name }}导入模板",
|
|
dependencies=[Depends(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",
|
|
},
|
|
) |