diff --git a/mrp_tag/models/mrp_tag.py b/mrp_tag/models/mrp_tag.py index 8c38a4cef..4cfff0173 100644 --- a/mrp_tag/models/mrp_tag.py +++ b/mrp_tag/models/mrp_tag.py @@ -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("mrp.tag", index=True, ondelete="cascade") + child_ids = fields.One2many("mrp.tag", "parent_id") + parent_path = fields.Char(index=True, unaccent=False) _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.")) diff --git a/mrp_tag/views/mrp_production_view.xml b/mrp_tag/views/mrp_production_view.xml index 8c9920534..34f53678d 100644 --- a/mrp_tag/views/mrp_production_view.xml +++ b/mrp_tag/views/mrp_production_view.xml @@ -37,5 +37,19 @@ + + mrp.production.search + mrp.production + + + + + + + diff --git a/mrp_tag/views/mrp_tag_view.xml b/mrp_tag/views/mrp_tag_view.xml index 5be1243b3..03a73f5bd 100644 --- a/mrp_tag/views/mrp_tag_view.xml +++ b/mrp_tag/views/mrp_tag_view.xml @@ -19,7 +19,8 @@ - + + @@ -31,8 +32,9 @@ mrp.tag.view.tree mrp.tag - + +