core/orm/shout.py

155 lines
5.7 KiB
Python
Raw Normal View History

2023-11-03 10:10:22 +00:00
import time
2023-11-22 16:38:39 +00:00
2025-03-22 06:31:53 +00:00
from sqlalchemy import JSON, Boolean, Column, ForeignKey, Index, Integer, String
2023-11-22 16:38:39 +00:00
from sqlalchemy.orm import relationship
2025-05-16 06:23:48 +00:00
from auth.orm import Author
2024-02-20 09:53:15 +00:00
from orm.reaction import Reaction
from orm.topic import Topic
2024-04-08 07:38:58 +00:00
from services.db import Base
2022-09-19 13:50:43 +00:00
class ShoutTopic(Base):
2025-03-22 06:31:53 +00:00
"""
Связь между публикацией и темой.
Attributes:
shout (int): ID публикации
topic (int): ID темы
main (bool): Признак основной темы
"""
2024-04-17 15:32:23 +00:00
__tablename__ = "shout_topic"
2022-09-19 13:50:43 +00:00
id = None # type: ignore
2024-08-07 08:35:59 +00:00
shout = Column(ForeignKey("shout.id"), primary_key=True, index=True)
topic = Column(ForeignKey("topic.id"), primary_key=True, index=True)
2023-12-09 17:15:57 +00:00
main = Column(Boolean, nullable=True)
2025-03-22 06:31:53 +00:00
# Определяем дополнительные индексы
__table_args__ = (
# Оптимизированный составной индекс для запросов, которые ищут публикации по теме
Index("idx_shout_topic_topic_shout", "topic", "shout"),
)
2021-09-24 14:39:37 +00:00
class ShoutReactionsFollower(Base):
2024-04-17 15:32:23 +00:00
__tablename__ = "shout_reactions_followers"
2022-09-03 10:50:14 +00:00
id = None # type: ignore
2024-08-07 08:35:59 +00:00
follower = Column(ForeignKey("author.id"), primary_key=True, index=True)
shout = Column(ForeignKey("shout.id"), primary_key=True, index=True)
2022-09-03 10:50:14 +00:00
auto = Column(Boolean, nullable=False, default=False)
2023-11-03 10:10:22 +00:00
created_at = Column(Integer, nullable=False, default=lambda: int(time.time()))
deleted_at = Column(Integer, nullable=True)
2022-09-03 10:50:14 +00:00
2022-06-21 12:21:02 +00:00
2021-08-28 10:13:50 +00:00
class ShoutAuthor(Base):
2025-03-22 06:31:53 +00:00
"""
Связь между публикацией и автором.
Attributes:
shout (int): ID публикации
author (int): ID автора
caption (str): Подпись автора
"""
2024-04-17 15:32:23 +00:00
__tablename__ = "shout_author"
2022-09-03 10:50:14 +00:00
id = None # type: ignore
2024-08-07 08:35:59 +00:00
shout = Column(ForeignKey("shout.id"), primary_key=True, index=True)
author = Column(ForeignKey("author.id"), primary_key=True, index=True)
2024-04-17 15:32:23 +00:00
caption = Column(String, nullable=True, default="")
2022-09-03 10:50:14 +00:00
2025-03-22 06:31:53 +00:00
# Определяем дополнительные индексы
__table_args__ = (
# Оптимизированный индекс для запросов, которые ищут публикации по автору
Index("idx_shout_author_author_shout", "author", "shout"),
)
2022-09-03 10:50:14 +00:00
class Shout(Base):
2025-03-22 06:31:53 +00:00
"""
Публикация в системе.
Attributes:
body (str)
slug (str)
cover (str) : "Cover image url"
cover_caption (str) : "Cover image alt caption"
2025-05-29 09:37:39 +00:00
lead (str)
title (str)
subtitle (str)
layout (str)
media (dict)
authors (list[Author])
topics (list[Topic])
reactions (list[Reaction])
lang (str)
version_of (int)
oid (str)
seo (str) : JSON
draft (int)
created_at (int)
updated_at (int)
published_at (int)
featured_at (int)
deleted_at (int)
created_by (int)
updated_by (int)
deleted_by (int)
community (int)
2025-03-22 06:31:53 +00:00
"""
2024-04-17 15:32:23 +00:00
__tablename__ = "shout"
2022-09-03 10:50:14 +00:00
2025-01-25 08:23:20 +00:00
created_at: int = Column(Integer, nullable=False, default=lambda: int(time.time()))
updated_at: int | None = Column(Integer, nullable=True, index=True)
published_at: int | None = Column(Integer, nullable=True, index=True)
featured_at: int | None = Column(Integer, nullable=True, index=True)
deleted_at: int | None = Column(Integer, nullable=True, index=True)
created_by: int = Column(ForeignKey("author.id"), nullable=False)
updated_by: int | None = Column(ForeignKey("author.id"), nullable=True)
deleted_by: int | None = Column(ForeignKey("author.id"), nullable=True)
community: int = Column(ForeignKey("community.id"), nullable=False)
body: str = Column(String, nullable=False, comment="Body")
slug: str = Column(String, unique=True)
cover: str | None = Column(String, nullable=True, comment="Cover image url")
cover_caption: str | None = Column(String, nullable=True, comment="Cover image alt caption")
lead: str | None = Column(String, nullable=True)
title: str = Column(String, nullable=False)
subtitle: str | None = Column(String, nullable=True)
layout: str = Column(String, nullable=False, default="article")
media: dict | None = Column(JSON, nullable=True)
2023-10-23 14:47:11 +00:00
2024-04-17 15:32:23 +00:00
authors = relationship(Author, secondary="shout_author")
topics = relationship(Topic, secondary="shout_topic")
2024-02-20 09:53:15 +00:00
reactions = relationship(Reaction)
2023-02-17 14:30:38 +00:00
2025-01-25 08:23:20 +00:00
lang: str = Column(String, nullable=False, default="ru", comment="Language")
version_of: int | None = Column(ForeignKey("shout.id"), nullable=True)
oid: str | None = Column(String, nullable=True)
2023-12-17 05:08:35 +00:00
2025-01-25 08:23:20 +00:00
seo: str | None = Column(String, nullable=True) # JSON
2025-02-09 14:18:01 +00:00
draft: int | None = Column(ForeignKey("draft.id"), nullable=True)
2025-03-22 06:31:53 +00:00
# Определяем индексы
__table_args__ = (
# Индекс для быстрого поиска неудаленных публикаций
Index("idx_shout_deleted_at", "deleted_at", postgresql_where=deleted_at.is_(None)),
# Индекс для быстрой фильтрации по community
Index("idx_shout_community", "community"),
# Индекс для быстрого поиска по slug
Index("idx_shout_slug", "slug"),
# Составной индекс для фильтрации опубликованных неудаленных публикаций
Index(
"idx_shout_published_deleted",
"published_at",
"deleted_at",
postgresql_where=published_at.is_not(None) & deleted_at.is_(None),
),
)