This commit is contained in:
parent
9cc0c5b011
commit
3f68e25230
|
@ -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 sqlalchemy.orm import aliased
|
||||||
|
|
||||||
from orm.author import AuthorRating, Author
|
from orm.author import AuthorRating, Author
|
||||||
|
@ -171,21 +171,36 @@ def add_rating_columns(q, group_list):
|
||||||
|
|
||||||
# by comments
|
# by comments
|
||||||
replied_comment = aliased(Reaction)
|
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_(
|
and_(
|
||||||
replied_comment.kind == ReactionKind.COMMENT.value,
|
replied_comment.kind == ReactionKind.COMMENT.value,
|
||||||
replied_comment.created_by == Author.id,
|
replied_comment.created_by == Author.id,
|
||||||
Reaction.reply_to == replied_comment.id,
|
|
||||||
Reaction.kind.in_([ReactionKind.LIKE.value, ReactionKind.DISLIKE.value]),
|
Reaction.kind.in_([ReactionKind.LIKE.value, ReactionKind.DISLIKE.value]),
|
||||||
|
Reaction.reply_to == replied_comment.id,
|
||||||
Reaction.deleted_at.is_(None)
|
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.outerjoin(comments_subq, comments_subq.c.reply_to == replied_comment.id)
|
||||||
q = q.add_columns(
|
q = q.add_columns(
|
||||||
func.count(distinct(case((comments_subq.c.kind == ReactionKind.LIKE.value, 1)))).label('comments_likes'),
|
func.coalesce(func.sum(
|
||||||
func.count(distinct(case((comments_subq.c.kind == ReactionKind.DISLIKE.value, 1)))).label('comments_dislikes'),
|
case(
|
||||||
|
(Reaction.kind == ReactionKind.LIKE.value, 1),
|
||||||
|
(Reaction.kind == ReactionKind.DISLIKE.value, -1),
|
||||||
|
else_=0
|
||||||
)
|
)
|
||||||
group_list.extend([comments_subq.c.comments_likes, comments_subq.c.comments_dislikes])
|
)).label('comments_rating')
|
||||||
|
)
|
||||||
|
group_list.extend([comments_subq.c.comments_rating])
|
||||||
|
|
||||||
return q, group_list
|
return q, group_list
|
||||||
|
|
|
@ -135,9 +135,9 @@ def get_with_stat(q, with_rating=False):
|
||||||
stat['comments'] = cols[4]
|
stat['comments'] = cols[4]
|
||||||
if with_rating:
|
if with_rating:
|
||||||
logger.debug(cols)
|
logger.debug(cols)
|
||||||
stat['rating'] = cols[6] - cols[7]
|
stat['rating'] = cols[6]
|
||||||
stat['rating_shouts'] = cols[8] - cols[9]
|
stat['rating_shouts'] = cols[7]
|
||||||
stat['rating_comments'] = cols[10] - cols[11]
|
stat['rating_comments'] = cols[8]
|
||||||
entity.stat = stat
|
entity.stat = stat
|
||||||
records.append(entity)
|
records.append(entity)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user