diff --git a/resolvers/zine/load.py b/resolvers/zine/load.py index f9e984d4..46e7f028 100644 --- a/resolvers/zine/load.py +++ b/resolvers/zine/load.py @@ -1,7 +1,7 @@ from datetime import datetime, timedelta, timezone from sqlalchemy.orm import joinedload, aliased -from sqlalchemy.sql.expression import desc, asc, select, func, case, and_ +from sqlalchemy.sql.expression import desc, asc, select, func, case, and_, text, nulls_last from auth.authenticate import login_required from auth.credentials import AuthCredentials @@ -23,7 +23,7 @@ def add_stat_columns(q): ).label('reacted_stat'), func.sum( case( - (aliased_reaction.body.is_not(None), 1), + (aliased_reaction.kind == ReactionKind.COMMENT, 1), else_=0 ) ).label('commented_stat'), @@ -39,7 +39,11 @@ def add_stat_columns(q): (aliased_reaction.kind == ReactionKind.LIKE, 1), (aliased_reaction.kind == ReactionKind.DISLIKE, -1), else_=0) - ).label('rating_stat')) + ).label('rating_stat'), + func.max(case( + (aliased_reaction.kind != ReactionKind.COMMENT, None), + else_=aliased_reaction.createdAt + )).label('last_comment')) return q @@ -95,7 +99,7 @@ async def load_shout(_, info, slug=None, shout_id=None): ).group_by(Shout.id) try: - [shout, reacted_stat, commented_stat, rating_stat] = session.execute(q).first() + [shout, reacted_stat, commented_stat, rating_stat, last_comment] = session.execute(q).first() viewed_stat_query = select().select_from( Shout @@ -180,22 +184,19 @@ async def load_shouts_by(_, info, options): auth: AuthCredentials = info.context["request"].auth q = apply_filters(q, options.get("filters", {}), auth.user_id) - order_by = options.get("order_by", Shout.createdAt) - if order_by == 'reacted': - aliased_reaction = aliased(Reaction) - q.outerjoin(aliased_reaction).add_columns(func.max(aliased_reaction.createdAt).label('reacted')) + order_by = options.get("order_by", Shout.publishedAt) query_order_by = desc(order_by) if options.get('order_by_desc', True) else asc(order_by) offset = options.get("offset", 0) limit = options.get("limit", 10) - q = q.group_by(Shout.id).order_by(query_order_by).limit(limit).offset(offset) + q = q.group_by(Shout.id).order_by(nulls_last(query_order_by)).limit(limit).offset(offset) shouts = [] with local_session() as session: shouts_map = {} - for [shout, reacted_stat, commented_stat, rating_stat] in session.execute(q).unique(): + for [shout, reacted_stat, commented_stat, rating_stat, last_comment] in session.execute(q).unique(): shouts.append(shout) shout.stat = { "viewed": 0, @@ -238,8 +239,7 @@ async def get_my_feed(_, info, options): auth: AuthCredentials = info.context["request"].auth user_id = auth.user_id - subquery = select(Shout.id) - subquery = subquery.join( + subquery = select(Shout.id).join( ShoutAuthor ).join( AuthorFollower, AuthorFollower.follower == user_id @@ -264,20 +264,17 @@ async def get_my_feed(_, info, options): q = apply_filters(q, options.get("filters", {}), user_id) order_by = options.get("order_by", Shout.publishedAt) - if order_by == 'reacted': - aliased_reaction = aliased(Reaction) - q.outerjoin(aliased_reaction).add_columns(func.max(aliased_reaction.createdAt).label('reacted')) query_order_by = desc(order_by) if options.get('order_by_desc', True) else asc(order_by) offset = options.get("offset", 0) limit = options.get("limit", 10) - q = q.group_by(Shout.id).order_by(query_order_by).limit(limit).offset(offset) + q = q.group_by(Shout.id).order_by(nulls_last(query_order_by)).limit(limit).offset(offset) shouts = [] with local_session() as session: shouts_map = {} - for [shout, reacted_stat, commented_stat, rating_stat] in session.execute(q).unique(): + for [shout, reacted_stat, commented_stat, rating_stat, last_comment] in session.execute(q).unique(): shouts.append(shout) shout.stat = { "viewed": 0, diff --git a/test.py b/test.py deleted file mode 100644 index b47defdf..00000000 --- a/test.py +++ /dev/null @@ -1,26 +0,0 @@ -from sqlalchemy import select -from sqlalchemy.orm import joinedload - -from ai.preprocess import get_clear_text -from base.orm import local_session -from orm import Shout, Topic - -if __name__ == "__main__": - with local_session() as session: - q = select(Shout).options( - joinedload(Shout.authors), - joinedload(Shout.topics), - ).where( - Shout.deletedAt.is_(None) - ) - - for [shout] in session.execute(q).unique(): - print(shout.topics) - # clear_shout_body = get_clear_text(shout.body) - # print(clear_shout_body) - # - - topics_q = select(Topic) - for [topic] in session.execute(topics_q): - print(topic.body) -