From 32cb810f512c8861b2a924f494f6a7c3858b498f Mon Sep 17 00:00:00 2001 From: Untone Date: Sat, 3 May 2025 11:52:10 +0300 Subject: [PATCH] unpublish-fix7 --- resolvers/editor.py | 58 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 9 deletions(-) diff --git a/resolvers/editor.py b/resolvers/editor.py index eb3d25e9..95361c20 100644 --- a/resolvers/editor.py +++ b/resolvers/editor.py @@ -651,17 +651,55 @@ def get_main_topic(topics): logger.debug( f"Topics data: {[(t.slug, getattr(t, 'main', False)) for t in topics] if topics else []}" ) + if not topics: logger.warning("No topics provided to get_main_topic") - return + return {"id": 0, "title": "no topic", "slug": "notopic", "is_main": True} + + # Проверяем, является ли topics списком объектов ShoutTopic или Topic + if hasattr(topics[0], 'topic') and topics[0].topic: + # Для ShoutTopic объектов (старый формат) + # Find first main topic in original order + main_topic_rel = next((st for st in topics if getattr(st, 'main', False)), None) + logger.debug( + f"Found main topic relation: {main_topic_rel.topic.slug if main_topic_rel and main_topic_rel.topic else None}" + ) + + if main_topic_rel and main_topic_rel.topic: + result = { + "slug": main_topic_rel.topic.slug, + "title": main_topic_rel.topic.title, + "id": main_topic_rel.topic.id, + "is_main": True, + } + logger.info(f"Returning main topic: {result}") + return result + + # If no main found but topics exist, return first + if topics and topics[0].topic: + logger.info(f"No main topic found, using first topic: {topics[0].topic.slug}") + result = { + "slug": topics[0].topic.slug, + "title": topics[0].topic.title, + "id": topics[0].topic.id, + "is_main": True, + } + return result else: - logger.info(f"Using first topic as main: {topics[0].slug}") - return { - "slug": topics[0].slug, - "title": topics[0].title, - "id": topics[0].id, - "is_main": True, - } + # Для Topic объектов (новый формат из selectinload) + # После смены на selectinload у нас просто список Topic объектов + if topics: + logger.info(f"Using first topic as main: {topics[0].slug}") + result = { + "slug": topics[0].slug, + "title": topics[0].title, + "id": topics[0].id, + "is_main": True, + } + return result + + logger.warning("No valid topics found, returning default") + return {"slug": "notopic", "title": "no topic", "id": 0, "is_main": True} @mutation.field("unpublish_shout") @login_required @@ -745,7 +783,9 @@ async def unpublish_shout(_, info, shout_id: int): ) # Добавляем main_topic - shout_dict["main_topic"] = get_main_topic(shout.topics) + main_topic = get_main_topic(shout.topics) + if main_topic["id"]: + shout_dict["main_topic"] = main_topic # Добавляем авторов shout_dict["authors"] = (