From 05b53fa6c9e272632fed76b739cbc6b4f207a306 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Thu, 24 Sep 2020 22:49:09 +0200 Subject: [PATCH] Allow overriding digits in Lang.currency and Report.format_currency issue9588 review292221002 --- CHANGELOG | 1 + trytond/ir/lang.py | 6 ++++-- trytond/report/report.py | 7 ++++--- trytond/tests/test_ir.py | 24 ++++++++++++++++++++++-- 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 315d0c22c2..e4dbc52522 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Allow overriding digits in Lang.currency and Report.format_currency * Add record name in report filename * Add deletable and writable state from ir.rule to read * Add language attribute to data tag diff --git a/trytond/ir/lang.py b/trytond/ir/lang.py index 1d564a41c9..3c97e16491 100644 --- a/trytond/ir/lang.py +++ b/trytond/ir/lang.py @@ -459,14 +459,16 @@ class Lang(DeactivableMixin, ModelSQL, ModelView): formatted = _strip_padding(formatted, seps) return formatted - def currency(self, val, currency, symbol=True, grouping=False): + def currency( + self, val, currency, symbol=True, grouping=False, digits=None): """ Formats val according to the currency settings in lang. """ # Code from currency in locale.py # check for illegal values - digits = currency.digits + if digits is None: + digits = currency.digits if digits == 127: raise ValueError("Currency formatting is not possible using " "the 'C' locale.") diff --git a/trytond/report/report.py b/trytond/report/report.py index a0a47b1be2..bbd71094df 100644 --- a/trytond/report/report.py +++ b/trytond/report/report.py @@ -383,13 +383,14 @@ class Report(URLMixin, PoolBase): return text @classmethod - def format_currency(cls, value, lang, currency, symbol=True, - grouping=True): + def format_currency( + cls, value, lang, currency, symbol=True, grouping=True, + digits=None): pool = Pool() Lang = pool.get('ir.lang') if lang is None: lang = Lang.get() - return lang.currency(value, currency, symbol, grouping) + return lang.currency(value, currency, symbol, grouping, digits=digits) @classmethod def format_number(cls, value, lang, digits=2, grouping=True, diff --git a/trytond/tests/test_ir.py b/trytond/tests/test_ir.py index 6e7a50406b..0617de8255 100644 --- a/trytond/tests/test_ir.py +++ b/trytond/tests/test_ir.py @@ -1,9 +1,11 @@ # This file is part of Tryton. The COPYRIGHT file at the top level of # this repository contains the full copyright notices and license terms. -from dateutil.relativedelta import relativedelta import datetime import unittest -from unittest.mock import patch, ANY +from decimal import Decimal +from unittest.mock import Mock, patch, ANY + +from dateutil.relativedelta import relativedelta from trytond.config import config from trytond.pool import Pool @@ -50,6 +52,24 @@ class IrTestCase(ModuleTestCase): Model = pool.get('ir.model') Model.global_search('User', 10) + @with_transaction() + def test_lang_currency(self): + "Test Lang.currency" + pool = Pool() + Lang = pool.get('ir.lang') + lang = Lang.get('en') + currency = Mock() + currency.digits = 2 + currency.symbol = '$' + test_data = [ + (Decimal('10.50'), True, False, None, '$10.50'), + (Decimal('10.50'), True, False, 4, '$10.5000'), + ] + for value, symbol, grouping, digits, result in test_data: + self.assertEqual( + lang.currency(value, currency, symbol, grouping, digits), + result) + @with_transaction() def test_lang_format(self): "Test Lang.format"