2024-10-21 08:08:16 +00:00
|
|
|
from sqlalchemy.ext.hybrid import hybrid_property
|
2024-10-21 08:29:57 +00:00
|
|
|
from sqlalchemy import ARRAY, Column, ForeignKey, Integer, String, func, Enum
|
2023-10-23 14:47:11 +00:00
|
|
|
from sqlalchemy.orm import relationship
|
2024-10-21 08:29:57 +00:00
|
|
|
import enum
|
|
|
|
import time
|
2023-10-23 14:47:11 +00:00
|
|
|
|
2024-02-20 09:53:15 +00:00
|
|
|
from orm.author import Author
|
2024-04-08 07:38:58 +00:00
|
|
|
from services.db import Base
|
2023-10-23 14:47:11 +00:00
|
|
|
|
2021-08-26 21:14:20 +00:00
|
|
|
|
2024-10-21 08:29:57 +00:00
|
|
|
class CommunityRole(enum.Enum):
|
|
|
|
AUTHOR = "author"
|
|
|
|
READER = "reader"
|
|
|
|
EDITOR = "editor"
|
|
|
|
CRITIC = "critic"
|
|
|
|
EXPERT = "expert"
|
|
|
|
ARTIST = "artist"
|
|
|
|
|
|
|
|
|
2024-06-05 14:45:55 +00:00
|
|
|
class CommunityFollower(Base):
|
2024-04-17 15:32:23 +00:00
|
|
|
__tablename__ = "community_author"
|
2022-06-12 07:51:22 +00:00
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
author = Column(ForeignKey("author.id"), primary_key=True)
|
|
|
|
community = Column(ForeignKey("community.id"), primary_key=True)
|
2023-11-03 10:10:22 +00:00
|
|
|
joined_at = Column(Integer, nullable=False, default=lambda: int(time.time()))
|
2024-10-21 08:29:57 +00:00
|
|
|
roles = Column(ARRAY(Enum(CommunityRole)), nullable=False, default=[CommunityRole.READER])
|
2022-06-12 07:51:22 +00:00
|
|
|
|
2021-08-26 21:14:20 +00:00
|
|
|
|
|
|
|
class Community(Base):
|
2024-04-17 15:32:23 +00:00
|
|
|
__tablename__ = "community"
|
2022-09-03 10:50:14 +00:00
|
|
|
|
2023-10-23 14:47:11 +00:00
|
|
|
name = Column(String, nullable=False)
|
2024-02-19 14:22:38 +00:00
|
|
|
slug = Column(String, nullable=False, unique=True)
|
2024-04-17 15:32:23 +00:00
|
|
|
desc = Column(String, nullable=False, default="")
|
|
|
|
pic = Column(String, nullable=False, default="")
|
2023-11-03 10:10:22 +00:00
|
|
|
created_at = Column(Integer, nullable=False, default=lambda: int(time.time()))
|
2023-10-23 14:47:11 +00:00
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
authors = relationship(Author, secondary="community_author")
|
2024-10-21 07:52:23 +00:00
|
|
|
|
2024-10-21 08:08:16 +00:00
|
|
|
@hybrid_property
|
|
|
|
def stat(self):
|
|
|
|
return CommunityStats(self)
|
2024-10-21 07:52:23 +00:00
|
|
|
|
|
|
|
|
2024-10-21 08:08:16 +00:00
|
|
|
class CommunityStats:
|
|
|
|
def __init__(self, community):
|
|
|
|
self.community = community
|
2024-10-21 07:52:23 +00:00
|
|
|
|
2024-10-21 08:08:16 +00:00
|
|
|
@property
|
|
|
|
def shouts(self):
|
|
|
|
from orm.shout import ShoutCommunity
|
2024-10-21 08:29:57 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
self.community.session.query(func.count(ShoutCommunity.shout_id))
|
|
|
|
.filter(ShoutCommunity.community_id == self.community.id)
|
2024-10-21 08:08:16 +00:00
|
|
|
.scalar()
|
2024-10-21 08:29:57 +00:00
|
|
|
)
|
2024-10-21 08:08:16 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def followers(self):
|
2024-10-21 08:29:57 +00:00
|
|
|
return (
|
|
|
|
self.community.session.query(func.count(CommunityFollower.author))
|
|
|
|
.filter(CommunityFollower.community == self.community.id)
|
2024-10-21 08:08:16 +00:00
|
|
|
.scalar()
|
2024-10-21 08:29:57 +00:00
|
|
|
)
|