feat: 自动学习功能,后台定时发送心跳积累学时

This commit is contained in:
2026-11-15 01:01:45 +08:00
parent 2acf310e0c
commit ffa563d97c
13 changed files with 959 additions and 16 deletions
+37
View File
@@ -69,6 +69,22 @@ def auto_attempt_count(db: Session) -> int:
return int(load_config(db).auto_jigsaw_attempts)
def heartbeat_intervals(db: Session | None = None) -> dict:
"""返回 {mode, video, document, scorm}。mode 为 'auto' 时三个值仅作备用。"""
if db is None:
from euniv.db import SessionLocal
with SessionLocal() as owned:
row = load_config(owned)
else:
row = load_config(db)
return {
"mode": row.heartbeat_interval_mode or "manual",
"video": int(row.heartbeat_video_seconds),
"document": int(row.heartbeat_document_seconds),
"scorm": int(row.heartbeat_scorm_seconds),
}
def settings_view(row: SystemConfig) -> dict:
return {
"refresh_interval_minutes": int(row.refresh_interval_minutes),
@@ -76,6 +92,10 @@ def settings_view(row: SystemConfig) -> dict:
"manual_preference": bool(row.manual_preference),
"auto_jigsaw_attempts": int(row.auto_jigsaw_attempts),
"admin_username": row.admin_username,
"heartbeat_interval_mode": row.heartbeat_interval_mode or "manual",
"heartbeat_video_seconds": int(row.heartbeat_video_seconds),
"heartbeat_document_seconds": int(row.heartbeat_document_seconds),
"heartbeat_scorm_seconds": int(row.heartbeat_scorm_seconds),
}
@@ -118,6 +138,19 @@ def save_settings(db: Session, payload: dict) -> SystemConfig:
if password is not None and password != "" and len(str(password)) > 128:
raise SettingsError("管理员密码过长")
hb_mode = str(payload.get("heartbeat_interval_mode") or "manual")
if hb_mode not in ("auto", "manual"):
raise SettingsError("心跳间隔模式须为 auto 或 manual")
hb_video = _as_int(payload.get("heartbeat_video_seconds"))
hb_doc = _as_int(payload.get("heartbeat_document_seconds"))
hb_scorm = _as_int(payload.get("heartbeat_scorm_seconds"))
if hb_video is None or not 10 <= hb_video <= 300:
raise SettingsError("视频心跳间隔须为 10 到 300 的整数")
if hb_doc is None or not 30 <= hb_doc <= 600:
raise SettingsError("文档心跳间隔须为 30 到 600 的整数")
if hb_scorm is None or not 30 <= hb_scorm <= 600:
raise SettingsError("Scorm 心跳间隔须为 30 到 600 的整数")
row = load_config(db)
previous_interval = int(row.refresh_interval_minutes)
row.refresh_interval_minutes = interval
@@ -125,6 +158,10 @@ def save_settings(db: Session, payload: dict) -> SystemConfig:
row.manual_preference = bool(payload.get("manual_preference"))
row.auto_jigsaw_attempts = attempts
row.admin_username = username
row.heartbeat_interval_mode = hb_mode
row.heartbeat_video_seconds = hb_video
row.heartbeat_document_seconds = hb_doc
row.heartbeat_scorm_seconds = hb_scorm
if password is not None and password != "":
row.admin_password_cipher = encrypt_secret(str(password))
db.commit()