Merge PR #1195 into 16.0

Signed-off-by gurneyalex
This commit is contained in:
OCA-git-bot
2024-02-02 07:33:13 +00:00
3 changed files with 55 additions and 4 deletions

View File

@@ -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."))

View File

@@ -37,5 +37,19 @@
</field>
</record>
<record id="view_mrp_production_filter" model="ir.ui.view">
<field name="name">mrp.production.search</field>
<field name="model">mrp.production</field>
<field name="inherit_id" ref="mrp.view_mrp_production_filter" />
<field name="arch" type="xml">
<xpath expr="/search/field[@name='origin']" position="after">
<field
name="tag_ids"
string="Tag"
filter_domain="[('tag_ids', 'child_of', self)]"
/>
</xpath>
</field>
</record>
</odoo>

View File

@@ -19,7 +19,8 @@
</div>
<group>
<group>
<field name="color" required="True" />
<field name="color" required="True" widget="color_picker" />
<field name="parent_id" />
</group>
</group>
</sheet>
@@ -31,8 +32,9 @@
<field name="name">mrp.tag.view.tree</field>
<field name="model">mrp.tag</field>
<field name="arch" type="xml">
<tree name="Tags" editable="bottom">
<tree name="Tags">
<field name="name" />
<field name="parent_id" optional="hide" />
<field name="color" widget="color_picker" />
</tree>
</field>