From 0d908b594cd858b74b32541e23a090fa7f321617 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adeodato=20Sim=C3=B3?= Date: Mon, 1 Jan 2024 18:23:51 +0100 Subject: [PATCH] naturalday_partial: do not naturalize dates with missing parts --- bookwyrm/templatetags/date_ext.py | 4 +++- bookwyrm/tests/templatetags/test_date_ext.py | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/bookwyrm/templatetags/date_ext.py b/bookwyrm/templatetags/date_ext.py index ae2690321..bdad92f4c 100644 --- a/bookwyrm/templatetags/date_ext.py +++ b/bookwyrm/templatetags/date_ext.py @@ -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) diff --git a/bookwyrm/tests/templatetags/test_date_ext.py b/bookwyrm/tests/templatetags/test_date_ext.py index ebeb82907..bd31a95c9 100644 --- a/bookwyrm/tests/templatetags/test_date_ext.py +++ b/bookwyrm/tests/templatetags/test_date_ext.py @@ -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"))