From 0e3936cb613430f128a2fdc9109f515bddf43118 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adeodato=20Sim=C3=B3?= Date: Mon, 1 Jan 2024 18:16:28 +0100 Subject: [PATCH 1/3] naturalday_partial: do naturalize date and datetime objects --- bookwyrm/templatetags/date_ext.py | 2 +- bookwyrm/tests/templatetags/test_date_ext.py | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/bookwyrm/templatetags/date_ext.py b/bookwyrm/templatetags/date_ext.py index 6dc320bed..ae2690321 100644 --- a/bookwyrm/templatetags/date_ext.py +++ b/bookwyrm/templatetags/date_ext.py @@ -17,7 +17,7 @@ def naturalday_partial(date, arg=None): """ django_formats = ("DATE_FORMAT", "SHORT_DATE_FORMAT", "YEAR_MONTH_FORMAT") if not isinstance(date, PartialDate): - return defaultfilters.date(date, arg) + return naturalday(date, arg) if arg is None: arg = "DATE_FORMAT" if date.has_day: diff --git a/bookwyrm/tests/templatetags/test_date_ext.py b/bookwyrm/tests/templatetags/test_date_ext.py index f7ea73891..ebeb82907 100644 --- a/bookwyrm/tests/templatetags/test_date_ext.py +++ b/bookwyrm/tests/templatetags/test_date_ext.py @@ -2,9 +2,15 @@ from dateutil.parser import isoparse from django.test import TestCase, override_settings +from django.utils import timezone 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") @@ -60,3 +66,14 @@ class PartialDateTags(TestCase): self.assertEqual( "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)) 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 2/3] 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")) From 4711b3bc1931e38fae814738b2ac72ef6970ab35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adeodato=20Sim=C3=B3?= Date: Mon, 1 Jan 2024 18:29:00 +0100 Subject: [PATCH 3/3] naturalday_partial: simplify/refactor --- bookwyrm/templatetags/date_ext.py | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/bookwyrm/templatetags/date_ext.py b/bookwyrm/templatetags/date_ext.py index bdad92f4c..efe55f2d9 100644 --- a/bookwyrm/templatetags/date_ext.py +++ b/bookwyrm/templatetags/date_ext.py @@ -15,18 +15,10 @@ def naturalday_partial(date, arg=None): If arg is a Django-defined format such as "DATE_FORMAT", it will be adjusted so that the precision of the PartialDate object is honored. """ - django_formats = ("DATE_FORMAT", "SHORT_DATE_FORMAT", "YEAR_MONTH_FORMAT") - if not isinstance(date, PartialDate): + if not isinstance(date, PartialDate) or date.has_day: return naturalday(date, arg) - if arg is None: - arg = "DATE_FORMAT" - if date.has_day: - fmt = arg - elif date.has_month: - # there is no SHORT_YEAR_MONTH_FORMAT, so we ignore SHORT_DATE_FORMAT :( - fmt = "YEAR_MONTH_FORMAT" if arg == "DATE_FORMAT" else arg - else: - fmt = "Y" if arg in django_formats else arg - if date.has_day: - return naturalday(date, fmt) - return defaultfilters.date(date, fmt) + if not arg or arg == "DATE_FORMAT": + arg = "YEAR_MONTH_FORMAT" if date.has_month else "Y" + elif not date.has_month and arg in ("SHORT_DATE_FORMAT", "YEAR_MONTH_FORMAT"): + arg = "Y" + return defaultfilters.date(date, arg)