Ticket #12771: bug_12771.diff
File bug_12771.diff, 4.0 KB (added by , 15 years ago) |
---|
-
django/contrib/humanize/templatetags/humanize.py
diff --git a/django/contrib/humanize/templatetags/humanize.py b/django/contrib/humanize/templatetags/humanize.py index 3e19fed..d501fe7 100644
a b from django.utils.translation import ungettext, ugettext as _ 2 2 from django.utils.encoding import force_unicode 3 3 from django import template 4 4 from django.template import defaultfilters 5 from datetime import date 5 from datetime import date, datetime 6 6 import re 7 7 8 8 register = template.Library() … … def naturalday(value, arg=None): 97 97 return _(u'yesterday') 98 98 return defaultfilters.date(value, arg) 99 99 register.filter(naturalday) 100 101 def naturaltime(value, arg=None): 102 """ 103 For date and time values shows how many seconds, minutes or hours ago compared to 104 current timestamp returns representing string. Otherwise, returns a string 105 formatted according to settings.DATE_FORMAT 106 """ 107 try: 108 value = datetime(value.year, value.month, value.day, value.hour, value.minute, value.second) 109 except AttributeError: 110 return value 111 except ValueError: 112 return value 113 114 delta = datetime.now() - value 115 if delta.days != 0: 116 value = date(value.year, value.month, value.day) 117 return naturalday(value, arg) 118 elif delta.seconds == 0: 119 return _(u'now') 120 elif delta.seconds < 60: 121 return _(u"%s seconds ago" % (delta.seconds)) 122 elif delta.seconds / 60 < 2: 123 return _(r'a minute ago') 124 elif delta.seconds / 60 < 60: 125 return _(u"%s minutes ago" % (delta.seconds/60)) 126 elif delta.seconds / 60 / 60 < 2: 127 return _(u'an hour ago') 128 elif delta.seconds / 60 / 60 < 24: 129 return _(u"%s hours ago" % (delta.seconds/60/60)) 130 return naturalday(value, arg) 131 register.filter(naturaltime) -
tests/regressiontests/humanize/tests.py
diff --git a/tests/regressiontests/humanize/tests.py b/tests/regressiontests/humanize/tests.py index 8b45471..65f0f0f 100644
a b 1 1 import unittest 2 from datetime import timedelta, date 2 from datetime import timedelta, date, datetime 3 3 from django.template import Template, Context, add_to_builtins 4 4 from django.utils.dateformat import DateFormat 5 5 from django.utils.translation import ugettext as _ … … class HumanizeTests(unittest.TestCase): 54 54 55 55 def test_naturalday(self): 56 56 from django.template import defaultfilters 57 today = date .today()57 today = datetime.now() 58 58 yesterday = today - timedelta(days=1) 59 59 tomorrow = today + timedelta(days=1) 60 60 someday = today - timedelta(days=10) … … class HumanizeTests(unittest.TestCase): 66 66 someday_result, u"I'm not a date value") 67 67 self.humanize_tester(test_list, result_list, 'naturalday') 68 68 69 def test_naturaltime(self): 70 from django.template import defaultfilters 71 now = datetime.now() 72 seconds_ago = now - timedelta(seconds=30) 73 a_minute_ago = now - timedelta(minutes=1, seconds=30) 74 minutes_ago = now - timedelta(minutes=2) 75 an_hour_ago = now - timedelta(hours=1, minutes=30, seconds=30) 76 hours_ago = now - timedelta(hours=23, minutes=50, seconds=50) 77 78 test_list = (now, a_minute_ago, an_hour_ago) 79 result_list = (_(u'now'), _(u'a minute ago'), _(u'an hour ago')) 80 self.humanize_tester(test_list, result_list, 'naturaltime') 81 82 t = Template('{{ seconds_ago|%s }}' % 'naturaltime') 83 rendered = t.render(Context(locals())).strip() 84 self.assertTrue(u' seconds ago' in rendered) 85 86 t = Template('{{ minutes_ago|%s }}' % 'naturaltime') 87 rendered = t.render(Context(locals())).strip() 88 self.assertTrue(u' minutes ago' in rendered) 89 90 t = Template('{{ hours_ago|%s }}' % 'naturaltime') 91 rendered = t.render(Context(locals())).strip() 92 self.assertTrue(u' hours ago' in rendered) 93 94 69 95 if __name__ == '__main__': 70 96 unittest.main() 71 97