Files
mtusi2026lab4/app/models.py
T

111 lines
2.3 KiB
Python

from pydantic import BaseModel, Field, ConfigDict, field_validator
from typing import List, Optional
def _not_blank(value: str) -> str:
if not value.strip():
raise ValueError("must contain non-whitespace characters")
return value
def _not_blank_or_empty(value: str) -> str:
if value and not value.strip():
raise ValueError("must contain non-whitespace characters")
return value
class User(BaseModel):
id: str
username: str
display_name: str
created_at: str
class CreateUser(BaseModel):
model_config = ConfigDict(extra="forbid")
username: str = Field(..., pattern=r"^[a-z0-9_]{3,32}$")
display_name: str = Field(..., min_length=1, max_length=100)
password: str = Field(..., min_length=12, max_length=128)
_check_display_name = field_validator("display_name")(_not_blank)
class Chat(BaseModel):
id: str
title: str
member_ids: List[str]
created_at: str
class CreateChat(BaseModel):
model_config = ConfigDict(extra="forbid")
title: str = Field(..., min_length=1, max_length=100)
member_ids: List[str] = Field(..., min_length=1, max_length=100)
_check_title = field_validator("title")(_not_blank)
class Message(BaseModel):
id: str
chat_id: str
sender_id: str
text: str
created_at: str
attachment_ids: List[str]
class CreateMessage(BaseModel):
model_config = ConfigDict(extra="forbid")
text: str = Field("", max_length=4000)
attachment_ids: List[str] = Field(default_factory=list, max_length=10)
_check_text = field_validator("text")(_not_blank_or_empty)
class Attachment(BaseModel):
id: str
chat_id: str
owner_id: str
filename: str
content_type: str
size_bytes: int
sha256: str
status: str
variants: List[dict]
error_code: Optional[str]
created_at: str
updated_at: str
class ChatPage(BaseModel):
items: List[Chat]
next_cursor: Optional[str] = None
class MessagePage(BaseModel):
items: List[Message]
next_cursor: Optional[str] = None
class Health(BaseModel):
status: str = "ok"
class Session(BaseModel):
token: str
token_type: str = "Bearer"
expires_at: str
user: User
class ErrorBody(BaseModel):
code: str
message: str
request_id: str
class ErrorResponse(BaseModel):
error: ErrorBody