unpublish,delete-draft-fix
All checks were successful
Deploy on push / deploy (push) Successful in 1m23s
All checks were successful
Deploy on push / deploy (push) Successful in 1m23s
This commit is contained in:
parent
5f3d90fc90
commit
58ec60262b
|
@ -327,7 +327,7 @@ async def delete_draft(_, info, draft_id: int):
|
||||||
draft = session.query(Draft).filter(Draft.id == draft_id).first()
|
draft = session.query(Draft).filter(Draft.id == draft_id).first()
|
||||||
if not draft:
|
if not draft:
|
||||||
return {"error": "Draft not found"}
|
return {"error": "Draft not found"}
|
||||||
if author_id != draft.created_by_id and draft.authors.filter(Author.id == author_id).count() == 0:
|
if author_id != draft.created_by.id and draft.authors.filter(Author.id == author_id).count() == 0:
|
||||||
return {"error": "You are not allowed to delete this draft"}
|
return {"error": "You are not allowed to delete this draft"}
|
||||||
session.delete(draft)
|
session.delete(draft)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
|
@ -3,7 +3,7 @@ import time
|
||||||
import orjson
|
import orjson
|
||||||
import trafilatura
|
import trafilatura
|
||||||
from sqlalchemy import and_, desc, select
|
from sqlalchemy import and_, desc, select
|
||||||
from sqlalchemy.orm import joinedload
|
from sqlalchemy.orm import joinedload, selectinload
|
||||||
from sqlalchemy.sql.functions import coalesce
|
from sqlalchemy.sql.functions import coalesce
|
||||||
|
|
||||||
from cache.cache import (
|
from cache.cache import (
|
||||||
|
@ -711,27 +711,30 @@ async def unpublish_shout(_, info, shout_id: int):
|
||||||
shout = None
|
shout = None
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
try:
|
try:
|
||||||
# Загружаем Shout с предзагрузкой draft и его связей authors/topics
|
# Сначала загружаем Shout без связей
|
||||||
# Используем selectinload для коллекций authors/topics внутри draft -
|
shout = session.query(Shout).filter(Shout.id == shout_id).first()
|
||||||
# это может быть эффективнее joinedload, если draft один.
|
|
||||||
shout = (
|
|
||||||
session.query(Shout)
|
|
||||||
.options(
|
|
||||||
joinedload(Shout.draft) # Загружаем сам черновик
|
|
||||||
.selectinload(Draft.authors), # Загружаем авторов черновика через отдельный запрос
|
|
||||||
joinedload(Shout.draft)
|
|
||||||
.selectinload(Draft.topics) # Загружаем темы черновика через отдельный запрос
|
|
||||||
# Также предзагружаем авторов самой публикации, если они нужны для проверки прав или возврата
|
|
||||||
# selectinload(Shout.authors)
|
|
||||||
)
|
|
||||||
.filter(Shout.id == shout_id)
|
|
||||||
.first()
|
|
||||||
)
|
|
||||||
|
|
||||||
if not shout:
|
if not shout:
|
||||||
logger.warning(f"Shout not found for unpublish: ID {shout_id}")
|
logger.warning(f"Shout not found for unpublish: ID {shout_id}")
|
||||||
return {"error": "Shout not found"}
|
return {"error": "Shout not found"}
|
||||||
|
|
||||||
|
# Если у публикации есть связанный черновик, загружаем его с relationships
|
||||||
|
if shout.draft:
|
||||||
|
# Отдельно загружаем черновик с его связями
|
||||||
|
draft = (
|
||||||
|
session.query(Draft)
|
||||||
|
.options(
|
||||||
|
selectinload(Draft.authors),
|
||||||
|
selectinload(Draft.topics)
|
||||||
|
)
|
||||||
|
.filter(Draft.id == shout.draft)
|
||||||
|
.first()
|
||||||
|
)
|
||||||
|
|
||||||
|
# Связываем черновик с публикацией вручную для доступа через API
|
||||||
|
if draft:
|
||||||
|
shout.draft_obj = draft
|
||||||
|
|
||||||
# TODO: Добавить проверку прав доступа, если необходимо
|
# TODO: Добавить проверку прав доступа, если необходимо
|
||||||
# if author_id not in [a.id for a in shout.authors]: # Требует selectinload(Shout.authors) выше
|
# if author_id not in [a.id for a in shout.authors]: # Требует selectinload(Shout.authors) выше
|
||||||
# logger.warning(f"Author {author_id} denied unpublishing shout {shout_id}")
|
# logger.warning(f"Author {author_id} denied unpublishing shout {shout_id}")
|
||||||
|
@ -754,7 +757,7 @@ async def unpublish_shout(_, info, shout_id: int):
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
session.rollback()
|
session.rollback()
|
||||||
logger.error(f"Failed to unpublish shout {shout_id}: {e}", exc_info=True)
|
logger.error(f"Failed to unpublish shout {shout_id}: {e}", exc_info=True)
|
||||||
return {"error": "Failed to unpublish shout"}
|
return {"error": f"Failed to unpublish shout: {str(e)}"}
|
||||||
|
|
||||||
# Возвращаем объект shout с предзагруженным draft и его связями
|
# Возвращаем объект shout с предзагруженным draft и его связями
|
||||||
logger.info(f"Shout {shout_id} unpublished successfully by author {author_id}")
|
logger.info(f"Shout {shout_id} unpublished successfully by author {author_id}")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user