Ticket #3311: naturalday_unicode-5722.patch

File naturalday_unicode-5722.patch, 3.6 KB (added by Jyrki Pulliainen <jyrki.pulliainen@…>, 8 years ago)

Unicode-aware patch (fixed some typos) against 5722

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

     
    11from django.utils.translation import ungettext, ugettext as _
    22from django.utils.encoding import force_unicode
    33from django import template
     4from django.template import defaultfilters
     5from django.conf import settings
     6from datetime import date, timedelta
    47import re
    58
    69register = template.Library()
     
    6770        return value
    6871    return (_('one'), _('two'), _('three'), _('four'), _('five'), _('six'), _('seven'), _('eight'), _('nine'))[value-1]
    6972register.filter(apnumber)
     73
     74def naturalday(value, arg=None):
     75    """
     76    For date values that are tomorrow, today or yesterday compared to
     77    present day returns representing string. Otherwise, returns a string
     78    formatted according to settings.DATE_FORMAT.
     79    """
     80    try:
     81        value = date(value.year, value.month, value.day)
     82    except AttributeError:
     83        # Passed value wasn't a date object
     84        return value
     85    except ValueError:
     86        # Date arguments out of range
     87        return value
     88    delta = value - date.today()
     89    if delta.days == 0:
     90        return _(u'today')
     91    elif delta.days == 1:
     92        return _(u'tomorrow')
     93    elif delta.days == -1:
     94        return _(u'yesterday')
     95    return defaultfilters.date(value, arg)
     96register.filter(naturalday)
  • tests/regressiontests/humanize/tests.py

     
    11import unittest
     2from datetime import timedelta, date
    23from django.template import Template, Context, add_to_builtins
     4from django.utils.dateformat import DateFormat
     5from django.utils.translation import ugettext as _
    36
    47add_to_builtins('django.contrib.humanize.templatetags.humanize')
    58
     
    4952
    5053        self.humanize_tester(test_list, result_list, 'apnumber')
    5154
     55    def test_naturalday(self):
     56        from django.template import defaultfilters
     57        today = date.today()
     58        yesterday = today - timedelta(days=1)
     59        tomorrow = today + timedelta(days=1)
     60        someday = today - timedelta(days=10)
     61        notdate = u"I'm not a date value"
     62
     63        test_list = (today, yesterday, tomorrow, someday, notdate)
     64        someday_result = defaultfilters.date(someday)
     65        result_list = (_(u'today'), _(u'yesterday'), _(u'tomorrow'),
     66                       someday_result, u"I'm not a date value")
     67        self.humanize_tester(test_list, result_list, 'naturalday')
     68       
    5269if __name__ == '__main__':
    5370    unittest.main()
    5471
  • docs/add_ons.txt

     
    138138
    139139You can pass in either an integer or a string representation of an integer.
    140140
     141naturalday
     142----------
     143
     144Converts a date to natural string, if it's today, tomorrow or yesterday
     145
     146**Argument:** Date formatting string as described in default tag now_.
     147
     148.. _now: ../templates/#now
     149
     150Examples (when 'today' is 17 Feb 2007):
     151
     152    * ``16 Feb 2007`` becomes ``yesterday``.
     153    * ``17 Feb 2007`` becomes ``today``.
     154    * ``18 Feb 2007`` becomes ``tomorrow``.
     155    * Any other day is formatted according to given argument or `DATE_FORMAT`_ if
     156      argument is not given.
     157
     158.. _DATE_FORMAT: ../settings/#date_format
     159
    141160flatpages
    142161=========
    143162
Back to Top