feat(P5): 学科配置表(学科=配置非插件)——Course model+repo+service(7 学科种子,提示词自由化基座)+routes(公开列表/管理员 CRUD)+main 挂载 seed
这个提交包含在:
@@ -0,0 +1,64 @@
|
||||
"""学科配置 API:公开列表(注册页/工作台)+ 管理员 CRUD。"""
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import APIRouter, Depends, Header, HTTPException
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.db import get_session
|
||||
from app.services.auth_service import AuthService
|
||||
from app.services.course_service import CourseService
|
||||
|
||||
router = APIRouter(prefix="/courses", tags=["courses"])
|
||||
|
||||
|
||||
def _admin(authorization: str | None, session: Session):
|
||||
if not authorization or not authorization.lower().startswith("bearer "):
|
||||
raise HTTPException(401, "未登录")
|
||||
try:
|
||||
user = AuthService(session).me(authorization[7:].strip())
|
||||
except Exception as e:
|
||||
raise HTTPException(401, str(e))
|
||||
if not user.get("is_admin"):
|
||||
raise HTTPException(403, "仅教师/管理员可访问")
|
||||
return user
|
||||
|
||||
|
||||
@router.get("")
|
||||
async def list_courses(session: Session = Depends(get_session)):
|
||||
"""公开:可用学科列表(注册页学科选择 / 工作台 LaTeX 速查表键)。"""
|
||||
return CourseService(session).list_enabled()
|
||||
|
||||
|
||||
class CourseBody(BaseModel):
|
||||
code: str
|
||||
name: str
|
||||
prompt: str = ""
|
||||
latex_key: str = "common"
|
||||
problem_route: str = ""
|
||||
enabled: bool = True
|
||||
sort: int = 100
|
||||
|
||||
|
||||
@router.post("/admin")
|
||||
async def create_course(body: CourseBody, authorization: str | None = Header(None),
|
||||
session: Session = Depends(get_session)):
|
||||
_admin(authorization, session)
|
||||
return CourseService(session).upsert(body.code, body.name, body.prompt, body.latex_key,
|
||||
body.problem_route, body.enabled, body.sort)
|
||||
|
||||
|
||||
@router.get("/admin")
|
||||
async def list_all_courses(authorization: str | None = Header(None),
|
||||
session: Session = Depends(get_session)):
|
||||
"""管理员:全部学科(含禁用)。"""
|
||||
_admin(authorization, session)
|
||||
return CourseService(session).list_all()
|
||||
|
||||
|
||||
@router.delete("/admin/{course_id}")
|
||||
async def delete_course(course_id: int, authorization: str | None = Header(None),
|
||||
session: Session = Depends(get_session)):
|
||||
_admin(authorization, session)
|
||||
CourseService(session).delete(course_id)
|
||||
return {"ok": True}
|
||||
在新工单中引用
屏蔽一个用户