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

[16.0][IMP] product_template_tag: add parent tags #1505

Merged
Merged
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
37 changes: 36 additions & 1 deletion product_template_tags/models/product_template_tag.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -27,6 +29,9 @@
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 = [
(
Expand All @@ -50,3 +55,33 @@
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 = []

Check warning on line 60 in product_template_tags/models/product_template_tag.py

View check run for this annotation

Codecov / codecov/patch

product_template_tags/models/product_template_tag.py#L60

Added line #L60 was not covered by tests
for tag in self:
names = []
current = tag

Check warning on line 63 in product_template_tags/models/product_template_tag.py

View check run for this annotation

Codecov / codecov/patch

product_template_tags/models/product_template_tag.py#L62-L63

Added lines #L62 - L63 were not covered by tests
while current:
names.append(current.name)
current = current.parent_id
res.append((tag.id, " / ".join(reversed(names))))
return res

Check warning on line 68 in product_template_tags/models/product_template_tag.py

View check run for this annotation

Codecov / codecov/patch

product_template_tags/models/product_template_tag.py#L65-L68

Added lines #L65 - L68 were not covered by tests

@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(

Check warning on line 76 in product_template_tags/models/product_template_tag.py

View check run for this annotation

Codecov / codecov/patch

product_template_tags/models/product_template_tag.py#L75-L76

Added lines #L75 - L76 were not covered by tests
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."))

Check warning on line 87 in product_template_tags/models/product_template_tag.py

View check run for this annotation

Codecov / codecov/patch

product_template_tags/models/product_template_tag.py#L87

Added line #L87 was not covered by tests
2 changes: 2 additions & 0 deletions product_template_tags/views/product_template_tag.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<group>
<field name="sequence" groups="base.group_no_one" />
<field name="color" widget="color_picker" />
<field name="parent_id" />
</group>
<group>
<field
Expand Down Expand Up @@ -80,6 +81,7 @@
widget="selection"
groups="base.group_multi_company"
/>
<field name="parent_id" optional="hide" />
<field name="color" widget="color_picker" />
</tree>
</field>
Expand Down
Loading