From cbd8ba6b68c790addcb68e697b2f89f615f43ffe Mon Sep 17 00:00:00 2001 From: Untone Date: Tue, 27 Feb 2024 12:47:42 +0300 Subject: [PATCH] authors-subquery-json-fix --- resolvers/stat.py | 2 +- services/event_listeners.py | 43 ++++++++++++++++++++++--------------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/resolvers/stat.py b/resolvers/stat.py index 230501b8..7f66e835 100644 --- a/resolvers/stat.py +++ b/resolvers/stat.py @@ -167,7 +167,7 @@ def get_with_stat(q): entity = cols[0] entity.stat = {'shouts': cols[1], 'authors': cols[2], 'followers': cols[3]} 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_shouts'] = cols[7] - cols[8] pass diff --git a/services/event_listeners.py b/services/event_listeners.py index d77f095d..c13f2895 100644 --- a/services/event_listeners.py +++ b/services/event_listeners.py @@ -1,6 +1,6 @@ import asyncio -from sqlalchemy import select, event +from sqlalchemy import select, event, cast, String import json 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())) - @event.listens_for(Reaction, 'after_insert') def after_reaction_insert(mapper, connection, reaction: Reaction): - author_subquery = select(Author).where(Author.id == reaction.created_by) - replied_author_subquery = ( - select(Author) - .join(Reaction, Author.id == Reaction.created_by) - .where(Reaction.id == reaction.reply_to) - ) + try: + author_subquery = select(Author).where(Author.id == reaction.created_by) + replied_author_subquery = ( + select(Author) + .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( - select(replied_author_subquery.subquery()).select_from(replied_author_subquery.subquery()) - ) - authors = get_with_stat(author_query) + author_query = select( + author_subquery.subquery().c.id, + cast(author_subquery.subquery().c.links, String).label('links') + ).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: - asyncio.create_task(update_author_cache(author.dict())) + for author in authors: + asyncio.create_task(update_author_cache(author.dict())) - shout = connection.execute(select(Shout).select_from(Shout).where(Shout.id == reaction.shout)).first() - if shout: - after_shouts_update(mapper, connection, shout) + shout = connection.execute(select(Shout).select_from(Shout).where(Shout.id == reaction.shout)).first() + if shout: + after_shouts_update(mapper, connection, shout) + except Exception as exc: + logger.error(exc) @event.listens_for(Author, 'after_insert')