From 635ff4285e06378e02727dc0ebb17b581cbdecbb Mon Sep 17 00:00:00 2001 From: Untone Date: Mon, 21 Oct 2024 11:29:57 +0300 Subject: [PATCH] communityfollower-roles --- CHANGELOG.txt | 1 + orm/community.py | 30 ++++++++++++++++++++++-------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index da613afa..9b298dc2 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -6,6 +6,7 @@ - get_communities_all resolver fix - reaction filter by kinds - reaction sort enum added +- community follower roles enum added [0.4.4] - followers_stat removed for shout diff --git a/orm/community.py b/orm/community.py index c8edcbfa..9f48ef07 100644 --- a/orm/community.py +++ b/orm/community.py @@ -1,20 +1,29 @@ -import time - -from sqlalchemy import Column, ForeignKey, Integer, String, func from sqlalchemy.ext.hybrid import hybrid_property +from sqlalchemy import ARRAY, Column, ForeignKey, Integer, String, func, Enum from sqlalchemy.orm import relationship +import enum +import time from orm.author import Author from services.db import Base +class CommunityRole(enum.Enum): + AUTHOR = "author" + READER = "reader" + EDITOR = "editor" + CRITIC = "critic" + EXPERT = "expert" + ARTIST = "artist" + + class CommunityFollower(Base): __tablename__ = "community_author" author = Column(ForeignKey("author.id"), primary_key=True) community = Column(ForeignKey("community.id"), primary_key=True) joined_at = Column(Integer, nullable=False, default=lambda: int(time.time())) - role = Column(String, nullable=False) + roles = Column(ARRAY(Enum(CommunityRole)), nullable=False, default=[CommunityRole.READER]) class Community(Base): @@ -40,12 +49,17 @@ class CommunityStats: @property def shouts(self): from orm.shout import ShoutCommunity - return self.community.session.query(func.count(ShoutCommunity.shout_id))\ - .filter(ShoutCommunity.community_id == self.community.id)\ + + return ( + self.community.session.query(func.count(ShoutCommunity.shout_id)) + .filter(ShoutCommunity.community_id == self.community.id) .scalar() + ) @property def followers(self): - return self.community.session.query(func.count(CommunityFollower.author))\ - .filter(CommunityFollower.community == self.community.id)\ + return ( + self.community.session.query(func.count(CommunityFollower.author)) + .filter(CommunityFollower.community == self.community.id) .scalar() + )