naturalday_partial: do naturalize date and datetime objects

This commit is contained in:
Adeodato Simó 2024-01-01 18:16:28 +01:00
parent 000e5e6145
commit 0e3936cb61
No known key found for this signature in database
GPG key ID: CDF447845F1A986F
2 changed files with 19 additions and 2 deletions

View file

@ -17,7 +17,7 @@ def naturalday_partial(date, arg=None):
""" """
django_formats = ("DATE_FORMAT", "SHORT_DATE_FORMAT", "YEAR_MONTH_FORMAT") django_formats = ("DATE_FORMAT", "SHORT_DATE_FORMAT", "YEAR_MONTH_FORMAT")
if not isinstance(date, PartialDate): if not isinstance(date, PartialDate):
return defaultfilters.date(date, arg) return naturalday(date, arg)
if arg is None: if arg is None:
arg = "DATE_FORMAT" arg = "DATE_FORMAT"
if date.has_day: if date.has_day:

View file

@ -2,9 +2,15 @@
from dateutil.parser import isoparse from dateutil.parser import isoparse
from django.test import TestCase, override_settings from django.test import TestCase, override_settings
from django.utils import timezone
from bookwyrm.templatetags import date_ext from bookwyrm.templatetags import date_ext
from bookwyrm.utils.partial_date import MonthParts, YearParts, from_partial_isoformat from bookwyrm.utils.partial_date import (
MonthParts,
PartialDate,
YearParts,
from_partial_isoformat,
)
@override_settings(LANGUAGE_CODE="en-AU") @override_settings(LANGUAGE_CODE="en-AU")
@ -60,3 +66,14 @@ class PartialDateTags(TestCase):
self.assertEqual( self.assertEqual(
"December.31", date_ext.naturalday_partial(self._partial_year, "F.j") "December.31", date_ext.naturalday_partial(self._partial_year, "F.j")
) )
def test_natural_format(self):
"""today and yesterday are handled correctly"""
today = timezone.now()
today_date = today.date()
today_exact = PartialDate.from_datetime(today)
# exact dates can be naturalized
self.assertEqual("today", date_ext.naturalday_partial(today))
self.assertEqual("today", date_ext.naturalday_partial(today_date))
self.assertEqual("today", date_ext.naturalday_partial(today_exact))