core/orm/author.py

137 lines
5.6 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 08:47:19 +00:00
from sqlalchemy import JSON, Boolean, Column, ForeignKey, Index, Integer, String
2024-02-25 13:43:04 +00:00
2023-10-25 16:55:30 +00:00
from services.db import Base
2023-10-23 14:47:11 +00:00
2024-05-06 21:06:31 +00:00
# from sqlalchemy_utils import TSVectorType
2023-10-23 14:47:11 +00:00
class AuthorRating(Base):
2025-03-22 08:47:19 +00:00
"""
Рейтинг автора от другого автора.
Attributes:
rater (int): ID оценивающего автора
author (int): ID оцениваемого автора
plus (bool): Положительная/отрицательная оценка
"""
2024-04-17 15:32:23 +00:00
__tablename__ = "author_rating"
2023-10-23 14:47:11 +00:00
id = None # type: ignore
2024-04-17 15:32:23 +00:00
rater = Column(ForeignKey("author.id"), primary_key=True)
author = Column(ForeignKey("author.id"), primary_key=True)
2023-11-29 20:22:39 +00:00
plus = Column(Boolean)
2023-10-23 14:47:11 +00:00
2025-03-22 08:47:19 +00:00
# Определяем индексы
__table_args__ = (
# Индекс для быстрого поиска всех оценок конкретного автора
Index("idx_author_rating_author", "author"),
# Индекс для быстрого поиска всех оценок, оставленных конкретным автором
Index("idx_author_rating_rater", "rater"),
)
2023-10-23 14:47:11 +00:00
class AuthorFollower(Base):
2025-03-22 08:47:19 +00:00
"""
Подписка одного автора на другого.
Attributes:
follower (int): ID подписчика
author (int): ID автора, на которого подписываются
created_at (int): Время создания подписки
auto (bool): Признак автоматической подписки
"""
2024-04-17 15:32:23 +00:00
__tablename__ = "author_follower"
2023-10-23 14:47:11 +00:00
id = None # type: ignore
2024-04-17 15:32:23 +00:00
follower = Column(ForeignKey("author.id"), primary_key=True)
author = Column(ForeignKey("author.id"), primary_key=True)
2023-11-03 10:10:22 +00:00
created_at = Column(Integer, nullable=False, default=lambda: int(time.time()))
2023-10-23 14:47:11 +00:00
auto = Column(Boolean, nullable=False, default=False)
2025-03-22 08:47:19 +00:00
# Определяем индексы
__table_args__ = (
# Индекс для быстрого поиска всех подписчиков автора
Index("idx_author_follower_author", "author"),
# Индекс для быстрого поиска всех авторов, на которых подписан конкретный автор
Index("idx_author_follower_follower", "follower"),
)
2023-10-23 14:47:11 +00:00
2024-10-21 07:52:23 +00:00
class AuthorBookmark(Base):
2025-03-22 08:47:19 +00:00
"""
Закладка автора на публикацию.
Attributes:
author (int): ID автора
shout (int): ID публикации
"""
2024-10-21 07:52:23 +00:00
__tablename__ = "author_bookmark"
id = None # type: ignore
author = Column(ForeignKey("author.id"), primary_key=True)
shout = Column(ForeignKey("shout.id"), primary_key=True)
2025-03-22 08:47:19 +00:00
# Определяем индексы
__table_args__ = (
# Индекс для быстрого поиска всех закладок автора
Index("idx_author_bookmark_author", "author"),
# Индекс для быстрого поиска всех авторов, добавивших публикацию в закладки
Index("idx_author_bookmark_shout", "shout"),
)
2024-10-21 07:52:23 +00:00
2023-10-23 14:47:11 +00:00
class Author(Base):
2025-03-22 08:47:19 +00:00
"""
Модель автора в системе.
Attributes:
name (str): Отображаемое имя
slug (str): Уникальный строковый идентификатор
bio (str): Краткая биография/статус
about (str): Полное описание
pic (str): URL изображения профиля
links (dict): Ссылки на социальные сети и сайты
created_at (int): Время создания профиля
last_seen (int): Время последнего посещения
updated_at (int): Время последнего обновления
deleted_at (int): Время удаления (если профиль удален)
"""
2024-04-17 15:32:23 +00:00
__tablename__ = "author"
2023-10-23 14:47:11 +00:00
2024-04-17 15:32:23 +00:00
name = Column(String, nullable=True, comment="Display name")
2024-02-19 14:22:38 +00:00
slug = Column(String, unique=True, comment="Author's slug")
2024-04-17 15:32:23 +00:00
bio = Column(String, nullable=True, comment="Bio") # status description
about = Column(String, nullable=True, comment="About") # long and formatted
pic = Column(String, nullable=True, comment="Picture")
links = Column(JSON, nullable=True, comment="Links")
2023-11-03 10:10:22 +00:00
created_at = Column(Integer, nullable=False, default=lambda: int(time.time()))
last_seen = Column(Integer, nullable=False, default=lambda: int(time.time()))
updated_at = Column(Integer, nullable=False, default=lambda: int(time.time()))
2024-04-17 15:32:23 +00:00
deleted_at = Column(Integer, nullable=True, comment="Deleted at")
2024-02-25 13:43:04 +00:00
2024-04-26 22:43:42 +00:00
# search_vector = Column(
# TSVectorType("name", "slug", "bio", "about", regconfig="pg_catalog.russian")
# )
2025-03-22 08:47:19 +00:00
# Определяем индексы
__table_args__ = (
2025-05-16 06:11:39 +00:00
# Индекс для быстрого поиска по имени
Index("idx_author_name", "name"),
2025-03-22 08:47:19 +00:00
# Индекс для быстрого поиска по slug
Index("idx_author_slug", "slug"),
# Индекс для фильтрации неудаленных авторов
2025-05-16 06:11:39 +00:00
Index(
"idx_author_deleted_at", "deleted_at", postgresql_where=deleted_at.is_(None)
),
2025-03-22 08:47:19 +00:00
# Индекс для сортировки по времени создания (для новых авторов)
Index("idx_author_created_at", "created_at"),
# Индекс для сортировки по времени последнего посещения
Index("idx_author_last_seen", "last_seen"),
)