granian+precommit
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
from sqlalchemy import and_, distinct, func, literal, select
|
||||
import logging
|
||||
|
||||
from sqlalchemy import and_, distinct, func, select
|
||||
from sqlalchemy.orm import aliased
|
||||
|
||||
from orm.author import Author
|
||||
@@ -8,16 +10,19 @@ from services.db import local_session
|
||||
from services.schema import query
|
||||
|
||||
|
||||
logger = logging.getLogger('\t[resolvers.community]\t')
|
||||
logger.setLevel(logging.DEBUG)
|
||||
|
||||
|
||||
def add_community_stat_columns(q):
|
||||
community_followers = aliased(CommunityAuthor)
|
||||
shout_community_aliased = aliased(ShoutCommunity)
|
||||
|
||||
|
||||
q = q.outerjoin(shout_community_aliased).add_columns(
|
||||
func.count(distinct(shout_community_aliased.shout)).label("shouts_stat")
|
||||
func.count(distinct(shout_community_aliased.shout)).label('shouts_stat')
|
||||
)
|
||||
q = q.outerjoin(community_followers, community_followers.author == Author.id).add_columns(
|
||||
func.count(distinct(community_followers.follower)).label("followers_stat")
|
||||
func.count(distinct(community_followers.follower)).label('followers_stat')
|
||||
)
|
||||
|
||||
q = q.group_by(Author.id)
|
||||
@@ -30,8 +35,8 @@ def get_communities_from_query(q):
|
||||
with local_session() as session:
|
||||
for [c, shouts_stat, followers_stat] in session.execute(q):
|
||||
c.stat = {
|
||||
"shouts": shouts_stat,
|
||||
"followers": followers_stat,
|
||||
'shouts': shouts_stat,
|
||||
'followers': followers_stat,
|
||||
# "commented": commented_stat,
|
||||
}
|
||||
ccc.append(c)
|
||||
@@ -69,8 +74,8 @@ def community_follow(follower_id, slug):
|
||||
session.add(cf)
|
||||
session.commit()
|
||||
return True
|
||||
except Exception:
|
||||
pass
|
||||
except Exception as ex:
|
||||
logger.debug(ex)
|
||||
return False
|
||||
|
||||
|
||||
@@ -90,7 +95,7 @@ def community_unfollow(follower_id, slug):
|
||||
return False
|
||||
|
||||
|
||||
@query.field("get_communities_all")
|
||||
@query.field('get_communities_all')
|
||||
async def get_communities_all(_, _info):
|
||||
q = select(Author)
|
||||
q = add_community_stat_columns(q)
|
||||
@@ -98,7 +103,7 @@ async def get_communities_all(_, _info):
|
||||
return get_communities_from_query(q)
|
||||
|
||||
|
||||
@query.field("get_community")
|
||||
@query.field('get_community')
|
||||
async def get_community(_, _info, slug):
|
||||
q = select(Community).where(Community.slug == slug)
|
||||
q = add_community_stat_columns(q)
|
||||
|
Reference in New Issue
Block a user