groupby-fix
This commit is contained in:
parent
34940178ad
commit
a8b8637057
|
@ -6,8 +6,7 @@ from services.auth import login_required
|
||||||
from services.db import local_session
|
from services.db import local_session
|
||||||
from services.schema import query
|
from services.schema import query
|
||||||
from orm.author import AuthorFollower, Author
|
from orm.author import AuthorFollower, Author
|
||||||
from orm.topic import TopicFollower, Topic
|
from orm.topic import TopicFollower
|
||||||
from orm.community import CommunityAuthor as CommunityFollower, Community
|
|
||||||
from orm.reaction import Reaction, ReactionKind
|
from orm.reaction import Reaction, ReactionKind
|
||||||
from orm.shout import Shout, ShoutAuthor, ShoutTopic, ShoutVisibility
|
from orm.shout import Shout, ShoutAuthor, ShoutTopic, ShoutVisibility
|
||||||
from services.search import SearchService
|
from services.search import SearchService
|
||||||
|
@ -18,7 +17,12 @@ def add_stat_columns(q):
|
||||||
aliased_reaction = aliased(Reaction)
|
aliased_reaction = aliased(Reaction)
|
||||||
q = q.outerjoin(aliased_reaction).add_columns(
|
q = q.outerjoin(aliased_reaction).add_columns(
|
||||||
func.sum(aliased_reaction.id).label("reacted_stat"),
|
func.sum(aliased_reaction.id).label("reacted_stat"),
|
||||||
func.sum(case((aliased_reaction.kind == ReactionKind.COMMENT.value, 1), else_=0)).label("commented_stat"),
|
func.sum(
|
||||||
|
case(
|
||||||
|
(aliased_reaction.kind == ReactionKind.COMMENT.value, 1),
|
||||||
|
else_=0
|
||||||
|
)
|
||||||
|
).label("commented_stat"),
|
||||||
func.sum(
|
func.sum(
|
||||||
case(
|
case(
|
||||||
(aliased_reaction.kind == ReactionKind.AGREE.value, 1),
|
(aliased_reaction.kind == ReactionKind.AGREE.value, 1),
|
||||||
|
@ -43,6 +47,7 @@ def add_stat_columns(q):
|
||||||
return q
|
return q
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def apply_filters(q, filters, author_id=None):
|
def apply_filters(q, filters, author_id=None):
|
||||||
# LoadShoutsFilters handling
|
# LoadShoutsFilters handling
|
||||||
if filters.get("reacted") and author_id:
|
if filters.get("reacted") and author_id:
|
||||||
|
@ -156,7 +161,7 @@ async def load_shouts_by(_, info, options):
|
||||||
q = apply_filters(q, options.get("filters", {}))
|
q = apply_filters(q, options.get("filters", {}))
|
||||||
|
|
||||||
# group
|
# group
|
||||||
q = q.group_by(Shout.id, Author.user)
|
q = q.group_by(Shout.id, Author.user, Author.name)
|
||||||
|
|
||||||
# order
|
# order
|
||||||
order_by = options.get("order_by", Shout.published_at)
|
order_by = options.get("order_by", Shout.published_at)
|
||||||
|
@ -177,6 +182,7 @@ async def load_shouts_by(_, info, options):
|
||||||
reacted_stat,
|
reacted_stat,
|
||||||
commented_stat,
|
commented_stat,
|
||||||
rating_stat,
|
rating_stat,
|
||||||
|
_last_comment
|
||||||
] in session.execute(q).unique():
|
] in session.execute(q).unique():
|
||||||
shouts.append(shout)
|
shouts.append(shout)
|
||||||
shout.stat = {
|
shout.stat = {
|
||||||
|
@ -184,6 +190,7 @@ async def load_shouts_by(_, info, options):
|
||||||
"reacted": reacted_stat,
|
"reacted": reacted_stat,
|
||||||
"commented": commented_stat,
|
"commented": commented_stat,
|
||||||
"rating": rating_stat,
|
"rating": rating_stat,
|
||||||
|
# "last_comment": last_comment
|
||||||
}
|
}
|
||||||
shouts_map[shout.id] = shout
|
shouts_map[shout.id] = shout
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user