Files
core/orm/shout.py

97 lines
3.5 KiB
Python
Raw Normal View History

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