This commit is contained in:
@@ -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}
|
||||
|
Reference in New Issue
Block a user