From e2716c1d62427e85b30518fe17a1e74681a8dd7f Mon Sep 17 00:00:00 2001 From: GuillemCForgeFlow Date: Mon, 2 Oct 2023 16:26:33 +0200 Subject: [PATCH] [IMP]product_harmonized_system: various improvements - extract `name_get` logic in another method to be inherited - remove _sql_constraints and change it for an api constrains method to have a better control and so that there no global duplicates (those that are not company specific) - replaced `replace` method for strip() --- product_harmonized_system/models/hs_code.py | 40 +++++++++++++-------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/product_harmonized_system/models/hs_code.py b/product_harmonized_system/models/hs_code.py index 3864d47a..c4cab181 100644 --- a/product_harmonized_system/models/hs_code.py +++ b/product_harmonized_system/models/hs_code.py @@ -4,7 +4,8 @@ # @author Luc de Meyer # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from odoo import api, fields, models +from odoo import _, api, fields, models +from odoo.exceptions import ValidationError class HSCode(models.Model): @@ -74,32 +75,41 @@ def _compute_product_tmpl_count(self): for code in self: code.product_tmpl_count = len(code.product_tmpl_ids) - @api.depends("local_code", "description") + def _get_name(self): + self.ensure_one() + name = self.local_code + if self.description: + name += " " + self.description + return len(name) > 55 and name[:55] + "..." or name + def name_get(self): res = [] for this in self: - name = this.local_code - if this.description: - name += " " + this.description - name = len(name) > 55 and name[:55] + "..." or name + name = this._get_name() res.append((this.id, name)) return res - _sql_constraints = [ - ( - "local_code_company_uniq", - "unique(local_code, company_id)", - "This code already exists for this company !", - ) - ] + @api.constrains("local_code", "company_id") + def _check_duplicate_hs_code(self): + for rec in self: + domain = [("local_code", "=", rec.local_code), ("id", "!=", rec.id)] + if rec.company_id: + domain += [("company_id", "=", rec.company_id.id)] + dup = self.search_count(domain) + if dup: + msg = ( + "There is already an existing H.S. Code with the specified Code: %s." + % rec.local_code + ) + raise ValidationError(_(msg)) @api.model def create(self, vals): if vals.get("local_code"): - vals["local_code"] = vals["local_code"].replace(" ", "") + vals["local_code"] = vals["local_code"].strip() return super().create(vals) def write(self, vals): if vals.get("local_code"): - vals["local_code"] = vals["local_code"].replace(" ", "") + vals["local_code"] = vals["local_code"].strip() return super().write(vals)