Files
stu_teacher/scheme/teachers.py
T

55 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# scheme/teachers.py
# 老师模块的请求/响应模型(沿用原 teacher 模块风格)
from typing import List, Optional
from pydantic import BaseModel, Field, field_validator
# -------------------- 请求体模型 -----------------------------
class TeacherAdd(BaseModel):
teacher_id: Optional[int] = Field(None, ge=1, description="教师ID(不传则自增)")
class_id: int = Field(..., ge=1, description="所带班级ID")
teacher_name: str = Field(..., min_length=1, description="教师姓名")
job_name: str = Field(..., min_length=2, description="主讲、班主任、助教")
is_deleted: int = Field(0, ge=0, le=0, description="只能输入0,0代表未删除")
@field_validator("job_name")
@classmethod
def validate_job_name(cls, v):
"""校验 job_name 必须是 主讲/班主任/助教 之一"""
allowed = ["主讲", "班主任", "助教"]
if v not in allowed:
raise ValueError(f"job_name 必须是 {allowed} 之一,当前值: {v}")
return v
class TeacherUpdate(BaseModel):
class_id: Optional[int] = Field(None, ge=1, description="所带班级ID")
teacher_name: Optional[str] = Field(None, min_length=1, description="教师姓名")
job_name: Optional[str] = Field(None, min_length=2, description="主讲、班主任、助教")
@field_validator("job_name")
@classmethod
def validate_job_name(cls, v):
allowed = ["主讲", "班主任", "助教"]
if v is not None and v not in allowed:
raise ValueError(f"job_name 必须是 {allowed} 之一,当前值: {v}")
return v
# -------------------- 响应模型 ----------------------------
class TeacherResponse(BaseModel):
teacher_id: int
class_id: int
teacher_name: str
job_name: str
class_name: Optional[str] = Field(None, description="冗余展示:班级名称")
class Config:
from_attributes = True
class TeacherListResponse(BaseModel):
total: int
items: List[TeacherResponse]