From 7cc24364058a6924375802e11fe976e12f0cac6a Mon Sep 17 00:00:00 2001 From: Jeremy Kersten Date: Thu, 29 Feb 2024 15:38:56 +0100 Subject: [PATCH] [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) --- odoo/addons/base/models/ir_qweb_fields.py | 27 +++++++++++++++++------ odoo/addons/base/tests/test_qweb.py | 11 +++++++++ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/odoo/addons/base/models/ir_qweb_fields.py b/odoo/addons/base/models/ir_qweb_fields.py index 3efe3dc30bc..a65e4dd6861 100644 --- a/odoo/addons/base/models/ir_qweb_fields.py +++ b/odoo/addons/base/models/ir_qweb_fields.py @@ -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) diff --git a/odoo/addons/base/tests/test_qweb.py b/odoo/addons/base/tests/test_qweb.py index 07fa58091ea..c0adc6b4b94 100644 --- a/odoo/addons/base/tests/test_qweb.py +++ b/odoo/addons/base/tests/test_qweb.py @@ -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': """ + + """ + }) + 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',