From 229785368bc38fe91a78732e99bfe1d4340fd9bf Mon Sep 17 00:00:00 2001 From: SilvioC2C Date: Mon, 29 Jan 2024 16:03:38 +0100 Subject: [PATCH] [IMP] product_template_tag: add parent tags --- .../models/product_template_tag.py | 37 ++++++++++++++++++- .../views/product_template_tag.xml | 2 + 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/product_template_tags/models/product_template_tag.py b/product_template_tags/models/product_template_tag.py index 263378f2e1f2..09f7d8ff004f 100644 --- a/product_template_tags/models/product_template_tag.py +++ b/product_template_tags/models/product_template_tag.py @@ -1,13 +1,15 @@ # Copyright 2017 ACSONE SA/NV # 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 class ProductTemplateTag(models.Model): _name = "product.template.tag" _description = "Product Tag" _order = "sequence, name" + _parent_store = True name = fields.Char(required=True, translate=True) sequence = fields.Integer(default=10) @@ -27,6 +29,9 @@ class ProductTemplateTag(models.Model): string="Company", default=lambda self: self.env.company, ) + parent_id = fields.Many2one("product.template.tag", index=True, ondelete="cascade") + child_ids = fields.One2many("product.template.tag", "parent_id") + parent_path = fields.Char(index=True, unaccent=False) _sql_constraints = [ ( @@ -50,3 +55,33 @@ def _compute_products_count(self): tag_id_product_count = dict(self.env.cr.fetchall()) for rec in self: rec.products_count = tag_id_product_count.get(rec.id, 0) + + 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 can't be recursive.")) diff --git a/product_template_tags/views/product_template_tag.xml b/product_template_tags/views/product_template_tag.xml index 0be40c967e13..db06af6ea1e6 100644 --- a/product_template_tags/views/product_template_tag.xml +++ b/product_template_tags/views/product_template_tag.xml @@ -34,6 +34,7 @@ + +