Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[13.0] Improvements on server.env.techname.mixin #75

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 47 additions & 35 deletions server_environment/models/server_env_tech_name_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -16,56 +17,67 @@ class ServerEnvTechNameMixin(models.AbstractModel):
This mixin helps solve the problem by providing a tech name field
and a cleanup machinery as well as a unique constrain.

To use this mixin add it to the _inherit attr of your module like:

_inherit = [
"my.model",
"server.env.techname.mixin",
"server.env.mixin",
]

Use it in place of "server.env.mixin".
"""

_name = "server.env.techname.mixin"
_inherit = "server.env.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(
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"

@api.onchange("name")
def _onchange_name_for_tech(self):
# Keep this specific name for the method to avoid possible overrides
# of existing `_onchange_name` methods
if self.name and not self.tech_name:
self.tech_name = self.name
@api.depends("name")
def _compute_tech_name(self):
for rec in self:
# Update tech_name only if it hasn't been set or if we're
# dealing with a new record.
if not rec.tech_name or not rec._origin.id:
rec.tech_name = self._normalize_tech_name(rec.name)

@api.onchange("tech_name")
def _onchange_tech_name(self):
if self.tech_name:
# make sure is normalized
self.tech_name = self._normalize_tech_name(self.tech_name)

@api.model
def create(self, vals):
self._handle_tech_name(vals)
return super().create(vals)

def write(self, vals):
self._handle_tech_name(vals)
return super().write(vals)

def _handle_tech_name(self, vals):
# 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"])
# make sure it's normalized
res = {}
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.constrains("tech_name")
def _check_tech_name(self):
for rec in self.filtered("tech_name"):
if rec.tech_name != self._normalize_tech_name(rec.tech_name):
raise ValidationError(
_(
"Environment Technical Name '%s' can't "
"contain special characters."
)
% rec.tech_name
)

@staticmethod
def _normalize_tech_name(name):
if not name:
return name
return slugify(name).replace("-", "_")
1 change: 1 addition & 0 deletions server_environment/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
* Thomas Binfeld <thomas.binsfeld@acsone.eu>
* Stéphane Bidoul <stefane.bidoul@acsone.com>
* Simone Orsi <simahawk@gmail.com>
* Iván Todorovich <ivan.todorovich@gmail.com>