Skip to content

Commit

Permalink
[FIX] base: duration widget for unsupported babel format
Browse files Browse the repository at this point in the history
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.

python-babel/babel#827

closes #155978

X-original-commit: 7cc2436
Signed-off-by: Martin Trigaux (mat) <mat@odoo.com>
Signed-off-by: Jérémy Kersten <jke@odoo.com>
  • Loading branch information
JKE-be committed Mar 4, 2024
1 parent 2fdf796 commit c781015
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
27 changes: 20 additions & 7 deletions odoo/addons/base/models/ir_qweb_fields.py
Expand Up @@ -639,13 +639,26 @@ def value_to_html(self, value, options):
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)

Expand Down
11 changes: 11 additions & 0 deletions odoo/addons/base/tests/test_qweb.py
Expand Up @@ -1659,6 +1659,17 @@ def test_render_widget_contact(self):
})
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',
Expand Down

0 comments on commit c781015

Please sign in to comment.