78 lines
2.6 KiB
Python
78 lines
2.6 KiB
Python
"""
|
|
用户相关Pydantic模式
|
|
"""
|
|
from datetime import datetime
|
|
from typing import Optional
|
|
|
|
from pydantic import BaseModel, EmailStr, Field
|
|
|
|
|
|
class UserBase(BaseModel):
|
|
"""用户基础模式"""
|
|
username: str = Field(..., min_length=3, max_length=50, description="用户名")
|
|
email: Optional[EmailStr] = Field(None, description="邮箱")
|
|
phone: Optional[str] = Field(None, max_length=20, description="手机号")
|
|
nickname: Optional[str] = Field(None, max_length=50, description="昵称")
|
|
status: bool = Field(default=True, description="状态")
|
|
|
|
|
|
class UserCreate(UserBase):
|
|
"""创建用户模式"""
|
|
password: str = Field(..., min_length=6, max_length=128, description="密码")
|
|
is_superuser: bool = Field(default=False, description="是否超级用户")
|
|
|
|
|
|
class UserUpdate(BaseModel):
|
|
"""更新用户模式"""
|
|
email: Optional[EmailStr] = Field(None, description="邮箱")
|
|
phone: Optional[str] = Field(None, max_length=20, description="手机号")
|
|
nickname: Optional[str] = Field(None, max_length=50, description="昵称")
|
|
status: Optional[bool] = Field(None, description="状态")
|
|
is_superuser: Optional[bool] = Field(None, description="是否超级用户")
|
|
password: Optional[str] = Field(None, min_length=6, max_length=128, description="密码")
|
|
|
|
|
|
class UserResponse(UserBase):
|
|
"""用户响应模式"""
|
|
id: int
|
|
is_superuser: bool
|
|
last_login_time: Optional[datetime] = None
|
|
last_login_ip: Optional[str] = None
|
|
created_at: datetime
|
|
updated_at: datetime
|
|
|
|
class Config:
|
|
from_attributes = True
|
|
|
|
|
|
class UserLogin(BaseModel):
|
|
"""用户登录模式"""
|
|
username: str = Field(..., description="用户名")
|
|
password: str = Field(..., description="密码")
|
|
|
|
|
|
class Token(BaseModel):
|
|
"""Token模式"""
|
|
access_token: str = Field(..., description="访问令牌")
|
|
token_type: str = Field(default="bearer", description="令牌类型")
|
|
expires_in: int = Field(..., description="过期时间(秒)")
|
|
refresh_token: Optional[str] = Field(None, description="刷新令牌")
|
|
|
|
|
|
class TokenData(BaseModel):
|
|
"""Token数据模式"""
|
|
username: Optional[str] = None
|
|
|
|
|
|
class PasswordChange(BaseModel):
|
|
"""修改密码模式"""
|
|
old_password: str = Field(..., description="旧密码")
|
|
new_password: str = Field(..., min_length=6, max_length=128, description="新密码")
|
|
|
|
|
|
class PasswordReset(BaseModel):
|
|
"""重置密码模式"""
|
|
email: EmailStr = Field(..., description="邮箱")
|
|
token: str = Field(..., description="重置令牌")
|
|
new_password: str = Field(..., min_length=6, max_length=128, description="新密码")
|