Skip to content

Commit

Permalink
API - add additional order on username when querying users
Browse files Browse the repository at this point in the history
  • Loading branch information
SamR1 committed Aug 13, 2023
1 parent 7946c05 commit 8e89d4f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
12 changes: 6 additions & 6 deletions fittrackee/tests/users/test_users_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -875,8 +875,8 @@ def test_it_gets_users_list_ordered_by_admin_rights(
data = json.loads(response.data.decode())
assert 'success' in data['status']
assert len(data['data']['users']) == 3
assert 'toto' in data['data']['users'][0]['username']
assert 'sam' in data['data']['users'][1]['username']
assert 'sam' in data['data']['users'][0]['username']
assert 'toto' in data['data']['users'][1]['username']
assert 'admin' in data['data']['users'][2]['username']
assert data['pagination'] == {
'has_next': False,
Expand All @@ -902,8 +902,8 @@ def test_it_gets_users_list_ordered_by_admin_rights_ascending(
data = json.loads(response.data.decode())
assert 'success' in data['status']
assert len(data['data']['users']) == 3
assert 'toto' in data['data']['users'][0]['username']
assert 'sam' in data['data']['users'][1]['username']
assert 'sam' in data['data']['users'][0]['username']
assert 'toto' in data['data']['users'][1]['username']
assert 'admin' in data['data']['users'][2]['username']
assert data['pagination'] == {
'has_next': False,
Expand All @@ -930,8 +930,8 @@ def test_it_gets_users_list_ordered_by_admin_rights_descending(
assert 'success' in data['status']
assert len(data['data']['users']) == 3
assert 'admin' in data['data']['users'][0]['username']
assert 'toto' in data['data']['users'][1]['username']
assert 'sam' in data['data']['users'][2]['username']
assert 'sam' in data['data']['users'][1]['username']
assert 'toto' in data['data']['users'][2]['username']
assert data['pagination'] == {
'has_next': False,
'has_prev': False,
Expand Down
5 changes: 4 additions & 1 deletion fittrackee/users/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ def get_users_list(auth_user: User) -> Dict:
per_page = 50
user_column = getattr(User, params.get('order_by', 'username'))
order = params.get('order', 'asc')
order_clauses = [asc(user_column) if order == 'asc' else desc(user_column)]
if user_column != 'username':
order_clauses.append(User.username.asc())
with_inactive = params.get('with_inactive', 'false').lower()
if not auth_user or not auth_user.admin:
with_inactive = 'false'
Expand All @@ -77,7 +80,7 @@ def get_users_list(auth_user: User) -> Dict:
if with_hidden_users == 'true'
else User.hide_profile_in_users_directory == False, # noqa
)
.order_by(asc(user_column) if order == 'asc' else desc(user_column))
.order_by(*order_clauses)
.paginate(page=page, per_page=per_page, error_out=False)
)
users = users_pagination.items
Expand Down

0 comments on commit 8e89d4f

Please sign in to comment.