From e246bf1448fa56522d372449c76255cb591a0354 Mon Sep 17 00:00:00 2001 From: MAHAMADASIF ANSARI Date: Tue, 5 Apr 2022 10:29:47 +0000 Subject: [PATCH] [IMP] base: allow to specify decimal points with `format_decimalized_number` Currently, we can format the number into decimal points with help of the `format_decimalized_number` option of `ir.qweb.field.integer` class. However, sometimes we need to decide how many decimal points we need to display, which is not possible with current implementation. This commit improves the above option and now allows to pass the number of decimal points, with a new option `precision_digits`, which defaults to one decimal point. task-2792149 Part-of: odoo/odoo#87996 --- odoo/addons/base/models/ir_qweb_fields.py | 3 ++- odoo/addons/base/tests/test_qweb_field.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/odoo/addons/base/models/ir_qweb_fields.py b/odoo/addons/base/models/ir_qweb_fields.py index 5bf43e581ad..cdc8fd1cc78 100644 --- a/odoo/addons/base/models/ir_qweb_fields.py +++ b/odoo/addons/base/models/ir_qweb_fields.py @@ -142,13 +142,14 @@ class IntegerConverter(models.AbstractModel): options = super(IntegerConverter, self).get_available_options() options.update( format_decimalized_number=dict(type='boolean', string=_('Decimalized number')), + precision_digits=dict(type='integer', string=_('Precision Digits')), ) return options @api.model def value_to_html(self, value, options): if options.get('format_decimalized_number'): - return tools.format_decimalized_number(value) + return tools.format_decimalized_number(value, options.get('precision_digits', 1)) return pycompat.to_text(self.user_lang().format('%d', value, grouping=True).replace(r'-', '-\N{ZERO WIDTH NO-BREAK SPACE}')) diff --git a/odoo/addons/base/tests/test_qweb_field.py b/odoo/addons/base/tests/test_qweb_field.py index 2320c474c56..4fb7823dcff 100644 --- a/odoo/addons/base/tests/test_qweb_field.py +++ b/odoo/addons/base/tests/test_qweb_field.py @@ -42,3 +42,17 @@ class TestQwebFieldTime(common.TransactionCase): # Only values inferior to 24 can be used with self.assertRaises(ValueError): self.value_to_html(24) + + +class TestQwebFieldInteger(common.TransactionCase): + def value_to_html(self, value, options=None): + options = options or {} + return self.env['ir.qweb.field.integer'].value_to_html(value, options) + + def test_integer_value_to_html(self): + self.assertEqual(self.value_to_html(1000), "1,000") + self.assertEqual(self.value_to_html(1000000, {'format_decimalized_number': True}), "1M") + self.assertEqual( + self.value_to_html(125125, {'format_decimalized_number': True, 'precision_digits': 3}), + "125.125k" + )