From 4f599e097fd06dedfdf0470dae1a5d3d059cbfc2 Mon Sep 17 00:00:00 2001 From: Untone Date: Wed, 26 Mar 2025 08:54:10 +0300 Subject: [PATCH] [0.4.17] - 2025-03-26 - Fixed `'Reaction' object is not subscriptable` error in hierarchical comments: - Modified `get_reactions_with_stat()` to convert Reaction objects to dictionaries - Added default values for limit/offset parameters - Fixed `load_first_replies()` implementation with proper parameter passing - Added doctest with example usage - Limited child comments to 100 per parent for performance --- CHANGELOG.md | 8 ++++++++ resolvers/reaction.py | 22 ++++++++++++++-------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 80c7577e..d25f8dcf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +#### [0.4.17] - 2025-03-26 +- Fixed `'Reaction' object is not subscriptable` error in hierarchical comments: + - Modified `get_reactions_with_stat()` to convert Reaction objects to dictionaries + - Added default values for limit/offset parameters + - Fixed `load_first_replies()` implementation with proper parameter passing + - Added doctest with example usage + - Limited child comments to 100 per parent for performance + #### [0.4.16] - 2025-03-22 - Added hierarchical comments pagination: - Created new GraphQL query `load_comments_branch` for efficient loading of hierarchical comments diff --git a/resolvers/reaction.py b/resolvers/reaction.py index 24d5f941..4a3fe56a 100644 --- a/resolvers/reaction.py +++ b/resolvers/reaction.py @@ -67,14 +67,17 @@ def add_reaction_stat_columns(q): return q -def get_reactions_with_stat(q, limit, offset): +def get_reactions_with_stat(q, limit=10, offset=0): """ Execute the reaction query and retrieve reactions with statistics. :param q: Query with reactions and statistics. :param limit: Number of reactions to load. :param offset: Pagination offset. - :return: List of reactions. + :return: List of reactions as dictionaries. + + >>> get_reactions_with_stat(q, 10, 0) # doctest: +SKIP + [{'id': 1, 'body': 'Текст комментария', 'stat': {'rating': 5, 'comments_count': 3}, ...}] """ q = q.limit(limit).offset(offset) reactions = [] @@ -87,10 +90,12 @@ def get_reactions_with_stat(q, limit, offset): logger.error(f"Пропущена реакция из-за отсутствия shout или author: {reaction.dict()}") continue - reaction.created_by = author.dict() - reaction.shout = shout.dict() - reaction.stat = {"rating": rating_stat, "comments_count": comments_count} - reactions.append(reaction) + # Преобразуем Reaction в словарь для доступа по ключу + reaction_dict = reaction.dict() + reaction_dict["created_by"] = author.dict() + reaction_dict["shout"] = shout.dict() + reaction_dict["stat"] = {"rating": rating_stat, "comments_count": comments_count} + reactions.append(reaction_dict) return reactions @@ -793,8 +798,9 @@ async def load_first_replies(comments, limit, offset, sort="newest"): q = q.order_by(order_by_stmt, Reaction.reply_to) - # Выполняем запрос - replies = get_reactions_with_stat(q) + # Выполняем запрос - указываем limit для неограниченного количества ответов + # но не более 100 на родительский комментарий + replies = get_reactions_with_stat(q, limit=100, offset=0) # Группируем ответы по родительским ID replies_by_parent = {}