Merge pull request #11426 from edx/peter-fogg/fix-date-block-format
Correctly format date block dates.
This commit is contained in:
@@ -68,28 +68,9 @@ class DateSummary(object):
|
||||
|
||||
def get_context(self):
|
||||
"""Return the template context used to render this summary block."""
|
||||
date = ''
|
||||
if self.date is not None:
|
||||
# Translators: relative_date is a fuzzy description of the
|
||||
# time from now until absolute_date. For example,
|
||||
# absolute_date might be "Jan 01, 2020", and if today were
|
||||
# December 5th, 2020, relative_date would be "1 month".
|
||||
locale = to_locale(get_language())
|
||||
try:
|
||||
relative_date = format_timedelta(self.date - datetime.now(pytz.UTC), locale=locale)
|
||||
# Babel doesn't have translations for Esperanto, so we get
|
||||
# a KeyError when testing translations with
|
||||
# ?preview-lang=eo. This should not happen with any other
|
||||
# languages. See https://github.com/python-babel/babel/issues/107
|
||||
except KeyError:
|
||||
relative_date = format_timedelta(self.date - datetime.now(pytz.UTC))
|
||||
date = _("in {relative_date} - {absolute_date}").format(
|
||||
relative_date=relative_date,
|
||||
absolute_date=self.date.strftime(self.date_format),
|
||||
)
|
||||
return {
|
||||
'title': self.title,
|
||||
'date': date,
|
||||
'date': self._format_date(),
|
||||
'description': self.description,
|
||||
'css_class': self.css_class,
|
||||
'link': self.link,
|
||||
@@ -102,6 +83,35 @@ class DateSummary(object):
|
||||
"""
|
||||
return render_to_string('courseware/date_summary.html', self.get_context())
|
||||
|
||||
def _format_date(self):
|
||||
"""
|
||||
Return this block's date in a human-readable format. If the date
|
||||
is None, returns the empty string.
|
||||
"""
|
||||
if self.date is None:
|
||||
return ''
|
||||
locale = to_locale(get_language())
|
||||
delta = self.date - datetime.now(pytz.UTC)
|
||||
try:
|
||||
relative_date = format_timedelta(delta, locale=locale)
|
||||
# Babel doesn't have translations for Esperanto, so we get
|
||||
# a KeyError when testing translations with
|
||||
# ?preview-lang=eo. This should not happen with any other
|
||||
# languages. See https://github.com/python-babel/babel/issues/107
|
||||
except KeyError:
|
||||
relative_date = format_timedelta(delta)
|
||||
date_has_passed = delta.days < 0
|
||||
# Translators: 'absolute' is a date such as "Jan 01,
|
||||
# 2020". 'relative' is a fuzzy description of the time until
|
||||
# 'absolute'. For example, 'absolute' might be "Jan 01, 2020",
|
||||
# and if today were December 5th, 2020, 'relative' would be "1
|
||||
# month".
|
||||
date_format = _("{relative} ago - {absolute}") if date_has_passed else _("in {relative} - {absolute}")
|
||||
return date_format.format(
|
||||
relative=relative_date,
|
||||
absolute=self.date.strftime(self.date_format),
|
||||
)
|
||||
|
||||
@property
|
||||
def is_enabled(self):
|
||||
"""
|
||||
|
||||
@@ -257,3 +257,18 @@ class CourseDateSummaryTest(SharedModuleStoreTestCase):
|
||||
)
|
||||
self.assertEqual(block.link_text, 'Learn More')
|
||||
self.assertEqual(block.link, '')
|
||||
|
||||
@freezegun.freeze_time('2015-01-02')
|
||||
@ddt.data(
|
||||
(-1, '1 day ago - Jan 01, 2015'),
|
||||
(1, 'in 1 day - Jan 03, 2015')
|
||||
)
|
||||
@ddt.unpack
|
||||
def test_render_date_string_past(self, delta, expected_date_string):
|
||||
self.setup_course_and_user(
|
||||
days_till_start=-10,
|
||||
verification_status='denied',
|
||||
days_till_verification_deadline=delta,
|
||||
)
|
||||
block = VerificationDeadlineDate(self.course, self.user)
|
||||
self.assertEqual(block.get_context()['date'], expected_date_string)
|
||||
|
||||
Reference in New Issue
Block a user