[Fix] server env tech name unique constraint

This commit is contained in:
Telmo Santos 2022-03-25 13:46:42 +01:00
parent ad9bf443da
commit d2b3b88c82
1 changed files with 15 additions and 4 deletions

View File

@ -2,7 +2,8 @@
# @author Simone Orsi <simahawk@gmail.com>
# 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
from odoo.addons.http_routing.models.ir_http import slugify
@ -28,9 +29,6 @@ class ServerEnvTechNameMixin(models.AbstractModel):
_name = "server.env.techname.mixin"
_description = "Server environment technical name"
_sql_constraints = [
("tech_name_uniq", "unique(tech_name)", "`tech_name` must be unique!",)
]
# TODO: could leverage the new option for computable / writable fields
# and get rid of some onchange / read / write code.
tech_name = fields.Char(
@ -38,7 +36,20 @@ class ServerEnvTechNameMixin(models.AbstractModel):
)
_server_env_section_name_field = "tech_name"
_tech_name_unique_per_company = False # off by default
@api.constrains("tech_name", "company_id")
def _check_tech_name(self):
company_dependent = self._tech_name_unique_per_company and "company_id" in self._fields
for record in self:
domain = [('id', '!=', record.id), ('tech_name', '=' , record.tech_name)]
error_msg = _("Tech name %s is duplicated. Tech name must be unique") % record.tech_name
if company_dependent:
domain.append(('company_id', '=', record.company_id.id))
error_msg += _(" per company")
if self.search_count(domain):
raise ValidationError(error_msg)
@api.onchange("name")
def _onchange_name_for_tech(self):
# Keep this specific name for the method to avoid possible overrides