feat: 每月定时开始学习并改为本地计算心跳摘要

This commit is contained in:
2026-11-15 01:30:24 +08:00
parent e9a1e9b315
commit caa79d79b7
14 changed files with 510 additions and 35 deletions
+22
View File
@@ -17,8 +17,10 @@ from admin.users import (
list_users,
read_credentials,
read_proxy,
read_study_start,
set_paused,
set_proxy,
set_study_start,
)
from euniv.courses import course_page, request_course_sync_now
from euniv.db import get_db
@@ -57,6 +59,14 @@ class AdminProxyClearBody(BaseModel):
employee_ids: list[str]
class AdminStudyStartBody(BaseModel):
employee_ids: list[str]
day: int
hour: int
minute: int
second: int
def require_admin(request: Request) -> str:
if not admin_session_active(request.cookies):
raise HTTPException(status_code=401, detail="未登录")
@@ -164,6 +174,18 @@ def get_user_proxy(employee_id: str, db: Session = Depends(get_db), _: str = Dep
return read_proxy(db, employee_id)
@admin_router.put("/users/study-start")
def put_users_study_start(
body: AdminStudyStartBody, db: Session = Depends(get_db), _: str = Depends(require_admin)
) -> dict:
return set_study_start(db, body.employee_ids, body.day, body.hour, body.minute, body.second)
@admin_router.get("/users/{employee_id}/study-start")
def get_user_study_start(employee_id: str, db: Session = Depends(get_db), _: str = Depends(require_admin)) -> dict:
return read_study_start(db, employee_id)
@admin_router.get("/users/{employee_id}/credentials")
def get_credentials(employee_id: str, db: Session = Depends(get_db), _: str = Depends(require_admin)) -> dict:
return read_credentials(db, employee_id)
+32
View File
@@ -12,6 +12,10 @@ from sqlalchemy.orm import Session
from euniv.crypto import decrypt_secret
from euniv.models import Account
from euniv.proxy import ProxyError, apply, clear, summary_of, url_of, view_of
from euniv.study_window import StudyWindowError
from euniv.study_window import apply as apply_start
from euniv.study_window import label_of as start_label
from euniv.study_window import view_of as start_view
_TOKEN_KEYS = (
"ucs_token",
@@ -67,6 +71,11 @@ def list_users(db: Session) -> list[dict]:
"month_hours": account.month_hours or "",
"has_proxy": bool(account.proxy_scheme and account.proxy_host and account.proxy_port),
"proxy": summary_of(account),
"study_start": start_label(account),
"study_start_day": int(account.study_start_day or 1),
"study_start_hour": int(account.study_start_hour or 0),
"study_start_minute": int(account.study_start_minute or 0),
"study_start_second": int(account.study_start_second or 0),
}
for account in accounts
]
@@ -217,6 +226,29 @@ def clear_proxy(db: Session, employee_ids: list[str]) -> dict:
return {"ok": True, "count": len(accounts)}
def read_study_start(db: Session, employee_id: str) -> dict:
return start_view(_require(db, employee_id))
def set_study_start(
db: Session,
employee_ids: list[str],
day: int,
hour: int,
minute: int,
second: int,
) -> dict:
accounts = _accounts_by_ids(db, employee_ids)
try:
for account in accounts:
apply_start(account, day, hour, minute, second)
except StudyWindowError as exc:
db.rollback()
raise HTTPException(status_code=400, detail=str(exc)) from exc
db.commit()
return {"ok": True, "count": len(accounts)}
def delete_user(db: Session, employee_id: str) -> dict:
account = _require(db, employee_id)
db.delete(account)