feat(training): 培训配置改造
This commit is contained in:
1 parent
d21c83433e
commit
15225ef1ce
15 files changed
+602
-56
No files matched your search
@@ -1,7 +1,10 @@
|
||||
"""
|
||||
backend/app/services/training_config_service.py
|
||||
|
||||
用途:提供培训配置 CRUD、三级资料绑定与员工培训知识库候选资料查询。
|
||||
依赖:培训、AgentGuide 与知识库 ORM 模型,以及统一业务异常。
|
||||
逻辑:管理端保存发布配置;候选与保存阶段双重限制知识库资料来源,员工端读取发布快照。
|
||||
逻辑:管理端保存发布配置;候选与保存阶段双重限制知识库资料来源,员工端读取发布快照;
|
||||
同时在 settings 中统一维护培训进度项与学员侧文案配置。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -22,11 +25,35 @@ RESOURCE_TYPES = {"agent_guide", "kb_document_version", "inline_questions"}
|
||||
CONFIG_STATUSES = {"draft", "published", "archived"}
|
||||
TRAINING_LEVELS = {"company", "department", "team"}
|
||||
TRAINING_TYPES = {"onboarding", "safety"}
|
||||
GENERIC_PROGRESS_STAGES = ("video", "summary", "exam", "result")
|
||||
SAFETY_PROGRESS_STAGES = ("company", "department", "team", "exam")
|
||||
TRAINING_TYPE_ALIASES = {
|
||||
"入职培训": "onboarding",
|
||||
"新人培训": "onboarding",
|
||||
"安全培训": "safety",
|
||||
}
|
||||
DEFAULT_TRAINING_SETTINGS: dict[str, dict[str, Any]] = {
|
||||
"onboarding": {
|
||||
"progress_items": [
|
||||
{"stage": "video", "label": "观看视频"},
|
||||
{"stage": "summary", "label": "复盘知识点"},
|
||||
{"stage": "exam", "label": "逐题考试"},
|
||||
{"stage": "result", "label": "结算结果"},
|
||||
],
|
||||
"progress_hint_text": "按 HR 要求完成视频、知识点、考试三个环节后才能视为完成。",
|
||||
"step_intro_text": "第一步:观看培训视频\n员工先完成入职培训视频学习,再进入知识点复盘和考试环节。",
|
||||
},
|
||||
"safety": {
|
||||
"progress_items": [
|
||||
{"stage": "company", "label": "公司级安全培训"},
|
||||
{"stage": "department", "label": "部门级安全培训"},
|
||||
{"stage": "team", "label": "班组级安全培训"},
|
||||
{"stage": "exam", "label": "统一考试"},
|
||||
],
|
||||
"progress_hint_text": "按国家安全生产法要求,新员工须依次完成公司级、部门级、班组级三级安全教育。学完三级的全部资料或视频后,进入 1 次统一考试,90 分及格方可结业。",
|
||||
"step_intro_text": "第一步:完成公司级安全教育\n员工需先完成公司级安全教育资料学习,再依次进入部门级、班组级和统一考试环节。",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def _normalize_type_code(value: Any) -> str:
|
||||
@@ -65,6 +92,94 @@ def _required_int(value: Any, field: str) -> int:
|
||||
return parsed
|
||||
|
||||
|
||||
def _allowed_progress_stages(training_type: str) -> tuple[str, ...]:
|
||||
return SAFETY_PROGRESS_STAGES if training_type == "safety" else GENERIC_PROGRESS_STAGES
|
||||
|
||||
|
||||
def _normalize_copy_text(field: str, value: Any) -> str | None:
|
||||
"""
|
||||
清洗培训工作区的可编辑长文本字段。
|
||||
|
||||
用途:统一校验进度说明和步骤引导文案长度,兼容历史空值读写逻辑。
|
||||
参数约束:允许空值;若传入非空内容则去首尾空白且长度不得超过 500 字符。
|
||||
"""
|
||||
if value is None:
|
||||
return None
|
||||
text = str(value).strip()
|
||||
if not text:
|
||||
return None
|
||||
if len(text) > 500:
|
||||
raise ValidationError(f"{field} exceeds max length", details={"field": field, "max_length": 500})
|
||||
return text
|
||||
|
||||
|
||||
def _normalize_progress_items(value: Any, training_type: str) -> list[dict[str, str]]:
|
||||
"""
|
||||
规范培训进度项配置。
|
||||
|
||||
用途:让后台保存的进度项始终携带稳定 stage 编码,供学员侧按配置动态渲染流程卡片。
|
||||
参数约束:允许空数组;每项必须包含唯一 stage 和非空 label。
|
||||
"""
|
||||
if value in (None, ""):
|
||||
return []
|
||||
if not isinstance(value, list):
|
||||
raise ValidationError("progress_items must be an array", details={"field": "settings.progress_items"})
|
||||
|
||||
allowed_stages = set(_allowed_progress_stages(training_type))
|
||||
seen_stages: set[str] = set()
|
||||
normalized: list[dict[str, str]] = []
|
||||
for index, item in enumerate(value):
|
||||
if not isinstance(item, dict):
|
||||
raise ValidationError("progress_items item must be an object", details={"field": "settings.progress_items", "index": index})
|
||||
stage = str(item.get("stage") or item.get("key") or "").strip()
|
||||
label = str(item.get("label") or item.get("title") or "").strip()
|
||||
if stage not in allowed_stages:
|
||||
raise ValidationError(
|
||||
"progress_items stage is invalid",
|
||||
details={"field": "settings.progress_items.stage", "index": index, "allowed": sorted(allowed_stages)},
|
||||
)
|
||||
if stage in seen_stages:
|
||||
raise ValidationError("progress_items contains duplicate stage", details={"field": "settings.progress_items.stage", "index": index})
|
||||
if not label:
|
||||
raise ValidationError("progress_items label is required", details={"field": "settings.progress_items.label", "index": index})
|
||||
if len(label) > 100:
|
||||
raise ValidationError("progress_items label exceeds max length", details={"field": "settings.progress_items.label", "index": index, "max_length": 100})
|
||||
seen_stages.add(stage)
|
||||
normalized.append({"stage": stage, "label": label})
|
||||
return normalized
|
||||
|
||||
|
||||
def _normalize_settings(training_type: str, value: Any, *, include_defaults: bool) -> dict[str, Any]:
|
||||
"""
|
||||
规范培训配置 settings 并按需注入存量默认值。
|
||||
|
||||
用途:统一维护发布配置中的工作区文本与进度项,避免管理端与学员端各自拼装字段。
|
||||
核心逻辑:先保留历史设置,再清洗新增字段;对 onboarding / safety 可按读取场景补默认值,保证旧配置展示不回退为空。
|
||||
"""
|
||||
settings = dict(value) if isinstance(value, dict) else {}
|
||||
normalized = {**settings}
|
||||
|
||||
if "progress_items" in settings:
|
||||
normalized["progress_items"] = _normalize_progress_items(settings.get("progress_items"), training_type)
|
||||
elif include_defaults and training_type in DEFAULT_TRAINING_SETTINGS:
|
||||
normalized["progress_items"] = [dict(item) for item in DEFAULT_TRAINING_SETTINGS[training_type]["progress_items"]]
|
||||
|
||||
progress_hint_text = _normalize_copy_text("settings.progress_hint_text", settings.get("progress_hint_text"))
|
||||
step_intro_text = _normalize_copy_text("settings.step_intro_text", settings.get("step_intro_text"))
|
||||
|
||||
if progress_hint_text is not None:
|
||||
normalized["progress_hint_text"] = progress_hint_text
|
||||
elif include_defaults and training_type in DEFAULT_TRAINING_SETTINGS:
|
||||
normalized["progress_hint_text"] = DEFAULT_TRAINING_SETTINGS[training_type]["progress_hint_text"]
|
||||
|
||||
if step_intro_text is not None:
|
||||
normalized["step_intro_text"] = step_intro_text
|
||||
elif include_defaults and training_type in DEFAULT_TRAINING_SETTINGS:
|
||||
normalized["step_intro_text"] = DEFAULT_TRAINING_SETTINGS[training_type]["step_intro_text"]
|
||||
|
||||
return normalized
|
||||
|
||||
|
||||
def _resource_payload(resource: TrainingConfigResource) -> dict[str, Any]:
|
||||
data = {
|
||||
"id": resource.id,
|
||||
@@ -95,6 +210,7 @@ def _resource_payload(resource: TrainingConfigResource) -> dict[str, Any]:
|
||||
|
||||
|
||||
def serialize_training_config(config: TrainingConfig) -> dict[str, Any]:
|
||||
settings = _normalize_settings(config.training_type, config.settings, include_defaults=True)
|
||||
return {
|
||||
"id": config.id,
|
||||
"training_type": config.training_type,
|
||||
@@ -102,7 +218,7 @@ def serialize_training_config(config: TrainingConfig) -> dict[str, Any]:
|
||||
"version": config.version,
|
||||
"status": config.status,
|
||||
"pass_score": config.pass_score,
|
||||
"settings": config.settings or {},
|
||||
"settings": settings,
|
||||
"published_at": config.published_at.isoformat() if config.published_at else None,
|
||||
"created_at": config.created_at.isoformat() if config.created_at else None,
|
||||
"updated_at": config.updated_at.isoformat() if config.updated_at else None,
|
||||
@@ -158,7 +274,7 @@ def _apply(config: TrainingConfig, data: dict[str, Any], *, user_id: int | None)
|
||||
if "settings" in data:
|
||||
if not isinstance(data["settings"], dict):
|
||||
raise ValidationError("settings must be an object", details={"field": "settings"})
|
||||
config.settings = data["settings"]
|
||||
config.settings = _normalize_settings(training_type, data["settings"], include_defaults=False)
|
||||
if "status" in data:
|
||||
status = str(data["status"]).strip()
|
||||
if status not in CONFIG_STATUSES:
|
||||
|
||||
Reference in new issue
Block a user