feat(training): 培训模块改造!

This commit is contained in:
runny committed 2026-09-01 09:22:00 +08:00
1 parent 57652ae581
commit 5f713f2c0c
11 files changed
+226 -51

No files matched your search

@@ -9,6 +9,8 @@ from __future__ import annotations
from datetime import datetime
from typing import Any
import re
from sqlalchemy import select, update
from app.core.errors import NotFoundError, ValidationError
@@ -27,6 +29,32 @@ TRAINING_TYPE_ALIASES = {
}
def _normalize_type_code(value: Any) -> str:
"""
校验并标准化培训类型编码。
用途:管理端新增或员工端读取培训类型时统一使用稳定编码,避免名称变化影响培训记录关联。
Args:
value: 培训类型编码;兼容历史中文别名。
Returns:
小写的合法类型编码。
"""
text = TRAINING_TYPE_ALIASES.get(str(value or "").strip(), str(value or "").strip().lower())
if not re.fullmatch(r"[a-z][a-z0-9_-]{0,49}", text):
raise ValidationError("training_type is invalid", details={"field": "training_type"})
return text
def list_training_type_codes() -> list[dict[str, str]]:
"""返回已发布培训类型的编码与名称,供员工端动态加载培训入口。"""
configs = db.session.execute(
select(TrainingConfig.training_type, TrainingConfig.name)
.where(TrainingConfig.status == "published")
.order_by(TrainingConfig.training_type)
).all()
return [{"code": type_code, "name": str(name or "").strip()} for type_code, name in configs]
def _required_int(value: Any, field: str) -> int:
try:
parsed = int(value)
@@ -92,7 +120,7 @@ def _validate_resource(item: dict[str, Any]) -> dict[str, Any]:
raise ValidationError("training_level is invalid", details={"field": "training_level", "allowed": sorted(TRAINING_LEVELS)})
if resource_type != "inline_questions" and not training_level:
raise ValidationError("training_level is required", details={"field": "training_level"})
result = {"resource_type": resource_type, "training_level": training_level, "title": item.get("title"), "required_duration": int(item.get("required_duration") or 0), "sort_order": int(item.get("sort_order") or 0)}
result = {"resource_type": resource_type, "training_level": training_level, "title": str(item.get("title") or "").strip() or None, "required_duration": int(item.get("required_duration") or 0), "sort_order": int(item.get("sort_order") or 0)}
if resource_type == "agent_guide":
result["agent_guide_id"] = _required_int(item.get("agent_guide_id"), "agent_guide_id")
if not db.session.get(AgentGuide, result["agent_guide_id"]):
@@ -114,11 +142,11 @@ def _validate_resource(item: dict[str, Any]) -> dict[str, Any]:
def _apply(config: TrainingConfig, data: dict[str, Any], *, user_id: int | None) -> None:
raw_training_type = str(data.get("training_type") or data.get("type") or config.training_type or "").strip()
training_type = TRAINING_TYPE_ALIASES.get(raw_training_type, raw_training_type.lower())
training_type = _normalize_type_code(raw_training_type)
name = str(data.get("name") or config.name or "").strip()
version = str(data.get("version") or config.version or "").strip()
if training_type not in TRAINING_TYPES:
raise ValidationError("training_type is invalid", details={"field": "training_type", "allowed": sorted(TRAINING_TYPES)})
if not training_type:
raise ValidationError("training_type is invalid", details={"field": "training_type"})
if not name or not version:
raise ValidationError("training_type, name and version are required")
config.training_type, config.name, config.version = training_type, name, version
@@ -215,8 +243,9 @@ def delete_config(config_id: int) -> None:
def get_published_config(training_type: str) -> dict[str, Any] | None:
normalized_type = TRAINING_TYPE_ALIASES.get(str(training_type or "").strip(), str(training_type or "").strip().lower())
if normalized_type not in TRAINING_TYPES:
try:
normalized_type = _normalize_type_code(training_type)
except ValidationError:
return None
config = db.session.execute(
select(TrainingConfig)