[FIX] base: duration widget for unsupported babel format

In case of exception of type KeyError during the formatting with Babel
instead to raise a Traceback, we first retry with the en_US locale.

Babel fixes each week new bug of formatting like the one in the test,
but we cannot bump our default babel version since it is not in the
stable Ubuntu 22 so it is a best effort fix that will not hide all bugs
but is better that nothing.

https://github.com/python-babel/babel/pull/827

closes odoo/odoo#155909

Signed-off-by: Martin Trigaux (mat) <mat@odoo.com>
This commit is contained in:
Jeremy Kersten
2024-02-29 17:43:07 +00:00
parent dd50481130
commit 7cc2436405
2 changed files with 31 additions and 7 deletions
+20 -7
View File
@@ -639,13 +639,26 @@ class DurationConverter(models.AbstractModel):
v, r = divmod(r, secs_per_unit)
if not v:
continue
section = babel.dates.format_timedelta(
v*secs_per_unit,
granularity=round_to,
add_direction=options.get('add_direction'),
format=options.get('format', 'long'),
threshold=1,
locale=locale)
try:
section = babel.dates.format_timedelta(
v*secs_per_unit,
granularity=round_to,
add_direction=options.get('add_direction'),
format=options.get('format', 'long'),
threshold=1,
locale=locale)
except KeyError:
# in case of wrong implementation of babel, try to fallback on en_US locale.
# https://github.com/python-babel/babel/pull/827/files
# Some bugs already fixed in 2.10 but ubuntu22 is 2.8
localeUS = babel_locale_parse('en_US')
section = babel.dates.format_timedelta(
v*secs_per_unit,
granularity=round_to,
add_direction=options.get('add_direction'),
format=options.get('format', 'long'),
threshold=1,
locale=localeUS)
if section:
sections.append(section)
+11
View File
@@ -1659,6 +1659,17 @@ class TestQWebBasic(TransactionCase):
})
self.env['ir.qweb']._render(view1.id, {'user': u}) # should not crash
def test_render_widget_duration_fallback(self):
self.env['res.lang'].with_context(active_test=False).search([('code', '=', 'pt_BR')]).active = True
view1 = self.env['ir.ui.view'].create({
'name': "dummy",
'type': 'qweb',
'arch': """
<t t-name="base.dummy"><root><span t-esc="3600" t-options='{"widget": "duration", "format": "short"}' /></root></t>
"""
})
self.env['ir.qweb'].with_context(lang='pt_BR')._render(view1.id, {}) # should not crash
def test_void_element(self):
view = self.env['ir.ui.view'].create({
'name': 'master',