Skip to content

Commit

Permalink
API - fix readable duration - fix #62
Browse files Browse the repository at this point in the history
+ add some tests on user utils
  • Loading branch information
SamR1 committed Jan 31, 2021
1 parent b385cca commit cd9caa6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
46 changes: 46 additions & 0 deletions fittrackee/tests/users/test_users_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from typing import Union
from uuid import uuid4

import pytest

from fittrackee.users.utils import (
display_readable_file_size,
get_readable_duration,
)


class TestDisplayReadableFileSize:
@pytest.mark.parametrize(
'size, expected_readable_size',
[
(0, '0 bytes'),
(1, '1 byte'),
(100, '100.0 bytes'),
(1024, '1.0KB'),
(286773663, '273.5MB'),
],
)
def test_it_returns_readable_file_size(
self, size: Union[float, int], expected_readable_size: str
) -> None:
readable_file_size = display_readable_file_size(size)

assert readable_file_size == expected_readable_size


class TestReadableDuration:
@pytest.mark.parametrize(
'locale, expected_duration',
[
('en', '30 seconds'),
('fr', '30 secondes'),
(None, '30 seconds'),
(uuid4().hex, '30 seconds'),
],
)
def test_it_returns_duration_in_locale(
self, locale: str, expected_duration: str
) -> None:
readable_duration = get_readable_duration(30, locale)

assert readable_duration == expected_duration
5 changes: 3 additions & 2 deletions fittrackee/users/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,10 +647,11 @@ def request_password_reset() -> Union[Dict, HttpResponse]:
if user:
password_reset_token = user.encode_password_reset_token(user.id)
ui_url = current_app.config['UI_URL']
user_language = 'en' if user.language is None else user.language
email_data = {
'expiration_delay': get_readable_duration(
current_app.config['PASSWORD_TOKEN_EXPIRATION_SECONDS'],
'en' if user.language is None else user.language,
user_language,
),
'username': user.username,
'password_reset_url': (
Expand All @@ -660,7 +661,7 @@ def request_password_reset() -> Union[Dict, HttpResponse]:
'browser_name': request.user_agent.browser, # type: ignore
}
user_data = {
'language': user.language if user.language else 'en',
'language': user_language,
'email': user.email,
}
reset_password_email.send(user_data, email_data)
Expand Down
13 changes: 9 additions & 4 deletions fittrackee/users/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,18 @@ def display_readable_file_size(size_in_bytes: Union[float, int]) -> str:
return f'{size_in_bytes} bytes'


def get_readable_duration(duration: int, locale: Optional[str] = 'en') -> str:
def get_readable_duration(duration: int, locale: Optional[str] = None) -> str:
"""
Return readable and localized duration from duration in seconds
"""
if locale is not None and locale != 'en':
_t = humanize.i18n.activate(locale) # noqa
if locale is None:
locale = 'en'
if locale != 'en':
try:
_t = humanize.i18n.activate(locale) # noqa
except FileNotFoundError:
locale = 'en'
readable_duration = humanize.naturaldelta(timedelta(seconds=duration))
if locale is not None and locale != 'en':
if locale != 'en':
humanize.i18n.deactivate()
return readable_duration

0 comments on commit cd9caa6

Please sign in to comment.