[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:
@@ -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)
|
||||
|
||||
|
||||
@@ -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',
|
||||
|
||||
Reference in New Issue
Block a user