feat: 管理端定时日扩到1至28号,学时顺带写回列表

This commit is contained in:
Nixevol
2026-11-17 22:45:47 +08:00
parent 305d458334
commit 7c498ebaf7
12 changed files with 196 additions and 53 deletions
+5 -5
View File
@@ -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
+25 -2
View File
@@ -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",
+43 -5
View File
@@ -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}"