allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brond...@apache.org
Subject [allura] branch master updated: ago_in_past helpers.ago returns 'in ...' if date is in future
Date Fri, 20 Mar 2020 15:16:25 GMT
This is an automated email from the ASF dual-hosted git repository.

brondsem pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/allura.git


The following commit(s) were added to refs/heads/master by this push:
     new 4375422  ago_in_past helpers.ago returns 'in ...' if date is in future
4375422 is described below

commit 4375422f9033cd6d8d5bce40a0a57ba8c3789f14
Author: Dillon Walls <dillon.walls@slashdotmedia.com>
AuthorDate: Tue Mar 17 19:31:33 2020 +0000

    ago_in_past helpers.ago returns 'in ...' if date is in future
---
 Allura/allura/lib/helpers.py                      | 10 ++++-
 Allura/allura/tests/test_helpers.py               |  6 +++
 Allura/allura/tests/unit/test_helpers/test_ago.py | 49 +++++++++++++++++++++--
 3 files changed, 59 insertions(+), 6 deletions(-)

diff --git a/Allura/allura/lib/helpers.py b/Allura/allura/lib/helpers.py
index 3f537ad..9d6c68d 100644
--- a/Allura/allura/lib/helpers.py
+++ b/Allura/allura/lib/helpers.py
@@ -450,13 +450,15 @@ def ago(start_time, show_date_after=7):
     """
     Return time since starting time as a rounded, human readable string.
     E.g., "3 hours ago"
+    Also works with future times
+    E.g., "in 3 hours"
     """
 
     if start_time is None:
         return 'unknown'
     granularities = ['century', 'decade', 'year', 'month', 'day', 'hour', 'minute', 'second']
     end_time = datetime.utcnow()
-    if show_date_after is not None and end_time - start_time > timedelta(days=show_date_after):
+    if show_date_after is not None and abs(end_time - start_time) > timedelta(days=show_date_after):
         return start_time.strftime('%Y-%m-%d')
 
     while True:
@@ -465,7 +467,11 @@ def ago(start_time, show_date_after=7):
         rounded_to_one_granularity = 'and' not in ago
         if rounded_to_one_granularity:
             break
-    return ago + ' ago'
+
+    if (end_time - start_time).total_seconds() >= 0:
+        return ago + ' ago'
+    else:
+        return 'in ' + ago
 
 
 def ago_ts(timestamp):
diff --git a/Allura/allura/tests/test_helpers.py b/Allura/allura/tests/test_helpers.py
index d5ea156..8db28c1 100644
--- a/Allura/allura/tests/test_helpers.py
+++ b/Allura/allura/tests/test_helpers.py
@@ -192,6 +192,7 @@ def test_encode_keys():
 
 def test_ago():
     assert_equals(h.ago(datetime.utcnow() - timedelta(days=2)), '2 days ago')
+    assert_equals(h.ago(datetime.utcnow() + timedelta(days=2)), 'in 2 days')
     assert_equals(h.ago_ts(time.time() - 60 * 60 * 2), '2 hours ago')
     d_str = (datetime.utcnow() - timedelta(hours=3)).isoformat()
     assert_equals(h.ago_string(d_str), '3 hours ago')
@@ -203,6 +204,11 @@ def test_ago():
     assert_equals(h.ago(monthish, show_date_after=90), '1 month ago')
     assert_equals(h.ago(monthish, show_date_after=None), '1 month ago')
 
+    monthish = datetime.utcnow() + timedelta(days=32)
+    assert 'in ' not in h.ago(monthish)
+    assert_equals(h.ago(monthish, show_date_after=90), 'in 1 month')
+    assert_equals(h.ago(monthish, show_date_after=None), 'in 1 month')
+
 
 def test_urlquote_unicode():
     # No exceptions please
diff --git a/Allura/allura/tests/unit/test_helpers/test_ago.py b/Allura/allura/tests/unit/test_helpers/test_ago.py
index ea09bdf..d688bd2 100644
--- a/Allura/allura/tests/unit/test_helpers/test_ago.py
+++ b/Allura/allura/tests/unit/test_helpers/test_ago.py
@@ -37,33 +37,74 @@ class TestAgo(object):
         self.assertTimeSince('less than 1 second ago', 2010, 1, 1, 0, 0, 0, 1)
         # the are not rounded, actually.  oh well
         self.assertTimeSince('less than 1 second ago', 2010, 1, 1, 0, 0, 0, 999999)
+        # negative partial seconds don't say 'less than'
+        self.assertTimeSince('in 1 second', 2009, 12, 31, 23, 59, 59, 1)
+        self.assertTimeSince('in 1 second', 2009, 12, 31, 23, 59, 59, 999999)
 
     def test_that_minutes_are_rounded(self):
         self.assertTimeSince('1 minute ago', 2010, 1, 1, 0, 1, 29)
         self.assertTimeSince('2 minutes ago', 2010, 1, 1, 0, 1, 31)
+        self.assertTimeSince('in 1 minute', 2009, 12, 31, 23, 58, 31)
+        self.assertTimeSince('in 2 minutes', 2009, 12, 31, 23, 58, 29)
 
     def test_that_hours_are_rounded(self):
         self.assertTimeSince('1 hour ago', 2010, 1, 1, 1, 29, 0)
         self.assertTimeSince('2 hours ago', 2010, 1, 1, 1, 31, 0)
+        self.assertTimeSince('in 1 hour', 2009, 12, 31, 22, 31, 0)
+        self.assertTimeSince('in 2 hours', 2009, 12, 31, 22, 29, 0)
 
     def test_that_days_are_rounded(self):
         self.assertTimeSince('1 day ago', 2010, 1, 2, 11, 0, 0)
         self.assertTimeSince('2 days ago', 2010, 1, 2, 13, 0, 0)
+        self.assertTimeSince('in 1 day', 2009, 12, 30, 13, 0, 0)
+        self.assertTimeSince('in 2 days', 2009, 12, 30, 11, 0, 0)
+
+    def test_that_days_do_not_show_after(self):
+        self.assertTimeSince('7 days ago', 2010, 1, 8, 0, 0, 0)
+        self.assertTimeSince('2010-01-01', 2010, 1, 8, 0, 0, 1)
+        self.assertTimeSince('7 days ago', 2010, 1, 8, 11, 0, 0, show_date_after=8)
+        self.assertTimeSince('8 days ago', 2010, 1, 8, 13, 0, 0, show_date_after=8)
+        self.assertTimeSince('8 days ago', 2010, 1, 9, 0, 0, 0, show_date_after=8)
+        self.assertTimeSince('2010-01-01', 2010, 1, 9, 0, 0, 1, show_date_after=8)
 
     def test_that_months_are_rounded(self):
         self.assertTimeSince('2010-01-01', 2010, 2, 8, 0, 0, 0)
         self.assertTimeSince('2010-01-01', 2010, 2, 9, 0, 0, 0)
         self.assertTimeSince('2010-01-01', 2010, 2, 20, 0, 0, 0)
 
+        # the 9th is the rounding down cutoff for february /shrug
+        self.assertTimeSince('1 month ago', 2010, 2, 1, 0, 0, 0, show_date_after=None)
+        self.assertTimeSince('1 month ago', 2010, 2, 8, 0, 0, 0, show_date_after=None)
+        self.assertTimeSince('2 months ago', 2010, 2, 9, 0, 0, 0, show_date_after=None)
+        self.assertTimeSince('2 months ago', 2010, 2, 20, 0, 0, 0, show_date_after=None)
+        self.assertTimeSince('2 months ago', 2010, 2, 28, 0, 0, 0, show_date_after=None)
+
+        self.assertTimeSince('2010-01-01', 2009, 11, 24, 0, 0, 0)
+        self.assertTimeSince('2010-01-01', 2009, 11, 23, 0, 0, 0)
+        self.assertTimeSince('2010-01-01', 2009, 11, 1, 0, 0, 0)
+
+        # the 24th is the rounding up cutoff for november /shrug
+        self.assertTimeSince('in 1 month', 2009, 11, 30, 0, 0, 0, show_date_after=None)
+        self.assertTimeSince('in 1 month', 2009, 11, 24, 0, 0, 0, show_date_after=None)
+        self.assertTimeSince('in 2 months', 2009, 11, 23, 0, 0, 0, show_date_after=None)
+        self.assertTimeSince('in 2 months', 2009, 11, 1, 0, 0, 0, show_date_after=None)
+
     def test_that_years_are_rounded(self):
         self.assertTimeSince('2010-01-01', 2011, 6, 1, 0, 0, 0)
         self.assertTimeSince('2010-01-01', 2011, 8, 1, 0, 0, 0)
+        self.assertTimeSince('1 year ago', 2011, 6, 1, 0, 0, 0, show_date_after=None)
+        self.assertTimeSince('2 years ago', 2011, 8, 1, 0, 0, 0, show_date_after=None)
+
+        self.assertTimeSince('2010-01-01', 2008, 8, 1, 0, 0, 0)
+        self.assertTimeSince('2010-01-01', 2008, 6, 1, 0, 0, 0)
+        self.assertTimeSince('in 1 year', 2008, 8, 1, 0, 0, 0, show_date_after=None)
+        self.assertTimeSince('in 2 years', 2008, 6, 1, 0, 0, 0, show_date_after=None)
 
-    def assertTimeSince(self, time_string, *time_components):
-        assert_equal(time_string, self.time_since(*time_components))
+    def assertTimeSince(self, time_string, *time_components, **kwargs):
+        assert_equal(time_string, self.time_since(*time_components, **kwargs))
 
-    def time_since(self, *time_components):
+    def time_since(self, *time_components, **kwargs):
         end_time = datetime(*time_components)
         with patch('allura.lib.helpers.datetime') as datetime_class:
             datetime_class.utcnow.return_value = end_time
-            return helpers.ago(self.start_time)
+            return helpers.ago(self.start_time, **kwargs)


Mime
View raw message