diff --git a/resolvers/__init__.py b/resolvers/__init__.py index add9ddfd..f5068b54 100644 --- a/resolvers/__init__.py +++ b/resolvers/__init__.py @@ -19,7 +19,7 @@ from resolvers.draft import ( unpublish_draft, update_draft, ) -from resolvers.editor import create_shout, delete_shout, update_shout + from resolvers.feed import ( load_shouts_coauthored, load_shouts_discussed, @@ -100,10 +100,6 @@ __all__ = [ "follow", "unfollow", "get_shout_followers", - # editor - "create_shout", - "update_shout", - "delete_shout", # reaction "create_reaction", "update_reaction", diff --git a/resolvers/editor.py b/resolvers/editor.py index 1b81847c..43bef5c0 100644 --- a/resolvers/editor.py +++ b/resolvers/editor.py @@ -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}") shout_by_id = ( 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) .first() ) @@ -562,7 +562,10 @@ async def update_shout(_, info, shout_id: int, shout_input=None, publish=False): # Получаем полные данные шаута со связями shout_with_relations = ( 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) .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 - 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"] = ( [ @@ -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.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} else: 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"} -def get_main_topic_slug(topics): - """Get the slug of the main topic from a list of topics. - - Args: - topics: List of ShoutTopic objects - - Returns: - dict: Topic dictionary with slug, title and id - """ +def get_main_topic(topics): + """Get the main topic from a list of ShoutTopic objects.""" if not topics: - return {"slug": "notopic", "title": "no topic", "id": 0} + return None - # Convert to list if it's not already and reverse - topics_list = list(topics) - topics_list.reverse() + # Find first main topic in original order + main_topic_rel = next((st for st in topics if st.main), None) - main_topic = next((t for t in topics_list if t.main), None) - if main_topic: - return main_topic.topic.dict() + if main_topic_rel and main_topic_rel.topic: + return { + "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} diff --git a/resolvers/reader.py b/resolvers/reader.py index 6cd9fc65..06dc949a 100644 --- a/resolvers/reader.py +++ b/resolvers/reader.py @@ -9,7 +9,6 @@ from orm.author import Author from orm.reaction import Reaction, ReactionKind from orm.shout import Shout, ShoutAuthor, ShoutTopic from orm.topic import Topic -from services.auth import login_accepted from services.db import json_array_builder, json_builder, local_session from services.schema import query from services.search import search_text