完成数据模式相关代码
This commit is contained in:
parent
01ff669e8d
commit
e9be684b2e
52
schemas/auth.py
Normal file
52
schemas/auth.py
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
from pydantic import BaseModel
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
class Token(BaseModel):
|
||||||
|
"""
|
||||||
|
表示一个JWT令牌的模型类。
|
||||||
|
|
||||||
|
Attributes:
|
||||||
|
access_token (str): 访问令牌,用于身份验证和授权。
|
||||||
|
refresh_token (str): 刷新令牌,用于获取新的访问令牌。
|
||||||
|
token_type (str): 令牌类型,通常是"Bearer"。
|
||||||
|
expires_in (int): 访问令牌的有效期,以秒为单位。
|
||||||
|
"""
|
||||||
|
access_token: str
|
||||||
|
refresh_token: str
|
||||||
|
token_type: str
|
||||||
|
expires_in: int
|
||||||
|
|
||||||
|
class TokenData(BaseModel):
|
||||||
|
"""
|
||||||
|
表示JWT令牌中存储的数据的模型类。
|
||||||
|
|
||||||
|
Attributes:
|
||||||
|
id (int): 用户的唯一标识符。
|
||||||
|
username (str): 用户的用户名。
|
||||||
|
role (str): 用户的角色或权限。
|
||||||
|
exp (datetime): 令牌的过期时间。
|
||||||
|
"""
|
||||||
|
id: int
|
||||||
|
username: str
|
||||||
|
role: str
|
||||||
|
exp: datetime
|
||||||
|
|
||||||
|
class LoginRequest(BaseModel):
|
||||||
|
"""
|
||||||
|
表示用户登录请求的模型类。
|
||||||
|
|
||||||
|
Attributes:
|
||||||
|
username (str): 用户登录时输入的用户名。
|
||||||
|
password (str): 用户登录时输入的密码。
|
||||||
|
"""
|
||||||
|
username: str
|
||||||
|
password: str
|
||||||
|
|
||||||
|
class RefreshTokenRequest(BaseModel):
|
||||||
|
"""
|
||||||
|
表示刷新令牌请求的模型类。
|
||||||
|
|
||||||
|
Attributes:
|
||||||
|
refresh_token (str): 用于刷新访问令牌的刷新令牌。
|
||||||
|
"""
|
||||||
|
refresh_token: str
|
43
schemas/user.py
Normal file
43
schemas/user.py
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
from datetime import datetime
|
||||||
|
from enum import Enum
|
||||||
|
from pydantic import BaseModel, Field, root_validator
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
# 用户角色枚举
|
||||||
|
class UserRole(str, Enum):
|
||||||
|
SYSTEM_ADMIN = "system_admin"
|
||||||
|
ADMIN = "admin"
|
||||||
|
USER = "user"
|
||||||
|
|
||||||
|
# 基础用户模型
|
||||||
|
class UserBase(BaseModel):
|
||||||
|
username: str = Field(..., max_length=50, description="用户名")
|
||||||
|
role: UserRole = Field(default=UserRole.USER, description="用户角色")
|
||||||
|
description: Optional[str] = Field(None, max_length=255, description="用户描述")
|
||||||
|
|
||||||
|
# 用户创建模型
|
||||||
|
class UserCreate(UserBase):
|
||||||
|
password: str = Field(..., min_length=6, max_length=255, description="用户密码")
|
||||||
|
|
||||||
|
|
||||||
|
# 用户更新模型
|
||||||
|
class UserUpdate(BaseModel):
|
||||||
|
username: Optional[str] = Field(None, max_length=50, description="用户名")
|
||||||
|
role: Optional[UserRole] = Field(None, description="用户角色")
|
||||||
|
description: Optional[str] = Field(None, max_length=255, description="用户描述")
|
||||||
|
|
||||||
|
# 可选:确保至少更新一个字段
|
||||||
|
@root_validator
|
||||||
|
def validate_at_least_one_field(cls, values):
|
||||||
|
if not any(values.values()):
|
||||||
|
raise ValueError("至少需要更新一个字段")
|
||||||
|
return values
|
||||||
|
|
||||||
|
# 用户响应模型
|
||||||
|
class UserResponse(UserBase):
|
||||||
|
id: int = Field(..., description="用户ID")
|
||||||
|
created_at: datetime = Field(..., description="创建时间")
|
||||||
|
updated_at: datetime = Field(..., description="更新时间")
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
orm_mode = True # 允许从 ORM 对象加载数据
|
Loading…
x
Reference in New Issue
Block a user