|
1 | 1 | import logging |
2 | 2 | from datetime import datetime |
3 | | -from typing import cast |
| 3 | +from typing import Callable, cast |
4 | 4 |
|
5 | 5 | import sqlalchemy as sa |
6 | 6 | from aiohttp import web |
@@ -160,11 +160,11 @@ def _set_ordering( |
160 | 160 | base_query: GenerativeSelect, |
161 | 161 | order_by: OrderBy, |
162 | 162 | ) -> GenerativeSelect: |
163 | | - if order_by.direction == OrderDirection.ASC: |
164 | | - list_query = base_query.order_by(asc(getattr(folders_v2.c, order_by.field))) |
165 | | - else: |
166 | | - list_query = base_query.order_by(desc(getattr(folders_v2.c, order_by.field))) |
167 | | - return list_query |
| 163 | + direction_func: Callable = {OrderDirection.ASC: asc, OrderDirection: desc}[ |
| 164 | + order_by.direction |
| 165 | + ] |
| 166 | + column = getattr(folders_v2.c, order_by.field) |
| 167 | + return base_query.order_by(direction_func(column)) |
168 | 168 |
|
169 | 169 |
|
170 | 170 | async def list_( # pylint: disable=too-many-arguments,too-many-branches |
@@ -247,8 +247,9 @@ async def list_( # pylint: disable=too-many-arguments,too-many-branches |
247 | 247 | count_query = select(func.count()).select_from(combined_query.subquery()) |
248 | 248 |
|
249 | 249 | # Ordering and pagination |
250 | | - list_query = _set_ordering(combined_query, order_by=order_by) |
251 | | - list_query = list_query.offset(offset).limit(limit) |
| 250 | + list_query = ( |
| 251 | + _set_ordering(combined_query, order_by=order_by).offset(offset).limit(limit) |
| 252 | + ) |
252 | 253 |
|
253 | 254 | async with pass_or_acquire_connection(get_asyncpg_engine(app), connection) as conn: |
254 | 255 | total_count = await conn.scalar(count_query) |
@@ -293,8 +294,7 @@ async def list_trashed_folders( |
293 | 294 | count_query = select(func.count()).select_from(base_query.subquery()) |
294 | 295 |
|
295 | 296 | # Ordering and pagination |
296 | | - list_query = _set_ordering(base_query, order_by) |
297 | | - list_query = list_query.offset(offset).limit(limit) |
| 297 | + list_query = _set_ordering(base_query, order_by).offset(offset).limit(limit) |
298 | 298 |
|
299 | 299 | async with pass_or_acquire_connection(get_asyncpg_engine(app), connection) as conn: |
300 | 300 | total_count = await conn.scalar(count_query) |
|
0 commit comments