diff --git a/resolvers/author.py b/resolvers/author.py index 06ff0249..3b0223ae 100644 --- a/resolvers/author.py +++ b/resolvers/author.py @@ -98,7 +98,7 @@ def count_author_shouts_rating(session, author_id) -> int: async def load_author_with_stats(q): - q = add_author_stat_columns(q, author_model=Author) + q = add_author_stat_columns(q) result = await get_authors_from_query(q) @@ -176,7 +176,7 @@ async def get_author_id(_, _info, user: str): @query.field('load_authors_by') async def load_authors_by(_, _info, by, limit, offset): q = select(Author) - q = add_author_stat_columns(q, author_model=Author) + q = add_author_stat_columns(q) if by.get('slug'): q = q.filter(Author.slug.ilike(f"%{by['slug']}%")) elif by.get('name'): @@ -273,7 +273,7 @@ async def create_author(user_id: str, slug: str, name: str = ''): @query.field('get_author_followers') async def get_author_followers(_, _info, slug) -> List[Author]: q = select(Author) - q = add_author_stat_columns(q, author_model=Author) + q = add_author_stat_columns(q) aliased_author = aliased(Author) q = ( diff --git a/resolvers/follower.py b/resolvers/follower.py index 48e50f66..0ccee9b7 100644 --- a/resolvers/follower.py +++ b/resolvers/follower.py @@ -110,7 +110,7 @@ def query_follows(user_id: str): .filter(TopicFollower.topic == Topic.id) ) - authors_query = add_author_stat_columns(authors_query, author_model=aliased_author) + authors_query = add_author_stat_columns(authors_query) topics_query = add_topic_stat_columns(topics_query) authors = [ { diff --git a/resolvers/stat.py b/resolvers/stat.py index 50e50366..1bcf1f9f 100644 --- a/resolvers/stat.py +++ b/resolvers/stat.py @@ -8,8 +8,7 @@ from services.db import local_session # from services.viewed import ViewedStorage -def add_author_stat_columns(q, author_model=None): - aliased_author = author_model or aliased(Author) +def add_author_stat_columns(q): shout_author_aliased = aliased(ShoutAuthor) q = q.outerjoin(shout_author_aliased).add_columns( func.count(distinct(shout_author_aliased.shout)).label('shouts_stat') @@ -17,15 +16,15 @@ def add_author_stat_columns(q, author_model=None): authors_table = aliased(AuthorFollower) q = q.outerjoin( - authors_table, authors_table.follower == aliased_author.id + authors_table, authors_table.follower == Author.id ).add_columns(func.count(distinct(authors_table.author)).label('authors_stat')) followers_table = aliased(AuthorFollower) - q = q.outerjoin(followers_table, followers_table.author == aliased_author.id).add_columns( + q = q.outerjoin(followers_table, followers_table.author == Author.id).add_columns( func.count(distinct(followers_table.follower)).label('followers_stat') ) - q = q.group_by(aliased_author.id) + q = q.group_by(Author.id) return q diff --git a/services/follows.py b/services/follows.py index 89aa7d30..ff8a93c3 100644 --- a/services/follows.py +++ b/services/follows.py @@ -77,7 +77,7 @@ async def update_follows_for_user( async def handle_author_follower_change(connection, author_id, follower_id, is_insert): q = select(Author).filter(Author.id == author_id) - q = add_author_stat_columns(q, author_model=Author) + q = add_author_stat_columns(q) async with connection.begin() as conn: [author, shouts_stat, followers_stat, followings_stat] = await conn.execute( q