Django

Code

Changeset 5985

Show
Ignore:
Timestamp:
08/20/07 03:50:08 (1 year ago)
Author:
mtredinnick
Message:

Fixed #3311 -- Added naturalday filter to contrib.humanize. Thanks, Jyrki Pulliainen.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/AUTHORS

    r5942 r5985  
    227227    Daniel Poelzleithner <http://poelzi.org/> 
    228228    polpak@yahoo.com 
     229    Jyrki Pulliainen <jyrki.pulliainen@gmail.com> 
    229230    Johann Queuniet <johann.queuniet@adh.naellia.eu> 
    230231    J. Rademaker 
  • django/trunk/django/contrib/humanize/templatetags/humanize.py

    r5609 r5985  
    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 
     
    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) 
  • django/trunk/docs/add_ons.txt

    r5796 r5985  
    139139You can pass in either an integer or a string representation of an integer. 
    140140 
     141naturalday 
     142---------- 
     143 
     144**New in Django development version** 
     145 
     146For dates that are the current day or within one day, return "today", 
     147"tomorrow" or "yesterday", as appropriate. Otherwise, format the date using 
     148the passed in format string. 
     149 
     150**Argument:** Date formatting string as described in default tag now_. 
     151 
     152.. _now: ../templates/#now 
     153 
     154Examples (when 'today' is 17 Feb 2007): 
     155 
     156    * ``16 Feb 2007`` becomes ``yesterday``. 
     157    * ``17 Feb 2007`` becomes ``today``. 
     158    * ``18 Feb 2007`` becomes ``tomorrow``. 
     159    * Any other day is formatted according to given argument or the 
     160      `DATE_FORMAT`_ setting if no argument is given. 
     161 
     162.. _DATE_FORMAT: ../settings/#date_format 
     163 
    141164flatpages 
    142165========= 
  • django/trunk/tests/regressiontests/humanize/tests.py

    r5876 r5985  
    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') 
     
    912        # Using max below ensures we go through both lists 
    1013        # However, if the lists are not equal length, this raises an exception 
    11         for index in xrange(len(max(test_list,result_list))): 
     14        for index in xrange(max(len(test_list), len(result_list))): 
    1215            test_content = test_list[index] 
    1316            t = Template('{{ test_content|%s }}' % method) 
    1417            rendered = t.render(Context(locals())).strip() 
    1518            self.assertEqual(rendered, result_list[index], 
    16                              msg="""%s test failed, produced %s, 
    17 should've produced %s""" % (method, rendered, result_list[index])) 
     19                             msg="%s test failed, produced %s, should've produced %s" % (method, rendered, result_list[index])) 
    1820 
    1921    def test_ordinal(self): 
     
    5052        self.humanize_tester(test_list, result_list, 'apnumber') 
    5153 
     54    def test_naturalday(self): 
     55        from django.template import defaultfilters 
     56        today = date.today() 
     57        yesterday = today - timedelta(days=1) 
     58        tomorrow = today + timedelta(days=1) 
     59        someday = today - timedelta(days=10) 
     60        notdate = u"I'm not a date value" 
     61 
     62        test_list = (today, yesterday, tomorrow, someday, notdate) 
     63        someday_result = defaultfilters.date(someday) 
     64        result_list = (_(u'today'), _(u'yesterday'), _(u'tomorrow'), 
     65                       someday_result, u"I'm not a date value") 
     66        self.humanize_tester(test_list, result_list, 'naturalday') 
     67 
    5268if __name__ == '__main__': 
    5369    unittest.main()