diff --git a/backend/admin/users.py b/backend/admin/users.py index 9ff5738..53c80dc 100644 --- a/backend/admin/users.py +++ b/backend/admin/users.py @@ -19,7 +19,7 @@ from euniv.proxy import ProxyError, apply, clear, enabled, summary_of, url_of, v from euniv.study_control import start_now, stop_now from euniv.study_control import view_of as study_view from euniv.study_window import StudyWindowError -from euniv.study_window import apply as apply_start +from euniv.study_window import apply_admin as apply_start from euniv.study_window import label_of as start_label _TOKEN_KEYS = ( diff --git a/backend/euniv/api.py b/backend/euniv/api.py index 600a11c..df6c8b3 100644 --- a/backend/euniv/api.py +++ b/backend/euniv/api.py @@ -61,7 +61,7 @@ class ProxyBody(BaseModel): class StudyStartBody(BaseModel): - day: int = Field(strict=True, ge=5, le=25) + day: int = Field(strict=True, ge=1, le=28) hour: int = Field(strict=True, ge=0, le=23) minute: int = Field(strict=True, ge=0, le=59) second: int = Field(strict=True, ge=0, le=59) @@ -74,7 +74,7 @@ class CourseWorkersBody(BaseModel): class StudyBundleBody(BaseModel): monthly_target: int = Field(strict=True, ge=0, le=9999) - day: int = Field(strict=True, ge=5, le=25) + day: int = Field(strict=True, ge=1, le=28) hour: int = Field(strict=True, ge=0, le=23) minute: int = Field(strict=True, ge=0, le=59) second: int = Field(strict=True, ge=0, le=59) @@ -394,13 +394,13 @@ def get_study_start(request: Request, db: Session = Depends(get_db)) -> dict: @app.put("/api/settings/study-start") def put_study_start(body: StudyStartBody, request: Request, db: Session = Depends(get_db)) -> dict: - from euniv.study_window import StudyWindowError, apply, view_of + from euniv.study_window import StudyWindowError, apply_user, view_of account = db.get(Account, _signed_in(request, db)) if account is None: raise HTTPException(status_code=401, detail="未登录") try: - apply(account, body.day, body.hour, body.minute, body.second) + apply_user(account, body.day, body.hour, body.minute, body.second) except StudyWindowError as exc: raise HTTPException(status_code=400, detail=str(exc)) from exc if body.enabled is not None: @@ -426,7 +426,7 @@ 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 - from euniv.study_window import StudyWindowError, apply as apply_start + from euniv.study_window import StudyWindowError, apply_user as apply_start try: account.monthly_target = body.monthly_target diff --git a/backend/euniv/service.py b/backend/euniv/service.py index 093a415..7238fdc 100644 --- a/backend/euniv/service.py +++ b/backend/euniv/service.py @@ -367,7 +367,9 @@ def poll_qr_login(db: Session, attempt_id: str) -> dict: def fetch_home_summary(db: Session, employee_id: str, client_factory=EunivClient) -> dict: - return _live(db, employee_id, client_factory, lambda client: _apply_hours_source(client, client.home_summary())) + data = _live(db, employee_id, client_factory, lambda client: _apply_hours_source(client, client.home_summary())) + _remember_month_hours(db, employee_id, data) + return data def fetch_home_exams(db: Session, employee_id: str, client_factory=EunivClient) -> dict: @@ -402,7 +404,9 @@ def fetch_study_history(db: Session, employee_id: str, client_factory=EunivClien 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) + data = _live(db, employee_id, client_factory, read) + _remember_month_hours(db, employee_id, data) + return data def _apply_hours_source(client, summary: dict, items: list | None = None) -> dict: @@ -447,6 +451,25 @@ def _api_hour_fields(client) -> dict: } +def _remember_month_hours(db: Session, employee_id: str, payload: dict) -> None: + """首页、学时历史这些已经取到本月学时的地方,顺手写回用户列表。""" + if not isinstance(payload, dict): + return + raw = payload.get("month_hours") + if raw is None or isinstance(raw, bool): + return + text = str(raw).strip() + if not text: + return + account = db.get(Account, employee_id) + if account is None: + return + from euniv.study_control import stamp_hours + + stamp_hours(account, text) + db.commit() + + def _hour_fields(data: dict) -> dict: fields = { "hours_source": data.get("hours_source") or "api", diff --git a/backend/euniv/study_window.py b/backend/euniv/study_window.py index 3b50ded..56d107b 100644 --- a/backend/euniv/study_window.py +++ b/backend/euniv/study_window.py @@ -9,15 +9,25 @@ CN_TZ = timezone(timedelta(hours=8)) DAY_MIN = 5 DAY_MAX = 25 DAY_DEFAULT = 8 +ADMIN_DAY_MIN = 1 +ADMIN_DAY_MAX = 28 class StudyWindowError(ValueError): """开始时刻不符合要求。""" -def validate_start(day: object, hour: object, minute: object, second: object) -> tuple[int, int, int, int]: +def validate_start( + day: object, + hour: object, + minute: object, + second: object, + *, + day_min: int = DAY_MIN, + day_max: int = DAY_MAX, +) -> tuple[int, int, int, int]: for name, value, low, high in ( - ("日", day, DAY_MIN, DAY_MAX), + ("日", day, day_min, day_max), ("时", hour, 0, 23), ("分", minute, 0, 59), ("秒", second, 0, 59), @@ -44,7 +54,7 @@ def _stored_day(value: object) -> int: number = int(value) # type: ignore[arg-type] except (TypeError, ValueError): return DAY_DEFAULT - if DAY_MIN <= number <= DAY_MAX: + if ADMIN_DAY_MIN <= number <= ADMIN_DAY_MAX: return number return DAY_DEFAULT @@ -59,14 +69,42 @@ def stored_start(account) -> tuple[int, int, int, int]: ) -def apply(account, day: object, hour: object, minute: object, second: object) -> None: - d, h, m, s = validate_start(day, hour, minute, second) +def apply( + account, + day: object, + hour: object, + minute: object, + second: object, + *, + day_min: int = DAY_MIN, + day_max: int = DAY_MAX, +) -> None: + d, h, m, s = validate_start(day, hour, minute, second, day_min=day_min, day_max=day_max) account.study_start_day = d account.study_start_hour = h account.study_start_minute = m account.study_start_second = s +def apply_admin(account, day: object, hour: object, minute: object, second: object) -> None: + """管理端可以把开始日设成 1 到 28 号,用户端仍只能改到 5 到 25 号。""" + apply(account, day, hour, minute, second, day_min=ADMIN_DAY_MIN, day_max=ADMIN_DAY_MAX) + + +def apply_user(account, day: object, hour: object, minute: object, second: object) -> None: + """用户改时刻时仍限制 5 到 25 号。已经由管理端设好的 1 到 28 号,原样保存时保留。""" + stored = stored_start(account)[0] + if ( + isinstance(day, int) + and not isinstance(day, bool) + and day == stored + and not DAY_MIN <= day <= DAY_MAX + ): + apply(account, day, hour, minute, second, day_min=ADMIN_DAY_MIN, day_max=ADMIN_DAY_MAX) + return + apply(account, day, hour, minute, second) + + def label_of(account) -> str: day, hour, minute, second = stored_start(account) return f"每月{day}日 {hour:02d}:{minute:02d}:{second:02d}" diff --git a/backend/tests/test_admin.py b/backend/tests/test_admin.py index 547b857..5f2459d 100644 --- a/backend/tests/test_admin.py +++ b/backend/tests/test_admin.py @@ -804,6 +804,24 @@ def test_admin_can_set_study_start_for_selected_users() -> None: assert one["study_start"] == "每月20日 00:00:00" assert two["study_start"] == "每月5日 16:08:09" assert one["study_start_day"] == 20 + edges = client.put( + "/api/admin/users/study-settings", + json={"employee_ids": ["s1-user"], "day": 1, "hour": 0, "minute": 0, "second": 0}, + ) + assert edges.status_code == 200 + late = client.put( + "/api/admin/users/study-settings", + json={"employee_ids": ["s1-user"], "day": 28, "hour": 23, "minute": 59, "second": 59}, + ) + assert late.status_code == 200 + listed = client.get("/api/admin/users").json() + one = next(item for item in listed if item["employee_id"] == "s1-user") + assert one["study_start"] == "每月28日 23:59:59" + rejected = client.put( + "/api/admin/users/study-settings", + json={"employee_ids": ["s1-user"], "day": 29, "hour": 0, "minute": 0, "second": 0}, + ) + assert rejected.status_code == 400 def test_admin_can_set_course_workers_for_selected_users() -> None: diff --git a/backend/tests/test_api.py b/backend/tests/test_api.py index 279caaf..04b93d6 100644 --- a/backend/tests/test_api.py +++ b/backend/tests/test_api.py @@ -743,6 +743,16 @@ def test_user_can_set_monthly_study_start(monkeypatch) -> None: assert bad.status_code == 400 too_late = client.put("/api/settings/study-start", json={"day": 26, "hour": 16, "minute": 0, "second": 0}) assert too_late.status_code == 400 + with SessionLocal() as db: + account = db.get(Account, "6650000146") + assert account is not None + account.study_start_day = 1 + db.commit() + kept = client.put("/api/settings/study-start", json={"day": 1, "hour": 7, "minute": 0, "second": 0, "enabled": True}) + assert kept.status_code == 200 + assert kept.json()["label"] == "每月1日 07:00:00" + changed = client.put("/api/settings/study-start", json={"day": 2, "hour": 7, "minute": 0, "second": 0}) + assert changed.status_code == 400 def _freeze_beijing(monkeypatch, year: int, month: int, day: int) -> None: @@ -1051,6 +1061,10 @@ def test_user_can_read_study_history(monkeypatch) -> None: assert body["items"][0]["minutes"] == 2 assert body["hours_source"] == "api" assert body["month_hours"] == "60" + with SessionLocal() as db: + account = db.get(Account, "6650000146") + assert account is not None + assert account.month_hours == "60" assert body["year_hours"] == "100" assert body["today_hours"] == 1 assert body["total_hours"] == "10" diff --git a/backend/tests/test_autostudy.py b/backend/tests/test_autostudy.py index cd6345b..66da936 100644 --- a/backend/tests/test_autostudy.py +++ b/backend/tests/test_autostudy.py @@ -606,8 +606,9 @@ def test_study_window_clamps_day_into_allowed_range() -> None: assert window_open(account, datetime(2026, 2, 8, 15, 59, 59, tzinfo=CN_TZ)) is False assert window_open(account, datetime(2026, 2, 8, 16, 0, 0, tzinfo=CN_TZ)) is True early = _Start(1, 16, 0, 0) - assert window_open(early, datetime(2026, 11, 8, 15, 59, 59, tzinfo=CN_TZ)) is False - assert window_open(early, datetime(2026, 11, 8, 16, 0, 0, tzinfo=CN_TZ)) is True + assert window_open(early, datetime(2026, 11, 1, 15, 59, 59, tzinfo=CN_TZ)) is False + assert window_open(early, datetime(2026, 11, 1, 16, 0, 0, tzinfo=CN_TZ)) is True + assert window_open(early, datetime(2026, 11, 8, 15, 59, 59, tzinfo=CN_TZ)) is True def test_immediate_start_skips_monthly_window() -> None: @@ -899,6 +900,19 @@ def test_study_window_rejects_invalid_parts() -> None: validate_start(8, 24, 0, 0) assert validate_start(5, 0, 0, 0) == (5, 0, 0, 0) assert validate_start(25, 23, 59, 59) == (25, 23, 59, 59) + assert validate_start(1, 0, 0, 0, day_min=1, day_max=28) == (1, 0, 0, 0) + assert validate_start(28, 8, 0, 0, day_min=1, day_max=28) == (28, 8, 0, 0) + + +def test_stored_start_keeps_admin_days_outside_the_user_range() -> None: + from types import SimpleNamespace + + from euniv.study_window import stored_start + + early = SimpleNamespace(study_start_day=1, study_start_hour=6, study_start_minute=0, study_start_second=0) + late = SimpleNamespace(study_start_day=28, study_start_hour=0, study_start_minute=0, study_start_second=0) + assert stored_start(early) == (1, 6, 0, 0) + assert stored_start(late) == (28, 0, 0, 0) def test_course_workers_default_and_range() -> None: diff --git a/web/src/features/admin/SettingsView.vue b/web/src/features/admin/SettingsView.vue index 05b6b26..c3c347d 100644 --- a/web/src/features/admin/SettingsView.vue +++ b/web/src/features/admin/SettingsView.vue @@ -1,7 +1,7 @@