Files
mtusi2026lab7/src/app/serializers.py
T
2026-12-04 19:16:26 +03:00

35 lines
903 B
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
def iso_z(dt: datetime) -> str:
"""RFC3339 UTC с 'Z'. Postgres отдаёт timestamptz с '+00:00'."""
return dt.isoformat().replace("+00:00", "Z")
def row_to_user(row) -> dict:
return {
"id": str(row["id"]),
"username": row["username"],
"display_name": row["display_name"],
"created_at": iso_z(row["created_at"]),
}
def row_to_chat(row) -> dict:
return {
"id": str(row["id"]),
"title": row["title"],
"member_ids": row["member_ids"],
"created_at": iso_z(row["created_at"]),
}
def row_to_message(row) -> dict:
return {
"id": str(row["id"]),
"chat_id": str(row["chat_id"]),
"sender_id": str(row["sender_id"]),
"text": row["text"],
"created_at": iso_z(row["created_at"]),
"attachment_ids": row.get("attachment_ids", []),
}