naturalday_partial: do not naturalize dates with missing parts

This commit is contained in:
Adeodato Simó 2024-01-01 18:23:51 +01:00
parent 0e3936cb61
commit 0d908b594c
No known key found for this signature in database
GPG key ID: CDF447845F1A986F
2 changed files with 9 additions and 1 deletions

View file

@ -27,4 +27,6 @@ def naturalday_partial(date, arg=None):
fmt = "YEAR_MONTH_FORMAT" if arg == "DATE_FORMAT" else arg
else:
fmt = "Y" if arg in django_formats else arg
return naturalday(date, fmt)
if date.has_day:
return naturalday(date, fmt)
return defaultfilters.date(date, fmt)

View file

@ -77,3 +77,9 @@ class PartialDateTags(TestCase):
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))
# dates with missing parts can't
today_year = YearParts.from_datetime(today)
today_month = MonthParts.from_datetime(today)
self.assertEqual(str(today.year), date_ext.naturalday_partial(today_year))
self.assertEqual(str(today.year), date_ext.naturalday_partial(today_month, "Y"))