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

This commit is contained in:
Untone 2025-02-12 19:21:21 +03:00
parent fe661a5008
commit b011c0fd48
6 changed files with 36 additions and 27 deletions

1
.gitignore vendored
View File

@ -161,3 +161,4 @@ views.json
*.key *.key
*.crt *.crt
*cache.json *cache.json
.cursor

View File

@ -19,7 +19,6 @@ from resolvers.draft import (
unpublish_draft, unpublish_draft,
update_draft, update_draft,
) )
from resolvers.feed import ( from resolvers.feed import (
load_shouts_coauthored, load_shouts_coauthored,
load_shouts_discussed, load_shouts_discussed,

View File

@ -562,10 +562,7 @@ 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( .options(joinedload(Shout.topics).joinedload(ShoutTopic.topic), joinedload(Shout.authors))
joinedload(Shout.topics).joinedload(ShoutTopic.topic),
joinedload(Shout.authors)
)
.filter(Shout.id == shout_id) .filter(Shout.id == shout_id)
.first() .first()
) )
@ -596,7 +593,9 @@ 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]}") 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}")
@ -660,18 +659,14 @@ def get_main_topic_json(topics):
topic_dict = { topic_dict = {
"slug": main_topic_rel.topic.slug, "slug": main_topic_rel.topic.slug,
"title": main_topic_rel.topic.title, "title": main_topic_rel.topic.title,
"id": main_topic_rel.topic.id "id": main_topic_rel.topic.id,
} }
# Convert to JSON string to match reader.py behavior # Convert to JSON string to match reader.py behavior
return json.dumps(topic_dict) return json.dumps(topic_dict)
# If no main found but topics exist, return first # If no main found but topics exist, return first
if topics and topics[0].topic: if topics and topics[0].topic:
topic_dict = { topic_dict = {"slug": topics[0].topic.slug, "title": topics[0].topic.title, "id": topics[0].topic.id}
"slug": topics[0].topic.slug,
"title": topics[0].topic.title,
"id": topics[0].topic.id
}
return json.dumps(topic_dict) return json.dumps(topic_dict)
return json.dumps({"slug": "notopic", "title": "no topic", "id": 0}) return json.dumps({"slug": "notopic", "title": "no topic", "id": 0})

View File

@ -190,8 +190,6 @@ def get_shouts_with_links(info, q, limit=20, offset=0):
try: try:
q = q.limit(limit).offset(offset) q = q.limit(limit).offset(offset)
# logger.info(f"get shouts query: {q}")
with local_session() as session: with local_session() as session:
shouts_result = session.execute(q).all() shouts_result = session.execute(q).all()
@ -226,16 +224,33 @@ def get_shouts_with_links(info, q, limit=20, offset=0):
viewed = ViewedStorage.get_shout(shout_id=shout_id) or 0 viewed = ViewedStorage.get_shout(shout_id=shout_id) or 0
shout_dict["stat"] = {**stat, "viewed": viewed, "commented": stat.get("comments_count", 0)} shout_dict["stat"] = {**stat, "viewed": viewed, "commented": stat.get("comments_count", 0)}
if has_field(info, "main_topic") and hasattr(row, "main_topic"): # Обработка main_topic и topics
shout_dict["main_topic"] = ( topics = None
if has_field(info, "topics") and hasattr(row, "topics"):
topics = json.loads(row.topics) if isinstance(row.topics, str) else row.topics
shout_dict["topics"] = topics
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 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 = {
"id": topics[0]["id"],
"title": topics[0]["title"],
"slug": topics[0]["slug"],
"is_main": True,
}
shout_dict["main_topic"] = main_topic
if has_field(info, "authors") and hasattr(row, "authors"): if has_field(info, "authors") and hasattr(row, "authors"):
shout_dict["authors"] = ( shout_dict["authors"] = (
json.loads(row.authors) if isinstance(row.authors, str) else row.authors json.loads(row.authors) if isinstance(row.authors, str) else row.authors
) )
if has_field(info, "topics") and hasattr(row, "topics"):
shout_dict["topics"] = json.loads(row.topics) if isinstance(row.topics, str) else row.topics
if has_field(info, "media") and shout.media: if has_field(info, "media") and shout.media:
# Обработка поля media # Обработка поля media

View File

@ -22,4 +22,3 @@ class CommonResult:
topics: Optional[List[Topic]] = None topics: Optional[List[Topic]] = None
community: Optional[Community] = None community: Optional[Community] = None
communities: Optional[List[Community]] = None communities: Optional[List[Community]] = None