diff --git a/.gitignore b/.gitignore index 2c0a3b6e..4db9e7e4 100644 --- a/.gitignore +++ b/.gitignore @@ -160,4 +160,5 @@ views.json *.pem *.key *.crt -*cache.json \ No newline at end of file +*cache.json +.cursor \ No newline at end of file diff --git a/resolvers/__init__.py b/resolvers/__init__.py index f5068b54..4d2f8d69 100644 --- a/resolvers/__init__.py +++ b/resolvers/__init__.py @@ -19,7 +19,6 @@ from resolvers.draft import ( unpublish_draft, update_draft, ) - from resolvers.feed import ( load_shouts_coauthored, load_shouts_discussed, diff --git a/resolvers/draft.py b/resolvers/draft.py index 25b73f04..21dd9e2c 100644 --- a/resolvers/draft.py +++ b/resolvers/draft.py @@ -207,7 +207,7 @@ async def publish_shout(_, info, shout_id: int): draft.updated_at = now shout.updated_at = now - + # Устанавливаем published_at только если была ранее снята с публикации if not was_published: shout.published_at = now diff --git a/resolvers/editor.py b/resolvers/editor.py index 7f21366a..83c702fd 100644 --- a/resolvers/editor.py +++ b/resolvers/editor.py @@ -562,10 +562,7 @@ async def update_shout(_, info, shout_id: int, shout_input=None, publish=False): # Получаем полные данные шаута со связями shout_with_relations = ( session.query(Shout) - .options( - joinedload(Shout.topics).joinedload(ShoutTopic.topic), - joinedload(Shout.authors) - ) + .options(joinedload(Shout.topics).joinedload(ShoutTopic.topic), joinedload(Shout.authors)) .filter(Shout.id == shout_id) .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.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} else: logger.warning(f"Access denied: author #{author_id} cannot edit shout#{shout_id}") @@ -652,26 +651,22 @@ def get_main_topic_json(topics): """Get the main topic from a list of ShoutTopic objects.""" if not topics: return None - + # 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 = { "slug": main_topic_rel.topic.slug, "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 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 - } + topic_dict = {"slug": topics[0].topic.slug, "title": topics[0].topic.title, "id": topics[0].topic.id} return json.dumps(topic_dict) - + return json.dumps({"slug": "notopic", "title": "no topic", "id": 0}) diff --git a/resolvers/reader.py b/resolvers/reader.py index 06dc949a..c3105855 100644 --- a/resolvers/reader.py +++ b/resolvers/reader.py @@ -190,8 +190,6 @@ def get_shouts_with_links(info, q, limit=20, offset=0): try: q = q.limit(limit).offset(offset) - # logger.info(f"get shouts query: {q}") - with local_session() as session: 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 shout_dict["stat"] = {**stat, "viewed": viewed, "commented": stat.get("comments_count", 0)} - if has_field(info, "main_topic") and hasattr(row, "main_topic"): - shout_dict["main_topic"] = ( - json.loads(row.main_topic) if isinstance(row.main_topic, str) else row.main_topic - ) + # Обработка main_topic и topics + 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 + ) + + # Если 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"): shout_dict["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: # Обработка поля media diff --git a/services/common_result.py b/services/common_result.py index 64695173..6d73d133 100644 --- a/services/common_result.py +++ b/services/common_result.py @@ -22,4 +22,3 @@ class CommonResult: topics: Optional[List[Topic]] = None community: Optional[Community] = None communities: Optional[List[Community]] = None -