Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #3311 -- Added naturalday filter to contrib.humanize. Thanks, J…

…yrki Pulliainen.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@5985 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 14b5e074ba7a57a5421d5b7217141c63c9c481d5 1 parent b367ec2
Malcolm Tredinnick authored August 20, 2007
1  AUTHORS
@@ -226,6 +226,7 @@ answer newbie questions, and generally made Django that much better:
226 226
     plisk
227 227
     Daniel Poelzleithner <http://poelzi.org/>
228 228
     polpak@yahoo.com
  229
+    Jyrki Pulliainen <jyrki.pulliainen@gmail.com>
229 230
     Johann Queuniet <johann.queuniet@adh.naellia.eu>
230 231
     J. Rademaker
231 232
     Michael Radziej <mir@noris.de>
27  django/contrib/humanize/templatetags/humanize.py
... ...
@@ -1,6 +1,9 @@
1 1
 from django.utils.translation import ungettext, ugettext as _
2 2
 from django.utils.encoding import force_unicode
3 3
 from django import template
  4
+from django.template import defaultfilters
  5
+from django.conf import settings
  6
+from datetime import date, timedelta
4 7
 import re
5 8
 
6 9
 register = template.Library()
@@ -67,3 +70,27 @@ def apnumber(value):
67 70
         return value
68 71
     return (_('one'), _('two'), _('three'), _('four'), _('five'), _('six'), _('seven'), _('eight'), _('nine'))[value-1]
69 72
 register.filter(apnumber)
  73
+
  74
+def 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)
  96
+register.filter(naturalday)
23  docs/add_ons.txt
@@ -138,6 +138,29 @@ Examples:
138 138
 
139 139
 You can pass in either an integer or a string representation of an integer.
140 140
 
  141
+naturalday
  142
+----------
  143
+
  144
+**New in Django development version**
  145
+
  146
+For dates that are the current day or within one day, return "today",
  147
+"tomorrow" or "yesterday", as appropriate. Otherwise, format the date using
  148
+the passed in format string.
  149
+
  150
+**Argument:** Date formatting string as described in default tag now_.
  151
+
  152
+.. _now: ../templates/#now
  153
+
  154
+Examples (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
+
141 164
 flatpages
142 165
 =========
143 166
 
22  tests/regressiontests/humanize/tests.py
... ...
@@ -1,5 +1,8 @@
1 1
 import unittest
  2
+from datetime import timedelta, date
2 3
 from django.template import Template, Context, add_to_builtins
  4
+from django.utils.dateformat import DateFormat
  5
+from django.utils.translation import ugettext as _
3 6
 
4 7
 add_to_builtins('django.contrib.humanize.templatetags.humanize')
5 8
 
@@ -8,13 +11,12 @@ class HumanizeTests(unittest.TestCase):
8 11
     def humanize_tester(self, test_list, result_list, method):
9 12
         # Using max below ensures we go through both lists
10 13
         # 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))):
12 15
             test_content = test_list[index]
13 16
             t = Template('{{ test_content|%s }}' % method)
14 17
             rendered = t.render(Context(locals())).strip()
15 18
             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]))
18 20
 
19 21
     def test_ordinal(self):
20 22
         test_list = ('1','2','3','4','11','12',
@@ -49,6 +51,20 @@ def test_apnumber(self):
49 51
 
50 52
         self.humanize_tester(test_list, result_list, 'apnumber')
51 53
 
  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
+
52 68
 if __name__ == '__main__':
53 69
     unittest.main()
54 70
 

0 notes on commit 14b5e07

Please sign in to comment.
Something went wrong with that request. Please try again.