From 845477e6d2927d12eb98defad7806ae89fc6fdb3 Mon Sep 17 00:00:00 2001 From: SilvioC2C Date: Mon, 29 Jan 2024 17:32:47 +0100 Subject: [PATCH 1/3] [IMP] mrp_tag: add parent tags --- mrp_tag/models/mrp_tag.py | 39 ++++++++++++++++++++++++++++++++-- mrp_tag/views/mrp_tag_view.xml | 6 ++++-- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/mrp_tag/models/mrp_tag.py b/mrp_tag/models/mrp_tag.py index 8c38a4cef..0469cbf48 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("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.")) 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 - + + From b6f6b6caab1b43d98dcde19a633155c131b17745 Mon Sep 17 00:00:00 2001 From: SilvioC2C Date: Mon, 29 Jan 2024 17:33:25 +0100 Subject: [PATCH 2/3] [IMP] mrp_tag: add tags to ``mrp.production`` search view --- mrp_tag/models/mrp_tag.py | 4 ++-- mrp_tag/views/mrp_production_view.xml | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/mrp_tag/models/mrp_tag.py b/mrp_tag/models/mrp_tag.py index 0469cbf48..321e1fb60 100644 --- a/mrp_tag/models/mrp_tag.py +++ b/mrp_tag/models/mrp_tag.py @@ -18,8 +18,8 @@ class MrpTag(models.Model): name = fields.Char("Tag Name", required=True, translate=True) 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_id = fields.Many2one("mrp.tag", index=True, ondelete="cascade") + child_ids = fields.One2many("mrp.tag", "parent_id") parent_path = fields.Char(index=True) _sql_constraints = [ 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 + + + + + + + From 32ccdeb3153edb9ed938a2b8eaf2913737d83005 Mon Sep 17 00:00:00 2001 From: SilvioC2C Date: Tue, 30 Jan 2024 14:28:49 +0100 Subject: [PATCH 3/3] [IMP] mrp_tag: add `unaccent=False` to `parent_path` --- mrp_tag/models/mrp_tag.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mrp_tag/models/mrp_tag.py b/mrp_tag/models/mrp_tag.py index 321e1fb60..4cfff0173 100644 --- a/mrp_tag/models/mrp_tag.py +++ b/mrp_tag/models/mrp_tag.py @@ -20,7 +20,7 @@ class MrpTag(models.Model): 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) + parent_path = fields.Char(index=True, unaccent=False) _sql_constraints = [ ("tag_name_uniq", "unique (name)", "Tag name already exists !"),