fix(培训): 完善培训相关细节!
This commit is contained in:
1 parent
453d910364
commit
ae882d5d04
8 files changed
+96
-16
No files matched your search
@@ -28,6 +28,7 @@ from app.services.lean_training_service import (
|
||||
_already_pushed_resource_ids,
|
||||
_config_video_resource_ids,
|
||||
_current_week_bounds,
|
||||
format_beijing_iso,
|
||||
target_user_ids_for_task,
|
||||
)
|
||||
from app.services.schedule.timezone_util import utc_now_naive
|
||||
@@ -132,7 +133,8 @@ def serialize_push_log(log: LeanTrainingVideoPushLog) -> dict[str, object]:
|
||||
"status": log.status,
|
||||
"error_message": log.error_message,
|
||||
"created_by_id": log.created_by_id,
|
||||
"created_at": log.created_at.isoformat() if log.created_at else None,
|
||||
# 展示层转北京时间:管理端"推送记录"直接渲染该字段,UTC 直出会比实际早 8 小时
|
||||
"created_at": format_beijing_iso(log.created_at),
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -69,6 +69,55 @@ def _format_dashboard_datetime(value: str | None) -> str:
|
||||
return text[:16].replace("T", " ") if text else ""
|
||||
|
||||
|
||||
def format_beijing_iso(value: datetime | str | None) -> str | None:
|
||||
"""
|
||||
将存储的 UTC 时间转换为北京时间 ISO 字符串,供页面/Excel 展示。
|
||||
|
||||
用途:培训模块数据库统一存 UTC naive(观看/完成/截止时间),而业务要求所有对外展示为北京时间;
|
||||
此前序列化直接输出 UTC 导致页面显示比实际早 8 小时(如 18:47 显示 10:47)。
|
||||
Args:
|
||||
value: UTC naive datetime 或 ISO 字符串;naive 按 UTC 处理,带时区则先归一到绝对时间;
|
||||
字符串无法解析时原样返回,避免历史脏数据导致展示层报错。
|
||||
Returns:
|
||||
带 +08:00 偏移的 ISO 字符串(如 2026-09-22T18:47:00+08:00,前端 slice(0,16) 即得北京时间);入参为空返回 None。
|
||||
业务约束:仅用于序列化输出层,禁止将转换结果写回数据库——存储与服务端比较(超期判定等)仍一律 UTC。
|
||||
"""
|
||||
if value is None or (isinstance(value, str) and not value.strip()):
|
||||
return None
|
||||
if isinstance(value, str):
|
||||
try:
|
||||
parsed = datetime.fromisoformat(value.strip())
|
||||
except ValueError:
|
||||
return value.strip()
|
||||
else:
|
||||
parsed = value
|
||||
if parsed.tzinfo is None:
|
||||
parsed = parsed.replace(tzinfo=timezone.utc)
|
||||
return parsed.astimezone(resolve_schedule_tzinfo("Asia/Shanghai")).isoformat()
|
||||
|
||||
|
||||
def parse_client_iso_to_utc_text(text: str) -> str:
|
||||
"""
|
||||
将客户端回显的观看时间 ISO 字符串归一为 UTC naive ISO 文本,供存储。
|
||||
|
||||
用途:save_completion 会合并客户端提交的 watched_at 兜底值(服务端记录缺失时入库);
|
||||
客户端时间格式随版本演进(UTC 带 Z / 北京时间带 +08:00),必须归一为 UTC,
|
||||
否则 watched_at_json 混入非 UTC 值会破坏"存储一律 UTC"的约定,后续服务端比较将出错。
|
||||
Args:
|
||||
text: 客户端提交的 ISO 时间字符串。
|
||||
Returns:
|
||||
UTC naive ISO 文本;无法解析时原样返回(与历史行为一致,不因脏数据阻塞提交)。
|
||||
"""
|
||||
try:
|
||||
parsed = datetime.fromisoformat(str(text))
|
||||
except ValueError:
|
||||
return str(text)
|
||||
if parsed.tzinfo is None:
|
||||
# naive 视为北京时间墙钟(新版客户端口径),入库前转 UTC
|
||||
parsed = parsed.replace(tzinfo=resolve_schedule_tzinfo("Asia/Shanghai"))
|
||||
return to_utc_naive(parsed).isoformat()
|
||||
|
||||
|
||||
def _dashboard_status_label(status: str | None) -> str:
|
||||
"""将看板状态转换为页面一致的中文文案。"""
|
||||
return {
|
||||
@@ -244,7 +293,8 @@ def serialize_weekly_task(task: LeanTrainingWeeklyTask, *, learner_view: bool =
|
||||
"week_key": task.week_key,
|
||||
"week_end": (week_start + timedelta(days=6)).isoformat() if week_start else None,
|
||||
"week_index": week_index_for_task(task),
|
||||
"due_at": task.due_at.isoformat(),
|
||||
# 展示层转北京时间:管理端编辑器/看板头与学员端均直接渲染该字段,UTC 直出会比实际早 8 小时
|
||||
"due_at": format_beijing_iso(task.due_at),
|
||||
"video_resource_ids": visible_ids,
|
||||
"target_roles": list(task.target_roles or []),
|
||||
"target_user_ids": list(task.target_user_ids or []),
|
||||
@@ -414,8 +464,9 @@ def list_my_weekly_tasks(user: User) -> list[dict[str, Any]]:
|
||||
item["task_status"] = "completed" if completion and completion.completed_at else ("overdue" if now > task.due_at else "in_progress")
|
||||
item["completion"] = {
|
||||
"summary_text": completion.summary_text,
|
||||
"watched_at": dict(completion.watched_at_json or {}),
|
||||
"completed_at": completion.completed_at.isoformat() if completion.completed_at else None,
|
||||
# 展示层转北京时间:学员端"已观看"直接渲染该字典值,存储仍为 UTC
|
||||
"watched_at": {key: format_beijing_iso(value) for key, value in (completion.watched_at_json or {}).items()},
|
||||
"completed_at": format_beijing_iso(completion.completed_at),
|
||||
} if completion else None
|
||||
items.append(item)
|
||||
return items
|
||||
@@ -451,7 +502,8 @@ def mark_video_watched(user: User, data: dict[str, Any]) -> dict[str, Any]:
|
||||
# 观看草稿:保持 completed_at 为空,任务仍为进行中/已延期,直至感悟提交完成
|
||||
db.session.add(completion)
|
||||
db.session.commit()
|
||||
return {"watched_at": watched}
|
||||
# 返回值供学员端即时回显"已观看时间",展示层转北京时间;数据库中保持 UTC
|
||||
return {"watched_at": {key: format_beijing_iso(value) for key, value in watched.items()}}
|
||||
|
||||
|
||||
def save_completion(user: User, data: dict[str, Any]) -> dict[str, Any]:
|
||||
@@ -465,7 +517,8 @@ def save_completion(user: User, data: dict[str, Any]) -> dict[str, Any]:
|
||||
# 员工只能观看已推送的前缀视频,完成校验同样基于已推送范围而非全量选择列表
|
||||
required_ids = list(task.video_resource_ids or [])[: task.pushed_video_count]
|
||||
# 观看记录以服务端持久化为准(mark_video_watched 已逐个保存),客户端仅作回显兜底,防止伪造观看
|
||||
watched = {str(key): str(value) for key, value in (data.get("watched_at") or {}).items()}
|
||||
# 兜底值入库前归一为 UTC:客户端可能提交北京时间(+08:00)或 UTC(Z)格式,存储一律 UTC
|
||||
watched = {str(key): parse_client_iso_to_utc_text(value) for key, value in (data.get("watched_at") or {}).items()}
|
||||
completion = db.session.execute(
|
||||
select(LeanTrainingCompletion).where(LeanTrainingCompletion.weekly_task_id == task.id, LeanTrainingCompletion.user_id == user.id)
|
||||
).scalar_one_or_none() or LeanTrainingCompletion(weekly_task_id=task.id, user_id=user.id)
|
||||
@@ -501,7 +554,7 @@ def _task_video_rows(task: LeanTrainingWeeklyTask, completion: LeanTrainingCompl
|
||||
# [RULE] 看板视频列只展示已推送前缀(video_resource_ids[:pushed_video_count]):未推送视频不属于本周期学习范围,避免名单出现从未布置过的视频
|
||||
visible_ids = task.video_resource_ids[: task.pushed_video_count]
|
||||
return [
|
||||
{"name": titles.get(resource_id, ""), "watched_at": watched.get(str(resource_id))}
|
||||
{"name": titles.get(resource_id, ""), "watched_at": format_beijing_iso(watched.get(str(resource_id)))}
|
||||
for resource_id in visible_ids
|
||||
]
|
||||
|
||||
@@ -522,7 +575,7 @@ def serialize_completion(completion: LeanTrainingCompletion, task: LeanTrainingW
|
||||
"videos": _task_video_rows(task, completion),
|
||||
"summary_text": completion.summary_text,
|
||||
"status": status,
|
||||
"completed_at": completion.completed_at.isoformat() if completion.completed_at else None,
|
||||
"completed_at": format_beijing_iso(completion.completed_at),
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user