feat: 账号可设置同时学习课程数并支持关闭代理
This commit is contained in:
+21
-2
@@ -17,7 +17,9 @@ from admin.users import (
|
||||
list_users,
|
||||
read_credentials,
|
||||
read_proxy,
|
||||
read_course_workers,
|
||||
read_study_start,
|
||||
set_course_workers,
|
||||
set_paused,
|
||||
set_proxy,
|
||||
set_study_start,
|
||||
@@ -49,8 +51,8 @@ class PauseBody(BaseModel):
|
||||
class AdminProxyBody(BaseModel):
|
||||
employee_ids: list[str]
|
||||
scheme: str
|
||||
host: str
|
||||
port: int
|
||||
host: str = ""
|
||||
port: int | None = None
|
||||
username: str = ""
|
||||
password: str | None = None
|
||||
|
||||
@@ -67,6 +69,11 @@ class AdminStudyStartBody(BaseModel):
|
||||
second: int
|
||||
|
||||
|
||||
class AdminCourseWorkersBody(BaseModel):
|
||||
employee_ids: list[str]
|
||||
workers: int
|
||||
|
||||
|
||||
def require_admin(request: Request) -> str:
|
||||
if not admin_session_active(request.cookies):
|
||||
raise HTTPException(status_code=401, detail="未登录")
|
||||
@@ -186,6 +193,18 @@ def get_user_study_start(employee_id: str, db: Session = Depends(get_db), _: str
|
||||
return read_study_start(db, employee_id)
|
||||
|
||||
|
||||
@admin_router.put("/users/course-workers")
|
||||
def put_users_course_workers(
|
||||
body: AdminCourseWorkersBody, db: Session = Depends(get_db), _: str = Depends(require_admin)
|
||||
) -> dict:
|
||||
return set_course_workers(db, body.employee_ids, body.workers)
|
||||
|
||||
|
||||
@admin_router.get("/users/{employee_id}/course-workers")
|
||||
def get_user_course_workers(employee_id: str, db: Session = Depends(get_db), _: str = Depends(require_admin)) -> dict:
|
||||
return read_course_workers(db, employee_id)
|
||||
|
||||
|
||||
@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)
|
||||
|
||||
+23
-2
@@ -11,6 +11,10 @@ from sqlalchemy.orm import Session
|
||||
|
||||
from euniv.crypto import decrypt_secret
|
||||
from euniv.models import Account
|
||||
from euniv.course_workers import CourseWorkersError
|
||||
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, summary_of, url_of, view_of
|
||||
from euniv.study_window import StudyWindowError
|
||||
from euniv.study_window import apply as apply_start
|
||||
@@ -76,6 +80,7 @@ def list_users(db: Session) -> list[dict]:
|
||||
"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
|
||||
]
|
||||
@@ -202,8 +207,8 @@ def set_proxy(
|
||||
db: Session,
|
||||
employee_ids: list[str],
|
||||
scheme: str,
|
||||
host: str,
|
||||
port: int,
|
||||
host: str = "",
|
||||
port: object = None,
|
||||
username: str = "",
|
||||
password: str | None = None,
|
||||
) -> dict:
|
||||
@@ -249,6 +254,22 @@ def set_study_start(
|
||||
return {"ok": True, "count": len(accounts)}
|
||||
|
||||
|
||||
def read_course_workers(db: Session, employee_id: str) -> dict:
|
||||
return workers_view(_require(db, employee_id))
|
||||
|
||||
|
||||
def set_course_workers(db: Session, employee_ids: list[str], workers: int) -> dict:
|
||||
accounts = _accounts_by_ids(db, employee_ids)
|
||||
try:
|
||||
for account in accounts:
|
||||
apply_workers(account, workers)
|
||||
except CourseWorkersError 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