API & Client - order on user account status in administration

+ fixes
This commit is contained in:
Sam 2022-03-26 18:28:00 +01:00
parent 614c888ec4
commit eb02ede0d7
8 changed files with 115 additions and 11 deletions

View File

@ -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,

View File

@ -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:

View File

@ -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)
)

View File

@ -30,8 +30,8 @@
<th>
{{ capitalize($t('workouts.WORKOUT', 0)) }}
</th>
<th>{{ $t('user.ADMIN') }}</th>
<th>{{ $t('admin.ACTIVE') }}</th>
<th>{{ $t('user.ADMIN') }}</th>
<th>{{ $t('admin.ACTION') }}</th>
</tr>
</thead>
@ -76,19 +76,19 @@
</td>
<td class="text-center">
<span class="cell-heading">
{{ $t('user.ADMIN') }}
{{ $t('admin.ACTIVE') }}
</span>
<i
:class="`fa fa${user.admin ? '-check' : ''}-square-o`"
:class="`fa fa${user.is_active ? '-check' : ''}-square-o`"
aria-hidden="true"
/>
</td>
<td class="text-center">
<span class="cell-heading">
{{ $t('admin.ACTIVE') }}
{{ $t('user.ADMIN') }}
</span>
<i
:class="`fa fa${user.is_active ? '-check' : ''}-square-o`"
:class="`fa fa${user.admin ? '-check' : ''}-square-o`"
aria-hidden="true"
/>
</td>
@ -159,6 +159,7 @@
const router = useRouter()
const orderByList: string[] = [
'is_active',
'admin',
'created_at',
'username',

View File

@ -187,7 +187,6 @@
}
function displayEmailForm() {
resetErrorsAndSuccess()
console.log('user.value.email_to_confirm', user.value.email_to_confirm)
newUserEmail.value = user.value.email_to_confirm
? user.value.email_to_confirm
: ''

View File

@ -49,6 +49,7 @@
"ORDER_BY": {
"ADMIN": "admin status",
"CREATED_AT": "registration date",
"IS_ACTIVE": "account status",
"USERNAME": "username",
"WORKOUTS_COUNT": "workout count"
}

View File

@ -42,13 +42,14 @@
"USER_EMAIL_UPDATE_SUCCESSFUL": "L'adresse email a été mise à jour.",
"USERS": {
"TABLE": {
"ADD_ADMIN_RIGHTS": "Ajouter les drois d'admin",
"REMOVE_ADMIN_RIGHTS": "Retirer les drois d'admin"
"ADD_ADMIN_RIGHTS": "Ajouter les droits d'admin",
"REMOVE_ADMIN_RIGHTS": "Retirer les droits d'admin"
},
"SELECTS": {
"ORDER_BY": {
"ADMIN": "status administrateur",
"CREATED_AT": "date d'inscription",
"IS_ACTIVE": "statut du compte",
"USERNAME": "nom d'utilisateur",
"WORKOUTS_COUNT": "nombre de séances"
}

View File

@ -9,6 +9,7 @@ export interface IUserProfile {
created_at: string
email: string
email_to_confirm?: string
is_active: boolean
first_name: string | null
last_name: string | null
location: string | null