feat: 自动学习功能,后台定时发送心跳积累学时
This commit is contained in:
+42
-4
@@ -31,18 +31,29 @@ from euniv.service import (
|
||||
)
|
||||
from euniv.session import clear_session, read_employee_id, write_session
|
||||
|
||||
from pydantic import BaseModel as _BaseModel
|
||||
|
||||
|
||||
class MonthlyTargetBody(_BaseModel):
|
||||
monthly_target: int
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(_app: FastAPI):
|
||||
init_db()
|
||||
ensure_admin_seed()
|
||||
load_admin_identity()
|
||||
stop = None
|
||||
stop_refresh = None
|
||||
stop_study = None
|
||||
if os.environ.get("EUNIV_DISABLE_REFRESH") != "1":
|
||||
stop = start_credential_refresh()
|
||||
stop_refresh = start_credential_refresh()
|
||||
from euniv.autostudy import start_autostudy
|
||||
stop_study = start_autostudy()
|
||||
yield
|
||||
if stop is not None:
|
||||
stop.set()
|
||||
if stop_study is not None:
|
||||
stop_study.stop()
|
||||
if stop_refresh is not None:
|
||||
stop_refresh.set()
|
||||
|
||||
|
||||
app = FastAPI(title="EunivTools", lifespan=lifespan)
|
||||
@@ -186,6 +197,33 @@ def exam_detail(path: str, request: Request, db: Session = Depends(get_db)) -> d
|
||||
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
||||
|
||||
|
||||
@app.get("/api/settings/monthly-target")
|
||||
def get_monthly_target(request: Request, db: Session = Depends(get_db)) -> dict:
|
||||
employee_id = _signed_in(request, db)
|
||||
account = db.get(Account, employee_id)
|
||||
return {"monthly_target": account.monthly_target if account else 600}
|
||||
|
||||
|
||||
@app.put("/api/settings/monthly-target")
|
||||
def put_monthly_target(body: MonthlyTargetBody, request: Request, db: Session = Depends(get_db)) -> dict:
|
||||
employee_id = _signed_in(request, db)
|
||||
if not 0 <= body.monthly_target <= 9999:
|
||||
raise HTTPException(status_code=400, detail="每月学时目标须为 0 到 9999 的整数")
|
||||
account = db.get(Account, employee_id)
|
||||
if account is None:
|
||||
raise HTTPException(status_code=401, detail="未登录")
|
||||
account.monthly_target = body.monthly_target
|
||||
db.commit()
|
||||
return {"monthly_target": account.monthly_target}
|
||||
|
||||
|
||||
@app.get("/api/autostudy/status")
|
||||
def autostudy_state(request: Request, db: Session = Depends(get_db)) -> dict:
|
||||
_signed_in(request, db)
|
||||
from euniv.autostudy import autostudy_status
|
||||
return autostudy_status()
|
||||
|
||||
|
||||
@app.get("/api/training")
|
||||
def training_detail(path: str, request: Request, db: Session = Depends(get_db)) -> dict:
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user