Ticket #12771: bug_12771.diff

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

Fixed bug in patch and adding tests.

  • 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 _ 
    22from django.utils.encoding import force_unicode
    33from django import template
    44from django.template import defaultfilters
    5 from datetime import date
     5from datetime import date, datetime
    66import re
    77
    88register = template.Library()
    def naturalday(value, arg=None): 
    9797        return _(u'yesterday')
    9898    return defaultfilters.date(value, arg)
    9999register.filter(naturalday)
     100
     101def 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)
     131register.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  
    11import unittest
    2 from datetime import timedelta, date
     2from datetime import timedelta, date, datetime
    33from django.template import Template, Context, add_to_builtins
    44from django.utils.dateformat import DateFormat
    55from django.utils.translation import ugettext as _
    class HumanizeTests(unittest.TestCase): 
    5454
    5555    def test_naturalday(self):
    5656        from django.template import defaultfilters
    57         today = date.today()
     57        today = datetime.now()
    5858        yesterday = today - timedelta(days=1)
    5959        tomorrow = today + timedelta(days=1)
    6060        someday = today - timedelta(days=10)
    class HumanizeTests(unittest.TestCase): 
    6666                       someday_result, u"I'm not a date value")
    6767        self.humanize_tester(test_list, result_list, 'naturalday')
    6868
     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
    6995if __name__ == '__main__':
    7096    unittest.main()
    7197
Back to Top