Django

Code

Ticket #3311 (closed: fixed)

Opened 2 years ago

Last modified 1 year ago

New humanize filter "naturalday" (yesterday, today, tomorrow)

Reported by: exogen@gmail.com Assigned to: adrian
Milestone: Component: Template system
Version: Keywords:
Cc: jyrki.pulliainen@gmail.com Triage Stage: Ready for checkin
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

Just something I think people might want in humanize or defaultfilters. This will return 'yesterday', 'today', and 'tomorrow' when the given date has the appropriate relation with the current date. Otherwise, return the full day name. Maybe it could even be extended to do 'last Tuesday', 'this Tuesday', or 'next Tuesday' when appropriate. Anyway the idea is that it is very common to hear this method of communicating the day in speech.

import datetime

def naturalday(value):
    today = datetime.date.today()
    value = datetime.date(value.year, value.month, value.day)
    delta = datetime.timedelta(days=1)
    if value == today:
        return 'today'
    elif value == today + delta:
        return 'tomorrow'
    elif value == today - delta:
        return 'yesterday'
    return value.strftime('%A')

Attachments

natural_day_r4537.diff (3.7 kB) - added by Jyrki Pulliainen <jyrki.pulliainen@gmail.com> on 02/17/07 11:53:32.
naturalday patch incl. tests
naturalday_docs_r4537.diff (0.6 kB) - added by Jyrki Pulliainen <jyrki.pulliainen@gmail.com> on 02/17/07 12:05:21.
Documentation for naturalday
naturalday_docs_v2.diff (0.7 kB) - added by Jyrki Pulliainen <jyrki.pulliainen@gmail.com> on 02/17/07 16:11:05.
ReST typo fixed
naturalday_docs_v3.diff (0.8 kB) - added by Jyrki Pulliainen <jyrki.pulliainen@gmail.com> on 02/19/07 07:42:02.
New docs for latest changes
natural_day_tests_v3.diff (2.5 kB) - added by Jyrki Pulliainen <jyrki.pulliainen@gmail.com> on 02/19/07 07:42:33.
Tests against v3 patch
natural_day_v3.diff (1.4 kB) - added by Jyrki Pulliainen <jyrki.pulliainen@gmail.com> on 02/19/07 07:43:21.
New patch
natural_day_v4.diff (1.4 kB) - added by Jyrki Pulliainen <jyrki.pulliainen@gmail.com> on 02/19/07 10:04:24.
Naturalday patch with the time delta
naturalday_unicode.patch (4.1 kB) - added by Jyrki Pulliainen <jyrki.pulliainen@gmail.com> on 07/17/07 07:14:56.
Merged patch with latest trunk and made it use unicode gettext
naturalday_unicode-5722.patch (3.6 kB) - added by Jyrki Pulliainen <jyrki.pulliainen@gmail.com> on 07/17/07 07:19:14.
Unicode-aware patch (fixed some typos) against 5722

Change History

01/17/07 18:00:24 changed by mir@noris.de

  • summary changed from [Code] New humanize filter "naturalday" (yesterday, today, tomorrow) to New humanize filter "naturalday" (yesterday, today, tomorrow).
  • stage changed from Unreviewed to Design decision needed.

01/24/07 18:57:59 changed by adrian

  • stage changed from Design decision needed to Accepted.

Sounds good to me. Could you provide a patch plus tests and docs?

01/24/07 20:29:20 changed by Gary Wilson <gary.wilson@gmail.com>

  • needs_better_patch set to 1.
  • has_patch set to 1.
  • needs_tests set to 1.
  • needs_docs set to 1.

I wouldn't think you would want to output just the day of the week for dates and datetimes more/less than 1 day away/ago. Maybe it should instead default to settings.DATE_FORMAT or settings.DATETIME_FORMAT, also taking an optional date format string that would override the default.

02/17/07 11:53:32 changed by Jyrki Pulliainen <jyrki.pulliainen@gmail.com>

  • attachment natural_day_r4537.diff added.

naturalday patch incl. tests

02/17/07 11:54:51 changed by Jyrki Pulliainen <jyrki.pulliainen@gmail.com>

  • cc set to jyrki.pulliainen@gmail.com.
  • needs_better_patch deleted.
  • needs_tests deleted.

Added a patch including unittests. Presented patch now does the fallback formatting according to settings.DATE_FORMAT. If passed value is not a date value, it returns the value without throwing an exception

02/17/07 12:05:21 changed by Jyrki Pulliainen <jyrki.pulliainen@gmail.com>

  • attachment naturalday_docs_r4537.diff added.

Documentation for naturalday

02/17/07 16:11:05 changed by Jyrki Pulliainen <jyrki.pulliainen@gmail.com>

  • attachment naturalday_docs_v2.diff added.

ReST typo fixed

02/18/07 10:15:43 changed by Gary Wilson <gary.wilson@gmail.com>

  • needs_better_patch set to 1.
  • needs_tests set to 1.

I would suggest allowing a format string to be passed in as well and call django.template.defaultfilters.date() when the date is not yesterday, today, or tomorrow. That way, the default settings.DATE_FORMAT could be overridden in the template just as with the date template filter.

02/19/07 07:42:02 changed by Jyrki Pulliainen <jyrki.pulliainen@gmail.com>

  • attachment naturalday_docs_v3.diff added.

New docs for latest changes

02/19/07 07:42:33 changed by Jyrki Pulliainen <jyrki.pulliainen@gmail.com>

  • attachment natural_day_tests_v3.diff added.

Tests against v3 patch

02/19/07 07:43:21 changed by Jyrki Pulliainen <jyrki.pulliainen@gmail.com>

  • attachment natural_day_v3.diff added.

New patch

02/19/07 07:45:00 changed by Jyrki Pulliainen <jyrki.pulliainen@gmail.com>

  • needs_better_patch deleted.
  • needs_tests deleted.
  • needs_docs deleted.

New patch, uses now the defaultfilter date().

Argument using documented and new tests added.

(follow-up: ↓ 8 ) 02/19/07 09:41:53 changed by Brian Beck <exogen@gmail.com>

This is pretty minor, but I have a hunch it would be a little quicker to do:

delta = value - today
if delta.days == 0:
    return _('today')
if delta.days == 1:
    return _('tomorrow')
elif delta.days == -1:
    return _('yesterday')

...so that only one delta is calculated and less comparisons will happen (no date.__eq__/__cmp__).

P.S. Awesome job on the patches Jyrki!

02/19/07 10:04:24 changed by Jyrki Pulliainen <jyrki.pulliainen@gmail.com>

  • attachment natural_day_v4.diff added.

Naturalday patch with the time delta

(in reply to: ↑ 7 ) 02/19/07 10:05:43 changed by Jyrki Pulliainen <jyrki.pulliainen@gmail.com>

Replying to Brian Beck <exogen@gmail.com>:

...so that only one delta is calculated and less comparisons will happen (no date.__eq__/__cmp__).

Changed the code to do less compares. Thank you for the tip

P.S. Awesome job on the patches Jyrki!

Thanks!

02/19/07 13:38:38 changed by SmileyChris

I'm not up with the play on the whole gettext side of things, but I thought I remembered there is a move away from using _ as the name for gettext. Or maybe not... just checking.

07/16/07 15:57:06 changed by Brian Rosner <brosner@gmail.com>

FYI, this patch needs to get updated to trunk. The unicode branch is now requiring a slight change.

from django.utils.translation import gettext as _

to

from django.utils.translation import ugettext as _

07/17/07 07:14:56 changed by Jyrki Pulliainen <jyrki.pulliainen@gmail.com>

  • attachment naturalday_unicode.patch added.

Merged patch with latest trunk and made it use unicode gettext

07/17/07 07:19:14 changed by Jyrki Pulliainen <jyrki.pulliainen@gmail.com>

  • attachment naturalday_unicode-5722.patch added.

Unicode-aware patch (fixed some typos) against 5722

07/17/07 08:32:55 changed by Simon G. <dev@simon.net.nz>

  • stage changed from Accepted to Ready for checkin.

Last version looks good - tests, docs & everything.

08/20/07 03:50:09 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

(In [5985]) Fixed #3311 -- Added naturalday filter to contrib.humanize. Thanks, Jyrki Pulliainen.


Add/Change #3311 (New humanize filter "naturalday" (yesterday, today, tomorrow))




Change Properties
Action