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

git-svn-id: http://code.djangoproject.com/svn/django/trunk@5985 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2007-08-20 08:50:08 +00:00
parent b367ec2244
commit 14b5e074ba
4 changed files with 70 additions and 3 deletions

View File

@ -226,6 +226,7 @@ answer newbie questions, and generally made Django that much better:
plisk plisk
Daniel Poelzleithner <http://poelzi.org/> Daniel Poelzleithner <http://poelzi.org/>
polpak@yahoo.com polpak@yahoo.com
Jyrki Pulliainen <jyrki.pulliainen@gmail.com>
Johann Queuniet <johann.queuniet@adh.naellia.eu> Johann Queuniet <johann.queuniet@adh.naellia.eu>
J. Rademaker J. Rademaker
Michael Radziej <mir@noris.de> Michael Radziej <mir@noris.de>

View File

@ -1,6 +1,9 @@
from django.utils.translation import ungettext, ugettext as _ from django.utils.translation import ungettext, ugettext as _
from django.utils.encoding import force_unicode from django.utils.encoding import force_unicode
from django import template from django import template
from django.template import defaultfilters
from django.conf import settings
from datetime import date, timedelta
import re import re
register = template.Library() register = template.Library()
@ -67,3 +70,27 @@ def apnumber(value):
return value return value
return (_('one'), _('two'), _('three'), _('four'), _('five'), _('six'), _('seven'), _('eight'), _('nine'))[value-1] return (_('one'), _('two'), _('three'), _('four'), _('five'), _('six'), _('seven'), _('eight'), _('nine'))[value-1]
register.filter(apnumber) register.filter(apnumber)
def naturalday(value, arg=None):
"""
For date values that are tomorrow, today or yesterday compared to
present day returns representing string. Otherwise, returns a string
formatted according to settings.DATE_FORMAT.
"""
try:
value = date(value.year, value.month, value.day)
except AttributeError:
# Passed value wasn't a date object
return value
except ValueError:
# Date arguments out of range
return value
delta = value - date.today()
if delta.days == 0:
return _(u'today')
elif delta.days == 1:
return _(u'tomorrow')
elif delta.days == -1:
return _(u'yesterday')
return defaultfilters.date(value, arg)
register.filter(naturalday)

View File

@ -138,6 +138,29 @@ Examples:
You can pass in either an integer or a string representation of an integer. You can pass in either an integer or a string representation of an integer.
naturalday
----------
**New in Django development version**
For dates that are the current day or within one day, return "today",
"tomorrow" or "yesterday", as appropriate. Otherwise, format the date using
the passed in format string.
**Argument:** Date formatting string as described in default tag now_.
.. _now: ../templates/#now
Examples (when 'today' is 17 Feb 2007):
* ``16 Feb 2007`` becomes ``yesterday``.
* ``17 Feb 2007`` becomes ``today``.
* ``18 Feb 2007`` becomes ``tomorrow``.
* Any other day is formatted according to given argument or the
`DATE_FORMAT`_ setting if no argument is given.
.. _DATE_FORMAT: ../settings/#date_format
flatpages flatpages
========= =========

View File

@ -1,5 +1,8 @@
import unittest import unittest
from datetime import timedelta, date
from django.template import Template, Context, add_to_builtins from django.template import Template, Context, add_to_builtins
from django.utils.dateformat import DateFormat
from django.utils.translation import ugettext as _
add_to_builtins('django.contrib.humanize.templatetags.humanize') add_to_builtins('django.contrib.humanize.templatetags.humanize')
@ -8,13 +11,12 @@ class HumanizeTests(unittest.TestCase):
def humanize_tester(self, test_list, result_list, method): def humanize_tester(self, test_list, result_list, method):
# Using max below ensures we go through both lists # Using max below ensures we go through both lists
# However, if the lists are not equal length, this raises an exception # However, if the lists are not equal length, this raises an exception
for index in xrange(len(max(test_list,result_list))): for index in xrange(max(len(test_list), len(result_list))):
test_content = test_list[index] test_content = test_list[index]
t = Template('{{ test_content|%s }}' % method) t = Template('{{ test_content|%s }}' % method)
rendered = t.render(Context(locals())).strip() rendered = t.render(Context(locals())).strip()
self.assertEqual(rendered, result_list[index], self.assertEqual(rendered, result_list[index],
msg="""%s test failed, produced %s, msg="%s test failed, produced %s, should've produced %s" % (method, rendered, result_list[index]))
should've produced %s""" % (method, rendered, result_list[index]))
def test_ordinal(self): def test_ordinal(self):
test_list = ('1','2','3','4','11','12', test_list = ('1','2','3','4','11','12',
@ -49,6 +51,20 @@ should've produced %s""" % (method, rendered, result_list[index]))
self.humanize_tester(test_list, result_list, 'apnumber') self.humanize_tester(test_list, result_list, 'apnumber')
def test_naturalday(self):
from django.template import defaultfilters
today = date.today()
yesterday = today - timedelta(days=1)
tomorrow = today + timedelta(days=1)
someday = today - timedelta(days=10)
notdate = u"I'm not a date value"
test_list = (today, yesterday, tomorrow, someday, notdate)
someday_result = defaultfilters.date(someday)
result_list = (_(u'today'), _(u'yesterday'), _(u'tomorrow'),
someday_result, u"I'm not a date value")
self.humanize_tester(test_list, result_list, 'naturalday')
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()