This commit is contained in:
parent
fd6b0ce5fd
commit
2db1da3194
|
@ -260,7 +260,7 @@ def patch_topics(session, shout, topics_input):
|
||||||
if new_topics_to_link:
|
if new_topics_to_link:
|
||||||
logger.info(f"Creating new topics: {[t.dict() for t in new_topics_to_link]}")
|
logger.info(f"Creating new topics: {[t.dict() for t in new_topics_to_link]}")
|
||||||
session.add_all(new_topics_to_link)
|
session.add_all(new_topics_to_link)
|
||||||
session.flush() # Получаем ID для новых топиков
|
session.flush()
|
||||||
|
|
||||||
# Получаем текущие связи
|
# Получаем текущие связи
|
||||||
current_links = session.query(ShoutTopic).filter(ShoutTopic.shout == shout.id).all()
|
current_links = session.query(ShoutTopic).filter(ShoutTopic.shout == shout.id).all()
|
||||||
|
@ -277,28 +277,19 @@ def patch_topics(session, shout, topics_input):
|
||||||
for topic_input in topics_input:
|
for topic_input in topics_input:
|
||||||
topic_id = topic_input["id"]
|
topic_id = topic_input["id"]
|
||||||
if topic_id < 0:
|
if topic_id < 0:
|
||||||
# Для новых топиков берем ID из созданных
|
|
||||||
topic = next(t for t in new_topics_to_link if t.slug == topic_input["slug"])
|
topic = next(t for t in new_topics_to_link if t.slug == topic_input["slug"])
|
||||||
topic_id = topic.id
|
topic_id = topic.id
|
||||||
|
|
||||||
logger.info(f"Creating new topic link: shout#{shout.id} -> topic#{topic_id}")
|
logger.info(f"Creating new topic link: shout#{shout.id} -> topic#{topic_id}")
|
||||||
new_link = ShoutTopic(
|
new_link = ShoutTopic(shout=shout.id, topic=topic_id, main=False)
|
||||||
shout=shout.id,
|
|
||||||
topic=topic_id,
|
|
||||||
main=False, # main topic устанавливается отдельно через patch_main_topic
|
|
||||||
)
|
|
||||||
session.add(new_link)
|
session.add(new_link)
|
||||||
|
|
||||||
try:
|
|
||||||
session.flush()
|
session.flush()
|
||||||
logger.info(f"Successfully updated topics for shout#{shout.id}")
|
# Обновляем связи в объекте шаута
|
||||||
except Exception as e:
|
session.refresh(shout)
|
||||||
logger.error(f"Error flushing topic changes: {e}", exc_info=True)
|
|
||||||
raise
|
|
||||||
|
|
||||||
# Проверяем результат
|
logger.info(f"Successfully updated topics for shout#{shout.id}")
|
||||||
new_links = session.query(ShoutTopic).filter(ShoutTopic.shout == shout.id).all()
|
logger.info(f"Final shout topics: {[t.dict() for t in shout.topics]}")
|
||||||
logger.info(f"New topic links: {[{t.topic: t.main} for t in new_links]}")
|
|
||||||
|
|
||||||
|
|
||||||
@mutation.field("update_shout")
|
@mutation.field("update_shout")
|
||||||
|
@ -328,7 +319,12 @@ async def update_shout(_, info, shout_id: int, shout_input=None, publish=False):
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
if author_id:
|
if author_id:
|
||||||
logger.info(f"Processing update for shout#{shout_id} by author #{author_id}")
|
logger.info(f"Processing update for shout#{shout_id} by author #{author_id}")
|
||||||
shout_by_id = session.query(Shout).filter(Shout.id == shout_id).first()
|
shout_by_id = (
|
||||||
|
session.query(Shout)
|
||||||
|
.options(joinedload(Shout.authors), joinedload(Shout.topics))
|
||||||
|
.filter(Shout.id == shout_id)
|
||||||
|
.first()
|
||||||
|
)
|
||||||
|
|
||||||
if not shout_by_id:
|
if not shout_by_id:
|
||||||
logger.error(f"shout#{shout_id} not found")
|
logger.error(f"shout#{shout_id} not found")
|
||||||
|
@ -364,6 +360,10 @@ async def update_shout(_, info, shout_id: int, shout_input=None, publish=False):
|
||||||
try:
|
try:
|
||||||
patch_topics(session, shout_by_id, topics_input)
|
patch_topics(session, shout_by_id, topics_input)
|
||||||
logger.info(f"Successfully patched topics for shout#{shout_id}")
|
logger.info(f"Successfully patched topics for shout#{shout_id}")
|
||||||
|
|
||||||
|
# Обновляем связи в сессии после patch_topics
|
||||||
|
session.refresh(shout_by_id)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error patching topics: {e}", exc_info=True)
|
logger.error(f"Error patching topics: {e}", exc_info=True)
|
||||||
return {"error": f"Failed to update topics: {str(e)}"}
|
return {"error": f"Failed to update topics: {str(e)}"}
|
||||||
|
@ -408,6 +408,8 @@ async def update_shout(_, info, shout_id: int, shout_input=None, publish=False):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
session.commit()
|
session.commit()
|
||||||
|
# Обновляем объект после коммита чтобы получить все связи
|
||||||
|
session.refresh(shout_by_id)
|
||||||
logger.info(f"Successfully committed updates for shout#{shout_id}")
|
logger.info(f"Successfully committed updates for shout#{shout_id}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Commit failed: {e}", exc_info=True)
|
logger.error(f"Commit failed: {e}", exc_info=True)
|
||||||
|
@ -459,7 +461,9 @@ async def update_shout(_, info, shout_id: int, shout_input=None, publish=False):
|
||||||
for a in shout_by_id.authors:
|
for a in shout_by_id.authors:
|
||||||
await cache_by_id(Author, a.id, cache_author)
|
await cache_by_id(Author, a.id, cache_author)
|
||||||
logger.info(f"shout#{shout_id} updated")
|
logger.info(f"shout#{shout_id} updated")
|
||||||
return {"shout": shout_by_id.dict(), "error": None}
|
shout_dict = shout_by_id.dict()
|
||||||
|
logger.info(f"Final shout data: {shout_dict}") # Добавляем лог финальных данных
|
||||||
|
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}")
|
||||||
return {"error": "access denied", "shout": None}
|
return {"error": "access denied", "shout": None}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user