desc-order-fix

This commit is contained in:
Untone 2024-02-25 17:58:09 +03:00
parent 2e635abe5e
commit 309ac2d929
3 changed files with 6 additions and 6 deletions

View File

@ -1,7 +1,7 @@
import json
import time
from sqlalchemy import select, or_, and_, text
from sqlalchemy import select, or_, and_, text, desc
from sqlalchemy.orm import aliased
from sqlalchemy_searchable import search
@ -107,7 +107,7 @@ def load_authors_by(_, _info, by, limit, offset):
order = by.get('order')
if order in ['likes', 'shouts', 'followers']:
q = q.order_by(text(f'{order}_stat'))
q = q.order_by(desc(text(f'{order}_stat')))
q = q.limit(limit).offset(offset)

View File

@ -1,7 +1,7 @@
import time
from typing import List
from sqlalchemy import and_, case, desc, func, select
from sqlalchemy import and_, case, desc, func, select, text
from sqlalchemy.orm import aliased, joinedload
from sqlalchemy.sql import union
@ -373,7 +373,7 @@ async def load_reactions_by(_, info, by, limit=50, offset=0):
q = q.group_by(Reaction.id, Author.id, Shout.id, aliased_reaction.id)
# order by
q = q.order_by(desc('created_at'))
q = q.order_by(desc(text('created_at')))
# pagination
q = q.limit(limit).offset(offset)
@ -445,7 +445,7 @@ async def reacted_shouts_updates(follower_id: int, limit=50, offset=0) -> List[S
# Sort shouts by the `last_comment` field
combined_query = (
union(q1, q2).order_by(desc('last_comment_stat')).limit(limit).offset(offset)
union(q1, q2).order_by(desc(text('last_comment_stat'))).limit(limit).offset(offset)
)
results = session.execute(combined_query).scalars()

View File

@ -146,7 +146,7 @@ async def load_shouts_by(_, _info, options):
order_by = Shout.featured_at if filters.get('featured') else Shout.published_at
order_str = options.get('order_by')
if order_str in ['likes', 'shouts', 'followers', 'comments', 'last_comment']:
q = q.order_by(text(f'{order_str}_stat'))
q = q.order_by(desc(text(f'{order_str}_stat')))
query_order_by = (
desc(order_by) if options.get('order_by_desc', True) else asc(order_by)
)