forked from mtusi2026/mtusi2026lab5
Complete Lab 5 grade 4 async attachment pipeline
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
from pydantic import BaseModel, ConfigDict, Field, StrictStr, field_validator
|
||||
import uuid
|
||||
|
||||
|
||||
class StrictModel(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
|
||||
class CreateUser(StrictModel):
|
||||
username: StrictStr = Field(min_length=3, max_length=32, pattern=r"^[a-z0-9_]{3,32}$")
|
||||
display_name: StrictStr = Field(min_length=1, max_length=100)
|
||||
password: StrictStr = Field(min_length=12, max_length=128)
|
||||
|
||||
@field_validator("display_name")
|
||||
@classmethod
|
||||
def display_name_not_blank(cls, value: str) -> str:
|
||||
if not value.strip():
|
||||
raise ValueError("display_name must contain a non-whitespace character")
|
||||
return value
|
||||
|
||||
|
||||
class UserOut(BaseModel):
|
||||
id: str
|
||||
username: str
|
||||
display_name: str
|
||||
created_at: str
|
||||
|
||||
|
||||
class CreateChat(StrictModel):
|
||||
title: StrictStr = Field(min_length=1, max_length=100)
|
||||
member_ids: list[StrictStr] = Field(min_length=1, max_length=100)
|
||||
|
||||
@field_validator("title")
|
||||
@classmethod
|
||||
def title_not_blank(cls, value: str) -> str:
|
||||
if not value.strip():
|
||||
raise ValueError("title must contain a non-whitespace character")
|
||||
return value
|
||||
|
||||
@field_validator("member_ids")
|
||||
@classmethod
|
||||
def members_are_unique_uuids(cls, values: list[str]) -> list[str]:
|
||||
import uuid
|
||||
|
||||
try:
|
||||
normalized = [str(uuid.UUID(value)) for value in values]
|
||||
except (ValueError, AttributeError, TypeError) as exc:
|
||||
raise ValueError("member_ids must contain UUIDs") from exc
|
||||
if len(normalized) != len(set(normalized)):
|
||||
raise ValueError("member_ids must be unique")
|
||||
return normalized
|
||||
|
||||
|
||||
class ChatOut(BaseModel):
|
||||
id: str
|
||||
title: str
|
||||
member_ids: list[str]
|
||||
created_at: str
|
||||
|
||||
|
||||
class ChatPage(BaseModel):
|
||||
items: list[ChatOut]
|
||||
next_cursor: str | None
|
||||
|
||||
|
||||
class CreateMessage(StrictModel):
|
||||
text: StrictStr = Field(max_length=4000)
|
||||
attachment_ids: list[StrictStr] = Field(default_factory=list, max_length=10)
|
||||
|
||||
@field_validator("attachment_ids")
|
||||
@classmethod
|
||||
def attachment_ids_are_unique_uuids(cls, values: list[str]) -> list[str]:
|
||||
try:
|
||||
normalized = [str(uuid.UUID(value)) for value in values]
|
||||
except (ValueError, AttributeError, TypeError) as exc:
|
||||
raise ValueError("attachment_ids must contain UUIDs") from exc
|
||||
if len(normalized) != len(set(normalized)):
|
||||
raise ValueError("attachment_ids must be unique")
|
||||
return normalized
|
||||
|
||||
|
||||
class MessageOut(BaseModel):
|
||||
id: str
|
||||
chat_id: str
|
||||
sender_id: str
|
||||
text: str
|
||||
created_at: str
|
||||
attachment_ids: list[str]
|
||||
|
||||
|
||||
class AttachmentOut(BaseModel):
|
||||
id: str
|
||||
chat_id: str
|
||||
owner_id: str
|
||||
filename: str
|
||||
content_type: str
|
||||
size_bytes: int
|
||||
sha256: str
|
||||
status: str = "ready"
|
||||
variants: list[dict] = Field(default_factory=list)
|
||||
error_code: str | None = None
|
||||
created_at: str
|
||||
updated_at: str
|
||||
|
||||
|
||||
class MessagePage(BaseModel):
|
||||
items: list[MessageOut]
|
||||
next_cursor: str | None
|
||||
|
||||
|
||||
class SessionOut(BaseModel):
|
||||
token: str
|
||||
token_type: str = "Bearer"
|
||||
expires_at: str
|
||||
user: UserOut
|
||||
|
||||
|
||||
class Health(BaseModel):
|
||||
status: str = "ok"
|
||||
|
||||
|
||||
class ErrorBody(BaseModel):
|
||||
error: dict[str, str]
|
||||
Reference in New Issue
Block a user