create-shout-fix6

This commit is contained in:
Untone 2025-01-22 00:43:59 +03:00
parent 76a4c5fb53
commit 1f4b3d3eee
2 changed files with 13 additions and 10 deletions

View File

@ -322,9 +322,12 @@ async def update_shout(_, info, shout_id: int, shout_input=None, publish=False):
shout_input["updated_at"] = current_time shout_input["updated_at"] = current_time
if publish: if publish:
logger.info(f"publishing shout#{shout_id} with input: {shout_input}") logger.info(f"Publishing shout#{shout_id}")
logger.debug(f"Before update: published_at={shout_by_id.published_at}")
shout_input["published_at"] = current_time shout_input["published_at"] = current_time
Shout.update(shout_by_id, shout_input) Shout.update(shout_by_id, shout_input)
logger.debug(f"After update: published_at={shout_by_id.published_at}")
Shout.update(shout_by_id, shout_input)
session.add(shout_by_id) session.add(shout_by_id)
session.commit() session.commit()

View File

@ -330,9 +330,7 @@ def apply_sorting(q, options):
order_str = options.get("order_by") order_str = options.get("order_by")
if order_str in ["rating", "comments_count", "last_commented_at"]: if order_str in ["rating", "comments_count", "last_commented_at"]:
query_order_by = desc(text(order_str)) if options.get("order_by_desc", True) else asc(text(order_str)) query_order_by = desc(text(order_str)) if options.get("order_by_desc", True) else asc(text(order_str))
q = q.distinct(text(order_str), Shout.id).order_by( # DISTINCT ON включает поле сортировки q = q.distinct(text(order_str), Shout.id).order_by(nulls_last(query_order_by), Shout.id)
nulls_last(query_order_by), Shout.id
)
else: else:
q = q.distinct(Shout.published_at, Shout.id).order_by(Shout.published_at.desc(), Shout.id) q = q.distinct(Shout.published_at, Shout.id).order_by(Shout.published_at.desc(), Shout.id)
@ -343,19 +341,21 @@ def apply_sorting(q, options):
async def load_shouts_by(_, info: GraphQLResolveInfo, options): async def load_shouts_by(_, info: GraphQLResolveInfo, options):
""" """
Загрузка публикаций с фильтрацией, сортировкой и пагинацией. Загрузка публикаций с фильтрацией, сортировкой и пагинацией.
:param _: Корневой объект запроса (не используется)
:param info: Информация о контексте GraphQL
:param options: Опции фильтрации и сортировки
:return: Список публикаций, удовлетворяющих критериям
""" """
# Базовый запрос со статистикой # Базовый запрос со статистикой
q = query_with_stat(info) q = query_with_stat(info)
logger.debug(f"Base query created with options: {options}")
# Применяем остальные опции фильтрации # Применяем остальные опции фильтрации
q, limit, offset = apply_options(q, options) q, limit, offset = apply_options(q, options)
# Передача сформированного запроса в метод получения публикаций с учетом сортировки и пагинации # Логируем SQL запрос для отладки
from sqlalchemy.dialects import postgresql
sql = q.compile(dialect=postgresql.dialect(), compile_kwargs={"literal_binds": True})
logger.debug(f"Final SQL query: {sql}")
# Передача сформированного запроса в метод получения публикаций
return get_shouts_with_links(info, q, limit, offset) return get_shouts_with_links(info, q, limit, offset)