From eb02ede0d79bd7624e61d8e6486e9f4efebbcc23 Mon Sep 17 00:00:00 2001 From: Sam Date: Sat, 26 Mar 2022 18:28:00 +0100 Subject: [PATCH] API & Client - order on user account status in administration + fixes --- fittrackee/tests/users/test_users_api.py | 93 +++++++++++++++++++ fittrackee/users/models.py | 6 +- fittrackee/users/users.py | 8 +- .../components/Administration/AdminUsers.vue | 11 ++- .../User/ProfileDisplay/UserInfos.vue | 1 - .../src/locales/en/administration.json | 1 + .../src/locales/fr/administration.json | 5 +- fittrackee_client/src/types/user.ts | 1 + 8 files changed, 115 insertions(+), 11 deletions(-) diff --git a/fittrackee/tests/users/test_users_api.py b/fittrackee/tests/users/test_users_api.py index 805a3b81..9b65f897 100644 --- a/fittrackee/tests/users/test_users_api.py +++ b/fittrackee/tests/users/test_users_api.py @@ -761,6 +761,99 @@ class TestGetUsers(ApiTestCaseMixin): 'total': 3, } + def test_it_gets_users_list_ordered_by_account_status( + self, + app: Flask, + user_1_admin: User, + inactive_user: User, + ) -> None: + client, auth_token = self.get_test_client_and_auth_token( + app, user_1_admin.email + ) + + response = client.get( + '/api/users?order_by=is_active', + headers=dict(Authorization=f'Bearer {auth_token}'), + ) + + data = json.loads(response.data.decode()) + assert response.status_code == 200 + assert 'success' in data['status'] + assert len(data['data']['users']) == 2 + assert data['data']['users'][0]['username'] == inactive_user.username + assert not data['data']['users'][0]['is_active'] + assert data['data']['users'][1]['username'] == user_1_admin.username + assert data['data']['users'][1]['is_active'] + assert data['pagination'] == { + 'has_next': False, + 'has_prev': False, + 'page': 1, + 'pages': 1, + 'total': 2, + } + + def test_it_gets_users_list_ordered_by_account_status_ascending( + self, + app: Flask, + user_1_admin: User, + inactive_user: User, + ) -> None: + client, auth_token = self.get_test_client_and_auth_token( + app, user_1_admin.email + ) + + response = client.get( + '/api/users?order_by=is_active&order=asc', + headers=dict(Authorization=f'Bearer {auth_token}'), + ) + + data = json.loads(response.data.decode()) + assert response.status_code == 200 + assert 'success' in data['status'] + assert len(data['data']['users']) == 2 + assert data['data']['users'][0]['username'] == inactive_user.username + assert not data['data']['users'][0]['is_active'] + assert data['data']['users'][1]['username'] == user_1_admin.username + assert data['data']['users'][1]['is_active'] + assert data['pagination'] == { + 'has_next': False, + 'has_prev': False, + 'page': 1, + 'pages': 1, + 'total': 2, + } + + def test_it_gets_users_list_ordered_by_account_status_descending( + self, + app: Flask, + user_1_admin: User, + inactive_user: User, + ) -> None: + client, auth_token = self.get_test_client_and_auth_token( + app, user_1_admin.email + ) + + response = client.get( + '/api/users?order_by=is_active&order=desc', + headers=dict(Authorization=f'Bearer {auth_token}'), + ) + + data = json.loads(response.data.decode()) + assert response.status_code == 200 + assert 'success' in data['status'] + assert len(data['data']['users']) == 2 + assert data['data']['users'][0]['username'] == user_1_admin.username + assert data['data']['users'][0]['is_active'] + assert data['data']['users'][1]['username'] == inactive_user.username + assert not data['data']['users'][1]['is_active'] + assert data['pagination'] == { + 'has_next': False, + 'has_prev': False, + 'page': 1, + 'pages': 1, + 'total': 2, + } + def test_it_gets_users_list_ordered_by_workouts_count_descending( self, app: Flask, diff --git a/fittrackee/users/models.py b/fittrackee/users/models.py index 773eeb3c..bc19df4c 100644 --- a/fittrackee/users/models.py +++ b/fittrackee/users/models.py @@ -59,14 +59,16 @@ class User(BaseModel): username: str, email: str, password: str, - created_at: Optional[datetime] = datetime.utcnow(), + created_at: Optional[datetime] = None, ) -> None: self.username = username self.email = email self.password = bcrypt.generate_password_hash( password, current_app.config.get('BCRYPT_LOG_ROUNDS') ).decode() - self.created_at = created_at + self.created_at = ( + datetime.utcnow() if created_at is None else created_at + ) @staticmethod def encode_auth_token(user_id: int) -> str: diff --git a/fittrackee/users/users.py b/fittrackee/users/users.py index 2dd3b47b..45db4193 100644 --- a/fittrackee/users/users.py +++ b/fittrackee/users/users.py @@ -172,7 +172,7 @@ def get_users(auth_user: User) -> Dict: :query integer per_page: number of users per page (default: 10, max: 50) :query string q: query on user name :query string order_by: sorting criteria (``username``, ``created_at``, - ``workouts_count``, ``admin``) + ``workouts_count``, ``admin``, ``is_active``) :query string order: sorting order (default: ``asc``) :reqheader Authorization: OAuth 2.0 Bearer Token @@ -221,6 +221,12 @@ def get_users(auth_user: User) -> Dict: 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, ) .paginate(page, per_page, False) ) diff --git a/fittrackee_client/src/components/Administration/AdminUsers.vue b/fittrackee_client/src/components/Administration/AdminUsers.vue index 9fa140bf..912b9a2d 100644 --- a/fittrackee_client/src/components/Administration/AdminUsers.vue +++ b/fittrackee_client/src/components/Administration/AdminUsers.vue @@ -30,8 +30,8 @@ {{ capitalize($t('workouts.WORKOUT', 0)) }} - {{ $t('user.ADMIN') }} {{ $t('admin.ACTIVE') }} + {{ $t('user.ADMIN') }} {{ $t('admin.ACTION') }} @@ -76,19 +76,19 @@ - {{ $t('user.ADMIN') }} + {{ $t('admin.ACTIVE') }}