mirror of
https://github.com/OCA/manufacture.git
synced 2025-01-28 16:37:15 +02:00
[IMP] mrp_tag: add parent tags
This commit is contained in:
@@ -4,19 +4,54 @@
|
||||
|
||||
from random import randint
|
||||
|
||||
from odoo import fields, models
|
||||
from odoo import _, api, fields, models
|
||||
from odoo.exceptions import ValidationError
|
||||
|
||||
|
||||
class MrpTag(models.Model):
|
||||
_name = "mrp.tag"
|
||||
_description = "MRP Tag"
|
||||
_parent_store = True
|
||||
|
||||
def _get_default_color(self):
|
||||
return randint(1, 11)
|
||||
|
||||
name = fields.Char("Tag Name", required=True, translate=True)
|
||||
color = fields.Integer("Tag Color", default=_get_default_color)
|
||||
color = fields.Integer(default=lambda self: self._get_default_color())
|
||||
parent_id = fields.Many2one("purchase.tag", index=True, ondelete="cascade")
|
||||
child_ids = fields.One2many("purchase.tag", "parent_id")
|
||||
parent_path = fields.Char(index=True)
|
||||
|
||||
_sql_constraints = [
|
||||
("tag_name_uniq", "unique (name)", "Tag name already exists !"),
|
||||
]
|
||||
|
||||
def name_get(self):
|
||||
res = []
|
||||
for tag in self:
|
||||
names = []
|
||||
current = tag
|
||||
while current:
|
||||
names.append(current.name)
|
||||
current = current.parent_id
|
||||
res.append((tag.id, " / ".join(reversed(names))))
|
||||
return res
|
||||
|
||||
@api.model
|
||||
def _name_search(
|
||||
self, name="", args=None, operator="ilike", limit=100, name_get_uid=None
|
||||
):
|
||||
if name:
|
||||
args = [("name", operator, name.split(" / ")[-1])] + list(args or [])
|
||||
return super()._name_search(
|
||||
name=name,
|
||||
args=args,
|
||||
operator=operator,
|
||||
limit=limit,
|
||||
name_get_uid=name_get_uid,
|
||||
)
|
||||
|
||||
@api.constrains("parent_id")
|
||||
def _check_parent_recursion(self):
|
||||
if not self._check_recursion("parent_id"):
|
||||
raise ValidationError(_("Tags cannot be recursive."))
|
||||
|
||||
Reference in New Issue
Block a user