get-shouts-with-stats-fix5
This commit is contained in:
parent
f61a2d07fe
commit
0f87ac6a00
|
@ -146,7 +146,7 @@ def query_shouts(slug=None, shout_id=None):
|
||||||
return q, last_reaction
|
return q, last_reaction
|
||||||
|
|
||||||
|
|
||||||
def get_shouts_with_stats(q, limit=50, offset=0, author_id=None):
|
def get_shouts_with_stats(q, limit=10, offset=0, author_id=None):
|
||||||
"""
|
"""
|
||||||
Получение публикаций со статистикой, и подзапросами авторов и тем.
|
Получение публикаций со статистикой, и подзапросами авторов и тем.
|
||||||
|
|
||||||
|
@ -155,9 +155,9 @@ def get_shouts_with_stats(q, limit=50, offset=0, author_id=None):
|
||||||
:param offset: Смещение для пагинации.
|
:param offset: Смещение для пагинации.
|
||||||
:return: Список публикаций с включенной статистикой.
|
:return: Список публикаций с включенной статистикой.
|
||||||
"""
|
"""
|
||||||
# Определение алиасов подзапросов авторов и тем (предполагается, что они уже определены ранее)
|
# Определение алиасов подзапросов авторов и тем
|
||||||
authors_subquery = (
|
authors_subquery = (
|
||||||
select(
|
q.session.query(
|
||||||
ShoutAuthor.shout.label("shout_id"),
|
ShoutAuthor.shout.label("shout_id"),
|
||||||
func.json_agg(
|
func.json_agg(
|
||||||
func.json_build_object(
|
func.json_build_object(
|
||||||
|
@ -235,9 +235,6 @@ def get_shouts_with_stats(q, limit=50, offset=0, author_id=None):
|
||||||
)
|
)
|
||||||
.group_by(
|
.group_by(
|
||||||
Shout.id,
|
Shout.id,
|
||||||
authors_subquery.c.authors,
|
|
||||||
topics_subquery.c.topics,
|
|
||||||
topics_subquery.c.main_topic_slug,
|
|
||||||
last_reaction.c.last_reacted_at
|
last_reaction.c.last_reacted_at
|
||||||
)
|
)
|
||||||
.order_by(Shout.published_at.desc().nullslast())
|
.order_by(Shout.published_at.desc().nullslast())
|
||||||
|
@ -245,9 +242,13 @@ def get_shouts_with_stats(q, limit=50, offset=0, author_id=None):
|
||||||
.offset(offset)
|
.offset(offset)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Добавление фильтрации по author_id, если необходимо
|
||||||
|
if author_id:
|
||||||
|
query = q.filter(Shout.created_by == author_id)
|
||||||
|
|
||||||
# Выполнение запроса и обработка результатов
|
# Выполнение запроса и обработка результатов
|
||||||
with local_session() as session:
|
with q.session as session:
|
||||||
results = session.execute(q, {"author_id": author_id}).all() if author_id else session.execute(q).all()
|
results = session.execute(query).all()
|
||||||
|
|
||||||
# Формирование списка публикаций с их данными
|
# Формирование списка публикаций с их данными
|
||||||
shouts = []
|
shouts = []
|
||||||
|
|
Loading…
Reference in New Issue
Block a user