authors-subquery-json-fix

This commit is contained in:
Untone 2024-02-27 12:47:42 +03:00
parent ef2f8dca82
commit cbd8ba6b68
2 changed files with 27 additions and 18 deletions

View File

@ -167,7 +167,7 @@ def get_with_stat(q):
entity = cols[0] entity = cols[0]
entity.stat = {'shouts': cols[1], 'authors': cols[2], 'followers': cols[3]} entity.stat = {'shouts': cols[1], 'authors': cols[2], 'followers': cols[3]}
if is_author: if is_author:
entity.stat['comments'] = 0 # FIXME: cols[4] entity.stat['comments'] = 0 # FIXME: cols[4]
# entity.stat['rating'] = cols[5] - cols[6] # entity.stat['rating'] = cols[5] - cols[6]
# entity.stat['rating_shouts'] = cols[7] - cols[8] # entity.stat['rating_shouts'] = cols[7] - cols[8]
pass pass

View File

@ -1,6 +1,6 @@
import asyncio import asyncio
from sqlalchemy import select, event from sqlalchemy import select, event, cast, String
import json import json
from orm.author import Author, AuthorFollower from orm.author import Author, AuthorFollower
@ -65,27 +65,36 @@ def after_shouts_update(mapper, connection, shout: Shout):
asyncio.create_task(update_author_cache(author.dict())) asyncio.create_task(update_author_cache(author.dict()))
@event.listens_for(Reaction, 'after_insert') @event.listens_for(Reaction, 'after_insert')
def after_reaction_insert(mapper, connection, reaction: Reaction): def after_reaction_insert(mapper, connection, reaction: Reaction):
author_subquery = select(Author).where(Author.id == reaction.created_by) try:
replied_author_subquery = ( author_subquery = select(Author).where(Author.id == reaction.created_by)
select(Author) replied_author_subquery = (
.join(Reaction, Author.id == Reaction.created_by) select(Author)
.where(Reaction.id == reaction.reply_to) .join(Reaction, Author.id == Reaction.created_by)
) .where(Reaction.id == reaction.reply_to)
)
author_query = select(author_subquery.subquery()).select_from(author_subquery.subquery()).union( author_query = select(
select(replied_author_subquery.subquery()).select_from(replied_author_subquery.subquery()) author_subquery.subquery().c.id,
) cast(author_subquery.subquery().c.links, String).label('links')
authors = get_with_stat(author_query) ).select_from(author_subquery.subquery()).union(
select(
replied_author_subquery.subquery().c.id,
cast(replied_author_subquery.subquery().c.links, String).label('links'),
)
.select_from(replied_author_subquery.subquery())
)
authors = get_with_stat(author_query)
for author in authors: for author in authors:
asyncio.create_task(update_author_cache(author.dict())) asyncio.create_task(update_author_cache(author.dict()))
shout = connection.execute(select(Shout).select_from(Shout).where(Shout.id == reaction.shout)).first() shout = connection.execute(select(Shout).select_from(Shout).where(Shout.id == reaction.shout)).first()
if shout: if shout:
after_shouts_update(mapper, connection, shout) after_shouts_update(mapper, connection, shout)
except Exception as exc:
logger.error(exc)
@event.listens_for(Author, 'after_insert') @event.listens_for(Author, 'after_insert')