1. 历史记录:批次合并视图新增"批量反撤销"(需反撤销权限);单条/批量撤销与反撤销提示更明确(说明记录去向,不会删除);操作列权限门控补充 restore_records 2. 操作日志:批量撤销/反撤销写入日志;单条撤销/反撤销/加减分日志详情补充学生姓名(学号)、分值变动、原因;新增"详情关键词"筛选(学生姓名/原因等) 3. 节假日接口:证书校验失败自动回退重试,提高服务器兼容性 4. 作业按次登记:默认列出全部学生、缺交次数默认0(0=已交),支持全员已交登记;后端允许空缺交名单
144 lines
5.0 KiB
Python
144 lines
5.0 KiB
Python
# ===========================================
|
|
# 班级操行分管理系统 - 后端服务
|
|
#
|
|
# 开发者: Canglan
|
|
# 联系方式: admin@sea-studio.top
|
|
# 版权归属: Sea Network Technology Studio
|
|
# 许可证: MIT License
|
|
#
|
|
# 版权所有 © Sea Network Technology Studio
|
|
# ===========================================
|
|
|
|
from typing import Dict, Any, List
|
|
from utils.database import execute_insert, execute_one, execute_query
|
|
from utils.logger import get_logger
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
class LoginLogModel:
|
|
"""登录日志数据模型"""
|
|
|
|
@staticmethod
|
|
async def create(username: str, login_result: int, ip_address: str, user_agent: str = None, fail_reason: str = None) -> int:
|
|
"""
|
|
写入登录日志
|
|
:param username: 用户名
|
|
:param login_result: 登录结果 (1=成功, 0=失败)
|
|
:param ip_address: IP地址
|
|
:param user_agent: 浏览器UA
|
|
:param fail_reason: 失败原因
|
|
:return: log_id
|
|
"""
|
|
sql = """
|
|
INSERT INTO login_logs (username, login_result, fail_reason, ip_address, user_agent)
|
|
VALUES (%s, %s, %s, %s, %s)
|
|
"""
|
|
return await execute_insert(sql, (username, login_result, fail_reason, ip_address, user_agent))
|
|
|
|
|
|
class OperationLogModel:
|
|
"""操作日志数据模型"""
|
|
|
|
@staticmethod
|
|
async def create(operator_id: int, operator_name: str, operator_role: str,
|
|
operation_type: str, target_type: str = None, target_id: int = None,
|
|
details: str = None, ip_address: str = None) -> int:
|
|
"""
|
|
写入操作日志
|
|
:param operator_id: 操作者用户ID
|
|
:param operator_name: 操作者用户名
|
|
:param operator_role: 操作者角色
|
|
:param operation_type: 操作类型
|
|
:param target_type: 目标类型
|
|
:param target_id: 目标ID
|
|
:param details: 详细信息
|
|
:param ip_address: IP地址
|
|
:return: log_id
|
|
"""
|
|
sql = """
|
|
INSERT INTO operation_logs (operator_id, operator_name, operator_role,
|
|
operation_type, target_type, target_id, details, ip_address)
|
|
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
|
|
"""
|
|
return await execute_insert(sql, (operator_id, operator_name, operator_role,
|
|
operation_type, target_type, target_id, details, ip_address))
|
|
|
|
@staticmethod
|
|
def _build_filters(
|
|
operation_type: str = None,
|
|
operator: str = None,
|
|
start_date: str = None,
|
|
end_date: str = None,
|
|
keyword: str = None
|
|
):
|
|
conditions = ["1=1"]
|
|
params = []
|
|
if operation_type:
|
|
conditions.append("operation_type = %s")
|
|
params.append(operation_type)
|
|
if operator:
|
|
conditions.append("(operator_name LIKE %s OR operator_role LIKE %s)")
|
|
params.extend([f"%{operator}%", f"%{operator}%"])
|
|
if keyword:
|
|
conditions.append("details LIKE %s")
|
|
params.append(f"%{keyword}%")
|
|
if start_date:
|
|
conditions.append("DATE(created_at) >= %s")
|
|
params.append(start_date)
|
|
if end_date:
|
|
conditions.append("DATE(created_at) <= %s")
|
|
params.append(end_date)
|
|
return " AND ".join(conditions), params
|
|
|
|
@staticmethod
|
|
async def get_logs(
|
|
operation_type: str = None,
|
|
operator: str = None,
|
|
start_date: str = None,
|
|
end_date: str = None,
|
|
keyword: str = None,
|
|
limit: int = 20,
|
|
offset: int = 0
|
|
) -> List[Dict[str, Any]]:
|
|
"""查询操作日志(分页)"""
|
|
where, params = OperationLogModel._build_filters(
|
|
operation_type, operator, start_date, end_date, keyword
|
|
)
|
|
sql = f"""
|
|
SELECT log_id, operator_id, operator_name, operator_role,
|
|
operation_type, target_type, target_id, details, ip_address, created_at
|
|
FROM operation_logs
|
|
WHERE {where}
|
|
ORDER BY log_id DESC
|
|
LIMIT %s OFFSET %s
|
|
"""
|
|
params.extend([limit, offset])
|
|
return await execute_query(sql, tuple(params))
|
|
|
|
@staticmethod
|
|
async def count_logs(
|
|
operation_type: str = None,
|
|
operator: str = None,
|
|
start_date: str = None,
|
|
end_date: str = None,
|
|
keyword: str = None
|
|
) -> int:
|
|
"""统计操作日志数量"""
|
|
where, params = OperationLogModel._build_filters(
|
|
operation_type, operator, start_date, end_date, keyword
|
|
)
|
|
result = await execute_one(
|
|
f"SELECT COUNT(*) AS total FROM operation_logs WHERE {where}",
|
|
tuple(params)
|
|
)
|
|
return result["total"] if result else 0
|
|
|
|
@staticmethod
|
|
async def get_types() -> List[str]:
|
|
"""获取所有操作类型(用于筛选项)"""
|
|
rows = await execute_query(
|
|
"SELECT DISTINCT operation_type FROM operation_logs ORDER BY operation_type"
|
|
)
|
|
return [row["operation_type"] for row in rows]
|