From c70188b307138dde2f618fb846ac57cd1cc418fb Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Fri, 14 Mar 2025 10:54:15 -0500 Subject: Make display of number of minutes more human-friendly. --- gn_auth/auth/authorisation/users/views.py | 26 +++++++++++++++++++++++++- gn_auth/templates/emails/forgot-password.html | 2 +- gn_auth/templates/emails/forgot-password.txt | 2 +- gn_auth/templates/emails/verify-email.html | 2 +- gn_auth/templates/emails/verify-email.txt | 2 +- 5 files changed, 29 insertions(+), 5 deletions(-) (limited to 'gn_auth') diff --git a/gn_auth/auth/authorisation/users/views.py b/gn_auth/auth/authorisation/users/views.py index f2e89cc..469bfa1 100644 --- a/gn_auth/auth/authorisation/users/views.py +++ b/gn_auth/auth/authorisation/users/views.py @@ -114,6 +114,29 @@ def user_address(user: User) -> Address: """Compute the `email.headerregistry.Address` from a `User`""" return Address(display_name=user.name, addr_spec=user.email) + +def display_minutes_for_humans(minutes): + """Convert minutes into human-readable display.""" + _week_ = 10080 # minutes + _day_ = 1440 # minutes + _remainder_ = minutes + + _human_readable_ = "" + if _remainder_ >= _week_: + _weeks_ = _remainder_ // _week_ + _remainder_ = _remainder_ % _week_ + _human_readable_ += f"{_weeks_} week" + ("s" if _weeks_ > 1 else "") + + if _remainder_ >= _day_: + _days_ = _remainder_ // _day_ + _remainder_ = _remainder_ % _day_ + _human_readable_ += (" " if bool(_human_readable_) else "") + f"{_days_} day" + ("s" if _days_ > 1 else "") + + if _remainder_ > 0: + _human_readable_ += (" " if bool(_human_readable_) else "") + f"{_remainder_} minutes" + + return _human_readable_ + def send_verification_email( conn, user: User, @@ -137,7 +160,8 @@ def send_verification_email( client_id=client_id, redirect_uri=redirect_uri, verificationcode=verification_code)), - expiration_minutes=expiration_minutes) + expiration_minutes=display_minutes_for_humans( + expiration_minutes)) with db.cursor(conn) as cursor: cursor.execute( ("INSERT INTO " diff --git a/gn_auth/templates/emails/forgot-password.html b/gn_auth/templates/emails/forgot-password.html index e40ebb8..5f16a02 100644 --- a/gn_auth/templates/emails/forgot-password.html +++ b/gn_auth/templates/emails/forgot-password.html @@ -24,7 +24,7 @@

- The link will expire in {{expiration_minutes}} minutes. + The link will expire in {{expiration_minutes}}.


diff --git a/gn_auth/templates/emails/forgot-password.txt b/gn_auth/templates/emails/forgot-password.txt index 55a4b13..68abf16 100644 --- a/gn_auth/templates/emails/forgot-password.txt +++ b/gn_auth/templates/emails/forgot-password.txt @@ -7,6 +7,6 @@ You (or someone pretending to be you) made a request to change your password. Pl If you did not request to change your password, simply ignore this email. -The link will expire {{expiration_minutes}} minutes. +The link will expire in {{expiration_minutes}}. Note that if you requested to change your password multiple times, only the latest/newest token will be valid. diff --git a/gn_auth/templates/emails/verify-email.html b/gn_auth/templates/emails/verify-email.html index 7f85c1c..11ae575 100644 --- a/gn_auth/templates/emails/verify-email.html +++ b/gn_auth/templates/emails/verify-email.html @@ -20,7 +20,7 @@

Please note that the verification code will expire in - {{expiration_minutes}} minutes after it was generated. + {{expiration_minutes}} after it was generated.

diff --git a/gn_auth/templates/emails/verify-email.txt b/gn_auth/templates/emails/verify-email.txt index 281d682..ecfbfc0 100644 --- a/gn_auth/templates/emails/verify-email.txt +++ b/gn_auth/templates/emails/verify-email.txt @@ -9,4 +9,4 @@ If that does not work, please log in to GeneNetwork and copy the verification co {{verification_code}} -Please note that the verification code will expire {{expiration_minutes}} minutes after it was generated. +Please note that the verification code will expire {{expiration_minutes}} after it was generated. -- cgit v1.2.3