feat: ЛР 4 с хранением вложений в MinIO
This commit is contained in:
+49
-6
@@ -1,9 +1,9 @@
|
||||
from typing import Optional
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator
|
||||
|
||||
from ..domain.models import Chat, Message, Page, User
|
||||
from ..domain.models import Attachment, Chat, Message, Page, User
|
||||
from . import cursors
|
||||
from .timefmt import to_rfc3339
|
||||
|
||||
@@ -97,12 +97,21 @@ class ChatOut(Strict):
|
||||
|
||||
|
||||
class CreateMessage(Strict):
|
||||
text: str = Field(min_length=1, max_length=TEXT_MAX)
|
||||
text: str = Field(max_length=TEXT_MAX)
|
||||
attachment_ids: list[UUID] = Field(default_factory=list, max_length=10)
|
||||
|
||||
@field_validator('text')
|
||||
@field_validator('attachment_ids')
|
||||
@classmethod
|
||||
def _text(cls, value: str) -> str:
|
||||
return not_blank(value)
|
||||
def _attachments_unique(cls, value: list[UUID]) -> list[UUID]:
|
||||
if len(set(value)) != len(value):
|
||||
raise ValueError('attachment_ids не должен содержать дубликаты')
|
||||
return value
|
||||
|
||||
@model_validator(mode='after')
|
||||
def _content_present(self) -> 'CreateMessage':
|
||||
if not self.text.strip() and not self.attachment_ids:
|
||||
raise ValueError('сообщение должно содержать текст или вложение')
|
||||
return self
|
||||
|
||||
|
||||
class MessageOut(Strict):
|
||||
@@ -111,6 +120,7 @@ class MessageOut(Strict):
|
||||
sender_id: UUID
|
||||
text: str
|
||||
created_at: str
|
||||
attachment_ids: list[UUID]
|
||||
|
||||
@classmethod
|
||||
def from_domain(cls, message: Message) -> 'MessageOut':
|
||||
@@ -120,6 +130,39 @@ class MessageOut(Strict):
|
||||
sender_id=message.sender_id,
|
||||
text=message.text,
|
||||
created_at=to_rfc3339(message.created_at),
|
||||
attachment_ids=list(message.attachment_ids),
|
||||
)
|
||||
|
||||
|
||||
class AttachmentOut(Strict):
|
||||
id: UUID
|
||||
chat_id: UUID
|
||||
owner_id: UUID
|
||||
filename: str
|
||||
content_type: str
|
||||
size_bytes: int
|
||||
sha256: str
|
||||
status: str
|
||||
variants: list = Field(default_factory=list)
|
||||
error_code: str | None
|
||||
created_at: str
|
||||
updated_at: str
|
||||
|
||||
@classmethod
|
||||
def from_domain(cls, attachment: Attachment) -> 'AttachmentOut':
|
||||
return cls(
|
||||
id=attachment.id,
|
||||
chat_id=attachment.chat_id,
|
||||
owner_id=attachment.owner_id,
|
||||
filename=attachment.filename,
|
||||
content_type=attachment.content_type,
|
||||
size_bytes=attachment.size_bytes,
|
||||
sha256=attachment.sha256,
|
||||
status=attachment.status,
|
||||
variants=[],
|
||||
error_code=attachment.error_code,
|
||||
created_at=to_rfc3339(attachment.created_at),
|
||||
updated_at=to_rfc3339(attachment.updated_at),
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user