With this commit, list data is loaded using `web_search_read` instead of `search_read`. The goal is to fetch the currency (symbol, decimal places, etc.) of monetary fields in a single request, instead of 2 RPCs. Pros: - less code - one evaluation saved - one network request saved - easier future refactoring (see below) Cons: - overhead of data transferred over network (from 4.5MB to 6.5MB, unzipped and from 711kB to 725kB gzipped to fetch a list of 20K crm leads). Before this commit, here is what it looked like: 1. the list data is fetch (with the currency_field) 2. the cells are evaluated with the new data 3. we realize we want to format a currency amount. We already have the currency name but not the symbol, etc. So we fetch the currency data 4. evaluate the cells again with the new currency format Now: 1. fetch the list data with everything we need for the currency 2. evaluate the cells This commit also serves another goal for a future refactoring: in the hope of avoiding throwing "loading errors", I'd like to have an easy way to know if a data source is fully loaded or not (the data and the format). With this commit, everything is centralized in the list data source with a single RPC. The goal is therefore achieved with this commit. closes odoo/odoo#153434 Task: 3730232 Related: odoo/enterprise#56253 Signed-off-by: Pierre Rousseau (pro) <pro@odoo.com>
56 lines
2.1 KiB
Python
56 lines
2.1 KiB
Python
from odoo import api, models
|
|
|
|
|
|
class ResCurrency(models.Model):
|
|
_inherit = "res.currency"
|
|
|
|
# TODO remove this method in master. It's not used anymore.
|
|
@api.model
|
|
def get_currencies_for_spreadsheet(self, currency_names):
|
|
"""
|
|
Returns the currency structure of provided currency names.
|
|
This function is meant to be called by the spreadsheet js lib,
|
|
hence the formatting of the result.
|
|
|
|
:currency_names list(str): list of currency names (e.g. ["EUR", "USD", "CAD"])
|
|
:return: list of dicts of the form `{ "code": str, "symbol": str, "decimalPlaces": int, "position":str }`
|
|
"""
|
|
currencies = self.with_context(active_test=False).search(
|
|
[("name", "in", currency_names)],
|
|
)
|
|
result = []
|
|
for currency_name in currency_names:
|
|
currency = next(filter(lambda curr: curr.name == currency_name, currencies), None)
|
|
if currency:
|
|
currency_data = {
|
|
"code": currency.name,
|
|
"symbol": currency.symbol,
|
|
"decimalPlaces": currency.decimal_places,
|
|
"position": currency.position,
|
|
}
|
|
else:
|
|
currency_data = None
|
|
result.append(currency_data)
|
|
return result
|
|
|
|
@api.model
|
|
def get_company_currency_for_spreadsheet(self, company_id=None):
|
|
"""
|
|
Returns the currency structure for the currency of the company.
|
|
This function is meant to be called by the spreadsheet js lib,
|
|
hence the formatting of the result.
|
|
|
|
:company_id int: Id of the company
|
|
:return: dict of the form `{ "code": str, "symbol": str, "decimalPlaces": int, "position":str }`
|
|
"""
|
|
company = self.env["res.company"].browse(company_id) if company_id else self.env.company
|
|
if not company.exists():
|
|
return False
|
|
currency = company.currency_id
|
|
return {
|
|
"code": currency.name,
|
|
"symbol": currency.symbol,
|
|
"decimalPlaces": currency.decimal_places,
|
|
"position": currency.position,
|
|
}
|