changeset 9707d2e0f89d in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset;node=9707d2e0f89d
description:
Allow overriding digits in Lang.currency and Report.format_currency
issue9588
review292221002
diffstat:
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(-)
diffs (93 lines):
diff -r 8da01f3b37a5 -r 9707d2e0f89d CHANGELOG
--- a/CHANGELOG Thu Sep 24 18:38:41 2020 +0200
+++ b/CHANGELOG Thu Sep 24 22:49:09 2020 +0200
@@ -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 -r 8da01f3b37a5 -r 9707d2e0f89d trytond/ir/lang.py
--- a/trytond/ir/lang.py Thu Sep 24 18:38:41 2020 +0200
+++ b/trytond/ir/lang.py Thu Sep 24 22:49:09 2020 +0200
@@ -459,14 +459,16 @@
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 -r 8da01f3b37a5 -r 9707d2e0f89d trytond/report/report.py
--- a/trytond/report/report.py Thu Sep 24 18:38:41 2020 +0200
+++ b/trytond/report/report.py Thu Sep 24 22:49:09 2020 +0200
@@ -383,13 +383,14 @@
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 -r 8da01f3b37a5 -r 9707d2e0f89d trytond/tests/test_ir.py
--- a/trytond/tests/test_ir.py Thu Sep 24 18:38:41 2020 +0200
+++ b/trytond/tests/test_ir.py Thu Sep 24 22:49:09 2020 +0200
@@ -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
@@ -51,6 +53,24 @@
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"
pool = Pool()