feat: 立即开始停止学习并整理学时历史与界面
This commit is contained in:
+85
-19
@@ -16,6 +16,8 @@ from euniv.course_workers import apply as apply_workers
|
||||
from euniv.course_workers import of as workers_of
|
||||
from euniv.course_workers import view_of as workers_view
|
||||
from euniv.proxy import ProxyError, apply, clear, enabled, summary_of, url_of, view_of
|
||||
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 label_of as start_label
|
||||
@@ -65,25 +67,32 @@ def _iso_time(value: datetime | None) -> str | None:
|
||||
|
||||
def list_users(db: Session) -> list[dict]:
|
||||
accounts = db.scalars(select(Account).order_by(Account.employee_id)).all()
|
||||
return [
|
||||
{
|
||||
"employee_id": account.employee_id,
|
||||
"has_credential": bool(account.credential_cipher),
|
||||
"refresh_paused": bool(account.refresh_paused),
|
||||
"last_refresh_at": _iso_time(account.last_refresh_at),
|
||||
"credential_valid": _optional_bool(account.credential_valid),
|
||||
"month_hours": account.month_hours or "",
|
||||
"has_proxy": enabled(account),
|
||||
"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),
|
||||
"course_workers": workers_of(account),
|
||||
}
|
||||
for account in accounts
|
||||
]
|
||||
rows = []
|
||||
for account in accounts:
|
||||
control = study_view(account)
|
||||
rows.append(
|
||||
{
|
||||
"employee_id": account.employee_id,
|
||||
"has_credential": bool(account.credential_cipher),
|
||||
"refresh_paused": bool(account.refresh_paused),
|
||||
"last_refresh_at": _iso_time(account.last_refresh_at),
|
||||
"credential_valid": _optional_bool(account.credential_valid),
|
||||
"month_hours": account.month_hours or "",
|
||||
"monthly_target": int(account.monthly_target or 600),
|
||||
"has_proxy": enabled(account),
|
||||
"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),
|
||||
"course_workers": workers_of(account),
|
||||
"study_paused": control["paused"],
|
||||
"study_ignore_window": control["ignore_window"],
|
||||
"study_started": control["started"],
|
||||
}
|
||||
)
|
||||
return rows
|
||||
|
||||
|
||||
def _require(db: Session, employee_id: str) -> Account:
|
||||
@@ -270,6 +279,63 @@ def set_course_workers(db: Session, employee_ids: list[str], workers: int) -> di
|
||||
return {"ok": True, "count": len(accounts)}
|
||||
|
||||
|
||||
def set_study_action(db: Session, employee_ids: list[str], action: str) -> dict:
|
||||
kind = (action or "").strip().lower()
|
||||
if kind not in {"start", "stop"}:
|
||||
raise HTTPException(status_code=400, detail="请选择开始或停止")
|
||||
accounts = _accounts_by_ids(db, employee_ids)
|
||||
from euniv.autostudy import request_stop, request_wake
|
||||
|
||||
for account in accounts:
|
||||
if kind == "start":
|
||||
start_now(account)
|
||||
else:
|
||||
stop_now(account)
|
||||
db.commit()
|
||||
if kind == "start":
|
||||
request_wake()
|
||||
else:
|
||||
for account in accounts:
|
||||
request_stop(account.employee_id)
|
||||
return {"ok": True, "count": len(accounts), "action": kind}
|
||||
|
||||
|
||||
def set_study_settings(
|
||||
db: Session,
|
||||
employee_ids: list[str],
|
||||
monthly_target: int | None = None,
|
||||
day: int | None = None,
|
||||
hour: int | None = None,
|
||||
minute: int | None = None,
|
||||
second: int | None = None,
|
||||
workers: int | None = None,
|
||||
scheme: str | None = None,
|
||||
host: str = "",
|
||||
port: object = None,
|
||||
username: str = "",
|
||||
password: str | None = None,
|
||||
) -> dict:
|
||||
accounts = _accounts_by_ids(db, employee_ids)
|
||||
if monthly_target is not None:
|
||||
if not isinstance(monthly_target, int) or isinstance(monthly_target, bool) or not 0 <= monthly_target <= 9999:
|
||||
raise HTTPException(status_code=400, detail="每月学时目标须为 0 到 9999 的整数")
|
||||
try:
|
||||
for account in accounts:
|
||||
if monthly_target is not None:
|
||||
account.monthly_target = monthly_target
|
||||
if day is not None:
|
||||
apply_start(account, day, hour, minute, second)
|
||||
if workers is not None:
|
||||
apply_workers(account, workers)
|
||||
if scheme is not None:
|
||||
apply(account, scheme, host, port, username, password)
|
||||
except (StudyWindowError, CourseWorkersError, ProxyError) 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)
|
||||
|
||||
Reference in New Issue
Block a user