From f84be7b11bde091490246c9927108b2a354acd17 Mon Sep 17 00:00:00 2001 From: Untone Date: Wed, 12 Feb 2025 19:33:02 +0300 Subject: [PATCH] main_topic-fix7 --- resolvers/editor.py | 46 +++++++++++++++++++++++++++++++++++---------- resolvers/reader.py | 33 ++++++++++++++++++++++---------- 2 files changed, 59 insertions(+), 20 deletions(-) diff --git a/resolvers/editor.py b/resolvers/editor.py index 83c702fd..0f8c1fc6 100644 --- a/resolvers/editor.py +++ b/resolvers/editor.py @@ -581,7 +581,7 @@ async def update_shout(_, info, shout_id: int, shout_input=None, publish=False): ) # Add main_topic to the shout dictionary - shout_dict["main_topic"] = get_main_topic_json(shout_with_relations.topics) + shout_dict["main_topic"] = get_main_topic(shout_with_relations.topics) shout_dict["authors"] = ( [ @@ -647,26 +647,52 @@ async def delete_shout(_, info, shout_id: int): return {"error": "access denied"} -def get_main_topic_json(topics): - """Get the main topic from a list of ShoutTopic objects.""" +def get_main_topic(topics): + """Get the main topic from a list of ShoutTopic objects. + + Args: + topics: List of ShoutTopic objects + + Returns: + dict: Main topic data with id, slug, title and is_main fields + + Example: + >>> topics = [ShoutTopic(main=True, topic=Topic(id=1, slug='test', title='Test'))] + >>> result = get_main_topic(topics) + >>> assert result['id'] == 1 + >>> assert result['is_main'] == True + """ if not topics: - return None + return { + "id": 0, + "title": "no topic", + "slug": "notopic", + "is_main": True + } # Find first main topic in original order main_topic_rel = next((st for st in topics if st.main), None) if main_topic_rel and main_topic_rel.topic: - topic_dict = { + return { "slug": main_topic_rel.topic.slug, "title": main_topic_rel.topic.title, "id": main_topic_rel.topic.id, + "is_main": True } - # Convert to JSON string to match reader.py behavior - return json.dumps(topic_dict) # If no main found but topics exist, return first if topics and topics[0].topic: - topic_dict = {"slug": topics[0].topic.slug, "title": topics[0].topic.title, "id": topics[0].topic.id} - return json.dumps(topic_dict) + return { + "slug": topics[0].topic.slug, + "title": topics[0].topic.title, + "id": topics[0].topic.id, + "is_main": True + } - return json.dumps({"slug": "notopic", "title": "no topic", "id": 0}) + return { + "slug": "notopic", + "title": "no topic", + "id": 0, + "is_main": True + } diff --git a/resolvers/reader.py b/resolvers/reader.py index c3105855..1625d1e5 100644 --- a/resolvers/reader.py +++ b/resolvers/reader.py @@ -233,18 +233,31 @@ def get_shouts_with_links(info, q, limit=20, offset=0): if has_field(info, "main_topic"): main_topic = None if hasattr(row, "main_topic"): - main_topic = ( - json.loads(row.main_topic) if isinstance(row.main_topic, str) else row.main_topic - ) - - # Если main_topic не определен, берем первый топик из списка - if not main_topic and topics and len(topics) > 0: + main_topic = json.loads(row.main_topic) if isinstance(row.main_topic, str) else row.main_topic + + # Если main_topic не определен, ищем топик с main=True или берем первый + if not main_topic and topics: + # Сначала ищем топик с main=True + main_topic = next((t for t in topics if t.get("is_main")), None) + + # Если не нашли main=True, берем первый топик + if not main_topic and len(topics) > 0: + main_topic = { + "id": topics[0]["id"], + "title": topics[0]["title"], + "slug": topics[0]["slug"], + "is_main": True + } + + # Если все еще нет main_topic, используем заглушку + if not main_topic: main_topic = { - "id": topics[0]["id"], - "title": topics[0]["title"], - "slug": topics[0]["slug"], - "is_main": True, + "id": 0, + "title": "no topic", + "slug": "notopic", + "is_main": True } + shout_dict["main_topic"] = main_topic if has_field(info, "authors") and hasattr(row, "authors"):