Code

Ticket #12771: bug_12771.diff

File bug_12771.diff, 4.0 KB (added by djansoft, 4 years ago)

Fixed bug in patch and adding tests.

Line 
1diff --git a/django/contrib/humanize/templatetags/humanize.py b/django/contrib/humanize/templatetags/humanize.py
2index 3e19fed..d501fe7 100644
3--- a/django/contrib/humanize/templatetags/humanize.py
4+++ b/django/contrib/humanize/templatetags/humanize.py
5@@ -2,7 +2,7 @@ from django.utils.translation import ungettext, ugettext as _
6 from django.utils.encoding import force_unicode
7 from django import template
8 from django.template import defaultfilters
9-from datetime import date
10+from datetime import date, datetime
11 import re
12 
13 register = template.Library()
14@@ -97,3 +97,35 @@ def naturalday(value, arg=None):
15         return _(u'yesterday')
16     return defaultfilters.date(value, arg)
17 register.filter(naturalday)
18+
19+def naturaltime(value, arg=None):
20+    """
21+    For date and time values shows how many seconds, minutes or hours ago compared to
22+    current timestamp returns representing string. Otherwise, returns a string
23+    formatted according to settings.DATE_FORMAT
24+    """
25+    try:
26+        value = datetime(value.year, value.month, value.day, value.hour, value.minute, value.second)
27+    except AttributeError:
28+        return value
29+    except ValueError:
30+        return value
31+   
32+    delta = datetime.now() - value
33+    if delta.days != 0:
34+        value = date(value.year, value.month, value.day)
35+        return naturalday(value, arg)
36+    elif delta.seconds == 0:
37+        return _(u'now')
38+    elif delta.seconds < 60:
39+        return _(u"%s seconds ago" % (delta.seconds))
40+    elif delta.seconds / 60 < 2:
41+        return _(r'a minute ago')
42+    elif delta.seconds / 60 < 60:
43+        return _(u"%s minutes ago" % (delta.seconds/60))
44+    elif delta.seconds / 60 / 60 < 2:
45+        return _(u'an hour ago')
46+    elif delta.seconds / 60 / 60 < 24:
47+        return _(u"%s hours ago" % (delta.seconds/60/60))
48+    return naturalday(value, arg)
49+register.filter(naturaltime)
50diff --git a/tests/regressiontests/humanize/tests.py b/tests/regressiontests/humanize/tests.py
51index 8b45471..65f0f0f 100644
52--- a/tests/regressiontests/humanize/tests.py
53+++ b/tests/regressiontests/humanize/tests.py
54@@ -1,5 +1,5 @@
55 import unittest
56-from datetime import timedelta, date
57+from datetime import timedelta, date, datetime
58 from django.template import Template, Context, add_to_builtins
59 from django.utils.dateformat import DateFormat
60 from django.utils.translation import ugettext as _
61@@ -54,7 +54,7 @@ class HumanizeTests(unittest.TestCase):
62 
63     def test_naturalday(self):
64         from django.template import defaultfilters
65-        today = date.today()
66+        today = datetime.now()
67         yesterday = today - timedelta(days=1)
68         tomorrow = today + timedelta(days=1)
69         someday = today - timedelta(days=10)
70@@ -66,6 +66,32 @@ class HumanizeTests(unittest.TestCase):
71                        someday_result, u"I'm not a date value")
72         self.humanize_tester(test_list, result_list, 'naturalday')
73 
74+    def test_naturaltime(self):
75+        from django.template import defaultfilters
76+        now = datetime.now()
77+        seconds_ago = now - timedelta(seconds=30)
78+        a_minute_ago = now - timedelta(minutes=1, seconds=30)
79+        minutes_ago = now - timedelta(minutes=2)
80+        an_hour_ago = now - timedelta(hours=1, minutes=30, seconds=30)
81+        hours_ago = now - timedelta(hours=23, minutes=50, seconds=50)
82+
83+        test_list = (now, a_minute_ago, an_hour_ago)
84+        result_list = (_(u'now'), _(u'a minute ago'), _(u'an hour ago'))
85+        self.humanize_tester(test_list, result_list, 'naturaltime')
86+       
87+        t = Template('{{ seconds_ago|%s }}' % 'naturaltime')
88+        rendered = t.render(Context(locals())).strip()
89+        self.assertTrue(u' seconds ago' in rendered)
90+
91+        t = Template('{{ minutes_ago|%s }}' % 'naturaltime')
92+        rendered = t.render(Context(locals())).strip()
93+        self.assertTrue(u' minutes ago' in rendered)
94+
95+        t = Template('{{ hours_ago|%s }}' % 'naturaltime')
96+        rendered = t.render(Context(locals())).strip()
97+        self.assertTrue(u' hours ago' in rendered)
98+
99+
100 if __name__ == '__main__':
101     unittest.main()
102