diff --git a/resolvers/zine/load.py b/resolvers/zine/load.py index eea0408f..3dbda0ee 100644 --- a/resolvers/zine/load.py +++ b/resolvers/zine/load.py @@ -31,36 +31,6 @@ def apply_filters(q, filters, user=None): q = q.filter(Shout.createdAt > before) return q - -def extract_order(o, q): - if o: - q = q.add_columns(sa.func.count(Reaction.id).label(o)) - if o == 'comments': - q = q.join(Reaction, Shout.slug == Reaction.shout) - q = q.filter(Reaction.body.is_not(None)) - elif o == 'reacted': - q = q.join( - Reaction - ).add_columns( - sa.func.max(Reaction.createdAt).label(o) - ) - elif o == "rating": - q = q.join(Reaction).add_columns(sa.func.sum(case( - (Reaction.kind == ReactionKind.AGREE, 1), - (Reaction.kind == ReactionKind.DISAGREE, -1), - (Reaction.kind == ReactionKind.PROOF, 1), - (Reaction.kind == ReactionKind.DISPROOF, -1), - (Reaction.kind == ReactionKind.ACCEPT, 1), - (Reaction.kind == ReactionKind.REJECT, -1), - (Reaction.kind == ReactionKind.LIKE, 1), - (Reaction.kind == ReactionKind.DISLIKE, -1), - else_=0 - )).label(o)) - return o - else: - return 'createdAt' - - @query.field("loadShout") async def load_shout(_, info, slug): with local_session() as session: @@ -109,7 +79,33 @@ async def load_shouts_by(_, info, options): user = info.context["request"].user q = apply_filters(q, options.get("filters"), user) - order_by = extract_order(options.get("order_by"), q) + o = options.get("order_by") + if o: + q = q.add_columns(sa.func.count(Reaction.id).label(o)) + if o == 'comments': + q = q.join(Reaction, Shout.slug == Reaction.shout) + q = q.filter(Reaction.body.is_not(None)) + elif o == 'reacted': + q = q.join( + Reaction + ).add_columns( + sa.func.max(Reaction.createdAt).label(o) + ) + elif o == "rating": + q = q.join(Reaction).add_columns(sa.func.sum(case( + (Reaction.kind == ReactionKind.AGREE, 1), + (Reaction.kind == ReactionKind.DISAGREE, -1), + (Reaction.kind == ReactionKind.PROOF, 1), + (Reaction.kind == ReactionKind.DISPROOF, -1), + (Reaction.kind == ReactionKind.ACCEPT, 1), + (Reaction.kind == ReactionKind.REJECT, -1), + (Reaction.kind == ReactionKind.LIKE, 1), + (Reaction.kind == ReactionKind.DISLIKE, -1), + else_=0 + )).label(o)) + order_by = o + else: + order_by = 'createdAt' order_by_desc = True if options.get('order_by_desc') is None else options.get('order_by_desc')