Ticket #3311: natural_day_r4537.diff

File natural_day_r4537.diff, 3.7 KB (added by Jyrki Pulliainen <jyrki.pulliainen@…>, 18 years ago)

naturalday patch incl. tests

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

     
    11from django import template
     2from django.utils.translation import gettext as _
     3from django.utils.dateformat import DateFormat
     4from django.conf import settings
     5from datetime import date, timedelta
    26import re
    37
    48register = template.Library()
     
    6266        return value
    6367    return ('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine')[value-1]
    6468register.filter(apnumber)
     69
     70def naturalday(value):
     71    """
     72    For date values that are tomorrow, today or yesterday compared to
     73    present day returns representing string. Otherwise, returns a string
     74    formatted according to settings.DATE_FORMAT.
     75    """
     76    try:
     77        value = date(value.year, value.month, value.day)
     78    except AttributeError:
     79        # Passed value wasn't a date object
     80        return value
     81    except ValueError:
     82        # Date arguments out of range
     83        return value
     84    today = date.today()
     85    delta = timedelta(days=1)
     86    if value == today:
     87        return _('today')
     88    elif value == today + delta:
     89        return _('tomorrow')
     90    elif value == today - delta:
     91        return _('yesterday')
     92    return DateFormat(value).format(settings.DATE_FORMAT)
     93register.filter(naturalday)
  • tests/regressiontests/humanize/tests.py

     
     1# Quick tests for the humanize templatefilters (django.contrib.humanize)
     2
     3from django.template import Template, Context, add_to_builtins
     4from django.conf import settings
     5from django.utils.dateformat import DateFormat
     6from django.utils.translation import gettext as _
     7from datetime import timedelta, date
     8import unittest
     9
     10add_to_builtins('django.contrib.humanize.templatetags.humanize')
     11
     12class TemplateTest(unittest.TestCase):
     13    def test_naturalday(self):
     14       
     15        today = date.today()
     16        yesterday = today - timedelta(days=1)
     17        tomorrow = today + timedelta(days=1)
     18        someday = today - timedelta(days=10)
     19        notdate = "I'm not a date value"
     20
     21        t_today = Template('{{ today|naturalday }}')
     22        t_tomorrow = Template('{{ tomorrow|naturalday }}')
     23        t_yesterday = Template('{{ yesterday|naturalday }}')
     24        t_someday = Template('{{ someday|naturalday }}')
     25        t_notdate = Template('{{ notdate|naturalday }}')
     26
     27        rendered_today = t_today.render(Context(locals())).strip()
     28        self.assertEqual(rendered_today, _('today'))
     29
     30        rendered_yesterday = t_yesterday.render(Context(locals())).strip()
     31        self.assertEqual(rendered_yesterday, _('yesterday'))
     32
     33        rendered_tomorrow = t_tomorrow.render(Context(locals())).strip()
     34        self.assertEqual(rendered_tomorrow, _('tomorrow'))
     35
     36        rendered_someday = t_someday.render(Context(locals())).strip()
     37        self.assertEqual(rendered_someday, DateFormat(someday).format(settings.DATE_FORMAT))
     38
     39        rendered_notdate = t_notdate.render(Context(locals())).strip()
     40        self.assertEqual(rendered_notdate, "I'm not a date value")
     41       
  • tests/regressiontests/humanize/models.py

     
     1
Back to Top