2025-02-09 14:18:01 +00:00
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
from sqlalchemy import JSON, Boolean, Column, ForeignKey, Integer, String
|
|
|
|
|
from sqlalchemy.orm import relationship
|
|
|
|
|
|
|
|
|
|
from orm.author import Author
|
|
|
|
|
from orm.topic import Topic
|
|
|
|
|
from services.db import Base
|
2025-04-28 08:10:18 +00:00
|
|
|
|
from orm.shout import Shout
|
2025-02-09 14:18:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DraftTopic(Base):
|
|
|
|
|
__tablename__ = "draft_topic"
|
|
|
|
|
|
|
|
|
|
id = None # type: ignore
|
|
|
|
|
shout = Column(ForeignKey("draft.id"), primary_key=True, index=True)
|
|
|
|
|
topic = Column(ForeignKey("topic.id"), primary_key=True, index=True)
|
|
|
|
|
main = Column(Boolean, nullable=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DraftAuthor(Base):
|
|
|
|
|
__tablename__ = "draft_author"
|
|
|
|
|
|
|
|
|
|
id = None # type: ignore
|
|
|
|
|
shout = Column(ForeignKey("draft.id"), primary_key=True, index=True)
|
|
|
|
|
author = Column(ForeignKey("author.id"), primary_key=True, index=True)
|
|
|
|
|
caption = Column(String, nullable=True, default="")
|
|
|
|
|
|
|
|
|
|
|
2025-04-26 10:11:12 +00:00
|
|
|
|
|
2025-02-09 14:18:01 +00:00
|
|
|
|
class Draft(Base):
|
|
|
|
|
__tablename__ = "draft"
|
2025-02-11 09:00:35 +00:00
|
|
|
|
# required
|
2025-02-09 14:18:01 +00:00
|
|
|
|
created_at: int = Column(Integer, nullable=False, default=lambda: int(time.time()))
|
2025-04-26 12:50:20 +00:00
|
|
|
|
# Колонки для связей с автором
|
2025-04-26 10:11:12 +00:00
|
|
|
|
created_by: int = Column("created_by", ForeignKey("author.id"), nullable=False)
|
|
|
|
|
community: int = Column("community", ForeignKey("community.id"), nullable=False, default=1)
|
2025-04-15 17:16:01 +00:00
|
|
|
|
|
2025-02-11 09:00:35 +00:00
|
|
|
|
# optional
|
|
|
|
|
layout: str = Column(String, nullable=True, default="article")
|
2025-02-09 14:18:01 +00:00
|
|
|
|
slug: str = Column(String, unique=True)
|
2025-02-11 09:00:35 +00:00
|
|
|
|
title: str = Column(String, nullable=True)
|
|
|
|
|
subtitle: str | None = Column(String, nullable=True)
|
2025-02-09 14:18:01 +00:00
|
|
|
|
lead: str | None = Column(String, nullable=True)
|
2025-02-11 09:00:35 +00:00
|
|
|
|
body: str = Column(String, nullable=False, comment="Body")
|
2025-02-09 14:18:01 +00:00
|
|
|
|
media: dict | None = Column(JSON, nullable=True)
|
2025-02-11 09:00:35 +00:00
|
|
|
|
cover: str | None = Column(String, nullable=True, comment="Cover image url")
|
|
|
|
|
cover_caption: str | None = Column(String, nullable=True, comment="Cover image alt caption")
|
2025-02-09 14:18:01 +00:00
|
|
|
|
lang: str = Column(String, nullable=False, default="ru", comment="Language")
|
|
|
|
|
seo: str | None = Column(String, nullable=True) # JSON
|
|
|
|
|
|
2025-02-11 09:00:35 +00:00
|
|
|
|
# auto
|
|
|
|
|
updated_at: int | None = Column(Integer, nullable=True, index=True)
|
|
|
|
|
deleted_at: int | None = Column(Integer, nullable=True, index=True)
|
2025-04-26 10:11:12 +00:00
|
|
|
|
updated_by: int | None = Column("updated_by", ForeignKey("author.id"), nullable=True)
|
|
|
|
|
deleted_by: int | None = Column("deleted_by", ForeignKey("author.id"), nullable=True)
|
|
|
|
|
|
|
|
|
|
# --- Relationships ---
|
2025-04-26 12:50:20 +00:00
|
|
|
|
# Только many-to-many связи через вспомогательные таблицы
|
2025-04-26 10:11:12 +00:00
|
|
|
|
authors = relationship(Author, secondary="draft_author", lazy="select")
|
|
|
|
|
topics = relationship(Topic, secondary="draft_topic", lazy="select")
|
|
|
|
|
|
|
|
|
|
# Связь с Community (если нужна как объект, а не ID)
|
|
|
|
|
# community = relationship("Community", foreign_keys=[community_id], lazy="joined")
|
2025-04-28 08:10:18 +00:00
|
|
|
|
# Пока оставляем community_id как ID
|
|
|
|
|
|
|
|
|
|
# Связь с публикацией (один-к-одному или один-к-нулю)
|
|
|
|
|
# Загружается через joinedload в резолвере
|
|
|
|
|
publication = relationship(
|
|
|
|
|
"Shout",
|
|
|
|
|
primaryjoin="Draft.id == Shout.draft",
|
|
|
|
|
foreign_keys="Shout.draft",
|
|
|
|
|
uselist=False,
|
|
|
|
|
lazy="noload", # Не грузим по умолчанию, только через options
|
|
|
|
|
viewonly=True # Указываем, что это связь только для чтения
|
2025-05-07 07:37:18 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def dict(self):
|
|
|
|
|
"""
|
|
|
|
|
Сериализует объект Draft в словарь.
|
|
|
|
|
Гарантирует, что поля topics и authors всегда будут списками.
|
|
|
|
|
"""
|
|
|
|
|
return {
|
|
|
|
|
"id": self.id,
|
|
|
|
|
"created_at": self.created_at,
|
|
|
|
|
"created_by": self.created_by,
|
|
|
|
|
"community": self.community,
|
|
|
|
|
"layout": self.layout,
|
|
|
|
|
"slug": self.slug,
|
|
|
|
|
"title": self.title,
|
|
|
|
|
"subtitle": self.subtitle,
|
|
|
|
|
"lead": self.lead,
|
|
|
|
|
"body": self.body,
|
|
|
|
|
"media": self.media or [],
|
|
|
|
|
"cover": self.cover,
|
|
|
|
|
"cover_caption": self.cover_caption,
|
|
|
|
|
"lang": self.lang,
|
|
|
|
|
"seo": self.seo,
|
|
|
|
|
"updated_at": self.updated_at,
|
|
|
|
|
"deleted_at": self.deleted_at,
|
|
|
|
|
"updated_by": self.updated_by,
|
|
|
|
|
"deleted_by": self.deleted_by,
|
|
|
|
|
# Гарантируем, что topics и authors всегда будут списками
|
|
|
|
|
"topics": [topic.dict() for topic in (self.topics or [])],
|
|
|
|
|
"authors": [author.dict() for author in (self.authors or [])]
|
|
|
|
|
}
|