[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) <dle@odoo.com>
This commit is contained in:
Denis Ledoux
2022-11-21 11:49:59 +01:00
parent 5c4e180960
commit 9afb36b94e
3 changed files with 38 additions and 0 deletions
+14
View File
@@ -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)
+1
View File
@@ -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
@@ -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')