move-author-fix
All checks were successful
Deploy to core / deploy (push) Successful in 1m26s

This commit is contained in:
Untone 2024-02-22 23:32:26 +03:00
parent f8dafda86b
commit d69f29bda3
3 changed files with 12 additions and 14 deletions

View File

@ -98,7 +98,7 @@ def count_author_shouts_rating(session, author_id) -> int:
async def load_author_with_stats(q): async def load_author_with_stats(q):
q = add_author_stat_columns(q) q = add_author_stat_columns(q, author_model=Author)
result = await get_authors_from_query(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') @query.field('load_authors_by')
async def load_authors_by(_, _info, by, limit, offset): async def load_authors_by(_, _info, by, limit, offset):
q = select(Author) q = select(Author)
q = add_author_stat_columns(q) q = add_author_stat_columns(q, author_model=Author)
if by.get('slug'): if by.get('slug'):
q = q.filter(Author.slug.ilike(f"%{by['slug']}%")) q = q.filter(Author.slug.ilike(f"%{by['slug']}%"))
elif by.get('name'): elif by.get('name'):
@ -273,7 +273,7 @@ async def create_author(user_id: str, slug: str, name: str = ''):
@query.field('get_author_followers') @query.field('get_author_followers')
async def get_author_followers(_, _info, slug) -> List[Author]: async def get_author_followers(_, _info, slug) -> List[Author]:
q = select(Author) q = select(Author)
q = add_author_stat_columns(q) q = add_author_stat_columns(q, author_model=Author)
aliased_author = aliased(Author) aliased_author = aliased(Author)
q = ( q = (

View File

@ -8,23 +8,23 @@ from services.db import local_session
# from services.viewed import ViewedStorage # from services.viewed import ViewedStorage
def add_author_stat_columns(q, author_model = None): def add_author_stat_columns(q, author_model=None):
aliased_author = author_model or aliased(Author) aliased_author = author_model or aliased(Author)
shout_author_aliased = aliased(ShoutAuthor) shout_author_aliased = aliased(ShoutAuthor)
q = q.outerjoin(shout_author_aliased).add_columns( q = q.outerjoin(shout_author_aliased).add_columns(
func.count(distinct(shout_author_aliased.shout)).label('shouts_stat') func.count(distinct(shout_author_aliased.shout)).label('shouts_stat')
) )
authors_table = aliased(AuthorFollower)
q = q.outerjoin(
authors_table, authors_table.follower == aliased_author.id
).add_columns(func.count(distinct(authors_table.author)).label('authors_stat'))
followers_table = aliased(AuthorFollower) 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 == aliased_author.id).add_columns(
func.count(distinct(followers_table.follower)).label('followers_stat') func.count(distinct(followers_table.follower)).label('followers_stat')
) )
followings_table = aliased(AuthorFollower)
q = q.outerjoin(
followings_table, followings_table.follower == aliased_author.id
).add_columns(func.count(distinct(followers_table.author)).label('followings_stat'))
q = q.group_by(aliased_author.id) q = q.group_by(aliased_author.id)
return q return q
@ -32,13 +32,11 @@ def add_author_stat_columns(q, author_model = None):
async def get_authors_from_query(q): async def get_authors_from_query(q):
authors = [] authors = []
with local_session() as session: with local_session() as session:
for [author, shouts_stat, followers_stat, followings_stat] in session.execute( for [author, shouts_stat, authors_stat, followers_stat] in session.execute(q):
q
):
author.stat = { author.stat = {
'shouts': shouts_stat, 'shouts': shouts_stat,
'followers': followers_stat, 'followers': followers_stat,
'followings': followings_stat, 'followings': authors_stat,
# viewed # viewed
} }
authors.append(author) authors.append(author)

View File

@ -77,7 +77,7 @@ async def update_follows_for_user(
async def handle_author_follower_change(connection, author_id, follower_id, is_insert): async def handle_author_follower_change(connection, author_id, follower_id, is_insert):
q = select(Author).filter(Author.id == author_id) q = select(Author).filter(Author.id == author_id)
q = add_author_stat_columns(q) q = add_author_stat_columns(q, author_model=Author)
async with connection.begin() as conn: async with connection.begin() as conn:
[author, shouts_stat, followers_stat, followings_stat] = await conn.execute( [author, shouts_stat, followers_stat, followings_stat] = await conn.execute(
q q