feat: 点立即开始时按当前表单配置启动并同时保存
This commit is contained in:
+28
-10
@@ -88,6 +88,7 @@ class StudyBundleBody(BaseModel):
|
||||
|
||||
class StudyControlBody(BaseModel):
|
||||
action: str = Field(min_length=1, max_length=16)
|
||||
settings: StudyBundleBody | None = None
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
@@ -426,17 +427,12 @@ def get_heartbeat(request: Request, db: Session = Depends(get_db)) -> dict:
|
||||
return view_of(account)
|
||||
|
||||
|
||||
@app.put("/api/settings/study")
|
||||
def put_study_bundle(body: StudyBundleBody, request: Request, db: Session = Depends(get_db)) -> dict:
|
||||
def _apply_study_bundle(account: Account, body: StudyBundleBody) -> None:
|
||||
"""把首页这一组学习配置写到账号上。不符合要求时抛 400,由调用方回滚。"""
|
||||
from euniv.course_workers import CourseWorkersError, apply as apply_workers
|
||||
from euniv.heartbeat import HeartbeatError, apply as apply_heartbeat, view_of as heartbeat_view
|
||||
from euniv.study_window import StudyWindowError, apply as apply_start, view_of
|
||||
from euniv.heartbeat import HeartbeatError, apply as apply_heartbeat
|
||||
from euniv.study_window import StudyWindowError, apply as apply_start
|
||||
|
||||
if not 0 <= body.monthly_target <= 9999:
|
||||
raise HTTPException(status_code=400, detail="每月学时目标须为 0 到 9999 的整数")
|
||||
account = db.get(Account, _signed_in(request, db))
|
||||
if account is None:
|
||||
raise HTTPException(status_code=401, detail="未登录")
|
||||
try:
|
||||
account.monthly_target = body.monthly_target
|
||||
apply_start(account, body.day, body.hour, body.minute, body.second)
|
||||
@@ -456,8 +452,22 @@ def put_study_bundle(body: StudyBundleBody, request: Request, db: Session = Depe
|
||||
body.heartbeat_scorm_seconds,
|
||||
)
|
||||
except (StudyWindowError, CourseWorkersError, HeartbeatError) as exc:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
||||
|
||||
|
||||
@app.put("/api/settings/study")
|
||||
def put_study_bundle(body: StudyBundleBody, request: Request, db: Session = Depends(get_db)) -> dict:
|
||||
from euniv.heartbeat import view_of as heartbeat_view
|
||||
from euniv.study_window import view_of
|
||||
|
||||
account = db.get(Account, _signed_in(request, db))
|
||||
if account is None:
|
||||
raise HTTPException(status_code=401, detail="未登录")
|
||||
try:
|
||||
_apply_study_bundle(account, body)
|
||||
except HTTPException:
|
||||
db.rollback()
|
||||
raise
|
||||
db.commit()
|
||||
from euniv.autostudy import sync_work
|
||||
|
||||
@@ -549,11 +559,19 @@ def post_study_control(body: StudyControlBody, request: Request, db: Session = D
|
||||
raise HTTPException(status_code=401, detail="未登录")
|
||||
action = (body.action or "").strip().lower()
|
||||
if action == "start":
|
||||
if body.settings is not None:
|
||||
try:
|
||||
_apply_study_bundle(account, body.settings)
|
||||
except HTTPException:
|
||||
db.rollback()
|
||||
raise
|
||||
if int(account.monthly_target or 0) <= 0:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=400, detail="每月目标为 0,不会学习")
|
||||
if hours_met(account):
|
||||
from euniv.autostudy import halt_work
|
||||
|
||||
db.commit()
|
||||
halt_work(employee_id)
|
||||
else:
|
||||
from euniv.autostudy import clear_study_hold
|
||||
|
||||
Reference in New Issue
Block a user