feat: mini pretty dates (#31331)
Narrow format pretty dates for list views, works the same way as client side function.
This commit is contained in:
parent
6113452b73
commit
17d6d81fb5
2 changed files with 9 additions and 3 deletions
|
|
@ -685,7 +685,6 @@ class TestDateUtils(IntegrationTestCase):
|
|||
def test_pretty_date(self):
|
||||
from frappe import _
|
||||
|
||||
# differnt cases
|
||||
now = get_datetime()
|
||||
|
||||
test_cases = {
|
||||
|
|
@ -707,6 +706,8 @@ class TestDateUtils(IntegrationTestCase):
|
|||
for dt, exp_message in test_cases.items():
|
||||
self.assertEqual(pretty_date(dt), exp_message)
|
||||
|
||||
self.assertEqual(pretty_date(add_to_date(now, days=-5), mini=True), "5d")
|
||||
|
||||
def test_date_from_timegrain(self):
|
||||
start_date = getdate("2021-01-01")
|
||||
|
||||
|
|
|
|||
|
|
@ -1692,7 +1692,7 @@ def escape_html(text: str) -> str:
|
|||
return "".join(html_escape_table.get(c, c) for c in text)
|
||||
|
||||
|
||||
def pretty_date(iso_datetime: datetime.datetime | str) -> str:
|
||||
def pretty_date(iso_datetime: datetime.datetime | str, mini=False) -> str:
|
||||
"""Return a localized string representation of the delta to the current system time.
|
||||
|
||||
For example, "1 hour ago", "2 days ago", "in 5 seconds", etc.
|
||||
|
|
@ -1706,7 +1706,12 @@ def pretty_date(iso_datetime: datetime.datetime | str) -> str:
|
|||
iso_datetime = get_datetime(iso_datetime)
|
||||
now_dt = now_datetime()
|
||||
locale = frappe.local.lang.replace("-", "_") if frappe.local.lang else None
|
||||
return format_timedelta(iso_datetime - now_dt, add_direction=True, locale=locale)
|
||||
return format_timedelta(
|
||||
iso_datetime - now_dt,
|
||||
add_direction=not mini,
|
||||
locale=locale,
|
||||
format="long" if not mini else "narrow",
|
||||
)
|
||||
|
||||
|
||||
def comma_or(some_list: list | tuple, add_quotes=True) -> str:
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue