Accept argument in naturalday_partial, downcast format if necessary

This commit is contained in:
Adeodato Simó 2023-11-14 20:27:44 -03:00
parent aaea1b1b9e
commit 6aaff28c13
No known key found for this signature in database
GPG key ID: CDF447845F1A986F
2 changed files with 46 additions and 7 deletions

View file

@ -8,15 +8,23 @@ from bookwyrm.utils.partial_date import PartialDate
register = template.Library() register = template.Library()
@register.filter(expects_localtime=True, is_safe=False) @register.filter(expects_localtime=True)
def naturalday_partial(date): def naturalday_partial(date, arg=None):
"""allow templates to easily format PartialDate objects""" """chooses appropriate precision if date is a PartialDate object
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):
return defaultfilters.date(date) return defaultfilters.date(date, arg)
if arg is None:
arg = "DATE_FORMAT"
if date.has_day: if date.has_day:
fmt = "DATE_FORMAT" fmt = arg
elif date.has_month: elif date.has_month:
fmt = "YEAR_MONTH_FORMAT" # there is no SHORT_YEAR_MONTH_FORMAT, so we ignore SHORT_DATE_FORMAT :(
fmt = "YEAR_MONTH_FORMAT" if arg == "DATE_FORMAT" else arg
else: else:
fmt = "Y" fmt = "Y" if arg in django_formats else arg
return naturalday(date, fmt) return naturalday(date, fmt)

View file

@ -29,3 +29,34 @@ class PartialDateTags(TestCase):
self.assertEqual("2023", date_ext.naturalday_partial(self._partial_year)) self.assertEqual("2023", date_ext.naturalday_partial(self._partial_year))
self.assertEqual("June 2023", date_ext.naturalday_partial(self._partial_month)) self.assertEqual("June 2023", date_ext.naturalday_partial(self._partial_month))
self.assertEqual("30 Jun 2023", date_ext.naturalday_partial(self._partial_day)) self.assertEqual("30 Jun 2023", date_ext.naturalday_partial(self._partial_day))
def test_format_arg_is_used(self):
"""the provided format should be used by default"""
self.assertEqual("Dec.31", date_ext.naturalday_partial(self._dt, "M.j"))
self.assertEqual("Dec.31", date_ext.naturalday_partial(self._date, "M.j"))
self.assertEqual("June", date_ext.naturalday_partial(self._partial_day, "F"))
def test_month_precision_downcast(self):
"""precision is adjusted for well-known date formats"""
self.assertEqual(
"June 2023", date_ext.naturalday_partial(self._partial_month, "DATE_FORMAT")
)
def test_year_precision_downcast(self):
"""precision is adjusted for well-known date formats"""
for fmt in "DATE_FORMAT", "SHORT_DATE_FORMAT", "YEAR_MONTH_FORMAT":
with self.subTest(desc=fmt):
self.assertEqual(
"2023", date_ext.naturalday_partial(self._partial_year, fmt)
)
def test_nonstandard_formats_passthru(self):
"""garbage-in, garbage-out: we don't mess with unknown date formats"""
# Expected because there is no SHORT_YEAR_MONTH_FORMAT in Django that we can use
self.assertEqual(
"30/06/2023",
date_ext.naturalday_partial(self._partial_month, "SHORT_DATE_FORMAT"),
)
self.assertEqual(
"December.31", date_ext.naturalday_partial(self._partial_year, "F.j")
)