kopia lustrzana https://github.com/wagtail/wagtail
Improve timesince_last_update-tag
* "time_prefix" has been replaced by the boolean "show_time_prefix" to only handle the single prefix "at ". * It now always uses complete gettext strings to allow better customization in each language. * For dates older then the current date "user_display_name" is now used correctly. * Improved the tests to handle some of the missing cases.pull/9254/head
rodzic
4c7e1ea1ce
commit
a7fb8157d3
|
@ -1,6 +1,6 @@
|
|||
{% load i18n wagtailadmin_tags %}
|
||||
{% if last_updated %}
|
||||
<span title="{{ last_updated }}" data-wagtail-tooltip="{{ last_updated }}" {% if classname %}class="{{ classname }}"{% endif %}>
|
||||
{% if since_text %}{{ since_text }}{% endif %} {% timesince_last_update last_updated time_prefix=time_prefix %}
|
||||
{% if since_text %}{{ since_text }}{% endif %} {% timesince_last_update last_updated show_time_prefix=show_time_prefix %}
|
||||
</span>
|
||||
{% endif %}
|
||||
|
|
|
@ -694,6 +694,7 @@ def timesince_simple(d):
|
|||
1 week, 1 day ago -> 1 week ago
|
||||
0 minutes ago -> just now
|
||||
"""
|
||||
# Note: Duplicate code in timesince_last_update()
|
||||
time_period = timesince(d).split(",")[0]
|
||||
if time_period == avoid_wrapping(_("0 minutes")):
|
||||
return _("Just now")
|
||||
|
@ -702,29 +703,59 @@ def timesince_simple(d):
|
|||
|
||||
@register.simple_tag
|
||||
def timesince_last_update(
|
||||
last_update, time_prefix="", user_display_name="", use_shorthand=True
|
||||
last_update, show_time_prefix=False, user_display_name="", use_shorthand=True
|
||||
):
|
||||
"""
|
||||
Returns:
|
||||
- the time of update if last_update is today, if any prefix is supplied, the output will use it
|
||||
- the time of update if last_update is today, if show_time_prefix=True, the output will be prefixed with "at "
|
||||
- time since last update otherwise. Defaults to the simplified timesince,
|
||||
but can return the full string if needed
|
||||
"""
|
||||
# translation usage below is intentionally verbose to be easier to work with translations
|
||||
|
||||
if last_update.date() == datetime.today().date():
|
||||
if timezone.is_aware(last_update):
|
||||
time_str = timezone.localtime(last_update).strftime("%H:%M")
|
||||
else:
|
||||
time_str = last_update.strftime("%H:%M")
|
||||
|
||||
time_prefix = f"{time_prefix} " if time_prefix else time_prefix
|
||||
by_user = f" by {user_display_name}" if user_display_name else user_display_name
|
||||
|
||||
return f"{time_prefix}{time_str}{by_user}"
|
||||
|
||||
if show_time_prefix:
|
||||
if user_display_name:
|
||||
return _("at %(time)s by %(user_display_name)s") % {
|
||||
"time": time_str,
|
||||
"user_display_name": user_display_name,
|
||||
}
|
||||
else:
|
||||
return _("at %(time)s") % {"time": time_str}
|
||||
else:
|
||||
if user_display_name:
|
||||
return _("%(time)s by %(user_display_name)s") % {
|
||||
"time": time_str,
|
||||
"user_display_name": user_display_name,
|
||||
}
|
||||
else:
|
||||
return _("%(time)s") % {"time": time_str}
|
||||
else:
|
||||
if use_shorthand:
|
||||
return timesince_simple(last_update)
|
||||
return _("%(time_period)s ago") % {"time_period": timesince(last_update)}
|
||||
# Note: Duplicate code in timesince_simple()
|
||||
time_period = timesince(last_update).split(",")[0]
|
||||
if time_period == avoid_wrapping(_("0 minutes")):
|
||||
if user_display_name:
|
||||
return _("just now by %(user_display_name)s") % {
|
||||
"user_display_name": user_display_name
|
||||
}
|
||||
else:
|
||||
return _("just now")
|
||||
else:
|
||||
time_period = timesince(last_update)
|
||||
|
||||
if user_display_name:
|
||||
return _("%(time_period)s ago by %(user_display_name)s") % {
|
||||
"time_period": time_period,
|
||||
"user_display_name": user_display_name,
|
||||
}
|
||||
else:
|
||||
return _("%(time_period)s ago") % {"time_period": time_period}
|
||||
|
||||
|
||||
@register.filter
|
||||
|
|
|
@ -142,8 +142,8 @@ class TestTimesinceTags(TestCase):
|
|||
self.assertEqual(timesince, formatted_time)
|
||||
|
||||
# Check prefix output
|
||||
timesince = timesince_last_update(dt, time_prefix="my prefix")
|
||||
self.assertEqual(timesince, "my prefix {}".format(formatted_time))
|
||||
timesince = timesince_last_update(dt, show_time_prefix=True)
|
||||
self.assertEqual(timesince, "at {}".format(formatted_time))
|
||||
|
||||
# Check user output
|
||||
timesince = timesince_last_update(dt, user_display_name="Gary")
|
||||
|
@ -151,18 +151,47 @@ class TestTimesinceTags(TestCase):
|
|||
|
||||
# Check user and prefix output
|
||||
timesince = timesince_last_update(
|
||||
dt, time_prefix="my prefix", user_display_name="Gary"
|
||||
dt, show_time_prefix=True, user_display_name="Gary"
|
||||
)
|
||||
self.assertEqual(timesince, "my prefix {} by Gary".format(formatted_time))
|
||||
self.assertEqual(timesince, "at {} by Gary".format(formatted_time))
|
||||
|
||||
def test_timesince_last_update_before_today_shows_timeago(self):
|
||||
dt = timezone.now() - timedelta(weeks=1, days=2)
|
||||
|
||||
# 1) use_shorthand=False
|
||||
|
||||
timesince = timesince_last_update(dt, use_shorthand=False)
|
||||
self.assertEqual(timesince, "1\xa0week, 2\xa0days ago")
|
||||
# The prefix is not used, if the date is older than the current day.
|
||||
self.assertEqual(
|
||||
timesince_last_update(dt, use_shorthand=False, show_time_prefix=True),
|
||||
timesince,
|
||||
)
|
||||
|
||||
# Check user output
|
||||
timesince = timesince_last_update(
|
||||
dt, use_shorthand=False, user_display_name="Gary"
|
||||
)
|
||||
self.assertEqual(timesince, "1\xa0week, 2\xa0days ago by Gary")
|
||||
self.assertEqual(
|
||||
timesince_last_update(
|
||||
dt, use_shorthand=False, user_display_name="Gary", show_time_prefix=True
|
||||
),
|
||||
timesince,
|
||||
)
|
||||
|
||||
# 2) use_shorthand=True
|
||||
|
||||
timesince = timesince_last_update(dt)
|
||||
self.assertEqual(timesince, "1\xa0week ago")
|
||||
self.assertEqual(timesince_last_update(dt, show_time_prefix=True), timesince)
|
||||
|
||||
timesince = timesince_last_update(dt, user_display_name="Gary")
|
||||
self.assertEqual(timesince, "1\xa0week ago by Gary")
|
||||
self.assertEqual(
|
||||
timesince_last_update(dt, user_display_name="Gary", show_time_prefix=True),
|
||||
timesince,
|
||||
)
|
||||
|
||||
|
||||
class TestComponentTag(TestCase):
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
<li>
|
||||
<span class="avatar small" data-wagtail-tooltip="{{ latest_log_entry.user_display_name }}"><img src="{% avatar_url latest_log_entry.user size=25 %}" alt="" decoding="async" loading="async" /></span>
|
||||
{% trans "Last updated" %}
|
||||
{% include "wagtailadmin/shared/last_updated.html" with last_updated=latest_log_entry.timestamp time_prefix="at" %}
|
||||
{% include "wagtailadmin/shared/last_updated.html" with last_updated=latest_log_entry.timestamp show_time_prefix=True %}
|
||||
</li>
|
||||
{% if history_url %}
|
||||
<li><a href="{{ history_url }}">{% icon "history" class_name="default" %} {% trans "History" %}</a></li>
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
<li>
|
||||
<span class="avatar small" data-wagtail-tooltip="{{ latest_log_entry.user_display_name }}"><img src="{% avatar_url latest_log_entry.user size=25 %}" alt="" decoding="async" loading="lazy"/></span>
|
||||
{% trans "Last updated" %}
|
||||
{% include "wagtailadmin/shared/last_updated.html" with last_updated=latest_log_entry.timestamp time_prefix="at" %}
|
||||
{% include "wagtailadmin/shared/last_updated.html" with last_updated=latest_log_entry.timestamp show_time_prefix=True %}
|
||||
</li>
|
||||
{% if history_url %}
|
||||
<li><a href="{{ history_url }}">{% icon "history" class_name="default" %} {% trans "History" %}</a></li>
|
||||
|
|
Ładowanie…
Reference in New Issue