refactor(任务调度): 更新任务白名单和模块路径配置

删除示例模块相关文件
更新任务白名单配置从'module_task'到'function_task'
修改任务调度模块路径为新的功能任务路径
This commit is contained in:
zhangtao
2025-11-19 02:31:51 +08:00
parent 279f547f84
commit fb48b9ea1f
8 changed files with 2 additions and 537 deletions
@@ -1,129 +0,0 @@
# -*- coding:utf-8 -*-
from fastapi import APIRouter, Depends, UploadFile, Body, Path
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.core.base_params import PaginationQueryParam
from app.utils.common_util import bytes2file_response
from app.core.logger import log
from app.core.base_schema import BatchSetAvailable
from .service import GenDemo01Service
from .schema import GenDemo01CreateSchema, GenDemo01UpdateSchema
from .param import GenDemo01QueryParam
GenDemo01Router = APIRouter(route_class=OperationLogRoute, prefix='/gen_demo01', tags=["示例模块"])
@GenDemo01Router.get("/detail/{id}", summary="获取示例详情", description="获取示例详情")
async def get_gen_demo01_detail_controller(
id: int = Path(..., description="ID"),
auth: AuthSchema = Depends(AuthPermission(["module_gencode:gen_demo01:query"]))
) -> JSONResponse:
"""获取示例详情接口"""
result_dict = await GenDemo01Service.detail_gen_demo01_service(auth=auth, id=id)
log.info(f"获取示例详情成功 {id}")
return SuccessResponse(data=result_dict, msg="获取示例详情成功")
@GenDemo01Router.get("/list", summary="查询示例列表", description="查询示例列表")
async def get_gen_demo01_list_controller(
page: PaginationQueryParam = Depends(),
search: GenDemo01QueryParam = Depends(),
auth: AuthSchema = Depends(AuthPermission(["module_gencode:gen_demo01:query"]))
) -> JSONResponse:
"""查询示例列表接口(数据库分页)"""
result_dict = await GenDemo01Service.page_service(
auth=auth,
page_no=page.page_no if page.page_no is not None else 1,
page_size=page.page_size if page.page_size is not None else 10,
search=search,
order_by=page.order_by
)
log.info("查询示例列表成功")
return SuccessResponse(data=result_dict, msg="查询示例列表成功")
@GenDemo01Router.post("/create", summary="创建示例", description="创建示例")
async def create_gen_demo01_controller(
data: GenDemo01CreateSchema,
auth: AuthSchema = Depends(AuthPermission(["module_gencode:gen_demo01:create"]))
) -> JSONResponse:
"""创建示例接口"""
result_dict = await GenDemo01Service.create_gen_demo01_service(auth=auth, data=data)
log.info("创建示例成功")
return SuccessResponse(data=result_dict, msg="创建示例成功")
@GenDemo01Router.put("/update/{id}", summary="修改示例", description="修改示例")
async def update_gen_demo01_controller(
data: GenDemo01UpdateSchema,
id: int = Path(..., description="ID"),
auth: AuthSchema = Depends(AuthPermission(["module_gencode:gen_demo01:update"]))
) -> JSONResponse:
"""修改示例接口"""
result_dict = await GenDemo01Service.update_gen_demo01_service(auth=auth, id=id, data=data)
log.info("修改示例成功")
return SuccessResponse(data=result_dict, msg="修改示例成功")
@GenDemo01Router.delete("/delete", summary="删除示例", description="删除示例")
async def delete_gen_demo01_controller(
ids: list[int] = Body(..., description="ID列表"),
auth: AuthSchema = Depends(AuthPermission(["module_gencode:gen_demo01:delete"]))
) -> JSONResponse:
"""删除示例接口"""
await GenDemo01Service.delete_gen_demo01_service(auth=auth, ids=ids)
log.info(f"删除示例成功: {ids}")
return SuccessResponse(msg="删除示例成功")
@GenDemo01Router.patch("/available/setting", summary="批量修改示例状态", description="批量修改示例状态")
async def batch_set_available_gen_demo01_controller(
data: BatchSetAvailable,
auth: AuthSchema = Depends(AuthPermission(["module_gencode:gen_demo01:patch"]))
) -> JSONResponse:
"""批量修改示例状态接口"""
await GenDemo01Service.set_available_gen_demo01_service(auth=auth, data=data)
log.info(f"批量修改示例状态成功: {data.ids}")
return SuccessResponse(msg="批量修改示例状态成功")
@GenDemo01Router.post('/export', summary="导出示例", description="导出示例")
async def export_gen_demo01_list_controller(
search: GenDemo01QueryParam = Depends(),
auth: AuthSchema = Depends(AuthPermission(["module_gencode:gen_demo01:export"]))
) -> StreamingResponse:
"""导出示例接口"""
result_dict_list = await GenDemo01Service.list_gen_demo01_service(search=search, auth=auth)
export_result = await GenDemo01Service.batch_export_service(obj_list=result_dict_list)
log.info('导出示例成功')
return StreamResponse(
data=bytes2file_response(export_result),
media_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
headers={
'Content-Disposition': 'attachment; filename=gen_demo01.xlsx'
}
)
@GenDemo01Router.post('/import', summary="导入示例", description="导入示例")
async def import_gen_demo01_list_controller(
file: UploadFile,
auth: AuthSchema = Depends(AuthPermission(["module_gencode:gen_demo01:import"]))
) -> JSONResponse:
"""导入示例接口"""
batch_import_result = await GenDemo01Service.batch_import_gen_demo01_service(file=file, auth=auth, update_support=True)
log.info("导入示例成功")
return SuccessResponse(data=batch_import_result, msg="导入示例成功")
@GenDemo01Router.post('/download/template', summary="获取示例导入模板", description="获取示例导入模板", dependencies=[Depends(AuthPermission(["module_gencode:gen_demo01:download"]))])
async def export_gen_demo01_template_controller() -> StreamingResponse:
"""获取示例导入模板接口"""
example_import_template_result = await GenDemo01Service.import_template_download_gen_demo01_service()
log.info('获取示例导入模板成功')
return StreamResponse(
data=bytes2file_response(example_import_template_result),
media_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
headers={
'Content-Disposition': 'attachment; filename=gen_demo01_template.xlsx'
}
)
@@ -1,123 +0,0 @@
# -*- coding:utf-8 -*-
from typing import Dict, List, Optional, Sequence, Union, Any
from app.core.base_crud import CRUDBase
from app.api.v1.module_system.auth.schema import AuthSchema
from .model import GenDemo01Model
from .schema import GenDemo01CreateSchema, GenDemo01UpdateSchema, GenDemo01OutSchema
class GenDemo01CRUD(CRUDBase[GenDemo01Model, GenDemo01CreateSchema, GenDemo01UpdateSchema]):
"""示例数据层"""
def __init__(self, auth: AuthSchema) -> None:
"""
初始化CRUD数据层
参数:
- auth (AuthSchema): 认证信息模型
"""
super().__init__(model=GenDemo01Model, auth=auth)
async def get_by_id_gen_demo01_crud(self, id: int, preload: Optional[List[Union[str, Any]]] = None) -> Optional[GenDemo01Model]:
"""
详情
参数:
- id (int): 对象ID
- preload (Optional[List[Union[str, Any]]]): 预加载关系,未提供时使用模型默认项
返回:
- Optional[GenDemo01Model]: 模型实例或None
"""
return await self.get(id=id, preload=preload)
async def list_gen_demo01_crud(self, search: Optional[Dict] = None, order_by: Optional[List[Dict[str, str]]] = None, preload: Optional[List[Union[str, Any]]] = None) -> Sequence[GenDemo01Model]:
"""
列表查询
参数:
- search (Optional[Dict]): 查询参数
- order_by (Optional[List[Dict[str, str]]]): 排序参数
- preload (Optional[List[Union[str, Any]]]): 预加载关系,未提供时使用模型默认项
返回:
- Sequence[GenDemo01Model]: 模型实例序列
"""
return await self.list(search=search, order_by=order_by, preload=preload)
async def create_gen_demo01_crud(self, data: GenDemo01CreateSchema) -> Optional[GenDemo01Model]:
"""
创建
参数:
- data (GenDemo01CreateSchema): 创建模型
返回:
- Optional[GenDemo01Model]: 模型实例或None
"""
return await self.create(data=data)
async def update_gen_demo01_crud(self, id: int, data: GenDemo01UpdateSchema) -> Optional[GenDemo01Model]:
"""
更新
参数:
- id (int): 对象ID
- data (GenDemo01UpdateSchema): 更新模型
返回:
- Optional[GenDemo01Model]: 模型实例或None
"""
return await self.update(id=id, data=data)
async def delete_gen_demo01_crud(self, ids: List[int]) -> None:
"""
批量删除
参数:
- ids (List[int]): 对象ID列表
返回:
- None
"""
return await self.delete(ids=ids)
async def set_available_gen_demo01_crud(self, ids: List[int], status: bool) -> None:
"""
批量设置可用状态
参数:
- ids (List[int]): 对象ID列表
- status (bool): 可用状态
返回:
- None
"""
return await self.set(ids=ids, status=status)
async def page_gen_demo01_crud(self, offset: int, limit: int, order_by: Optional[List[Dict[str, str]]] = None, search: Optional[Dict] = None, preload: Optional[List[Union[str, Any]]] = None) -> Dict:
"""
分页查询
参数:
- offset (int): 偏移量
- limit (int): 每页数量
- order_by (Optional[List[Dict[str, str]]]): 排序参数
- search (Optional[Dict]): 查询参数
- preload (Optional[List[Union[str, Any]]]): 预加载关系,未提供时使用模型默认项
返回:
- Dict: 分页数据
"""
order_by_list = order_by or [{'id': 'asc'}]
search_dict = search or {}
return await self.page(
offset=offset,
limit=limit,
order_by=order_by_list,
search=search_dict,
out_schema=GenDemo01OutSchema,
preload=preload
)
@@ -1,21 +0,0 @@
# -*- coding: utf-8 -*-
from sqlalchemy import Integer, Text, String, SmallInteger, DateTime
from typing import Optional
from sqlalchemy.orm import Mapped, mapped_column
from app.core.base_model import CreatorMixin
class GenDemo01Model(CreatorMixin):
"""
示例表
"""
__tablename__ = 'gen_demo01'
__table_args__ = {'comment': '示例'}
__loader_options__ = ["creator"]
name: Mapped[Optional[str]] = mapped_column(String(64), nullable=True, comment='名称')
status: Mapped[Optional[int]] = mapped_column(SmallInteger, nullable=True, comment='是否启用(True:启用 False:禁用)')
@@ -1,31 +0,0 @@
# -*- coding: utf-8 -*-
from typing import Optional
from fastapi import Query
from app.core.validator import DateTimeStr
class GenDemo01QueryParam:
"""示例查询参数"""
def __init__(
self,
name: Optional[str] = Query(None, description="名称"),
creator: Optional[int] = Query(None, description="创建人"),
start_time: Optional[DateTimeStr] = Query(None, description="开始时间", example="2025-01-01 00:00:00"),
end_time: Optional[DateTimeStr] = Query(None, description="结束时间", example="2025-12-31 23:59:59"),
) -> None:
# 模糊查询字段
self.name = ("like", name)
# 精确查询字段
self.status = status
self.creator_id = creator_id
self.creator_id = creator
# 时间范围查询
if start_time and end_time:
self.created_at = ("between", (start_time, end_time))
@@ -1,29 +0,0 @@
# -*- coding:utf-8 -*-
from typing import Optional
from pydantic import BaseModel, ConfigDict, Field
from app.core.base_schema import BaseSchema
class GenDemo01CreateSchema(BaseModel):
"""
示例新增模型
"""
name: Optional[str] = Field(default=None, description='名称')
status: Optional[int] = Field(default=None, description='是否启用(True:启用 False:禁用)')
description: Optional[str] = Field(default=None, description='备注/描述')
class GenDemo01UpdateSchema(GenDemo01CreateSchema):
"""
示例更新模型
"""
...
class GenDemo01OutSchema(GenDemo01CreateSchema, BaseSchema):
"""
示例响应模型
"""
model_config = ConfigDict(from_attributes=True)
@@ -1,202 +0,0 @@
# -*- coding:utf-8 -*-
import io
from typing import Any, List, Dict, Optional
from fastapi import UploadFile
import pandas as pd
from app.core.base_schema import BatchSetAvailable
from app.core.exceptions import CustomException
from app.utils.excel_util import ExcelUtil
from app.core.logger import log
from app.api.v1.module_system.auth.schema import AuthSchema
from .schema import GenDemo01CreateSchema, GenDemo01UpdateSchema, GenDemo01OutSchema
from .param import GenDemo01QueryParam
from .crud import GenDemo01CRUD
class GenDemo01Service:
"""
示例服务层
"""
@classmethod
async def detail_gen_demo01_service(cls, auth: AuthSchema, id: int) -> Dict:
"""详情"""
obj = await GenDemo01CRUD(auth).get_by_id_gen_demo01_crud(id=id)
if not obj:
raise CustomException(msg="该数据不存在")
return GenDemo01OutSchema.model_validate(obj).model_dump()
@classmethod
async def list_gen_demo01_service(cls, auth: AuthSchema, search: Optional[GenDemo01QueryParam] = None, order_by: Optional[List[Dict[str, str]]] = None) -> List[Dict]:
"""列表查询"""
search_dict = search.__dict__ if search else None
obj_list = await GenDemo01CRUD(auth).list_gen_demo01_crud(search=search_dict, order_by=order_by)
return [GenDemo01OutSchema.model_validate(obj).model_dump() for obj in obj_list]
@classmethod
async def page_gen_demo01_service(cls, auth: AuthSchema, page_no: int, page_size: int, search: Optional[GenDemo01QueryParam] = None, order_by: Optional[List[Dict[str, str]]] = None) -> Dict:
"""分页查询(数据库分页)"""
search_dict = search.__dict__ if search else {}
order_by_list = order_by or [{'id': 'asc'}]
offset = (page_no - 1) * page_size
result = await GenDemo01CRUD(auth).page_gen_demo01_crud(
offset=offset,
limit=page_size,
order_by=order_by_list,
search=search_dict
)
return result
@classmethod
async def create_gen_demo01_service(cls, auth: AuthSchema, data: GenDemo01CreateSchema) -> Dict:
"""创建"""
# 检查唯一性约束
obj = await GenDemo01CRUD(auth).create_gen_demo01_crud(data=data)
return GenDemo01OutSchema.model_validate(obj).model_dump()
@classmethod
async def update_gen_demo01_service(cls, auth: AuthSchema, id: int, data: GenDemo01UpdateSchema) -> Dict:
"""更新"""
# 检查数据是否存在
obj = await GenDemo01CRUD(auth).get_by_id_gen_demo01_crud(id=id)
if not obj:
raise CustomException(msg='更新失败,该数据不存在')
# 检查唯一性约束
obj = await GenDemo01CRUD(auth).update_gen_demo01_crud(id=id, data=data)
return GenDemo01OutSchema.model_validate(obj).model_dump()
@classmethod
async def delete_gen_demo01_service(cls, auth: AuthSchema, ids: List[int]) -> None:
"""删除"""
if len(ids) < 1:
raise CustomException(msg='删除失败,删除对象不能为空')
for id in ids:
obj = await GenDemo01CRUD(auth).get_by_id_gen_demo01_crud(id=id)
if not obj:
raise CustomException(msg=f'删除失败,ID为{id}的数据不存在')
await GenDemo01CRUD(auth).delete_gen_demo01_crud(ids=ids)
@classmethod
async def set_available_gen_demo01_service(cls, auth: AuthSchema, data: BatchSetAvailable) -> None:
"""批量设置状态"""
await GenDemo01CRUD(auth).set_available_gen_demo01_crud(ids=data.ids, status=data.status)
@classmethod
async def batch_export_gen_demo01_service(cls, obj_list: List[Dict[str, Any]]) -> bytes:
"""批量导出"""
mapping_dict = {
'name': '名称',
'status': '是否启用(True:启用 False:禁用)',
'creator_id': '创建人ID',
'id': '主键ID',
'description': '备注/描述',
'created_at': '创建时间',
'updated_at': '更新时间',
'creator': '创建者',
}
data = obj_list.copy()
for item in data:
# 状态转换
if 'status' in item:
item['status'] = '正常' if item.get('status') else '停用'
# 创建者转换
creator_info = item.get('creator')
if isinstance(creator_info, dict):
item['creator'] = creator_info.get('name', '未知')
elif creator_info is None:
item['creator'] = '未知'
return ExcelUtil.export_list2excel(list_data=data, mapping_dict=mapping_dict)
@classmethod
async def batch_import_gen_demo01_service(cls, auth: AuthSchema, file: UploadFile, update_support: bool = False) -> str:
"""批量导入"""
header_dict = {
'名称': 'name',
'是否启用(True:启用 False:禁用)': 'status',
'创建人ID': 'creator_id',
'主键ID': 'id',
'备注/描述': 'description',
'创建时间': 'created_at',
'更新时间': 'updated_at',
}
try:
contents = await file.read()
df = pd.read_excel(io.BytesIO(contents))
await file.close()
if df.empty:
raise CustomException(msg="导入文件为空")
missing_headers = [header for header in header_dict.keys() if header not in df.columns]
if missing_headers:
raise CustomException(msg=f"导入文件缺少必要的列: {', '.join(missing_headers)}")
df.rename(columns=header_dict, inplace=True)
# 验证必填字段
error_msgs = []
success_count = 0
count = 0
for index, row in df.iterrows():
count += 1
try:
data = {
"name": row['name'],
"status": row['status'],
"creator_id": row['creator_id'],
"id": row['id'],
"description": row['description'],
"created_at": row['created_at'],
"updated_at": row['updated_at'],
}
# 使用CreateSchema做校验后入库
create_schema = GenDemo01CreateSchema.model_validate(data)
# 检查唯一性约束
await GenDemo01CRUD(auth).create_crud(data=create_schema)
success_count += 1
except Exception as e:
error_msgs.append(f"{count}行: {str(e)}")
continue
result = f"成功导入 {success_count} 条数据"
if error_msgs:
result += "\n错误信息:\n" + "\n".join(error_msgs)
return result
except Exception as e:
log.error(f"批量导入失败: {str(e)}")
raise CustomException(msg=f"导入失败: {str(e)}")
@classmethod
async def import_template_download_gen_demo01_service(cls) -> bytes:
"""下载导入模板"""
header_list = [
'名称',
'是否启用(True:启用 False:禁用)',
'创建人ID',
'主键ID',
'备注/描述',
'创建时间',
'更新时间',
]
selector_header_list = []
option_list = []
# 添加下拉选项
return ExcelUtil.get_excel_template(
header_list=header_list,
selector_header_list=selector_header_list,
option_list=option_list
)
+1 -1
View File
@@ -297,7 +297,7 @@ class JobConstant:
'}',
' ',
]
JOB_WHITE_LIST = ['module_task']
JOB_WHITE_LIST = ['function_task']
class MenuConstant:
+1 -1
View File
@@ -229,7 +229,7 @@ class SchedulerUtil:
# 1. 解析调用目标
# app.module_task.scheduler_test.job
module_path, func_name = str(job_info.func).rsplit('.', 1)
module_path = "app.module_task." + module_path
module_path = "app.api.v1.module_application.job.function_task." + module_path
try:
module = importlib.import_module(module_path)
job_func = getattr(module, func_name)