feat: 按账号调度自动学习并让首页实时跟上状态
This commit is contained in:
@@ -18,6 +18,7 @@ from euniv.client import (
|
||||
ManualJigsaw,
|
||||
QrWaiting,
|
||||
SessionExpired,
|
||||
history_hour_totals,
|
||||
is_qr_password,
|
||||
)
|
||||
from euniv.crypto import decrypt_secret, encrypt_secret
|
||||
@@ -248,11 +249,11 @@ def poll_qr_login(db: Session, attempt_id: str, client_factory=EunivClient) -> d
|
||||
|
||||
def fetch_home(db: Session, employee_id: str, client_factory=EunivClient) -> dict:
|
||||
"""向 e 学现查首页信息。个人信息只出现在返回值里,不写入数据库。凭证变化时更新密文。"""
|
||||
return _live(db, employee_id, client_factory, lambda client: client.home_snapshot())
|
||||
return _live(db, employee_id, client_factory, lambda client: _apply_hours_source(client, client.home_snapshot()))
|
||||
|
||||
|
||||
def fetch_home_summary(db: Session, employee_id: str, client_factory=EunivClient) -> dict:
|
||||
return _live(db, employee_id, client_factory, lambda client: client.home_summary())
|
||||
return _live(db, employee_id, client_factory, lambda client: _apply_hours_source(client, client.home_summary()))
|
||||
|
||||
|
||||
def fetch_home_exams(db: Session, employee_id: str, client_factory=EunivClient) -> dict:
|
||||
@@ -272,10 +273,57 @@ def fetch_training_detail(db: Session, employee_id: str, path: str, client_facto
|
||||
|
||||
|
||||
def fetch_study_history(db: Session, employee_id: str, client_factory=EunivClient) -> dict:
|
||||
items = _live(db, employee_id, client_factory, lambda client: client.study_history())
|
||||
if not isinstance(items, list):
|
||||
items = []
|
||||
return {"items": items, "total": len(items)}
|
||||
def read(client):
|
||||
items = client.study_history()
|
||||
if not isinstance(items, list):
|
||||
items = []
|
||||
hours = _hour_fields(_apply_hours_source(client, _api_hour_fields(client), items))
|
||||
return {"items": items, "total": len(items), **hours}
|
||||
|
||||
return _live(db, employee_id, client_factory, read)
|
||||
|
||||
|
||||
def _apply_hours_source(client, summary: dict, items: list | None = None) -> dict:
|
||||
from admin.settings import hours_source_of
|
||||
|
||||
data = dict(summary)
|
||||
source = hours_source_of()
|
||||
data["hours_source"] = source
|
||||
if source != "history":
|
||||
return data
|
||||
if items is None:
|
||||
raw = client.study_history()
|
||||
items = raw if isinstance(raw, list) else []
|
||||
totals = history_hour_totals(items)
|
||||
data["year_hours"] = totals["year"]
|
||||
data["month_hours"] = totals["month"]
|
||||
data["today_hours"] = totals["today"]
|
||||
data["total_hours"] = totals["total"]
|
||||
return data
|
||||
|
||||
|
||||
def _api_hour_fields(client) -> dict:
|
||||
from admin.settings import hours_source_of
|
||||
|
||||
if hours_source_of() == "history":
|
||||
return {}
|
||||
summary = client.home_summary()
|
||||
return {
|
||||
"year_hours": summary.get("year_hours"),
|
||||
"month_hours": summary.get("month_hours"),
|
||||
"today_hours": summary.get("today_hours"),
|
||||
"total_hours": summary.get("total_hours"),
|
||||
}
|
||||
|
||||
|
||||
def _hour_fields(data: dict) -> dict:
|
||||
return {
|
||||
"hours_source": data.get("hours_source") or "api",
|
||||
"year_hours": data.get("year_hours"),
|
||||
"month_hours": data.get("month_hours"),
|
||||
"today_hours": data.get("today_hours"),
|
||||
"total_hours": data.get("total_hours"),
|
||||
}
|
||||
|
||||
|
||||
def _auto_attempts(db: Session) -> int:
|
||||
@@ -415,7 +463,9 @@ def _refresh_one(employee_id: str, attempts: int, client_factory) -> None:
|
||||
pass
|
||||
record_outcome(account, valid)
|
||||
if hours:
|
||||
account.month_hours = hours
|
||||
from euniv.study_control import stamp_hours
|
||||
|
||||
stamp_hours(account, hours)
|
||||
db.commit()
|
||||
if checked_alive:
|
||||
_request_course_sync(employee_id)
|
||||
@@ -491,6 +541,17 @@ def start_credential_refresh() -> _RefreshGate:
|
||||
|
||||
|
||||
def _month_hours(client) -> str:
|
||||
from admin.settings import hours_source_of
|
||||
|
||||
if hours_source_of() == "history":
|
||||
reader = getattr(client, "month_history_minutes", None)
|
||||
if reader is None:
|
||||
return ""
|
||||
try:
|
||||
value = reader()
|
||||
except Exception:
|
||||
return ""
|
||||
return str(value) if isinstance(value, int) and not isinstance(value, bool) else ""
|
||||
reader = getattr(client, "month_hours", None)
|
||||
if reader is None:
|
||||
return ""
|
||||
|
||||
Reference in New Issue
Block a user