From 8d2eb9d869a5bbaa847799dfad5ed2a021c031df Mon Sep 17 00:00:00 2001 From: "John Laterre (jol)" Date: Mon, 19 Sep 2022 13:02:03 +0000 Subject: [PATCH] [FIX] base: prevent tracebacks on rate creation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since this commit, https://github.com/odoo/odoo/commit/32ada4a415a14052d780f351edb82a0011561451, when adding a new rate from the tree view, if the `name` field is emptied (despite having a default value), the `_onchange_rate_warning()` method will generate a traceback because it will call `_get_latest_rate()` that relies on the `name` field to order the company rates. Steps to reproduce: - Go to the Accounting App. - Click on Configuration > Currencies. - Select any currency and click on "Add a line" in the Rates tab. - Remove the default value from the "Date" field and leave it empty. - Now try to change "Unit per USD". A traceback should be triggered. New behavior: With this fix, an error message will now invite the user to set the `name` (a.k.a. 'Date') correctly. closes odoo/odoo#100874 X-original-commit: ddced61ca27f96b89af32f0b99e225321f966681 Signed-off-by: William André (wan) Signed-off-by: John Laterre (jol) --- odoo/addons/base/models/res_currency.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/odoo/addons/base/models/res_currency.py b/odoo/addons/base/models/res_currency.py index cfa39d13656..ef9ffb58cd6 100644 --- a/odoo/addons/base/models/res_currency.py +++ b/odoo/addons/base/models/res_currency.py @@ -3,11 +3,11 @@ import logging import math -import re -import time from lxml import etree + from odoo import api, fields, models, tools, _ +from odoo.exceptions import UserError from odoo.tools import parse_date _logger = logging.getLogger(__name__) @@ -349,6 +349,9 @@ class CurrencyRate(models.Model): return super().create([self._sanitize_vals(vals) for vals in vals_list]) def _get_latest_rate(self): + # Make sure 'name' is defined when creating a new rate. + if not self.name: + raise UserError(_("The date for the current rate is empty.\nPlease set it.")) return self.currency_id.rate_ids.sudo().filtered(lambda x: ( x.rate and x.company_id == (self.company_id or self.env.company)