From 9afb36b94e0fa2b467b0740367b030d6fb3191d0 Mon Sep 17 00:00:00 2001 From: Denis Ledoux Date: Wed, 2 Nov 2022 17:02:39 +0000 Subject: [PATCH] [FIX] base: res.currency, correct labels for company rate fields As `_get_view` is cached, and `_get_view` has been overrided to change the label of the fields `company_rate` and `inverse_company_rate` according to the company currency, the company currency must be added in the `_get_view` cache key, so it returns the view with the correct labels when you use two companies with distinct currencies, and not the label using the currency of the other company. closes odoo/odoo#105301 X-original-commit: b106361d60413940622b477f73cfdf1536f2ef38 Signed-off-by: Denis Ledoux (dle) --- odoo/addons/base/models/res_currency.py | 14 +++++++++++++ odoo/addons/base/tests/__init__.py | 1 + odoo/addons/base/tests/test_res_currency.py | 23 +++++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 odoo/addons/base/tests/test_res_currency.py diff --git a/odoo/addons/base/models/res_currency.py b/odoo/addons/base/models/res_currency.py index 48da5fb9af8..53544a15f52 100644 --- a/odoo/addons/base/models/res_currency.py +++ b/odoo/addons/base/models/res_currency.py @@ -284,6 +284,13 @@ class Currency(models.Model): JOIN res_company c ON (r.company_id is null or r.company_id = c.id) """ + @api.model + def _get_view_cache_key(self, view_id=None, view_type='form', **options): + """The override of _get_view changing the rate field labels according to the company currency + makes the view cache dependent on the company currency""" + key = super()._get_view_cache_key(view_id, view_type, **options) + return key + ((self.env['res.company'].browse(self._context.get('company_id')) or self.env.company).currency_id.name,) + @api.model def _get_view(self, view_id=None, view_type='form', **options): arch, view = super()._get_view(view_id, view_type, **options) @@ -417,6 +424,13 @@ class CurrencyRate(models.Model): def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None): return super()._name_search(parse_date(self.env, name), args, operator, limit, name_get_uid) + @api.model + def _get_view_cache_key(self, view_id=None, view_type='form', **options): + """The override of _get_view changing the rate field labels according to the company currency + makes the view cache dependent on the company currency""" + key = super()._get_view_cache_key(view_id, view_type, **options) + return key + ((self.env['res.company'].browse(self._context.get('company_id')) or self.env.company).currency_id.name,) + @api.model def _get_view(self, view_id=None, view_type='form', **options): arch, view = super()._get_view(view_id, view_type, **options) diff --git a/odoo/addons/base/tests/__init__.py b/odoo/addons/base/tests/__init__.py index 5585e8fb400..9cb7a758c57 100644 --- a/odoo/addons/base/tests/__init__.py +++ b/odoo/addons/base/tests/__init__.py @@ -44,6 +44,7 @@ from . import test_user_has_group from . import test_views from . import test_xmlrpc from . import test_res_company +from . import test_res_currency from . import test_res_partner from . import test_res_partner_bank from . import test_res_users diff --git a/odoo/addons/base/tests/test_res_currency.py b/odoo/addons/base/tests/test_res_currency.py new file mode 100644 index 00000000000..6a4fda7b8c3 --- /dev/null +++ b/odoo/addons/base/tests/test_res_currency.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. +from lxml import etree +from odoo.tests.common import TransactionCase + + +class TestResConfig(TransactionCase): + def test_view_company_rate_label(self): + """Tests the label of the company_rate and inverse_company_rate fields + are well set according to the company currency in the currency form view and the currency rate list view. + e.g. in the currency rate list view of a company using EUR, the company_rate label must be `Unit per EUR`""" + company_foo, company_bar = self.env['res.company'].create([ + {'name': 'foo', 'currency_id': self.env.ref('base.EUR').id}, + {'name': 'bar', 'currency_id': self.env.ref('base.USD').id}, + ]) + for company, expected_currency in [(company_foo, 'EUR'), (company_bar, 'USD')]: + for model, view_type in [('res.currency', 'form'), ('res.currency.rate', 'tree')]: + arch = self.env[model].with_company(company).get_view(view_type=view_type)['arch'] + tree = etree.fromstring(arch) + node_company_rate = tree.xpath('//field[@name="company_rate"]')[0] + node_inverse_company_rate = tree.xpath('//field[@name="inverse_company_rate"]')[0] + self.assertEqual(node_company_rate.get('string'), f'Unit per {expected_currency}') + self.assertEqual(node_inverse_company_rate.get('string'), f'{expected_currency} per Unit')