mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-23 21:18:09 +00:00
style: 统一代码格式和字符串引号使用
refactor: 优化代码结构和可读性 feat: 添加http_limit模块实现请求限制功能 fix: 修复异步任务中使用time.sleep的问题 chore: 更新依赖项并添加pytest测试框架 docs: 更新项目描述信息 perf: 优化Redis序列化方式使用JSON替代pickle test: 添加测试相关配置和依赖
This commit is contained in:
@@ -12,7 +12,11 @@ from app.core.dependencies import AuthPermission
|
||||
from app.core.logger import log
|
||||
from app.core.router_class import OperationLogRoute
|
||||
|
||||
from .schema import ApplicationCreateSchema, ApplicationQueryParam, ApplicationUpdateSchema
|
||||
from .schema import (
|
||||
ApplicationCreateSchema,
|
||||
ApplicationQueryParam,
|
||||
ApplicationUpdateSchema,
|
||||
)
|
||||
from .service import ApplicationService
|
||||
|
||||
MyAppRouter = APIRouter(route_class=OperationLogRoute, prefix="/myapp", tags=["应用管理"])
|
||||
@@ -21,7 +25,10 @@ MyAppRouter = APIRouter(route_class=OperationLogRoute, prefix="/myapp", tags=["
|
||||
@MyAppRouter.get("/detail/{id}", summary="获取应用详情", description="获取应用详情")
|
||||
async def get_obj_detail_controller(
|
||||
id: Annotated[int, Path(description="应用ID")],
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:myapp:detail"]))]
|
||||
auth: Annotated[
|
||||
AuthSchema,
|
||||
Depends(AuthPermission(["module_application:myapp:detail"])),
|
||||
],
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
获取应用详情
|
||||
@@ -42,7 +49,7 @@ async def get_obj_detail_controller(
|
||||
async def get_obj_list_controller(
|
||||
page: Annotated[PaginationQueryParam, Depends()],
|
||||
search: Annotated[ApplicationQueryParam, Depends()],
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:myapp:query"]))]
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:myapp:query"]))],
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
查询应用列表
|
||||
@@ -55,8 +62,14 @@ async def get_obj_list_controller(
|
||||
返回:
|
||||
- JSONResponse: 包含应用列表的JSON响应
|
||||
"""
|
||||
result_dict_list = await ApplicationService.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)
|
||||
result_dict_list = await ApplicationService.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,
|
||||
)
|
||||
log.info("查询应用列表成功")
|
||||
return SuccessResponse(data=result_dict, msg="查询应用列表成功")
|
||||
|
||||
@@ -64,7 +77,10 @@ async def get_obj_list_controller(
|
||||
@MyAppRouter.post("/create", summary="创建应用", description="创建应用")
|
||||
async def create_obj_controller(
|
||||
data: ApplicationCreateSchema,
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:myapp:create"]))]
|
||||
auth: Annotated[
|
||||
AuthSchema,
|
||||
Depends(AuthPermission(["module_application:myapp:create"])),
|
||||
],
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
创建应用
|
||||
@@ -85,7 +101,10 @@ async def create_obj_controller(
|
||||
async def update_obj_controller(
|
||||
data: ApplicationUpdateSchema,
|
||||
id: Annotated[int, Path(description="应用ID")],
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:myapp:update"]))]
|
||||
auth: Annotated[
|
||||
AuthSchema,
|
||||
Depends(AuthPermission(["module_application:myapp:update"])),
|
||||
],
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
修改应用
|
||||
@@ -106,7 +125,10 @@ async def update_obj_controller(
|
||||
@MyAppRouter.delete("/delete", summary="删除应用", description="删除应用")
|
||||
async def delete_obj_controller(
|
||||
ids: Annotated[list[int], Body(description="ID列表")],
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:myapp:delete"]))]
|
||||
auth: Annotated[
|
||||
AuthSchema,
|
||||
Depends(AuthPermission(["module_application:myapp:delete"])),
|
||||
],
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
删除应用
|
||||
@@ -123,10 +145,14 @@ async def delete_obj_controller(
|
||||
return SuccessResponse(msg="删除应用成功")
|
||||
|
||||
|
||||
@MyAppRouter.patch("/available/setting", summary="批量修改应用状态", description="批量修改应用状态")
|
||||
@MyAppRouter.patch(
|
||||
"/available/setting",
|
||||
summary="批量修改应用状态",
|
||||
description="批量修改应用状态",
|
||||
)
|
||||
async def batch_set_available_obj_controller(
|
||||
data: BatchSetAvailable,
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:myapp:patch"]))]
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:myapp:patch"]))],
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
批量修改应用状态
|
||||
|
||||
@@ -21,7 +21,9 @@ class ApplicationCRUD(CRUDBase[ApplicationModel, ApplicationCreateSchema, Applic
|
||||
self.auth = auth
|
||||
super().__init__(model=ApplicationModel, auth=auth)
|
||||
|
||||
async def get_by_id_crud(self, id: int, preload: list[str | Any] | None = None) -> ApplicationModel | None:
|
||||
async def get_by_id_crud(
|
||||
self, id: int, preload: list[str | Any] | None = None
|
||||
) -> ApplicationModel | None:
|
||||
"""
|
||||
根据id获取应用详情
|
||||
|
||||
@@ -34,7 +36,12 @@ class ApplicationCRUD(CRUDBase[ApplicationModel, ApplicationCreateSchema, Applic
|
||||
"""
|
||||
return await self.get(id=id, preload=preload)
|
||||
|
||||
async def list_crud(self, search: dict[str, Any] | None = None, order_by: list[dict[str, str]] | None = None, preload: list[str | Any] | None = None) -> Sequence[ApplicationModel]:
|
||||
async def list_crud(
|
||||
self,
|
||||
search: dict[str, Any] | None = None,
|
||||
order_by: list[dict[str, str]] | None = None,
|
||||
preload: list[str | Any] | None = None,
|
||||
) -> Sequence[ApplicationModel]:
|
||||
"""
|
||||
列表查询应用
|
||||
|
||||
|
||||
@@ -8,10 +8,11 @@ class ApplicationModel(ModelMixin, UserMixin):
|
||||
"""
|
||||
应用系统表
|
||||
"""
|
||||
__tablename__: str = 'app_myapp'
|
||||
__table_args__: dict[str, str] = ({'comment': '应用系统表'})
|
||||
|
||||
__tablename__: str = "app_myapp"
|
||||
__table_args__: dict[str, str] = {"comment": "应用系统表"}
|
||||
__loader_options__: list[str] = ["created_by", "updated_by"]
|
||||
|
||||
name: Mapped[str] = mapped_column(String(64), nullable=False, comment='应用名称')
|
||||
access_url: Mapped[str] = mapped_column(String(500), nullable=False, comment='访问地址')
|
||||
icon_url: Mapped[str | None] = mapped_column(String(300), nullable=True, comment='应用图标URL')
|
||||
name: Mapped[str] = mapped_column(String(64), nullable=False, comment="应用名称")
|
||||
access_url: Mapped[str] = mapped_column(String(500), nullable=False, comment="访问地址")
|
||||
icon_url: Mapped[str | None] = mapped_column(String(300), nullable=True, comment="应用图标URL")
|
||||
|
||||
@@ -9,34 +9,35 @@ from app.core.validator import DateTimeStr
|
||||
|
||||
class ApplicationCreateSchema(BaseModel):
|
||||
"""应用创建模型"""
|
||||
name: str = Field(..., max_length=64, description='应用名称')
|
||||
|
||||
name: str = Field(..., max_length=64, description="应用名称")
|
||||
access_url: str = Field(..., max_length=255, description="访问地址")
|
||||
icon_url: str | None = Field(None, max_length=300, description="应用图标URL")
|
||||
status: str = Field("0", description="是否启用(0:启用 1:禁用)")
|
||||
description: str | None = Field(default=None, max_length=255, description="描述")
|
||||
|
||||
@field_validator('access_url')
|
||||
@field_validator("access_url")
|
||||
@classmethod
|
||||
def _validate_access_url(cls, v: str) -> str:
|
||||
v = v.strip()
|
||||
if not v:
|
||||
raise ValueError('访问地址不能为空')
|
||||
raise ValueError("访问地址不能为空")
|
||||
parsed = urlparse(v)
|
||||
if parsed.scheme not in ('http', 'https'):
|
||||
raise ValueError('访问地址必须为 http/https URL')
|
||||
if parsed.scheme not in ("http", "https"):
|
||||
raise ValueError("访问地址必须为 http/https URL")
|
||||
return v
|
||||
|
||||
@field_validator('icon_url')
|
||||
@field_validator("icon_url")
|
||||
@classmethod
|
||||
def _validate_icon_url(cls, v: str | None) -> str | None:
|
||||
if v is None:
|
||||
return v
|
||||
v = v.strip()
|
||||
if v == "":
|
||||
if not v:
|
||||
return None
|
||||
parsed = urlparse(v)
|
||||
if parsed.scheme not in ('http', 'https'):
|
||||
raise ValueError('应用图标URL必须为 http/https URL')
|
||||
if parsed.scheme not in ("http", "https"):
|
||||
raise ValueError("应用图标URL必须为 http/https URL")
|
||||
return v
|
||||
|
||||
|
||||
@@ -46,6 +47,7 @@ class ApplicationUpdateSchema(ApplicationCreateSchema):
|
||||
|
||||
class ApplicationOutSchema(ApplicationCreateSchema, BaseSchema, UserBySchema):
|
||||
"""应用响应模型"""
|
||||
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
|
||||
@@ -56,8 +58,16 @@ class ApplicationQueryParam:
|
||||
self,
|
||||
name: str | None = Query(None, description="应用名称"),
|
||||
status: str | None = Query(None, description="是否启用"),
|
||||
created_time: list[DateTimeStr] | None = Query(None, description="创建时间范围", examples=["2025-01-01 00:00:00", "2025-12-31 23:59:59"]),
|
||||
updated_time: list[DateTimeStr] | None = Query(None, description="更新时间范围", examples=["2025-01-01 00:00:00", "2025-12-31 23:59:59"]),
|
||||
created_time: list[DateTimeStr] | None = Query(
|
||||
None,
|
||||
description="创建时间范围",
|
||||
examples=["2025-01-01 00:00:00", "2025-12-31 23:59:59"],
|
||||
),
|
||||
updated_time: list[DateTimeStr] | None = Query(
|
||||
None,
|
||||
description="更新时间范围",
|
||||
examples=["2025-01-01 00:00:00", "2025-12-31 23:59:59"],
|
||||
),
|
||||
created_id: int | None = Query(None, description="创建人"),
|
||||
updated_id: int | None = Query(None, description="更新人"),
|
||||
) -> None:
|
||||
|
||||
@@ -3,7 +3,12 @@ from app.core.base_schema import BatchSetAvailable
|
||||
from app.core.exceptions import CustomException
|
||||
|
||||
from .crud import ApplicationCRUD
|
||||
from .schema import ApplicationCreateSchema, ApplicationOutSchema, ApplicationQueryParam, ApplicationUpdateSchema
|
||||
from .schema import (
|
||||
ApplicationCreateSchema,
|
||||
ApplicationOutSchema,
|
||||
ApplicationQueryParam,
|
||||
ApplicationUpdateSchema,
|
||||
)
|
||||
|
||||
|
||||
class ApplicationService:
|
||||
@@ -25,11 +30,16 @@ class ApplicationService:
|
||||
"""
|
||||
obj = await ApplicationCRUD(auth).get_by_id_crud(id=id)
|
||||
if not obj:
|
||||
raise CustomException(msg='应用不存在')
|
||||
raise CustomException(msg="应用不存在")
|
||||
return ApplicationOutSchema.model_validate(obj).model_dump()
|
||||
|
||||
@classmethod
|
||||
async def list_service(cls, auth: AuthSchema, search: ApplicationQueryParam | None = None, order_by: list[dict[str, str]] | None = None) -> list[dict]:
|
||||
async def list_service(
|
||||
cls,
|
||||
auth: AuthSchema,
|
||||
search: ApplicationQueryParam | None = None,
|
||||
order_by: list[dict[str, str]] | None = None,
|
||||
) -> list[dict]:
|
||||
"""
|
||||
获取应用列表
|
||||
|
||||
@@ -61,7 +71,7 @@ class ApplicationService:
|
||||
# 检查名称是否重复
|
||||
obj = await ApplicationCRUD(auth).get(name=data.name)
|
||||
if obj:
|
||||
raise CustomException(msg='创建失败,应用名称已存在')
|
||||
raise CustomException(msg="创建失败,应用名称已存在")
|
||||
|
||||
obj = await ApplicationCRUD(auth).create_crud(data=data)
|
||||
return ApplicationOutSchema.model_validate(obj).model_dump()
|
||||
@@ -81,12 +91,12 @@ class ApplicationService:
|
||||
"""
|
||||
obj = await ApplicationCRUD(auth).get_by_id_crud(id=id)
|
||||
if not obj:
|
||||
raise CustomException(msg='更新失败,该应用不存在')
|
||||
raise CustomException(msg="更新失败,该应用不存在")
|
||||
|
||||
# 检查名称重复
|
||||
exist_obj = await ApplicationCRUD(auth).get(name=data.name)
|
||||
if exist_obj and exist_obj.id != id:
|
||||
raise CustomException(msg='更新失败,应用名称重复')
|
||||
raise CustomException(msg="更新失败,应用名称重复")
|
||||
|
||||
obj = await ApplicationCRUD(auth).update_crud(id=id, data=data)
|
||||
return ApplicationOutSchema.model_validate(obj).model_dump()
|
||||
@@ -104,11 +114,11 @@ class ApplicationService:
|
||||
- None
|
||||
"""
|
||||
if len(ids) < 1:
|
||||
raise CustomException(msg='删除失败,删除对象不能为空')
|
||||
raise CustomException(msg="删除失败,删除对象不能为空")
|
||||
for id in ids:
|
||||
obj = await ApplicationCRUD(auth).get_by_id_crud(id=id)
|
||||
if not obj:
|
||||
raise CustomException(msg=f'删除失败,应用 {id} 不存在')
|
||||
raise CustomException(msg=f"删除失败,应用 {id} 不存在")
|
||||
await ApplicationCRUD(auth).delete_crud(ids=ids)
|
||||
|
||||
@classmethod
|
||||
|
||||
Reference in New Issue
Block a user