core/orm/shout.py

78 lines
2.8 KiB
Python
Raw Normal View History

from datetime import datetime
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime, Boolean
2021-08-20 08:08:32 +00:00
from sqlalchemy.orm import relationship
from base.orm import Base
from orm.reaction import Reaction
from orm.topic import Topic, ShoutTopic
from orm.user import User
2022-09-03 10:50:14 +00:00
from services.stat.reacted import ReactedStorage
2022-08-11 09:09:57 +00:00
from services.stat.viewed import ViewedStorage
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
follower = Column(ForeignKey("user.slug"), primary_key=True)
shout = Column(ForeignKey("shout.slug"), primary_key=True)
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
shout = Column(ForeignKey("shout.slug"), primary_key=True)
user = Column(ForeignKey("user.slug"), primary_key=True)
caption = Column(String, nullable=True, default="")
class ShoutAllowed(Base):
__tablename__ = "shout_allowed"
2021-08-20 08:08:32 +00:00
2022-09-03 10:50:14 +00:00
id = None # type: ignore
shout = Column(ForeignKey("shout.slug"), primary_key=True)
user = Column(ForeignKey("user.id"), primary_key=True)
2021-08-25 21:20:53 +00:00
2022-09-03 10:50:14 +00:00
class Shout(Base):
__tablename__ = "shout"
id = None # type: ignore
slug = Column(String, primary_key=True)
2022-09-18 14:29:21 +00:00
community = Column(Integer, ForeignKey("community.id"), nullable=False, comment="Community")
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)
2022-09-18 14:29:21 +00:00
mainTopic = Column(ForeignKey("topic.slug"), nullable=True)
cover = Column(String, nullable=True)
authors = relationship(lambda: User, secondary=ShoutAuthor.__tablename__)
topics = relationship(lambda: Topic, secondary=ShoutTopic.__tablename__)
2022-09-18 14:29:21 +00:00
reactions = relationship(lambda: Reaction)
visibleFor = relationship(lambda: User, secondary=ShoutAllowed.__tablename__)
2022-09-18 14:29:21 +00:00
createdAt = Column(DateTime, nullable=False, default=datetime.now, comment="Created at")
updatedAt = Column(DateTime, nullable=True, comment="Updated at")
publishedAt = Column(DateTime, nullable=True)
versionOf = Column(ForeignKey("shout.slug"), nullable=True)
draft = Column(Boolean, default=False)
lang = Column(String, default='ru')
2022-09-03 10:50:14 +00:00
oid = Column(String, nullable=True)
2021-12-17 10:22:31 +00:00
@property
async def stat(self):
return {
2022-08-17 07:59:17 +00:00
"viewed": await ViewedStorage.get_shout(self.slug),
"reacted": len(await ReactedStorage.get_shout(self.slug)),
"commented": len(await ReactedStorage.get_comments(self.slug)),
2022-09-03 10:50:14 +00:00
"rating": await ReactedStorage.get_rating(self.slug),
}