mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-21 20:55:14 +00:00
refactor(backend): 重构数据库模型和初始化逻辑 fix(frontend): 修复权限指令在多个组件中的使用问题 perf(backend): 优化数据库连接和初始化性能 docs: 更新低代码生成器的README文档 style: 清理无用代码和注释 chore: 移除不再需要的依赖项 test: 更新用户权限相关测试用例 ci: 更新CI配置以支持新的权限检查 build: 更新依赖项版本
113 lines
6.1 KiB
Django/Jinja
113 lines
6.1 KiB
Django/Jinja
# -*- coding:utf-8 -*-
|
|
|
|
from fastapi import APIRouter, Depends, UploadFile
|
|
from fastapi.responses import StreamingResponse, JSONResponse
|
|
from app.common.response import SuccessResponse, StreamResponse
|
|
from app.core.dependencies import AuthPermission
|
|
from app.core.router_class import OperationLogRoute
|
|
from app.api.v1.module_system.auth.schema import AuthSchema
|
|
from app.api.v1.module_system.user.schema import UserOutSchema
|
|
from app.common.request import PaginationService
|
|
from app.core.base_params import PaginationQueryParam
|
|
from app.utils.common_util import bytes2file_response
|
|
|
|
from {{ packageName }}.service.{{ tableName }}_service import {{ tableName|snake_to_pascal_case }}Service
|
|
from {{ packageName }}.schema import {{ tableName|snake_to_pascal_case }}CreateSchema, {{ tableName|snake_to_pascal_case }}UpdateSchema
|
|
from {{ packageName }}.param import {{ tableName|snake_to_pascal_case }}QueryParam
|
|
|
|
{{ tableName|snake_to_camel }}Controller = APIRouter(route_class=OperationLogRoute, prefix='/{{ moduleName }}/{{ businessName }}', tags=["{{ functionName }}模块"])
|
|
|
|
|
|
@{{ tableName|snake_to_camel }}Controller.get('/list', summary="查询{{ functionName }}列表", description="查询{{ functionName }}列表")
|
|
async def get_{{ tableName }}_list(
|
|
page: PaginationQueryParam = Depends(),
|
|
search: {{ tableName|snake_to_pascal_case }}QueryParam = Depends(),
|
|
auth: AuthSchema = Depends(AuthPermission(permissions=["{{ permissionPrefix }}:list"]))
|
|
):
|
|
{{ tableName }}_result_list = await {{ tableName|snake_to_pascal_case }}Service.get_{{ tableName }}_list_service(auth, search, page.order_by)
|
|
{{ tableName }}_result = await PaginationService.paginate(
|
|
data_list={{ tableName }}_result_list,
|
|
page_no=page.page_no,
|
|
page_size=page.page_size
|
|
)
|
|
return SuccessResponse(data={{ tableName }}_result)
|
|
|
|
|
|
@{{ tableName|snake_to_camel }}Controller.get('/{id}', summary="获取{{ functionName }}详细信息", description="获取{{ functionName }}详细信息")
|
|
async def get_{{ tableName }}_by_id(
|
|
id: int,
|
|
auth: AuthSchema = Depends(AuthPermission(permissions=["{{ permissionPrefix }}:query"]))
|
|
):
|
|
{{ tableName }} = await {{ tableName|snake_to_pascal_case }}Service.get_{{ tableName }}_by_id_service(auth, id)
|
|
return SuccessResponse(data={{ tableName }})
|
|
|
|
|
|
@{{ tableName|snake_to_camel }}Controller.post('', summary="新增{{ functionName }}", description="新增{{ functionName }}")
|
|
async def add_{{ tableName }} (
|
|
add_model: {{ tableName|snake_to_pascal_case }}CreateSchema,
|
|
auth: AuthSchema = Depends(AuthPermission(permissions=["{{ permissionPrefix }}:add"])),
|
|
current_user: UserOutSchema = Depends(lambda auth: auth.user)
|
|
):
|
|
add_result = await {{ tableName|snake_to_pascal_case }}Service.add_{{ tableName }}_service(auth, add_model)
|
|
return SuccessResponse(msg="新增成功")
|
|
|
|
|
|
@{{ tableName|snake_to_camel }}Controller.put('/{id}', summary="修改{{ functionName }}", description="修改{{ functionName }}")
|
|
async def update_{{ tableName }}(
|
|
id: int,
|
|
edit_model: {{ tableName|snake_to_pascal_case }}UpdateSchema,
|
|
auth: AuthSchema = Depends(AuthPermission(permissions=["{{ permissionPrefix }}:edit"])),
|
|
current_user: UserOutSchema = Depends(lambda auth: auth.user)
|
|
):
|
|
update_result = await {{ tableName|snake_to_pascal_case }}Service.update_{{ tableName }}_service(auth, id, edit_model)
|
|
return SuccessResponse(msg="修改成功")
|
|
|
|
|
|
@{{ tableName|snake_to_camel }}Controller.delete('/{ids}', summary="删除{{ functionName }}", description="删除{{ functionName }}")
|
|
async def del_{{ tableName }}(
|
|
ids: str,
|
|
auth: AuthSchema = Depends(AuthPermission(permissions=["{{ permissionPrefix }}:remove"]))
|
|
):
|
|
id_list = [int(id) for id in ids.split(',')]
|
|
del_result = await {{ tableName|snake_to_pascal_case }}Service.del_{{ tableName }}_service(auth, id_list)
|
|
return SuccessResponse(msg="删除成功")
|
|
|
|
|
|
@{{ tableName|snake_to_camel }}Controller.post('/export', summary="导出{{ functionName }}", description="导出{{ functionName }}")
|
|
async def export_{{ tableName }}(
|
|
search: {{ tableName|snake_to_pascal_case }}QueryParam = Depends(),
|
|
auth: AuthSchema = Depends(AuthPermission(permissions=["{{ permissionPrefix }}:export"]))
|
|
) -> StreamingResponse:
|
|
# 获取全量数据
|
|
{{ tableName }}_list = await {{ tableName|snake_to_pascal_case }}Service.get_{{ tableName }}_list_service(auth=auth, search=search)
|
|
export_result = await {{ tableName|snake_to_pascal_case }}Service.export_{{ tableName }}_list_service({{ tableName }}_list)
|
|
return StreamResponse(
|
|
data=bytes2file_response(export_result),
|
|
media_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
headers = {
|
|
'Content-Disposition': 'attachment; filename={{ tableName }}.xlsx'
|
|
}
|
|
)
|
|
|
|
|
|
@{{ tableName|snake_to_camel }}Controller.post('/import', summary="导入{{ functionName }}", description="导入{{ functionName }}")
|
|
async def import_{{ tableName }}(
|
|
file: UploadFile,
|
|
auth: AuthSchema = Depends(AuthPermission(permissions=["{{ permissionPrefix }}:import"]))
|
|
) -> JSONResponse:
|
|
batch_import_result = await {{ tableName|snake_to_pascal_case }}Service.import_{{ tableName }}_service(file=file, auth=auth, update_support=True)
|
|
return SuccessResponse(data=batch_import_result, msg="导入成功")
|
|
|
|
|
|
@{{ tableName|snake_to_camel }}Controller.post('/download/template', summary="获取{{ functionName }}导入模板", description="获取{{ functionName }}导入模板")
|
|
async def export_{{ tableName }}_template(
|
|
auth: AuthSchema = Depends(AuthPermission(permissions=["{{ permissionPrefix }}:import"]))
|
|
) -> StreamingResponse:
|
|
{{ tableName }}_import_template_result = await {{ tableName|snake_to_pascal_case }}Service.get_import_template_{{ tableName }}_service()
|
|
return StreamResponse(
|
|
data=bytes2file_response({{ tableName }}_import_template_result),
|
|
media_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
headers = {
|
|
'Content-Disposition': f'attachment; filename={{ tableName }}_import_template.xlsx'
|
|
}
|
|
) |