main_topic-fix4
All checks were successful
Deploy on push / deploy (push) Successful in 56s

This commit is contained in:
Untone 2025-02-12 00:55:55 +03:00
parent a9dd593ac8
commit e97823f99c
3 changed files with 27 additions and 26 deletions

View File

@ -19,7 +19,7 @@ from resolvers.draft import (
unpublish_draft, unpublish_draft,
update_draft, update_draft,
) )
from resolvers.editor import create_shout, delete_shout, update_shout
from resolvers.feed import ( from resolvers.feed import (
load_shouts_coauthored, load_shouts_coauthored,
load_shouts_discussed, load_shouts_discussed,
@ -100,10 +100,6 @@ __all__ = [
"follow", "follow",
"unfollow", "unfollow",
"get_shout_followers", "get_shout_followers",
# editor
"create_shout",
"update_shout",
"delete_shout",
# reaction # reaction
"create_reaction", "create_reaction",
"update_reaction", "update_reaction",

View File

@ -419,7 +419,7 @@ async def update_shout(_, info, shout_id: int, shout_input=None, publish=False):
logger.info(f"Processing update for shout#{shout_id} by author #{author_id}") logger.info(f"Processing update for shout#{shout_id} by author #{author_id}")
shout_by_id = ( shout_by_id = (
session.query(Shout) session.query(Shout)
.options(joinedload(Shout.authors), joinedload(Shout.topics)) .options(joinedload(Shout.topics).joinedload(ShoutTopic.topic), joinedload(Shout.authors))
.filter(Shout.id == shout_id) .filter(Shout.id == shout_id)
.first() .first()
) )
@ -562,7 +562,10 @@ async def update_shout(_, info, shout_id: int, shout_input=None, publish=False):
# Получаем полные данные шаута со связями # Получаем полные данные шаута со связями
shout_with_relations = ( shout_with_relations = (
session.query(Shout) session.query(Shout)
.options(joinedload(Shout.topics), joinedload(Shout.authors)) .options(
joinedload(Shout.topics).joinedload(ShoutTopic.topic),
joinedload(Shout.authors)
)
.filter(Shout.id == shout_id) .filter(Shout.id == shout_id)
.first() .first()
) )
@ -581,7 +584,7 @@ async def update_shout(_, info, shout_id: int, shout_input=None, publish=False):
) )
# Add main_topic to the shout dictionary # Add main_topic to the shout dictionary
shout_dict["main_topic"] = get_main_topic_slug(shout_with_relations.topics) shout_dict["main_topic"] = get_main_topic(shout_with_relations.topics)
shout_dict["authors"] = ( shout_dict["authors"] = (
[ [
@ -593,6 +596,7 @@ async def update_shout(_, info, shout_id: int, shout_input=None, publish=False):
) )
logger.info(f"Final shout data with relations: {shout_dict}") logger.info(f"Final shout data with relations: {shout_dict}")
logger.debug(f"Loaded topics details: {[(t.topic.slug if t.topic else 'no-topic', t.main) for t in shout_with_relations.topics]}")
return {"shout": shout_dict, "error": None} return {"shout": shout_dict, "error": None}
else: else:
logger.warning(f"Access denied: author #{author_id} cannot edit shout#{shout_id}") logger.warning(f"Access denied: author #{author_id} cannot edit shout#{shout_id}")
@ -644,25 +648,27 @@ async def delete_shout(_, info, shout_id: int):
return {"error": "access denied"} return {"error": "access denied"}
def get_main_topic_slug(topics): def get_main_topic(topics):
"""Get the slug of the main topic from a list of topics. """Get the main topic from a list of ShoutTopic objects."""
Args:
topics: List of ShoutTopic objects
Returns:
dict: Topic dictionary with slug, title and id
"""
if not topics: if not topics:
return {"slug": "notopic", "title": "no topic", "id": 0} return None
# Convert to list if it's not already and reverse # Find first main topic in original order
topics_list = list(topics) main_topic_rel = next((st for st in topics if st.main), None)
topics_list.reverse()
main_topic = next((t for t in topics_list if t.main), None) if main_topic_rel and main_topic_rel.topic:
if main_topic: return {
return main_topic.topic.dict() "slug": main_topic_rel.topic.slug,
"title": main_topic_rel.topic.title,
"id": main_topic_rel.topic.id
}
# If no main found but topics exist, return first
if topics and topics[0].topic:
return {
"slug": topics[0].topic.slug,
"title": topics[0].topic.title,
"id": topics[0].topic.id
}
# If no main topic found, return default
return {"slug": "notopic", "title": "no topic", "id": 0} return {"slug": "notopic", "title": "no topic", "id": 0}

View File

@ -9,7 +9,6 @@ from orm.author import Author
from orm.reaction import Reaction, ReactionKind from orm.reaction import Reaction, ReactionKind
from orm.shout import Shout, ShoutAuthor, ShoutTopic from orm.shout import Shout, ShoutAuthor, ShoutTopic
from orm.topic import Topic from orm.topic import Topic
from services.auth import login_accepted
from services.db import json_array_builder, json_builder, local_session from services.db import json_array_builder, json_builder, local_session
from services.schema import query from services.schema import query
from services.search import search_text from services.search import search_text