core/orm/shout.py

88 lines
3.1 KiB
Python
Raw Normal View History

from datetime import datetime
from sqlalchemy import Boolean, Column, DateTime, ForeignKey, String, JSON
from sqlalchemy.orm import relationship
2022-11-19 11:35:34 +00:00
from base.orm import Base, local_session
from orm.reaction import Reaction
2022-09-19 13:50:43 +00:00
from orm.topic import Topic
from orm.user import User
2022-09-19 13:50:43 +00:00
class ShoutTopic(Base):
__tablename__ = "shout_topic"
id = None # type: ignore
2022-11-30 06:27:12 +00:00
shout = Column(ForeignKey("shout.id"), primary_key=True, index=True)
topic = Column(ForeignKey("topic.id"), primary_key=True, index=True)
2021-09-24 14:39:37 +00:00
class ShoutReactionsFollower(Base):
__tablename__ = "shout_reactions_followers"
2022-09-03 10:50:14 +00:00
id = None # type: ignore
2022-11-30 06:27:12 +00:00
follower = Column(ForeignKey("user.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)
2022-09-04 17:20:38 +00:00
createdAt = Column(
DateTime, nullable=False, default=datetime.now, comment="Created at"
)
2022-09-03 10:50:14 +00:00
deletedAt = Column(DateTime, nullable=True)
2022-06-21 12:21:02 +00:00
2021-08-28 10:13:50 +00:00
class ShoutAuthor(Base):
__tablename__ = "shout_author"
2022-09-03 10:50:14 +00:00
id = None # type: ignore
2022-11-30 06:27:12 +00:00
shout = Column(ForeignKey("shout.id"), primary_key=True, index=True)
user = Column(ForeignKey("user.id"), primary_key=True, index=True)
2022-09-03 10:50:14 +00:00
caption = Column(String, nullable=True, default="")
class Shout(Base):
__tablename__ = "shout"
2023-02-17 14:30:38 +00:00
# timestamps
createdAt = Column(DateTime, nullable=False, default=datetime.now, comment="Created at")
updatedAt = Column(DateTime, nullable=True, comment="Updated at")
publishedAt = Column(DateTime, nullable=True)
deletedAt = Column(DateTime, nullable=True)
createdBy = Column(ForeignKey("user.id"), comment="Created By")
deletedBy = Column(ForeignKey("user.id"), nullable=True)
2022-11-10 05:40:32 +00:00
slug = Column(String, unique=True)
cover = Column(String, nullable=True, comment="Cover image url")
2022-09-03 10:50:14 +00:00
body = Column(String, nullable=False, comment="Body")
title = Column(String, nullable=True)
subtitle = Column(String, nullable=True)
layout = Column(String, nullable=True)
2023-02-17 14:30:38 +00:00
media = Column(JSON, nullable=True)
authors = relationship(lambda: User, secondary=ShoutAuthor.__tablename__)
topics = relationship(lambda: Topic, secondary=ShoutTopic.__tablename__)
2023-02-17 14:30:38 +00:00
2022-09-18 14:29:21 +00:00
reactions = relationship(lambda: Reaction)
2023-02-17 14:30:38 +00:00
# TODO: these field should be used or modified
community = Column(ForeignKey("community.id"), default=1)
lang = Column(String, nullable=False, default='ru', comment="Language")
mainTopic = Column(ForeignKey("topic.slug"), nullable=True)
2022-11-13 04:07:52 +00:00
visibility = Column(String, nullable=True) # owner authors community public
2022-11-29 12:36:46 +00:00
versionOf = Column(ForeignKey("shout.id"), nullable=True)
2022-11-13 04:07:52 +00:00
oid = Column(String, nullable=True)
2022-11-19 11:35:34 +00:00
@staticmethod
def init_table():
with local_session() as session:
2022-11-19 12:02:45 +00:00
s = session.query(Shout).first()
if not s:
entry = {
"slug": "genesis-block",
"body": "",
"title": "Ничего",
"lang": "ru"
}
s = Shout.create(**entry)
session.add(s)
session.commit()