rating-fix-2
All checks were successful
Deploy on push / deploy (push) Successful in 25s

This commit is contained in:
Untone 2024-03-29 02:29:16 +03:00
parent 9cc0c5b011
commit 3f68e25230
2 changed files with 25 additions and 10 deletions

View File

@ -1,4 +1,4 @@
from sqlalchemy import and_, func, case, true, select, distinct
from sqlalchemy import and_, func, case, true, select
from sqlalchemy.orm import aliased
from orm.author import AuthorRating, Author
@ -171,21 +171,36 @@ def add_rating_columns(q, group_list):
# by comments
replied_comment = aliased(Reaction)
comments_subq = select(Reaction).where(
comments_subq = select(
Author.id,
func.coalesce(func.sum(
case(
(Reaction.kind == ReactionKind.LIKE.value, 1),
(Reaction.kind == ReactionKind.DISLIKE.value, -1),
else_=0
)
)).label('comments_rating'),
).select_from(Reaction).outerjoin(
replied_comment,
and_(
replied_comment.kind == ReactionKind.COMMENT.value,
replied_comment.created_by == Author.id,
Reaction.reply_to == replied_comment.id,
Reaction.kind.in_([ReactionKind.LIKE.value, ReactionKind.DISLIKE.value]),
Reaction.reply_to == replied_comment.id,
Reaction.deleted_at.is_(None)
)
).subquery()
).group_by(Author.id).subquery()
q = q.outerjoin(comments_subq, comments_subq.c.reply_to == replied_comment.id)
q = q.add_columns(
func.count(distinct(case((comments_subq.c.kind == ReactionKind.LIKE.value, 1)))).label('comments_likes'),
func.count(distinct(case((comments_subq.c.kind == ReactionKind.DISLIKE.value, 1)))).label('comments_dislikes'),
func.coalesce(func.sum(
case(
(Reaction.kind == ReactionKind.LIKE.value, 1),
(Reaction.kind == ReactionKind.DISLIKE.value, -1),
else_=0
)
)).label('comments_rating')
)
group_list.extend([comments_subq.c.comments_likes, comments_subq.c.comments_dislikes])
group_list.extend([comments_subq.c.comments_rating])
return q, group_list

View File

@ -135,9 +135,9 @@ def get_with_stat(q, with_rating=False):
stat['comments'] = cols[4]
if with_rating:
logger.debug(cols)
stat['rating'] = cols[6] - cols[7]
stat['rating_shouts'] = cols[8] - cols[9]
stat['rating_comments'] = cols[10] - cols[11]
stat['rating'] = cols[6]
stat['rating_shouts'] = cols[7]
stat['rating_comments'] = cols[8]
entity.stat = stat
records.append(entity)
except Exception as exc: