Files
core/orm/draft.py

80 lines
3.4 KiB
Python
Raw Normal View History

2025-02-09 17:18:01 +03:00
import time
2025-07-31 18:55:59 +03:00
from typing import Any
2025-02-09 17:18:01 +03:00
2025-07-31 18:55:59 +03:00
from sqlalchemy import JSON, Boolean, ForeignKey, Index, Integer, PrimaryKeyConstraint, String
from sqlalchemy.orm import Mapped, mapped_column, relationship
2025-02-09 17:18:01 +03:00
2025-05-16 09:23:48 +03:00
from auth.orm import Author
2025-07-25 01:04:15 +03:00
from orm.base import BaseModel as Base
2025-02-09 17:18:01 +03:00
from orm.topic import Topic
class DraftTopic(Base):
__tablename__ = "draft_topic"
2025-07-31 18:55:59 +03:00
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},
)
2025-02-09 17:18:01 +03:00
class DraftAuthor(Base):
__tablename__ = "draft_author"
2025-07-31 18:55:59 +03:00
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},
)
2025-02-09 17:18:01 +03:00
class Draft(Base):
__tablename__ = "draft"
2025-02-11 12:00:35 +03:00
# required
2025-07-31 18:55:59 +03:00
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)
2025-04-15 20:16:01 +03:00
2025-02-11 12:00:35 +03:00
# optional
2025-07-31 18:55:59 +03:00
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
2025-02-09 17:18:01 +03:00
2025-02-11 12:00:35 +03:00
# auto
2025-07-31 18:55:59 +03:00
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(Author, 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},
)