Files
game-marathon/backend/app/schemas/user.py
2025-12-18 17:15:21 +07:00

86 lines
2.1 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 datetime import datetime
from pydantic import BaseModel, Field, field_validator
import re
class UserBase(BaseModel):
nickname: str = Field(..., min_length=2, max_length=50)
class UserRegister(UserBase):
login: str = Field(..., min_length=3, max_length=50)
password: str = Field(..., min_length=6, max_length=100)
@field_validator("login")
@classmethod
def validate_login(cls, v: str) -> str:
if not re.match(r"^[a-zA-Z0-9_]+$", v):
raise ValueError("Login can only contain letters, numbers, and underscores")
return v.lower()
class UserLogin(BaseModel):
login: str
password: str
class UserUpdate(BaseModel):
nickname: str | None = Field(None, min_length=2, max_length=50)
class UserPublic(UserBase):
"""Public user info visible to other users - minimal data"""
id: int
avatar_url: str | None = None
role: str = "user"
telegram_avatar_url: str | None = None # Only TG avatar is public
created_at: datetime
class Config:
from_attributes = True
class UserPrivate(UserPublic):
"""Full user info visible only to the user themselves"""
login: str
telegram_id: int | None = None
telegram_username: str | None = None
telegram_first_name: str | None = None
telegram_last_name: str | None = None
class TokenResponse(BaseModel):
access_token: str
token_type: str = "bearer"
user: UserPrivate
class TelegramLink(BaseModel):
telegram_id: int
telegram_username: str | None = None
class PasswordChange(BaseModel):
current_password: str = Field(..., min_length=6)
new_password: str = Field(..., min_length=6, max_length=100)
class UserStats(BaseModel):
"""Статистика пользователя по марафонам"""
marathons_count: int
wins_count: int
completed_assignments: int
total_points_earned: int
class UserProfilePublic(BaseModel):
"""Публичный профиль пользователя со статистикой"""
id: int
nickname: str
avatar_url: str | None = None
created_at: datetime
stats: UserStats
class Config:
from_attributes = True