mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-21 12:52:26 +00:00
refactor: 优化代码注释和文档字符串格式
style: 统一代码风格和格式 docs: 完善函数和方法的文档字符串 refactor(base_model): 移除冗余的表名和表参数生成方法 refactor(constant): 更新返回码注释格式 refactor(router_class): 添加路由处理器的详细文档 refactor(database): 完善数据库连接函数的文档 refactor(security): 添加认证类和方法的详细文档 refactor(validator): 更新验证器函数的文档格式 refactor(serialize): 优化序列化工具类的文档 refactor(response): 完善响应类的文档字符串 refactor(dependencies): 添加依赖函数的详细文档 refactor(initialize): 完善初始化脚本的文档 refactor(plugin): 添加生命周期和中间件注册的文档 refactor(service): 完善服务层方法的文档 refactor(controller): 添加控制器方法的详细文档 refactor(crud): 完善CRUD操作的文档字符串 refactor(schema): 简化模型类并移除冗余字段 refactor(param): 更新查询参数类的注释格式 refactor(template): 优化代码生成模板的格式 refactor(console): 添加控制台输出功能的实现 refactor(util): 完善工具函数的文档字符串
This commit is contained in:
@@ -27,6 +27,16 @@ async def get_obj_detail_controller(
|
||||
id: int = Path(..., description="公告ID"),
|
||||
auth: AuthSchema = Depends(AuthPermission(["system:notice:query"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
获取公告详情。
|
||||
|
||||
参数:
|
||||
- id (int): 公告ID。
|
||||
- auth (AuthSchema): 认证信息模型。
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含公告详情的响应模型。
|
||||
"""
|
||||
result_dict = await NoticeService.get_notice_detail_service(id=id, auth=auth)
|
||||
logger.info(f"获取公告详情成功 {id}")
|
||||
return SuccessResponse(data=result_dict, msg="获取公告详情成功")
|
||||
@@ -37,6 +47,17 @@ async def get_obj_list_controller(
|
||||
search: NoticeQueryParam = Depends(),
|
||||
auth: AuthSchema = Depends(AuthPermission(["system:notice:query"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
查询公告。
|
||||
|
||||
参数:
|
||||
- page (PaginationQueryParam): 分页查询参数模型。
|
||||
- search (NoticeQueryParam): 查询公告参数模型。
|
||||
- auth (AuthSchema): 认证信息模型。
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含分页公告详情的响应模型。
|
||||
"""
|
||||
result_dict_list = await NoticeService.get_notice_list_service(auth=auth, search=search, order_by=page.order_by)
|
||||
result_dict = await PaginationService.paginate(data_list= result_dict_list, page_no= page.page_no, page_size = page.page_size)
|
||||
logger.info(f"查询公告列表成功")
|
||||
@@ -47,6 +68,16 @@ async def create_obj_controller(
|
||||
data: NoticeCreateSchema,
|
||||
auth: AuthSchema = Depends(AuthPermission(["system:notice:create"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
创建公告。
|
||||
|
||||
参数:
|
||||
- data (NoticeCreateSchema): 创建公告负载模型。
|
||||
- auth (AuthSchema): 认证信息模型。
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含创建公告结果的响应模型。
|
||||
"""
|
||||
result_dict = await NoticeService.create_notice_service(auth=auth, data=data)
|
||||
logger.info(f"创建公告成功: {result_dict}")
|
||||
return SuccessResponse(data=result_dict, msg="创建公告成功")
|
||||
@@ -57,6 +88,17 @@ async def update_obj_controller(
|
||||
id: int = Path(..., description="公告ID"),
|
||||
auth: AuthSchema = Depends(AuthPermission(["system:notice:update"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
修改公告。
|
||||
|
||||
参数:
|
||||
- data (NoticeUpdateSchema): 修改公告负载模型。
|
||||
- id (int): 公告ID。
|
||||
- auth (AuthSchema): 认证信息模型。
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含修改公告结果的响应模型。
|
||||
"""
|
||||
result_dict = await NoticeService.update_notice_service(auth=auth, id=id, data=data)
|
||||
logger.info(f"修改公告成功: {result_dict}")
|
||||
return SuccessResponse(data=result_dict, msg="修改公告成功")
|
||||
@@ -66,6 +108,16 @@ async def delete_obj_controller(
|
||||
ids: list[int] = Body(..., description="ID列表"),
|
||||
auth: AuthSchema = Depends(AuthPermission(["system:notice:delete"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
删除公告。
|
||||
|
||||
参数:
|
||||
- ids (list[int]): 公告ID列表。
|
||||
- auth (AuthSchema): 认证信息模型。
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含删除公告结果的响应模型。
|
||||
"""
|
||||
await NoticeService.delete_notice_service(auth=auth, ids=ids)
|
||||
logger.info(f"删除公告成功: {ids}")
|
||||
return SuccessResponse(msg="删除公告成功")
|
||||
@@ -75,6 +127,16 @@ async def batch_set_available_obj_controller(
|
||||
data: BatchSetAvailable,
|
||||
auth: AuthSchema = Depends(AuthPermission(["system:notice:patch"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
批量修改公告状态。
|
||||
|
||||
参数:
|
||||
- data (BatchSetAvailable): 批量修改公告状态负载模型。
|
||||
- auth (AuthSchema): 认证信息模型。
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含批量修改公告状态结果的响应模型。
|
||||
"""
|
||||
await NoticeService.set_notice_available_service(auth=auth, data=data)
|
||||
logger.info(f"批量修改公告状态成功: {data.ids}")
|
||||
return SuccessResponse(msg="批量修改公告状态成功")
|
||||
@@ -84,7 +146,16 @@ async def export_obj_list_controller(
|
||||
search: NoticeQueryParam = Depends(),
|
||||
auth: AuthSchema = Depends(AuthPermission(["system:notice:export"]))
|
||||
) -> StreamingResponse:
|
||||
# 获取全量数据
|
||||
"""
|
||||
导出公告。
|
||||
|
||||
参数:
|
||||
- search (NoticeQueryParam): 查询公告参数模型。
|
||||
- auth (AuthSchema): 认证信息模型。
|
||||
|
||||
返回:
|
||||
- StreamingResponse: 包含导出公告的流式响应模型。
|
||||
"""
|
||||
result_dict_list = await NoticeService.get_notice_list_service(search=search, auth=auth)
|
||||
export_result = await NoticeService.export_notice_service(notice_list=result_dict_list)
|
||||
logger.info('导出公告成功')
|
||||
@@ -102,6 +173,15 @@ async def export_obj_list_controller(
|
||||
async def get_obj_list_available_controller(
|
||||
auth: AuthSchema = Depends(get_current_user)
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
获取全局启用公告。
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型。
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含分页已启用公告详情的响应模型。
|
||||
"""
|
||||
result_dict_list = await NoticeService.get_notice_list_available_service(auth=auth)
|
||||
result_dict = await PaginationService.paginate(data_list= result_dict_list)
|
||||
logger.info(f"查询已启用公告列表成功")
|
||||
|
||||
@@ -12,30 +12,86 @@ class NoticeCRUD(CRUDBase[NoticeModel, NoticeCreateSchema, NoticeUpdateSchema]):
|
||||
"""公告数据层"""
|
||||
|
||||
def __init__(self, auth: AuthSchema) -> None:
|
||||
"""初始化CRUD"""
|
||||
"""
|
||||
初始化公告数据层。
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型。
|
||||
"""
|
||||
self.auth = auth
|
||||
super().__init__(model=NoticeModel, auth=auth)
|
||||
|
||||
async def get_by_id_crud(self, id: int) -> Optional[NoticeModel]:
|
||||
"""获取公告详情"""
|
||||
"""
|
||||
根据ID获取公告详情。
|
||||
|
||||
参数:
|
||||
- id (int): 公告ID。
|
||||
|
||||
返回:
|
||||
- Optional[NoticeModel]: 公告模型实例。
|
||||
"""
|
||||
return await self.get(id=id)
|
||||
|
||||
async def get_list_crud(self, search: Optional[Dict] = None, order_by: Optional[List[Dict[str, str]]] = None) -> Sequence[NoticeModel]:
|
||||
"""获取公告列表"""
|
||||
"""
|
||||
获取公告列表。
|
||||
|
||||
参数:
|
||||
- search (Optional[Dict]): 查询参数。
|
||||
- order_by (Optional[List[Dict[str, str]]]): 排序参数。
|
||||
|
||||
返回:
|
||||
- Sequence[NoticeModel]: 公告模型实例列表。
|
||||
"""
|
||||
return await self.list(search=search, order_by=order_by)
|
||||
|
||||
async def create_crud(self, data: NoticeCreateSchema) -> Optional[NoticeModel]:
|
||||
"""创建公告"""
|
||||
"""
|
||||
创建公告。
|
||||
|
||||
参数:
|
||||
- data (NoticeCreateSchema): 公告创建模型。
|
||||
|
||||
返回:
|
||||
- Optional[NoticeModel]: 公告模型实例。
|
||||
"""
|
||||
return await self.create(data=data)
|
||||
|
||||
async def update_crud(self, id: int, data: NoticeUpdateSchema) -> Optional[NoticeModel]:
|
||||
"""更新公告"""
|
||||
"""
|
||||
更新公告。
|
||||
|
||||
参数:
|
||||
- id (int): 公告ID。
|
||||
- data (NoticeUpdateSchema): 公告更新模型。
|
||||
|
||||
返回:
|
||||
- Optional[NoticeModel]: 公告模型实例。
|
||||
"""
|
||||
return await self.update(id=id, data=data)
|
||||
|
||||
async def delete_crud(self, ids: List[int]) -> None:
|
||||
"""删除公告"""
|
||||
"""
|
||||
删除公告。
|
||||
|
||||
参数:
|
||||
- ids (List[int]): 公告ID列表。
|
||||
|
||||
返回:
|
||||
- None
|
||||
"""
|
||||
return await self.delete(ids=ids)
|
||||
|
||||
async def set_available_crud(self, ids: List[int], status: bool) -> None:
|
||||
"""设置公告的可用状态"""
|
||||
"""
|
||||
设置公告的可用状态。
|
||||
|
||||
参数:
|
||||
- ids (List[int]): 公告ID列表。
|
||||
- status (bool): 可用状态。
|
||||
|
||||
返回:
|
||||
- None
|
||||
"""
|
||||
return await self.set(ids=ids, status=status)
|
||||
@@ -19,21 +19,64 @@ class NoticeService:
|
||||
|
||||
@classmethod
|
||||
async def get_notice_detail_service(cls, auth: AuthSchema, id: int) -> Dict:
|
||||
"""
|
||||
获取公告详情。
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型。
|
||||
- id (int): 公告ID。
|
||||
|
||||
返回:
|
||||
- Dict: 公告详情字典。
|
||||
"""
|
||||
notice_obj = await NoticeCRUD(auth).get_by_id_crud(id=id)
|
||||
return NoticeOutSchema.model_validate(notice_obj).model_dump()
|
||||
|
||||
@classmethod
|
||||
async def get_notice_list_available_service(cls, auth: AuthSchema) -> List[Dict]:
|
||||
"""
|
||||
获取可用的公告列表。
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型。
|
||||
|
||||
返回:
|
||||
- List[Dict]: 可用公告详情字典列表。
|
||||
"""
|
||||
notice_obj_list = await NoticeCRUD(auth).get_list_crud(search={'status': True})
|
||||
return [NoticeOutSchema.model_validate(notice_obj).model_dump() for notice_obj in notice_obj_list]
|
||||
|
||||
@classmethod
|
||||
async def get_notice_list_service(cls, auth: AuthSchema, search: Optional[NoticeQueryParam] = None, order_by: Optional[List[Dict[str, str]]] = None) -> List[Dict]:
|
||||
"""
|
||||
获取公告列表。
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型。
|
||||
- search (Optional[NoticeQueryParam]): 查询参数模型。
|
||||
- order_by (Optional[List[Dict[str, str]]]): 排序参数列表。
|
||||
|
||||
返回:
|
||||
- List[Dict]: 公告详情字典列表。
|
||||
"""
|
||||
notice_obj_list = await NoticeCRUD(auth).get_list_crud(search=search.__dict__, order_by=order_by)
|
||||
return [NoticeOutSchema.model_validate(notice_obj).model_dump() for notice_obj in notice_obj_list]
|
||||
|
||||
@classmethod
|
||||
async def create_notice_service(cls, auth: AuthSchema, data: NoticeCreateSchema) -> Dict:
|
||||
"""
|
||||
创建公告。
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型。
|
||||
- data (NoticeCreateSchema): 创建公告负载模型。
|
||||
|
||||
返回:
|
||||
- Dict: 创建的公告详情字典。
|
||||
|
||||
异常:
|
||||
- CustomException: 创建失败,该公告通知已存在。
|
||||
"""
|
||||
notice = await NoticeCRUD(auth).get(notice_title=data.notice_title)
|
||||
if notice:
|
||||
raise CustomException(msg='创建失败,该公告通知已存在')
|
||||
@@ -42,6 +85,20 @@ class NoticeService:
|
||||
|
||||
@classmethod
|
||||
async def update_notice_service(cls, auth: AuthSchema, id: int, data: NoticeUpdateSchema) -> Dict:
|
||||
"""
|
||||
更新公告。
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型。
|
||||
- id (int): 公告ID。
|
||||
- data (NoticeUpdateSchema): 更新公告负载模型。
|
||||
|
||||
返回:
|
||||
- Dict: 更新的公告详情字典。
|
||||
|
||||
异常:
|
||||
- CustomException: 更新失败,该公告通知不存在或公告通知标题重复。
|
||||
"""
|
||||
notice = await NoticeCRUD(auth).get_by_id_crud(id=id)
|
||||
if not notice:
|
||||
raise CustomException(msg='更新失败,该公告通知不存在')
|
||||
@@ -53,6 +110,16 @@ class NoticeService:
|
||||
|
||||
@classmethod
|
||||
async def delete_notice_service(cls, auth: AuthSchema, ids: list[int]) -> None:
|
||||
"""
|
||||
删除公告。
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型。
|
||||
- ids (list[int]): 删除的ID列表。
|
||||
|
||||
异常:
|
||||
- CustomException: 删除失败,删除对象不能为空或该公告通知不存在。
|
||||
"""
|
||||
if len(ids) < 1:
|
||||
raise CustomException(msg='删除失败,删除对象不能为空')
|
||||
for id in ids:
|
||||
@@ -63,11 +130,29 @@ class NoticeService:
|
||||
|
||||
@classmethod
|
||||
async def set_notice_available_service(cls, auth: AuthSchema, data: BatchSetAvailable) -> None:
|
||||
"""
|
||||
批量设置公告状态。
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型。
|
||||
- data (BatchSetAvailable): 批量设置可用负载模型。
|
||||
|
||||
异常:
|
||||
- CustomException: 批量设置失败,该公告通知不存在。
|
||||
"""
|
||||
await NoticeCRUD(auth).set_available_crud(ids=data.ids, status=data.status)
|
||||
|
||||
@classmethod
|
||||
async def export_notice_service(cls, notice_list: List[Dict[str, Any]]) -> bytes:
|
||||
"""导出公告列表"""
|
||||
"""
|
||||
导出公告列表。
|
||||
|
||||
参数:
|
||||
- notice_list (List[Dict[str, Any]]): 公告详情字典列表。
|
||||
|
||||
返回:
|
||||
- bytes: Excel 文件的字节流。
|
||||
"""
|
||||
mapping_dict = {
|
||||
'id': '编号',
|
||||
'notice_title': '公告标题',
|
||||
|
||||
Reference in New Issue
Block a user