reaction-by-fix4
All checks were successful
Deploy on push / deploy (push) Successful in 45s

This commit is contained in:
Untone 2025-04-26 15:57:51 +03:00
parent af7fbd2fc9
commit 6d9513f1b2

View File

@ -64,9 +64,9 @@ async def load_drafts(_, info):
if not user_id or not author_id:
return {"error": "User ID and author ID are required"}
try:
with local_session() as session:
# Предзагружаем authors и topics, т.к. они lazy='select' в модели
# created_by, updated_by, deleted_by загрузятся автоматически (lazy='joined')
# Предзагружаем authors и topics
drafts = (
session.query(Draft)
.options(
@ -78,7 +78,18 @@ async def load_drafts(_, info):
.all()
)
return {"drafts": drafts}
# Преобразуем объекты в словари, пока они в контексте сессии
drafts_data = []
for draft in drafts:
draft_dict = draft.dict()
draft_dict["topics"] = [topic.dict() for topic in draft.topics]
draft_dict["authors"] = [author.dict() for author in draft.authors]
drafts_data.append(draft_dict)
return {"drafts": drafts_data}
except Exception as e:
logger.error(f"Failed to load drafts: {e}", exc_info=True)
return {"error": f"Failed to load drafts: {str(e)}"}
@mutation.field("create_draft")
@ -272,17 +283,17 @@ async def publish_draft(_, info, draft_id: int):
try:
with local_session() as session:
shout_id = session.query(Draft.shout).filter(Draft.id == draft_id).first()
shout = session.query(Shout).filter(Shout.id == shout_id).first()
if not shout:
return {"error": "Shout not found"}
was_published = shout.published_at is not None
draft = session.query(Draft).where(Draft.id == shout.draft).first()
# Сначала находим черновик
draft = session.query(Draft).filter(Draft.id == draft_id).first()
if not draft:
return {"error": "Draft not found"}
# Находим черновик если не передан
# Ищем существующий shout для этого черновика
shout = session.query(Shout).filter(Shout.draft == draft_id).first()
was_published = shout.published_at if shout else None
if not shout:
# Создаем новый shout если не существует
shout = create_shout_from_draft(session, draft, author_id)
else:
# Обновляем существующую публикацию