This commit is contained in:
parent
724f901bbd
commit
1476d4262d
|
@ -1,21 +1,19 @@
|
||||||
import enum
|
import enum
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from sqlalchemy import ARRAY, Column, ForeignKey, Integer, String, distinct, func
|
from sqlalchemy import Column, ForeignKey, Integer, String, Text, distinct, func
|
||||||
from sqlalchemy.ext.hybrid import hybrid_property
|
from sqlalchemy.ext.hybrid import hybrid_property
|
||||||
|
|
||||||
from orm.author import Author
|
from orm.author import Author
|
||||||
from orm.shout import Shout
|
|
||||||
from services.db import Base
|
from services.db import Base
|
||||||
|
|
||||||
|
|
||||||
class CommunityRole(enum.Enum):
|
class CommunityRole(enum.Enum):
|
||||||
AUTHOR = "author"
|
READER = "reader" # can read and comment
|
||||||
READER = "reader"
|
AUTHOR = "author" # + can vote and invite collaborators
|
||||||
EDITOR = "editor"
|
ARTIST = "artist" # + can be credited as featured artist
|
||||||
CRITIC = "critic"
|
EXPERT = "expert" # + can add proof or disproof to shouts, can manage topics
|
||||||
EXPERT = "expert"
|
EDITOR = "editor" # + can manage topics, comments and community settings
|
||||||
ARTIST = "artist"
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def as_string_array(cls, roles):
|
def as_string_array(cls, roles):
|
||||||
|
@ -28,7 +26,7 @@ class CommunityFollower(Base):
|
||||||
author = Column(ForeignKey("author.id"), primary_key=True)
|
author = Column(ForeignKey("author.id"), primary_key=True)
|
||||||
community = Column(ForeignKey("community.id"), primary_key=True)
|
community = Column(ForeignKey("community.id"), primary_key=True)
|
||||||
joined_at = Column(Integer, nullable=False, default=lambda: int(time.time()))
|
joined_at = Column(Integer, nullable=False, default=lambda: int(time.time()))
|
||||||
roles = Column(ARRAY(String), nullable=False, default=lambda: CommunityRole.as_string_array([CommunityRole.READER]))
|
roles = Column(Text, nullable=True, comment="Roles (comma-separated)")
|
||||||
|
|
||||||
def set_roles(self, roles):
|
def set_roles(self, roles):
|
||||||
self.roles = CommunityRole.as_string_array(roles)
|
self.roles = CommunityRole.as_string_array(roles)
|
||||||
|
@ -51,6 +49,14 @@ class Community(Base):
|
||||||
def stat(self):
|
def stat(self):
|
||||||
return CommunityStats(self)
|
return CommunityStats(self)
|
||||||
|
|
||||||
|
@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
|
||||||
|
|
||||||
|
|
||||||
class CommunityStats:
|
class CommunityStats:
|
||||||
def __init__(self, community):
|
def __init__(self, community):
|
||||||
|
@ -58,11 +64,9 @@ class CommunityStats:
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def shouts(self):
|
def shouts(self):
|
||||||
return (
|
from orm.shout import Shout
|
||||||
self.community.session.query(func.count(Shout.id))
|
|
||||||
.filter(Shout.community == self.community.id)
|
return self.community.session.query(func.count(Shout.id)).filter(Shout.community == self.community.id).scalar()
|
||||||
.scalar()
|
|
||||||
)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def followers(self):
|
def followers(self):
|
||||||
|
@ -74,12 +78,29 @@ class CommunityStats:
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def authors(self):
|
def authors(self):
|
||||||
|
from orm.shout import Shout
|
||||||
|
|
||||||
# author has a shout with community id and its featured_at is not null
|
# author has a shout with community id and its featured_at is not null
|
||||||
return (
|
return (
|
||||||
self.community.session.query(func.count(distinct(Author.id)))
|
self.community.session.query(func.count(distinct(Author.id)))
|
||||||
.join(Shout)
|
.join(Shout)
|
||||||
.filter(
|
.filter(Shout.community == self.community.id, Shout.featured_at.is_not(None), Author.id.in_(Shout.authors))
|
||||||
Shout.community == self.community.id, Shout.featured_at.is_not(None), Author.id.in_(Shout.authors)
|
|
||||||
)
|
|
||||||
.scalar()
|
.scalar()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
|
11
orm/shout.py
11
orm/shout.py
|
@ -4,7 +4,6 @@ from sqlalchemy import JSON, Boolean, Column, ForeignKey, Integer, String
|
||||||
from sqlalchemy.orm import relationship
|
from sqlalchemy.orm import relationship
|
||||||
|
|
||||||
from orm.author import Author
|
from orm.author import Author
|
||||||
from orm.community import Community
|
|
||||||
from orm.reaction import Reaction
|
from orm.reaction import Reaction
|
||||||
from orm.topic import Topic
|
from orm.topic import Topic
|
||||||
from services.db import Base
|
from services.db import Base
|
||||||
|
@ -39,14 +38,6 @@ class ShoutAuthor(Base):
|
||||||
caption = Column(String, nullable=True, default="")
|
caption = Column(String, nullable=True, default="")
|
||||||
|
|
||||||
|
|
||||||
class ShoutCommunity(Base):
|
|
||||||
__tablename__ = "shout_community"
|
|
||||||
|
|
||||||
id = None # type: ignore
|
|
||||||
shout = Column(ForeignKey("shout.id"), primary_key=True, index=True)
|
|
||||||
community = Column(ForeignKey("community.id"), primary_key=True, index=True)
|
|
||||||
|
|
||||||
|
|
||||||
class Shout(Base):
|
class Shout(Base):
|
||||||
__tablename__ = "shout"
|
__tablename__ = "shout"
|
||||||
|
|
||||||
|
@ -59,6 +50,7 @@ class Shout(Base):
|
||||||
created_by = Column(ForeignKey("author.id"), nullable=False)
|
created_by = Column(ForeignKey("author.id"), nullable=False)
|
||||||
updated_by = Column(ForeignKey("author.id"), nullable=True)
|
updated_by = Column(ForeignKey("author.id"), nullable=True)
|
||||||
deleted_by = Column(ForeignKey("author.id"), nullable=True)
|
deleted_by = Column(ForeignKey("author.id"), nullable=True)
|
||||||
|
community = Column(ForeignKey("community.id"), nullable=False)
|
||||||
|
|
||||||
body = Column(String, nullable=False, comment="Body")
|
body = Column(String, nullable=False, comment="Body")
|
||||||
slug = Column(String, unique=True)
|
slug = Column(String, unique=True)
|
||||||
|
@ -73,7 +65,6 @@ class Shout(Base):
|
||||||
|
|
||||||
authors = relationship(Author, secondary="shout_author")
|
authors = relationship(Author, secondary="shout_author")
|
||||||
topics = relationship(Topic, secondary="shout_topic")
|
topics = relationship(Topic, secondary="shout_topic")
|
||||||
communities = relationship(Community, secondary="shout_community")
|
|
||||||
reactions = relationship(Reaction)
|
reactions = relationship(Reaction)
|
||||||
|
|
||||||
lang = Column(String, nullable=False, default="ru", comment="Language")
|
lang = Column(String, nullable=False, default="ru", comment="Language")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user