From 9590643527643f264e2abe940a9a1f1769843140 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Fri, 14 Feb 2020 15:09:03 +0000 Subject: [PATCH] Use humanize for fuzzy time differences MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It seems to do a bit better than ago (e.g. 4 months vs 146 days), and looks like it’s maintained more often. --- app/__init__.py | 17 +++-------------- requirements-app.txt | 1 + requirements.txt | 5 +++-- tests/app/main/views/test_dashboard.py | 8 ++++---- 4 files changed, 11 insertions(+), 20 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index bffed68b2..6b7197f3c 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -5,7 +5,7 @@ from datetime import datetime, timedelta, timezone from functools import partial from time import monotonic -import ago +import humanize import jinja2 from flask import ( Markup, @@ -348,28 +348,17 @@ def format_delta(date): return "just now" if delta < timedelta(seconds=60): return "in the last minute" - return ago.human( - delta, - future_tense='{} from now', # No-one should ever see this - past_tense='{} ago', - precision=1 - ) + return humanize.naturaltime(delta) def format_delta_days(date): now = datetime.now(timezone.utc) date = utc_string_to_aware_gmt_datetime(date) - delta = now - date if date.strftime('%Y-%M-%D') == now.strftime('%Y-%M-%D'): return "today" if date.strftime('%Y-%M-%D') == (now - timedelta(days=1)).strftime('%Y-%M-%D'): return "yesterday" - return ago.human( - delta, - precision=1, - future_tense='{} from now', - past_tense='{} ago', - ) + return humanize.naturaltime(now - date) def valid_phone_number(phone_number): diff --git a/requirements-app.txt b/requirements-app.txt index d2bd53f6d..ea804c360 100644 --- a/requirements-app.txt +++ b/requirements-app.txt @@ -2,6 +2,7 @@ # with package version changes made in requirements-app.txt ago==0.0.93 +humanize==1.0.0 Flask==1.1.1 Flask-WTF==0.14.3 Flask-Login==0.4.1 diff --git a/requirements.txt b/requirements.txt index 89e522545..9b7ca375f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,6 +4,7 @@ # with package version changes made in requirements-app.txt ago==0.0.93 +humanize==1.0.0 Flask==1.1.1 Flask-WTF==0.14.3 Flask-Login==0.4.1 @@ -29,10 +30,10 @@ git+https://github.com/alphagov/notifications-utils.git@36.6.0#egg=notifications git+https://github.com/alphagov/govuk-frontend-jinja.git@v0.5.1-alpha#egg=govuk-frontend-jinja==0.5.1-alpha ## The following requirements were added by pip freeze: -awscli==1.17.15 +awscli==1.18.2 bleach==3.1.0 boto3==1.10.38 -botocore==1.14.15 +botocore==1.15.2 certifi==2019.11.28 chardet==3.0.4 Click==7.0 diff --git a/tests/app/main/views/test_dashboard.py b/tests/app/main/views/test_dashboard.py index 0aafafb88..6920b840c 100644 --- a/tests/app/main/views/test_dashboard.py +++ b/tests/app/main/views/test_dashboard.py @@ -262,9 +262,9 @@ def test_inbound_messages_shows_count_of_messages_when_there_are_no_messages( @pytest.mark.parametrize('index, expected_row', enumerate([ - '07900 900000 message-1 1 hour ago', - '07900 900000 message-2 1 hour ago', - '07900 900000 message-3 1 hour ago', + '07900 900000 message-1 an hour ago', + '07900 900000 message-2 an hour ago', + '07900 900000 message-3 an hour ago', '07900 900002 message-4 3 hours ago', '07900 900004 message-5 5 hours ago', '07900 900006 message-6 7 hours ago', @@ -558,7 +558,7 @@ def test_returned_letters_shows_count_of_recently_returned_letters( '0 returned letters latest report 8 days ago' )), ('2019-09-09', ( - '0 returned letters latest report 146 days ago' + '0 returned letters latest report 4 months ago' )), ('2010-10-10', ( '0 returned letters latest report 9 years ago'