Files
MemoryBear/api/app/schemas/user_schema.py
2026-03-11 10:45:07 +08:00

111 lines
3.3 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.
from dataclasses import field
from pydantic import BaseModel, EmailStr, Field, field_validator, validator, ConfigDict
from typing import Optional
import datetime
import uuid
from app.models import Workspace
from app.models.workspace_model import WorkspaceRole
class UserBase(BaseModel):
username: str
email: EmailStr
class UserCreate(UserBase):
password: str
class UserUpdate(BaseModel):
username: Optional[str] = None
email: Optional[EmailStr] = None
is_active: Optional[bool] = None
is_superuser: Optional[bool] = None
class ChangePasswordRequest(BaseModel):
"""修改密码请求"""
old_password: str = Field(..., description="当前密码")
new_password: str = Field(..., min_length=6, description="新密码至少6位")
class AdminChangePasswordRequest(BaseModel):
"""管理员修改用户密码请求"""
user_id: uuid.UUID = Field(..., description="要修改密码的用户ID")
new_password: Optional[str] = Field(None, min_length=6, description="新密码至少6位。如果不提供则自动生成随机密码")
class ChangeEmailRequest(BaseModel):
"""修改邮箱请求"""
password: str = Field(..., description="当前密码")
new_email: EmailStr = Field(..., description="新邮箱地址")
class SendEmailCodeRequest(BaseModel):
"""发送邮箱验证码请求"""
email: EmailStr = Field(..., description="邮箱地址")
class VerifyEmailCodeRequest(BaseModel):
"""验证邮箱验证码并修改邮箱请求"""
new_email: EmailStr = Field(..., description="新邮箱地址")
code: str = Field(..., min_length=6, max_length=6, description="验证码")
class VerifyPasswordRequest(BaseModel):
"""验证密码请求"""
password: str = Field(..., description="密码")
class LanguagePreferenceRequest(BaseModel):
"""语言偏好设置请求"""
language: str = Field(..., min_length=2, max_length=10, description="语言代码,如 'zh', 'en'")
class LanguagePreferenceResponse(BaseModel):
"""语言偏好响应"""
language: str = Field(..., description="当前语言偏好")
class ChangePasswordResponse(BaseModel):
"""修改密码响应"""
message: str
success: bool = True
generated_password: Optional[str] = Field(None, description="自动生成的密码(仅在管理员重置时返回)")
class User(UserBase):
id: uuid.UUID
is_active: bool
is_superuser: bool
created_at: int
last_login_at: Optional[int] = None
current_workspace_id: Optional[uuid.UUID] = None
current_workspace_name: Optional[str] = None
role: Optional[WorkspaceRole] = None
preferred_language: Optional[str] = "zh" # 用户语言偏好
# 将 datetime 转换为毫秒时间戳
@validator("created_at", pre=True)
def _created_at_to_ms(cls, v):
if isinstance(v, datetime.datetime):
return int(v.timestamp() * 1000)
if isinstance(v, (int, float)):
return int(v)
return v
model_config = ConfigDict(from_attributes=True)
@field_validator("last_login_at", mode="before")
@classmethod
def _last_login_to_ms(cls, v):
if v is None:
return None
if isinstance(v, datetime.datetime):
return int(v.timestamp() * 1000)
if isinstance(v, (int, float)):
return int(v)
return v