API - fix order by for PGSQL15 support and update tests

This commit is contained in:
Sam
2022-10-26 18:05:22 +02:00
parent ef1c96d1f9
commit b86a1b93f9
5 changed files with 54 additions and 69 deletions

View File

@ -3,7 +3,7 @@ import shutil
from typing import Any, Dict, Tuple, Union
from flask import Blueprint, current_app, request, send_file
from sqlalchemy import exc
from sqlalchemy import asc, desc, exc
from fittrackee import db, limiter
from fittrackee.emails.tasks import (
@ -191,45 +191,14 @@ def get_users(auth_user: User) -> Dict:
per_page = int(params.get('per_page', USER_PER_PAGE))
if per_page > 50:
per_page = 50
order_by = params.get('order_by')
user_column = getattr(User, params.get('order_by', 'username'))
order = params.get('order', 'asc')
query = params.get('q')
users_pagination = (
User.query.filter(
User.username.ilike('%' + query + '%') if query else True,
)
.order_by(
User.workouts_count.asc() # type: ignore
if order_by == 'workouts_count' and order == 'asc'
else True,
User.workouts_count.desc() # type: ignore
if order_by == 'workouts_count' and order == 'desc'
else True,
User.username.asc()
if order_by == 'username' and order == 'asc'
else True,
User.username.desc()
if order_by == 'username' and order == 'desc'
else True,
User.created_at.asc()
if order_by == 'created_at' and order == 'asc'
else True,
User.created_at.desc()
if order_by == 'created_at' and order == 'desc'
else True,
User.admin.asc()
if order_by == 'admin' and order == 'asc'
else True,
User.admin.desc()
if order_by == 'admin' and order == 'desc'
else True,
User.is_active.asc()
if order_by == 'is_active' and order == 'asc'
else True,
User.is_active.desc()
if order_by == 'is_active' and order == 'desc'
else True,
)
.order_by(asc(user_column) if order == 'asc' else desc(user_column))
.paginate(page, per_page, False)
)
users = users_pagination.items