mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #12771 -- Added naturaltime filter to humanize contrib app. Thanks, phinpho, djansoft and xtrqt.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16071 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from datetime import timedelta, date
|
||||
from datetime import timedelta, date, datetime
|
||||
|
||||
from django.template import Template, Context, add_to_builtins
|
||||
from django.utils import unittest
|
||||
@@ -72,6 +72,32 @@ class HumanizeTests(unittest.TestCase):
|
||||
someday_result, u"I'm not a date value", None)
|
||||
self.humanize_tester(test_list, result_list, 'naturalday')
|
||||
|
||||
def test_naturaltime(self):
|
||||
from django.template import defaultfilters
|
||||
now = datetime.now()
|
||||
seconds_ago = now - timedelta(seconds=30)
|
||||
a_minute_ago = now - timedelta(minutes=1, seconds=30)
|
||||
minutes_ago = now - timedelta(minutes=2)
|
||||
an_hour_ago = now - timedelta(hours=1, minutes=30, seconds=30)
|
||||
hours_ago = now - timedelta(hours=23, minutes=50, seconds=50)
|
||||
|
||||
test_list = (now, a_minute_ago, an_hour_ago)
|
||||
result_list = (_(u'now'), _(u'a minute ago'), _(u'an hour ago'))
|
||||
self.humanize_tester(test_list, result_list, 'naturaltime')
|
||||
|
||||
t = Template('{{ seconds_ago|%s }}' % 'naturaltime')
|
||||
rendered = t.render(Context(locals())).strip()
|
||||
self.assertTrue(u' seconds ago' in rendered)
|
||||
|
||||
t = Template('{{ minutes_ago|%s }}' % 'naturaltime')
|
||||
rendered = t.render(Context(locals())).strip()
|
||||
self.assertTrue(u' minutes ago' in rendered)
|
||||
|
||||
t = Template('{{ hours_ago|%s }}' % 'naturaltime')
|
||||
rendered = t.render(Context(locals())).strip()
|
||||
self.assertTrue(u' hours ago' in rendered)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user