This commit is contained in:
parent
c72588800f
commit
9416165699
|
@ -172,7 +172,7 @@ def get_shouts_with_stats(q, limit=20, offset=0, author_id=None):
|
||||||
.select_from(ShoutAuthor)
|
.select_from(ShoutAuthor)
|
||||||
.join(Author, ShoutAuthor.author == Author.id)
|
.join(Author, ShoutAuthor.author == Author.id)
|
||||||
.where(ShoutAuthor.shout == Shout.id)
|
.where(ShoutAuthor.shout == Shout.id)
|
||||||
.correlate(Shout) # Явная корреляция с таблицей Shout
|
.correlate(Shout)
|
||||||
.scalar_subquery()
|
.scalar_subquery()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -197,16 +197,24 @@ def get_shouts_with_stats(q, limit=20, offset=0, author_id=None):
|
||||||
.select_from(ShoutTopic)
|
.select_from(ShoutTopic)
|
||||||
.join(Topic, ShoutTopic.topic == Topic.id)
|
.join(Topic, ShoutTopic.topic == Topic.id)
|
||||||
.where(ShoutTopic.shout == Shout.id)
|
.where(ShoutTopic.shout == Shout.id)
|
||||||
.group_by(ShoutTopic.shout)
|
.correlate(Shout)
|
||||||
.correlate(Shout) # Явная корреляция с таблицей Shout
|
|
||||||
.scalar_subquery()
|
.scalar_subquery()
|
||||||
)
|
)
|
||||||
|
|
||||||
# Скалярный подзапрос для последней реакции
|
# Скалярный подзапрос для основного топика
|
||||||
last_reaction_subquery = (
|
main_topic_subquery = (
|
||||||
select(func.max(Reaction.created_at).label("last_reacted_at"))
|
select(
|
||||||
.where(Reaction.shout == Shout.id, Reaction.deleted_at.is_(None))
|
func.max(Topic.slug).label("main_topic_slug")
|
||||||
.correlate(Shout) # Явная корреляция с таблицей Shout
|
)
|
||||||
|
.select_from(ShoutTopic)
|
||||||
|
.join(Topic, ShoutTopic.topic == Topic.id)
|
||||||
|
.where(
|
||||||
|
and_(
|
||||||
|
ShoutTopic.shout == Shout.id,
|
||||||
|
ShoutTopic.main.is_(True),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.correlate(Shout)
|
||||||
.scalar_subquery()
|
.scalar_subquery()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -214,35 +222,34 @@ def get_shouts_with_stats(q, limit=20, offset=0, author_id=None):
|
||||||
query = (
|
query = (
|
||||||
select(
|
select(
|
||||||
Shout,
|
Shout,
|
||||||
func.count(Reaction.id.distinct()).label("comments_stat"),
|
func.count(distinct(Reaction.id)).label("comments_stat"),
|
||||||
func.sum(
|
func.sum(
|
||||||
case(
|
case(
|
||||||
(Reaction.kind == "LIKE", 1),
|
[(Reaction.kind == "LIKE", 1),
|
||||||
(Reaction.kind == "DISLIKE", -1),
|
(Reaction.kind == "DISLIKE", -1)],
|
||||||
else_=0
|
else_=0
|
||||||
)
|
)
|
||||||
).label("rating_stat"),
|
).label("rating_stat"),
|
||||||
last_reaction_subquery,
|
func.max(Reaction.created_at).label("last_reacted_at"),
|
||||||
authors_subquery,
|
authors_subquery,
|
||||||
topics_subquery,
|
topics_subquery,
|
||||||
func.coalesce(
|
main_topic_subquery
|
||||||
func.json_extract_path_text(topics_subquery, 'main_topic_slug'),
|
|
||||||
''
|
|
||||||
).label("main_topic_slug")
|
|
||||||
)
|
)
|
||||||
.join(Reaction, Reaction.shout == Shout.id, isouter=True)
|
.outerjoin(Reaction, Reaction.shout == Shout.id)
|
||||||
.where(
|
.where(
|
||||||
Shout.published_at.is_not(None),
|
and_(
|
||||||
Shout.deleted_at.is_(None),
|
Shout.published_at.isnot(None),
|
||||||
Shout.featured_at.is_not(None)
|
Shout.deleted_at.is_(None),
|
||||||
|
Shout.featured_at.isnot(None)
|
||||||
|
)
|
||||||
)
|
)
|
||||||
.group_by(Shout.id)
|
.group_by(Shout.id)
|
||||||
.order_by(Shout.published_at.desc().nullslast())
|
.order_by(Shout.published_at.desc().nulls_last())
|
||||||
.limit(limit)
|
.limit(limit)
|
||||||
.offset(offset)
|
.offset(offset)
|
||||||
)
|
)
|
||||||
|
|
||||||
# Добавление фильтрации по author_id, если необходимо
|
# Применение дополнительных фильтров, если необходимо
|
||||||
if author_id:
|
if author_id:
|
||||||
query = query.filter(Shout.created_by == author_id)
|
query = query.filter(Shout.created_by == author_id)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user