[IMP] server.env.techname.mixin: Use computed stored writable fields instead of onchange.

This commit is contained in:
Ivàn Todorovich 2021-07-05 11:41:59 -03:00
parent 154090abb7
commit ac0d12e1ad
1 changed files with 44 additions and 26 deletions

View File

@ -2,7 +2,8 @@
# @author Simone Orsi <simahawk@gmail.com> # @author Simone Orsi <simahawk@gmail.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # 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 from odoo.addons.http_routing.models.ir_http import slugify
@ -25,41 +26,58 @@ class ServerEnvTechNameMixin(models.AbstractModel):
_sql_constraints = [ _sql_constraints = [
("tech_name_uniq", "unique(tech_name)", "`tech_name` must be unique!",) ("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( tech_name = fields.Char(
help="Unique name for technical purposes. Eg: server env keys.", string="Environment Technical Name",
help="Unique name for server environment configuration keys.",
compute="_compute_tech_name",
store=True,
readonly=False,
) )
_server_env_section_name_field = "tech_name" _server_env_section_name_field = "tech_name"
@api.onchange("name") @api.depends("name")
def _onchange_name_for_tech(self): def _compute_tech_name(self):
# Keep this specific name for the method to avoid possible overrides for rec in self:
# of existing `_onchange_name` methods # Update tech_name only if it hasn't been set or if we're
if self.name and not self.tech_name: # dealing with a new record.
self.tech_name = self.name if not rec.tech_name or not rec._origin.id:
rec.tech_name = self._normalize_tech_name(rec.name)
@api.onchange("tech_name") @api.onchange("tech_name")
def _onchange_tech_name(self): def _onchange_tech_name(self):
if self.tech_name: # make sure it's normalized
# make sure is normalized res = {}
self.tech_name = self._normalize_tech_name(self.tech_name) normalized = self._normalize_tech_name(self.tech_name)
if self.tech_name != normalized:
res = {
"warning": {
"title": _("Technical Name"),
"message": _(
"Environment Technical Name '%s' can't "
"contain special characters."
)
% self.tech_name,
}
}
self.tech_name = normalized
return res
@api.model @api.constrains("tech_name")
def create(self, vals): def _check_tech_name(self):
self._handle_tech_name(vals) for rec in self.filtered("tech_name"):
return super().create(vals) if rec.tech_name != self._normalize_tech_name(rec.tech_name):
raise ValidationError(
def write(self, vals): _(
self._handle_tech_name(vals) "Environment Technical Name '%s' can't "
return super().write(vals) "contain special characters."
)
def _handle_tech_name(self, vals): % rec.tech_name
# make sure technical names are always there )
if not vals.get("tech_name") and vals.get("name"):
vals["tech_name"] = self._normalize_tech_name(vals["name"])
@staticmethod @staticmethod
def _normalize_tech_name(name): def _normalize_tech_name(name):
if not name:
return name
return slugify(name).replace("-", "_") return slugify(name).replace("-", "_")