Files
core/orm/draft.py
Untone 9a2b792f08
Some checks failed
Deploy on push / deploy (push) Failing after 6s
refactored
2025-08-17 17:56:31 +03:00

86 lines
3.7 KiB
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.
import time
from typing import Any
from sqlalchemy import JSON, Boolean, ForeignKey, Index, Integer, PrimaryKeyConstraint, String
from sqlalchemy.orm import Mapped, mapped_column, relationship
from auth.orm import Author
from orm.base import BaseModel as Base
from orm.topic import Topic
# Author уже импортирован в начале файла
def get_author_model():
"""Возвращает модель Author для использования в запросах"""
return Author
class DraftTopic(Base):
__tablename__ = "draft_topic"
draft: Mapped[int] = mapped_column(ForeignKey("draft.id"), index=True)
topic: Mapped[int] = mapped_column(ForeignKey("topic.id"), index=True)
main: Mapped[bool | None] = mapped_column(Boolean, nullable=True)
__table_args__ = (
PrimaryKeyConstraint(draft, topic),
Index("idx_draft_topic_topic", "topic"),
Index("idx_draft_topic_draft", "draft"),
{"extend_existing": True},
)
class DraftAuthor(Base):
__tablename__ = "draft_author"
draft: Mapped[int] = mapped_column(ForeignKey("draft.id"), index=True)
author: Mapped[int] = mapped_column(ForeignKey("author.id"), index=True)
caption: Mapped[str | None] = mapped_column(String, nullable=True, default="")
__table_args__ = (
PrimaryKeyConstraint(draft, author),
Index("idx_draft_author_author", "author"),
Index("idx_draft_author_draft", "draft"),
{"extend_existing": True},
)
class Draft(Base):
__tablename__ = "draft"
# required
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
created_at: Mapped[int] = mapped_column(Integer, nullable=False, default=lambda: int(time.time()))
created_by: Mapped[int] = mapped_column(ForeignKey("author.id"), nullable=False)
community: Mapped[int] = mapped_column(ForeignKey("community.id"), nullable=False, default=1)
# optional
layout: Mapped[str | None] = mapped_column(String, nullable=True, default="article")
slug: Mapped[str | None] = mapped_column(String, unique=True)
title: Mapped[str | None] = mapped_column(String, nullable=True)
subtitle: Mapped[str | None] = mapped_column(String, nullable=True)
lead: Mapped[str | None] = mapped_column(String, nullable=True)
body: Mapped[str] = mapped_column(String, nullable=False, comment="Body")
media: Mapped[dict[str, Any] | None] = mapped_column(JSON, nullable=True)
cover: Mapped[str | None] = mapped_column(String, nullable=True, comment="Cover image url")
cover_caption: Mapped[str | None] = mapped_column(String, nullable=True, comment="Cover image alt caption")
lang: Mapped[str] = mapped_column(String, nullable=False, default="ru", comment="Language")
seo: Mapped[str | None] = mapped_column(String, nullable=True) # JSON
# auto
updated_at: Mapped[int | None] = mapped_column(Integer, nullable=True, index=True)
deleted_at: Mapped[int | None] = mapped_column(Integer, nullable=True, index=True)
updated_by: Mapped[int | None] = mapped_column(ForeignKey("author.id"), nullable=True)
deleted_by: Mapped[int | None] = mapped_column(ForeignKey("author.id"), nullable=True)
authors = relationship(get_author_model(), secondary=DraftAuthor.__table__)
topics = relationship(Topic, secondary=DraftTopic.__table__)
# shout/publication
# Временно закомментировано для совместимости с тестами
# shout: Mapped[int | None] = mapped_column(ForeignKey("shout.id"), nullable=True)
__table_args__ = (
Index("idx_draft_created_by", "created_by"),
Index("idx_draft_community", "community"),
{"extend_existing": True},
)