core/orm/topic.py

67 lines
2.5 KiB
Python
Raw Permalink 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
2023-10-23 14:51:13 +00:00
from services.db import Base
2024-02-21 07:27:16 +00:00
2022-09-03 10:50:14 +00:00
class TopicFollower(Base):
2025-03-22 06:31:53 +00:00
"""
Связь между топиком и его подписчиком.
Attributes:
follower (int): ID подписчика
topic (int): ID топика
created_at (int): Время создания связи
auto (bool): Автоматическая подписка
"""
2024-04-17 15:32:23 +00:00
__tablename__ = "topic_followers"
2021-08-20 09:27:19 +00:00
2022-09-03 10:50:14 +00:00
id = None # type: ignore
2024-04-17 15:32:23 +00:00
follower = Column(Integer, ForeignKey("author.id"), primary_key=True)
topic = Column(Integer, ForeignKey("topic.id"), primary_key=True)
2024-02-23 18:22:55 +00:00
created_at = Column(Integer, nullable=False, default=int(time.time()))
2022-09-18 14:29:21 +00:00
auto = Column(Boolean, nullable=False, default=False)
2021-08-20 08:08:32 +00:00
2025-03-22 06:31:53 +00:00
# Определяем индексы
__table_args__ = (
# Индекс для быстрого поиска всех подписчиков топика
Index("idx_topic_followers_topic", "topic"),
# Индекс для быстрого поиска всех топиков, на которые подписан автор
Index("idx_topic_followers_follower", "follower"),
)
2024-02-24 10:22:35 +00:00
2022-09-03 10:50:14 +00:00
class Topic(Base):
2025-03-22 06:31:53 +00:00
"""
Модель топика (темы) публикаций.
Attributes:
slug (str): Уникальный строковый идентификатор темы
title (str): Название темы
body (str): Описание темы
pic (str): URL изображения темы
community (int): ID сообщества
oid (str): Старый ID
parent_ids (list): IDs родительских тем
"""
2024-04-17 15:32:23 +00:00
__tablename__ = "topic"
2021-12-12 13:00:38 +00:00
2024-02-19 14:22:38 +00:00
slug = Column(String, unique=True)
2024-04-17 15:32:23 +00:00
title = Column(String, nullable=False, comment="Title")
body = Column(String, nullable=True, comment="Body")
pic = Column(String, nullable=True, comment="Picture")
community = Column(ForeignKey("community.id"), default=1)
oid = Column(String, nullable=True, comment="Old ID")
2024-10-31 16:48:06 +00:00
parent_ids = Column(JSON, nullable=True, comment="Parent Topic IDs")
2025-03-22 06:31:53 +00:00
# Определяем индексы
__table_args__ = (
# Индекс для быстрого поиска по slug
Index("idx_topic_slug", "slug"),
# Индекс для быстрого поиска по сообществу
Index("idx_topic_community", "community"),
)