feat: 立即开始停止学习并整理学时历史与界面
This commit is contained in:
@@ -22,11 +22,16 @@ from admin.users import (
|
||||
set_course_workers,
|
||||
set_paused,
|
||||
set_proxy,
|
||||
set_study_action,
|
||||
set_study_settings,
|
||||
set_study_start,
|
||||
)
|
||||
from euniv.courses import course_page, request_course_sync_now
|
||||
from euniv.db import get_db
|
||||
from euniv.proxy_pool import ProxyProbeError
|
||||
from euniv.proxy_pool import page as proxy_page
|
||||
from euniv.proxy_pool import probe_ids as probe_proxy_ids
|
||||
from euniv.proxy_pool import probe_one as probe_proxy_one
|
||||
from euniv.proxy_pool import request_refresh as request_proxy_refresh
|
||||
|
||||
public_router = APIRouter()
|
||||
@@ -76,6 +81,30 @@ class AdminCourseWorkersBody(BaseModel):
|
||||
workers: int
|
||||
|
||||
|
||||
class AdminStudyActionBody(BaseModel):
|
||||
employee_ids: list[str]
|
||||
action: str
|
||||
|
||||
|
||||
class AdminStudySettingsBody(BaseModel):
|
||||
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: int | None = None
|
||||
username: str = ""
|
||||
password: str | None = None
|
||||
|
||||
|
||||
class AdminProxyProbeBody(BaseModel):
|
||||
ids: list[int] | None = None
|
||||
|
||||
|
||||
def require_admin(request: Request) -> str:
|
||||
if not admin_session_active(request.cookies):
|
||||
raise HTTPException(status_code=401, detail="未登录")
|
||||
@@ -148,6 +177,22 @@ def post_proxies_refresh(_: str = Depends(require_admin)) -> dict:
|
||||
return request_proxy_refresh()
|
||||
|
||||
|
||||
@admin_router.post("/proxies/probe")
|
||||
def post_proxies_probe(body: AdminProxyProbeBody, _: str = Depends(require_admin)) -> dict:
|
||||
try:
|
||||
return probe_proxy_ids(body.ids)
|
||||
except ProxyProbeError as exc:
|
||||
raise HTTPException(status_code=404, detail=str(exc)) from exc
|
||||
|
||||
|
||||
@admin_router.post("/proxies/{proxy_id}/probe")
|
||||
def post_proxy_probe(proxy_id: int, _: str = Depends(require_admin)) -> dict:
|
||||
try:
|
||||
return probe_proxy_one(proxy_id)
|
||||
except ProxyProbeError as exc:
|
||||
raise HTTPException(status_code=404, detail=str(exc)) from exc
|
||||
|
||||
|
||||
@admin_router.get("/users")
|
||||
def get_users(db: Session = Depends(get_db), _: str = Depends(require_admin)) -> list[dict]:
|
||||
return list_users(db)
|
||||
@@ -224,6 +269,34 @@ def get_user_course_workers(employee_id: str, db: Session = Depends(get_db), _:
|
||||
return read_course_workers(db, employee_id)
|
||||
|
||||
|
||||
@admin_router.post("/users/study")
|
||||
def post_users_study(
|
||||
body: AdminStudyActionBody, db: Session = Depends(get_db), _: str = Depends(require_admin)
|
||||
) -> dict:
|
||||
return set_study_action(db, body.employee_ids, body.action)
|
||||
|
||||
|
||||
@admin_router.put("/users/study-settings")
|
||||
def put_users_study_settings(
|
||||
body: AdminStudySettingsBody, db: Session = Depends(get_db), _: str = Depends(require_admin)
|
||||
) -> dict:
|
||||
return set_study_settings(
|
||||
db,
|
||||
body.employee_ids,
|
||||
monthly_target=body.monthly_target,
|
||||
day=body.day,
|
||||
hour=body.hour,
|
||||
minute=body.minute,
|
||||
second=body.second,
|
||||
workers=body.workers,
|
||||
scheme=body.scheme,
|
||||
host=body.host,
|
||||
port=body.port,
|
||||
username=body.username,
|
||||
password=body.password,
|
||||
)
|
||||
|
||||
|
||||
@admin_router.get("/users/{employee_id}/credentials")
|
||||
def get_credentials(employee_id: str, db: Session = Depends(get_db), _: str = Depends(require_admin)) -> dict:
|
||||
return read_credentials(db, employee_id)
|
||||
|
||||
+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