This commit is contained in:
parent
54f7dd9c1f
commit
b0e2551e9b
|
@ -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, author_model=Author)
|
q = add_author_stat_columns(q)
|
||||||
|
|
||||||
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, author_model=Author)
|
q = add_author_stat_columns(q)
|
||||||
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, author_model=Author)
|
q = add_author_stat_columns(q)
|
||||||
|
|
||||||
aliased_author = aliased(Author)
|
aliased_author = aliased(Author)
|
||||||
q = (
|
q = (
|
||||||
|
|
|
@ -110,7 +110,7 @@ def query_follows(user_id: str):
|
||||||
.filter(TopicFollower.topic == Topic.id)
|
.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)
|
topics_query = add_topic_stat_columns(topics_query)
|
||||||
authors = [
|
authors = [
|
||||||
{
|
{
|
||||||
|
|
|
@ -8,8 +8,7 @@ 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):
|
||||||
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')
|
||||||
|
@ -17,15 +16,15 @@ def add_author_stat_columns(q, author_model=None):
|
||||||
|
|
||||||
authors_table = aliased(AuthorFollower)
|
authors_table = aliased(AuthorFollower)
|
||||||
q = q.outerjoin(
|
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'))
|
).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 == Author.id).add_columns(
|
||||||
func.count(distinct(followers_table.follower)).label('followers_stat')
|
func.count(distinct(followers_table.follower)).label('followers_stat')
|
||||||
)
|
)
|
||||||
|
|
||||||
q = q.group_by(aliased_author.id)
|
q = q.group_by(Author.id)
|
||||||
return q
|
return q
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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, author_model=Author)
|
q = add_author_stat_columns(q)
|
||||||
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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user