hxf/backend/th_agenter/schemas/user.py

61 lines
1.9 KiB
Python
Raw Permalink Normal View History

2025-12-04 14:48:38 +08:00
"""User schemas."""
2025-12-17 19:26:36 +08:00
from typing import Optional, List, Dict, Any
2025-12-04 14:48:38 +08:00
from pydantic import BaseModel, Field
2025-12-17 19:26:36 +08:00
from datetime import datetime
from ..utils.schemas import BaseResponse
2025-12-04 14:48:38 +08:00
class UserBase(BaseModel):
"""User base schema."""
username: str = Field(..., min_length=3, max_length=50)
email: str = Field(..., max_length=100)
full_name: Optional[str] = Field(None, max_length=100)
bio: Optional[str] = None
avatar_url: Optional[str] = None
2025-12-17 19:26:36 +08:00
2025-12-04 14:48:38 +08:00
class UserCreate(UserBase):
"""User creation schema."""
password: str = Field(..., min_length=6)
2025-12-17 19:26:36 +08:00
2025-12-04 14:48:38 +08:00
class UserUpdate(BaseModel):
"""User update schema."""
username: Optional[str] = Field(None, min_length=3, max_length=50)
email: Optional[str] = Field(None, max_length=100)
full_name: Optional[str] = Field(None, max_length=100)
bio: Optional[str] = None
avatar_url: Optional[str] = None
password: Optional[str] = Field(None, min_length=6)
is_active: Optional[bool] = None
2025-12-17 19:26:36 +08:00
2025-12-04 14:48:38 +08:00
class ChangePasswordRequest(BaseModel):
"""Change password request schema."""
current_password: str = Field(..., description="Current password")
new_password: str = Field(..., min_length=6, description="New password")
2025-12-17 19:26:36 +08:00
2025-12-04 14:48:38 +08:00
class ResetPasswordRequest(BaseModel):
"""Admin reset password request schema."""
new_password: str = Field(..., min_length=6, description="New password")
2025-12-17 19:26:36 +08:00
2025-12-04 14:48:38 +08:00
class UserResponse(BaseResponse, UserBase):
"""User response schema."""
is_active: bool
is_superuser: Optional[bool] = Field(default=False, description="是否为超级管理员")
2025-12-17 19:26:36 +08:00
class Config:
from_attributes = True
2025-12-04 14:48:38 +08:00
@classmethod
2025-12-17 19:26:36 +08:00
def from_orm(cls, obj):
"""从ORM对象创建响应模型正确处理is_superuser方法"""
data = obj.__dict__.copy()
# 调用is_superuser方法获取布尔值
if hasattr(obj, 'is_superuser') and callable(obj.is_superuser):
data['is_superuser'] = obj.is_superuser()
return cls(**data)