v0.8.5测试

This commit is contained in:
2026-04-16 10:36:34 +08:00
parent 4cbf294cd9
commit 112dc94f7c
12 changed files with 297 additions and 90 deletions

View File

@@ -98,4 +98,11 @@ class UserModel:
"""检查用户名是否存在"""
sql = "SELECT 1 FROM users WHERE username = %s"
result = await execute_one(sql, (username,))
return result is not None
return result is not None
@staticmethod
async def update_status(user_id: int, status: int) -> bool:
"""更新用户状态0=禁用1=启用)"""
sql = "UPDATE users SET status = %s WHERE user_id = %s"
result = await execute_update(sql, (status, user_id))
return result > 0

View File

@@ -26,7 +26,8 @@ from services.log_service import LogService
from schemas.admin import (
AddPointsRequest, RevokeRequest, AddAdminRequest,
AddStudentRequest,
UpdateHomeworkStatusRequest, AddAttendanceRequest
UpdateHomeworkStatusRequest, AddAttendanceRequest,
UpdateAdminRequest, DeleteAdminRequest
)
from utils.response import success_response, error_response
from utils.logger import get_logger
@@ -374,4 +375,64 @@ async def get_admins(request: Request):
return success_response(data=result)
except Exception as e:
logger.error(f"获取管理员列表失败: {e}", exc_info=True)
return error_response(message=f"获取管理员列表失败: {str(e)}")
return error_response(message=f"获取管理员列表失败: {str(e)}")
@router.put("/update/{user_id}")
async def update_admin(request: Request, user_id: int, req: UpdateAdminRequest):
"""更新管理员信息(班主任)"""
user = await get_current_user(request)
is_teacher = await PermissionChecker.check_is_teacher(user["user_id"])
if not is_teacher:
return error_response(message="仅班主任可更新管理员", code=403)
if req.role_type not in ["班长", "学习委员", "考勤委员", "劳动委员", "志愿委员"]:
return error_response(message="无效的角色类型", code=400)
from models.admin_role import AdminRoleModel
result = await AdminRoleModel.update_role(
user_id=user_id,
role_type=req.role_type
)
if result:
await LogService.write_operation_log(
operator_id=user["user_id"], operator_name=user["username"],
operator_role="班主任", operation_type="update_admin",
target_type="admin", target_id=user_id,
details=f"更新管理员角色为: {req.role_type}",
ip=request.client.host
)
return success_response(message="管理员更新成功")
else:
return error_response(message="更新失败或管理员不存在")
@router.delete("/delete/{user_id}")
async def delete_admin(request: Request, user_id: int):
"""删除管理员(班主任)"""
user = await get_current_user(request)
is_teacher = await PermissionChecker.check_is_teacher(user["user_id"])
if not is_teacher:
return error_response(message="仅班主任可删除管理员", code=403)
# 防止删除自己
if user_id == user["user_id"]:
return error_response(message="不能删除当前登录的管理员", code=400)
from models.admin_role import AdminRoleModel
from models.user import UserModel
# 先删除角色记录
role_deleted = await AdminRoleModel.delete(user_id)
if role_deleted:
# 再删除用户账号(软删除,将状态设为禁用)
await UserModel.update_status(user_id, 0)
await LogService.write_operation_log(
operator_id=user["user_id"], operator_name=user["username"],
operator_role="班主任", operation_type="delete_admin",
target_type="admin", target_id=user_id,
details=f"删除管理员: ID={user_id}",
ip=request.client.host
)
return success_response(message="管理员删除成功")
else:
return error_response(message="删除失败或管理员不存在")

View File

@@ -85,4 +85,16 @@ class AddAttendanceRequest(BaseModel):
status: str
reason: Optional[str] = None
apply_deduction: bool = True
custom_deduction: Optional[int] = Field(default=None, gt=0, description="自定义扣分值")
custom_deduction: Optional[int] = Field(default=None, gt=0, description="自定义扣分值")
class UpdateAdminRequest(BaseModel):
"""更新管理员请求"""
user_id: int = Field(..., description="用户ID")
real_name: str = Field(..., min_length=1, max_length=50, description="真实姓名")
role_type: str = Field(..., description="角色类型")
class DeleteAdminRequest(BaseModel):
"""删除管理员请求"""
user_id: int = Field(..., description="用户ID")

View File

@@ -78,12 +78,6 @@ class AttendanceService:
# 创建扣分记录
student = await StudentModel.get_by_id(student_id)
if student:
# 检查分数是否会超出范围(防止溢出)
current_points = student.get("total_points", 0)
new_points = current_points + points_change
if new_points < 0:
return {"success": False, "message": f"分数不能为负(当前{current_points},扣{abs(points_change)}"}
# 获取操作人姓名
user = await UserModel.get_by_user_id(recorder_id)
recorder_name = user.get("real_name", "班主任") if user else "班主任"

View File

@@ -80,18 +80,6 @@ class ConductService:
fail_count += 1
continue
# 检查分数是否会超出范围(防止溢出)
current_points = student.get("total_points", 0)
new_points = current_points + points_change
if new_points < 0:
details.append({"student_id": student_id, "error": f"分数不能为负(当前{current_points},操作{points_change}"})
fail_count += 1
continue
if new_points > 100:
details.append({"student_id": student_id, "error": f"分数不能超过100当前{current_points},操作{points_change}"})
fail_count += 1
continue
# 创建记录
record_id = await ConductModel.create_record(
student_id=student_id,

View File

@@ -109,12 +109,6 @@ class HomeworkService:
# 创建扣分记录
student = await StudentModel.get_by_id(submission["student_id"])
if student:
# 检查分数是否会超出范围(防止溢出)
current_points = student.get("total_points", 0)
new_points = current_points + points_change
if new_points < 0:
return {"success": False, "message": f"分数不能为负(当前{current_points},扣{abs(points_change)}"}
# 获取操作人姓名
from models.user import UserModel
user = await UserModel.get_by_user_id(operator_id)