Ticket #12771: patch_with_docs.diff

File patch_with_docs.diff, 5.3 KB (added by xtrqt, 4 years ago)

Added documentation.

  • django/contrib/humanize/templatetags/humanize.py

    diff --git a/django/contrib/humanize/templatetags/humanize.py b/django/contrib/humanize/templatetags/humanize.py
    index cfa93fe..6a3ecd5 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): 
    100100        return _(u'yesterday')
    101101    return defaultfilters.date(value, arg)
    102102register.filter(naturalday)
     103
     104def naturaltime(value, arg=None):
     105    """
     106    For date and time values shows how many seconds, minutes or hours ago compared to
     107    current timestamp returns representing string. Otherwise, returns a string
     108    formatted according to settings.DATE_FORMAT
     109    """
     110    try:
     111        value = datetime(value.year, value.month, value.day, value.hour, value.minute, value.second)
     112    except AttributeError:
     113        return value
     114    except ValueError:
     115        return value
     116   
     117    delta = datetime.now() - value
     118    if delta.days != 0:
     119        value = date(value.year, value.month, value.day)
     120        return naturalday(value, arg)
     121    elif delta.seconds == 0:
     122        return _(u'now')
     123    elif delta.seconds < 60:
     124        return _(u"%s seconds ago" % (delta.seconds))
     125    elif delta.seconds / 60 < 2:
     126        return _(r'a minute ago')
     127    elif delta.seconds / 60 < 60:
     128        return _(u"%s minutes ago" % (delta.seconds/60))
     129    elif delta.seconds / 60 / 60 < 2:
     130        return _(u'an hour ago')
     131    elif delta.seconds / 60 / 60 < 24:
     132        return _(u"%s hours ago" % (delta.seconds/60/60))
     133    return naturalday(value, arg)
     134register.filter(naturaltime)
  • docs/ref/contrib/humanize.txt

    diff --git a/docs/ref/contrib/humanize.txt b/docs/ref/contrib/humanize.txt
    index ef48f48..a9a2c17 100644
    a b Examples (when 'today' is 17 Feb 2007): 
    8282    * Any other day is formatted according to given argument or the
    8383      :setting:`DATE_FORMAT` setting if no argument is given.
    8484
     85.. templatefilter:: naturaltime
     86
     87naturaltime
     88----------
     89
     90For date and time values shows how many seconds, minutes or hours ago compared
     91to current timestamp returns representing string. Otherwise, it behaves like
     92:tfilter:`naturaldate`, so it can also take string argument for date formating.
     93       
     94**Argument:** Date formatting string as described in the :tfilter:`date` tag.
     95
     96Examples (when 'now' is 17 Feb 2007 16:30:00):
     97
     98    * ``17 Feb 2007 16:30:00`` becomes ``now``.
     99    * ``17 Feb 2007 16:29:31`` becomes ``29 seconds ago``.
     100    * ``17 Feb 2007 16:29:00`` becomes ``a minute ago``.
     101    * ``17 Feb 2007 16:25:35`` becomes ``4 minutes ago``.
     102    * ``17 Feb 2007 15:30:29`` becomes ``an hours ago``.
     103    * ``17 Feb 2007 13:31:29`` becomes ``2 hours ago``.
     104    * ``16 Feb 2007 13:31:29`` becomes ``yesterday``.
     105    * Any other day is formatted according to given argument or the
     106      :setting:`DATE_FORMAT` setting if no argument is given.
     107
    85108.. templatefilter:: ordinal
    86109
    87110ordinal
  • tests/regressiontests/humanize/tests.py

    diff --git a/tests/regressiontests/humanize/tests.py b/tests/regressiontests/humanize/tests.py
    index 7eeaaee..c53060b 100644
    a b  
    1 from datetime import timedelta, date
     1from datetime import timedelta, date, datetime
    22
    33from django.template import Template, Context, add_to_builtins
    44from django.utils import unittest
    class HumanizeTests(unittest.TestCase): 
    6060
    6161    def test_naturalday(self):
    6262        from django.template import defaultfilters
    63         today = date.today()
     63        today = datetime.now()
    6464        yesterday = today - timedelta(days=1)
    6565        tomorrow = today + timedelta(days=1)
    6666        someday = today - timedelta(days=10)
    class HumanizeTests(unittest.TestCase): 
    7272                       someday_result, u"I'm not a date value", None)
    7373        self.humanize_tester(test_list, result_list, 'naturalday')
    7474
     75    def test_naturaltime(self):
     76        from django.template import defaultfilters
     77        now = datetime.now()
     78        seconds_ago = now - timedelta(seconds=30)
     79        a_minute_ago = now - timedelta(minutes=1, seconds=30)
     80        minutes_ago = now - timedelta(minutes=2)
     81        an_hour_ago = now - timedelta(hours=1, minutes=30, seconds=30)
     82        hours_ago = now - timedelta(hours=23, minutes=50, seconds=50)
     83
     84        test_list = (now, a_minute_ago, an_hour_ago)
     85        result_list = (_(u'now'), _(u'a minute ago'), _(u'an hour ago'))
     86        self.humanize_tester(test_list, result_list, 'naturaltime')
     87       
     88        t = Template('{{ seconds_ago|%s }}' % 'naturaltime')
     89        rendered = t.render(Context(locals())).strip()
     90        self.assertTrue(u' seconds ago' in rendered)
     91
     92        t = Template('{{ minutes_ago|%s }}' % 'naturaltime')
     93        rendered = t.render(Context(locals())).strip()
     94        self.assertTrue(u' minutes ago' in rendered)
     95
     96        t = Template('{{ hours_ago|%s }}' % 'naturaltime')
     97        rendered = t.render(Context(locals())).strip()
     98        self.assertTrue(u' hours ago' in rendered)
     99
     100
    75101if __name__ == '__main__':
    76102    unittest.main()
    77103
Back to Top