mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-26 03:21:05 +00:00
Accept argument in naturalday_partial, downcast format if necessary
This commit is contained in:
parent
aaea1b1b9e
commit
6aaff28c13
2 changed files with 46 additions and 7 deletions
|
@ -8,15 +8,23 @@ from bookwyrm.utils.partial_date import PartialDate
|
|||
register = template.Library()
|
||||
|
||||
|
||||
@register.filter(expects_localtime=True, is_safe=False)
|
||||
def naturalday_partial(date):
|
||||
"""allow templates to easily format PartialDate objects"""
|
||||
@register.filter(expects_localtime=True)
|
||||
def naturalday_partial(date, arg=None):
|
||||
"""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):
|
||||
return defaultfilters.date(date)
|
||||
return defaultfilters.date(date, arg)
|
||||
if arg is None:
|
||||
arg = "DATE_FORMAT"
|
||||
if date.has_day:
|
||||
fmt = "DATE_FORMAT"
|
||||
fmt = arg
|
||||
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:
|
||||
fmt = "Y"
|
||||
fmt = "Y" if arg in django_formats else arg
|
||||
return naturalday(date, fmt)
|
||||
|
|
|
@ -29,3 +29,34 @@ class PartialDateTags(TestCase):
|
|||
self.assertEqual("2023", date_ext.naturalday_partial(self._partial_year))
|
||||
self.assertEqual("June 2023", date_ext.naturalday_partial(self._partial_month))
|
||||
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")
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue