Files
core/orm/community.py

107 lines
3.3 KiB
Python
Raw Normal View History

2024-10-21 11:29:57 +03:00
import enum
import time
2023-10-23 17:47:11 +03:00
2024-10-21 16:59:25 +03:00
from sqlalchemy import Column, ForeignKey, Integer, String, Text, distinct, func
2024-10-21 11:48:51 +03:00
from sqlalchemy.ext.hybrid import hybrid_property
2024-02-20 12:53:15 +03:00
from orm.author import Author
2024-04-08 10:38:58 +03:00
from services.db import Base
2023-10-23 17:47:11 +03:00
2021-08-27 00:14:20 +03:00
2024-10-21 11:29:57 +03:00
class CommunityRole(enum.Enum):
2024-10-21 16:59:25 +03:00
READER = "reader" # can read and comment
AUTHOR = "author" # + can vote and invite collaborators
ARTIST = "artist" # + can be credited as featured artist
EXPERT = "expert" # + can add proof or disproof to shouts, can manage topics
EDITOR = "editor" # + can manage topics, comments and community settings
2024-10-21 11:29:57 +03:00
2024-10-21 11:48:51 +03:00
@classmethod
def as_string_array(cls, roles):
return [role.value for role in roles]
2024-10-21 11:29:57 +03:00
2024-06-05 17:45:55 +03:00
class CommunityFollower(Base):
2024-04-17 18:32:23 +03:00
__tablename__ = "community_author"
2024-04-17 18:32:23 +03:00
author = Column(ForeignKey("author.id"), primary_key=True)
community = Column(ForeignKey("community.id"), primary_key=True)
2023-11-03 13:10:22 +03:00
joined_at = Column(Integer, nullable=False, default=lambda: int(time.time()))
2024-10-21 16:59:25 +03:00
roles = Column(Text, nullable=True, comment="Roles (comma-separated)")
2024-10-21 11:48:51 +03:00
def set_roles(self, roles):
self.roles = CommunityRole.as_string_array(roles)
def get_roles(self):
return [CommunityRole(role) for role in self.roles]
2021-08-27 00:14:20 +03:00
class Community(Base):
2024-04-17 18:32:23 +03:00
__tablename__ = "community"
2022-09-03 13:50:14 +03:00
2023-10-23 17:47:11 +03:00
name = Column(String, nullable=False)
2024-02-19 17:22:38 +03:00
slug = Column(String, nullable=False, unique=True)
2024-04-17 18:32:23 +03:00
desc = Column(String, nullable=False, default="")
pic = Column(String, nullable=False, default="")
2023-11-03 13:10:22 +03:00
created_at = Column(Integer, nullable=False, default=lambda: int(time.time()))
2024-10-21 16:42:30 +03:00
created_by = Column(ForeignKey("author.id"), nullable=False)
2024-10-21 10:52:23 +03:00
2024-10-21 11:08:16 +03:00
@hybrid_property
def stat(self):
return CommunityStats(self)
2024-10-21 10:52:23 +03:00
2024-10-21 16:59:25 +03:00
@property
def role_list(self):
return self.roles.split(",") if self.roles else []
@role_list.setter
def role_list(self, value):
self.roles = ",".join(value) if value else None
2024-10-21 10:52:23 +03:00
2024-10-21 11:08:16 +03:00
class CommunityStats:
def __init__(self, community):
self.community = community
2024-10-21 10:52:23 +03:00
2024-10-21 11:08:16 +03:00
@property
def shouts(self):
2024-10-21 16:59:25 +03:00
from orm.shout import Shout
return self.community.session.query(func.count(Shout.id)).filter(Shout.community == self.community.id).scalar()
2024-10-21 11:08:16 +03:00
@property
def followers(self):
2024-10-21 11:29:57 +03:00
return (
self.community.session.query(func.count(CommunityFollower.author))
.filter(CommunityFollower.community == self.community.id)
2024-10-21 11:08:16 +03:00
.scalar()
2024-10-21 11:29:57 +03:00
)
2024-10-21 16:42:30 +03:00
@property
def authors(self):
2024-10-21 16:59:25 +03:00
from orm.shout import Shout
2024-10-21 16:57:03 +03:00
# author has a shout with community id and its featured_at is not null
2024-10-21 16:42:30 +03:00
return (
self.community.session.query(func.count(distinct(Author.id)))
.join(Shout)
2024-10-21 16:59:25 +03:00
.filter(Shout.community == self.community.id, Shout.featured_at.is_not(None), Author.id.in_(Shout.authors))
2024-10-21 16:42:30 +03:00
.scalar()
)
2024-10-21 16:59:25 +03:00
class CommunityAuthor(Base):
__tablename__ = "community_author"
id = Column(Integer, primary_key=True)
community_id = Column(Integer, ForeignKey("community.id"))
author_id = Column(Integer, ForeignKey("author.id"))
roles = Column(Text, nullable=True, comment="Roles (comma-separated)")
@property
def role_list(self):
return self.roles.split(",") if self.roles else []
@role_list.setter
def role_list(self, value):
self.roles = ",".join(value) if value else None