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
|
||||
|
||||
@@ -816,6 +816,72 @@ def test_user_can_start_and_stop_study_immediately(monkeypatch) -> None:
|
||||
assert bad.status_code == 400
|
||||
|
||||
|
||||
def test_start_saves_the_settings_sent_with_it(monkeypatch) -> None:
|
||||
import euniv.api as api_module
|
||||
import euniv.service as service_module
|
||||
from euniv.autostudy import work_running
|
||||
|
||||
monkeypatch.setattr(
|
||||
api_module,
|
||||
"login_and_store",
|
||||
lambda db, employee_id, password, manual=False, auto_attempts=3: service_module.login_and_store(
|
||||
db, employee_id, password, FakeClient, manual=manual, auto_attempts=auto_attempts
|
||||
),
|
||||
)
|
||||
client = TestClient(api_module.app)
|
||||
_freeze_beijing(monkeypatch, 2026, 11, 15)
|
||||
assert client.post("/api/login", json={"employee_id": "6650000146", "password": "secret-pass"}).status_code == 200
|
||||
settings = _study_body(
|
||||
monthly_target=900,
|
||||
day=20,
|
||||
hour=6,
|
||||
minute=30,
|
||||
second=5,
|
||||
enabled=False,
|
||||
workers=3,
|
||||
heartbeat_mode="manual",
|
||||
heartbeat_video_seconds=0,
|
||||
heartbeat_document_seconds=7,
|
||||
heartbeat_scorm_seconds=9,
|
||||
)
|
||||
started = client.post("/api/settings/study-control", json={"action": "start", "settings": settings})
|
||||
assert started.status_code == 200
|
||||
assert started.json()["started"] is True
|
||||
assert started.json()["monthly_target"] == 900
|
||||
assert work_running("6650000146") is True
|
||||
with SessionLocal() as db:
|
||||
account = db.get(Account, "6650000146")
|
||||
assert account is not None
|
||||
assert account.monthly_target == 900
|
||||
assert (account.study_start_day, account.study_start_hour) == (20, 6)
|
||||
assert (account.study_start_minute, account.study_start_second) == (30, 5)
|
||||
assert account.study_schedule_enabled is False
|
||||
assert account.course_workers == 3
|
||||
assert account.heartbeat_mode == "manual"
|
||||
assert account.heartbeat_video_seconds == 0
|
||||
assert account.heartbeat_document_seconds == 7
|
||||
assert account.heartbeat_scorm_seconds == 9
|
||||
client.post("/api/settings/study-control", json={"action": "stop"})
|
||||
rejected = client.post(
|
||||
"/api/settings/study-control",
|
||||
json={"action": "start", "settings": _study_body(monthly_target=0, workers=5)},
|
||||
)
|
||||
assert rejected.status_code == 400
|
||||
assert rejected.json()["detail"] == "每月目标为 0,不会学习"
|
||||
bad = client.post(
|
||||
"/api/settings/study-control",
|
||||
json={"action": "start", "settings": _study_body(workers=9)},
|
||||
)
|
||||
assert bad.status_code == 400
|
||||
assert bad.json()["detail"] == "同时学习课程数须为 1 到 8"
|
||||
with SessionLocal() as db:
|
||||
account = db.get(Account, "6650000146")
|
||||
assert account is not None
|
||||
assert account.monthly_target == 900
|
||||
assert account.course_workers == 3
|
||||
assert work_running("6650000146") is False
|
||||
|
||||
|
||||
def test_study_control_stays_stopped_when_month_hours_met(monkeypatch) -> None:
|
||||
import euniv.api as api_module
|
||||
import euniv.service as service_module
|
||||
|
||||
Reference in New Issue
Block a user