This commit is contained in:
parent
f84be7b11b
commit
d3ed335fde
|
@ -263,28 +263,9 @@ async def create_shout(_, info, inp):
|
||||||
|
|
||||||
|
|
||||||
def patch_main_topic(session, main_topic_slug, shout):
|
def patch_main_topic(session, main_topic_slug, shout):
|
||||||
"""Update the main topic for a shout.
|
"""Update the main topic for a shout."""
|
||||||
|
|
||||||
Args:
|
|
||||||
session: SQLAlchemy session
|
|
||||||
main_topic_slug (str): Slug of the topic to set as main
|
|
||||||
shout (Shout): The shout to update
|
|
||||||
|
|
||||||
Side Effects:
|
|
||||||
- Updates ShoutTopic.main flags in database
|
|
||||||
- Only one topic can be main at a time
|
|
||||||
|
|
||||||
Example:
|
|
||||||
>>> def test_patch_main_topic():
|
|
||||||
... with local_session() as session:
|
|
||||||
... shout = session.query(Shout).first()
|
|
||||||
... patch_main_topic(session, 'tech', shout)
|
|
||||||
... main_topic = session.query(ShoutTopic).filter_by(
|
|
||||||
... shout=shout.id, main=True).first()
|
|
||||||
... assert main_topic.topic.slug == 'tech'
|
|
||||||
... return main_topic
|
|
||||||
"""
|
|
||||||
logger.info(f"Starting patch_main_topic for shout#{shout.id} with slug '{main_topic_slug}'")
|
logger.info(f"Starting patch_main_topic for shout#{shout.id} with slug '{main_topic_slug}'")
|
||||||
|
logger.debug(f"Current shout topics: {[(t.topic.slug, t.main) for t in shout.topics]}")
|
||||||
|
|
||||||
with session.begin():
|
with session.begin():
|
||||||
# Получаем текущий главный топик
|
# Получаем текущий главный топик
|
||||||
|
@ -292,7 +273,9 @@ def patch_main_topic(session, main_topic_slug, shout):
|
||||||
session.query(ShoutTopic).filter(and_(ShoutTopic.shout == shout.id, ShoutTopic.main.is_(True))).first()
|
session.query(ShoutTopic).filter(and_(ShoutTopic.shout == shout.id, ShoutTopic.main.is_(True))).first()
|
||||||
)
|
)
|
||||||
if old_main:
|
if old_main:
|
||||||
logger.info(f"Found current main topic: {old_main.topic}")
|
logger.info(f"Found current main topic: {old_main.topic.slug}")
|
||||||
|
else:
|
||||||
|
logger.info("No current main topic found")
|
||||||
|
|
||||||
# Находим новый главный топик
|
# Находим новый главный топик
|
||||||
main_topic = session.query(Topic).filter(Topic.slug == main_topic_slug).first()
|
main_topic = session.query(Topic).filter(Topic.slug == main_topic_slug).first()
|
||||||
|
@ -300,7 +283,7 @@ def patch_main_topic(session, main_topic_slug, shout):
|
||||||
logger.error(f"Main topic with slug '{main_topic_slug}' not found")
|
logger.error(f"Main topic with slug '{main_topic_slug}' not found")
|
||||||
return
|
return
|
||||||
|
|
||||||
logger.info(f"Found new main topic: {main_topic.id}")
|
logger.info(f"Found new main topic: {main_topic.slug} (id={main_topic.id})")
|
||||||
|
|
||||||
# Находим связь с новым главным топиком
|
# Находим связь с новым главным топиком
|
||||||
new_main = (
|
new_main = (
|
||||||
|
@ -308,9 +291,10 @@ def patch_main_topic(session, main_topic_slug, shout):
|
||||||
.filter(and_(ShoutTopic.shout == shout.id, ShoutTopic.topic == main_topic.id))
|
.filter(and_(ShoutTopic.shout == shout.id, ShoutTopic.topic == main_topic.id))
|
||||||
.first()
|
.first()
|
||||||
)
|
)
|
||||||
|
logger.debug(f"Found new main topic relation: {new_main is not None}")
|
||||||
|
|
||||||
if old_main and new_main and old_main is not new_main:
|
if old_main and new_main and old_main is not new_main:
|
||||||
logger.info("Updating main topic flags")
|
logger.info(f"Updating main topic flags: {old_main.topic.slug} -> {new_main.topic.slug}")
|
||||||
old_main.main = False
|
old_main.main = False
|
||||||
session.add(old_main)
|
session.add(old_main)
|
||||||
|
|
||||||
|
@ -319,6 +303,8 @@ def patch_main_topic(session, main_topic_slug, shout):
|
||||||
|
|
||||||
session.flush()
|
session.flush()
|
||||||
logger.info(f"Main topic updated for shout#{shout.id}")
|
logger.info(f"Main topic updated for shout#{shout.id}")
|
||||||
|
else:
|
||||||
|
logger.warning(f"No changes needed for main topic (old={old_main is not None}, new={new_main is not None})")
|
||||||
|
|
||||||
|
|
||||||
def patch_topics(session, shout, topics_input):
|
def patch_topics(session, shout, topics_input):
|
||||||
|
@ -648,21 +634,12 @@ async def delete_shout(_, info, shout_id: int):
|
||||||
|
|
||||||
|
|
||||||
def get_main_topic(topics):
|
def get_main_topic(topics):
|
||||||
"""Get the main topic from a list of ShoutTopic objects.
|
"""Get the main topic from a list of ShoutTopic objects."""
|
||||||
|
logger.info(f"Starting get_main_topic with {len(topics) if topics else 0} topics")
|
||||||
Args:
|
logger.debug(f"Topics data: {[(t.topic.slug if t.topic else 'no-topic', t.main) for t in topics] if topics else []}")
|
||||||
topics: List of ShoutTopic objects
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
dict: Main topic data with id, slug, title and is_main fields
|
|
||||||
|
|
||||||
Example:
|
|
||||||
>>> topics = [ShoutTopic(main=True, topic=Topic(id=1, slug='test', title='Test'))]
|
|
||||||
>>> result = get_main_topic(topics)
|
|
||||||
>>> assert result['id'] == 1
|
|
||||||
>>> assert result['is_main'] == True
|
|
||||||
"""
|
|
||||||
if not topics:
|
if not topics:
|
||||||
|
logger.warning("No topics provided to get_main_topic")
|
||||||
return {
|
return {
|
||||||
"id": 0,
|
"id": 0,
|
||||||
"title": "no topic",
|
"title": "no topic",
|
||||||
|
@ -672,24 +649,30 @@ def get_main_topic(topics):
|
||||||
|
|
||||||
# Find first main topic in original order
|
# Find first main topic in original order
|
||||||
main_topic_rel = next((st for st in topics if st.main), None)
|
main_topic_rel = next((st for st in topics if st.main), None)
|
||||||
|
logger.debug(f"Found main topic relation: {main_topic_rel.topic.slug if main_topic_rel and main_topic_rel.topic else None}")
|
||||||
|
|
||||||
if main_topic_rel and main_topic_rel.topic:
|
if main_topic_rel and main_topic_rel.topic:
|
||||||
return {
|
result = {
|
||||||
"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,
|
||||||
"is_main": True
|
"is_main": True
|
||||||
}
|
}
|
||||||
|
logger.info(f"Returning main topic: {result}")
|
||||||
|
return result
|
||||||
|
|
||||||
# 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:
|
||||||
return {
|
logger.info(f"No main topic found, using first topic: {topics[0].topic.slug}")
|
||||||
|
result = {
|
||||||
"slug": topics[0].topic.slug,
|
"slug": topics[0].topic.slug,
|
||||||
"title": topics[0].topic.title,
|
"title": topics[0].topic.title,
|
||||||
"id": topics[0].topic.id,
|
"id": topics[0].topic.id,
|
||||||
"is_main": True
|
"is_main": True
|
||||||
}
|
}
|
||||||
|
return result
|
||||||
|
|
||||||
|
logger.warning("No valid topics found, returning default")
|
||||||
return {
|
return {
|
||||||
"slug": "notopic",
|
"slug": "notopic",
|
||||||
"title": "no topic",
|
"title": "no topic",
|
||||||
|
|
|
@ -188,12 +188,15 @@ def get_shouts_with_links(info, q, limit=20, offset=0):
|
||||||
"""
|
"""
|
||||||
shouts = []
|
shouts = []
|
||||||
try:
|
try:
|
||||||
|
logger.info(f"Starting get_shouts_with_links with limit={limit}, offset={offset}")
|
||||||
q = q.limit(limit).offset(offset)
|
q = q.limit(limit).offset(offset)
|
||||||
|
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
shouts_result = session.execute(q).all()
|
shouts_result = session.execute(q).all()
|
||||||
|
logger.info(f"Got {len(shouts_result) if shouts_result else 0} shouts from query")
|
||||||
|
|
||||||
if not shouts_result:
|
if not shouts_result:
|
||||||
|
logger.warning("No shouts found in query result")
|
||||||
return []
|
return []
|
||||||
|
|
||||||
for idx, row in enumerate(shouts_result):
|
for idx, row in enumerate(shouts_result):
|
||||||
|
@ -201,6 +204,7 @@ def get_shouts_with_links(info, q, limit=20, offset=0):
|
||||||
shout = None
|
shout = None
|
||||||
if hasattr(row, "Shout"):
|
if hasattr(row, "Shout"):
|
||||||
shout = row.Shout
|
shout = row.Shout
|
||||||
|
logger.debug(f"Processing shout#{shout.id} at index {idx}")
|
||||||
if shout:
|
if shout:
|
||||||
shout_id = int(f"{shout.id}")
|
shout_id = int(f"{shout.id}")
|
||||||
shout_dict = shout.dict()
|
shout_dict = shout.dict()
|
||||||
|
@ -228,37 +232,34 @@ def get_shouts_with_links(info, q, limit=20, offset=0):
|
||||||
topics = None
|
topics = None
|
||||||
if has_field(info, "topics") and hasattr(row, "topics"):
|
if has_field(info, "topics") and hasattr(row, "topics"):
|
||||||
topics = json.loads(row.topics) if isinstance(row.topics, str) else row.topics
|
topics = json.loads(row.topics) if isinstance(row.topics, str) else row.topics
|
||||||
|
logger.debug(f"Shout#{shout_id} topics: {topics}")
|
||||||
shout_dict["topics"] = topics
|
shout_dict["topics"] = topics
|
||||||
|
|
||||||
if has_field(info, "main_topic"):
|
if has_field(info, "main_topic"):
|
||||||
main_topic = None
|
main_topic = None
|
||||||
if hasattr(row, "main_topic"):
|
if hasattr(row, "main_topic"):
|
||||||
|
logger.debug(f"Raw main_topic for shout#{shout_id}: {row.main_topic}")
|
||||||
main_topic = json.loads(row.main_topic) if isinstance(row.main_topic, str) else row.main_topic
|
main_topic = json.loads(row.main_topic) if isinstance(row.main_topic, str) else row.main_topic
|
||||||
|
logger.debug(f"Parsed main_topic for shout#{shout_id}: {main_topic}")
|
||||||
# Если main_topic не определен, ищем топик с main=True или берем первый
|
|
||||||
if not main_topic and topics:
|
if not main_topic and topics and len(topics) > 0:
|
||||||
# Сначала ищем топик с main=True
|
logger.info(f"No main_topic found for shout#{shout_id}, using first topic from list")
|
||||||
main_topic = next((t for t in topics if t.get("is_main")), None)
|
main_topic = {
|
||||||
|
"id": topics[0]["id"],
|
||||||
# Если не нашли main=True, берем первый топик
|
"title": topics[0]["title"],
|
||||||
if not main_topic and len(topics) > 0:
|
"slug": topics[0]["slug"],
|
||||||
main_topic = {
|
"is_main": True
|
||||||
"id": topics[0]["id"],
|
}
|
||||||
"title": topics[0]["title"],
|
elif not main_topic:
|
||||||
"slug": topics[0]["slug"],
|
logger.warning(f"No main_topic and no topics found for shout#{shout_id}")
|
||||||
"is_main": True
|
|
||||||
}
|
|
||||||
|
|
||||||
# Если все еще нет main_topic, используем заглушку
|
|
||||||
if not main_topic:
|
|
||||||
main_topic = {
|
main_topic = {
|
||||||
"id": 0,
|
"id": 0,
|
||||||
"title": "no topic",
|
"title": "no topic",
|
||||||
"slug": "notopic",
|
"slug": "notopic",
|
||||||
"is_main": True
|
"is_main": True
|
||||||
}
|
}
|
||||||
|
|
||||||
shout_dict["main_topic"] = main_topic
|
shout_dict["main_topic"] = main_topic
|
||||||
|
logger.debug(f"Final main_topic for shout#{shout_id}: {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"] = (
|
||||||
|
@ -278,12 +279,14 @@ def get_shouts_with_links(info, q, limit=20, offset=0):
|
||||||
shouts.append(shout_dict)
|
shouts.append(shout_dict)
|
||||||
|
|
||||||
except Exception as row_error:
|
except Exception as row_error:
|
||||||
logger.error(f"Ошибка при обработке строки {idx}: {row_error}", exc_info=True)
|
logger.error(f"Error processing row {idx}: {row_error}", exc_info=True)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Фатальная ошибка в get_shouts_with_links: {e}", exc_info=True)
|
logger.error(f"Fatal error in get_shouts_with_links: {e}", exc_info=True)
|
||||||
raise
|
raise
|
||||||
finally:
|
finally:
|
||||||
|
logger.info(f"Returning {len(shouts)} shouts from get_shouts_with_links")
|
||||||
return shouts
|
return shouts
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user