mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-06-02 21:39:23 +00:00
Merge pull request #3177 from dato/naturalday_partial_fixes
Adjustments to naturalday_partial
This commit is contained in:
commit
fb16806afe
|
@ -15,16 +15,10 @@ def naturalday_partial(date, arg=None):
|
||||||
If arg is a Django-defined format such as "DATE_FORMAT", it will be adjusted
|
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.
|
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) or date.has_day:
|
||||||
if not isinstance(date, PartialDate):
|
return naturalday(date, arg)
|
||||||
return defaultfilters.date(date, arg)
|
if not arg or arg == "DATE_FORMAT":
|
||||||
if arg is None:
|
arg = "YEAR_MONTH_FORMAT" if date.has_month else "Y"
|
||||||
arg = "DATE_FORMAT"
|
elif not date.has_month and arg in ("SHORT_DATE_FORMAT", "YEAR_MONTH_FORMAT"):
|
||||||
if date.has_day:
|
arg = "Y"
|
||||||
fmt = arg
|
return defaultfilters.date(date, 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
|
|
||||||
return naturalday(date, fmt)
|
|
||||||
|
|
|
@ -2,9 +2,15 @@
|
||||||
from dateutil.parser import isoparse
|
from dateutil.parser import isoparse
|
||||||
|
|
||||||
from django.test import TestCase, override_settings
|
from django.test import TestCase, override_settings
|
||||||
|
from django.utils import timezone
|
||||||
|
|
||||||
from bookwyrm.templatetags import date_ext
|
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")
|
@override_settings(LANGUAGE_CODE="en-AU")
|
||||||
|
@ -60,3 +66,20 @@ class PartialDateTags(TestCase):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
"December.31", date_ext.naturalday_partial(self._partial_year, "F.j")
|
"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))
|
||||||
|
|
||||||
|
# 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"))
|
||||||
|
|
Loading…
Reference in a new issue