Skip to content
Open
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
35 changes: 34 additions & 1 deletion document_page_tag/models/document_page_tag.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,53 @@
# Copyright 2015-2018 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo import api, fields, models
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError


class DocumentPageTag(models.Model):
_name = "document.page.tag"
_description = "A keyword for document pages"
_parent_store = True

name = fields.Char(required=True, translate=True)
color = fields.Integer(string="Color Index")
active = fields.Boolean(default=True)
parent_id = fields.Many2one(
"document.page.tag", string="Parent Tag", index=True, ondelete="restrict"
)
child_ids = fields.One2many("document.page.tag", "parent_id", string="Child Tags")
parent_path = fields.Char(index=True)

_sql_constraints = [
("unique_name", "unique(name)", "Tags must be unique"),
]

@api.constrains("parent_id")
def _check_parent_id(self):
if self._has_cycle(): # pragma: no cover
raise ValidationError(_("You can not create recursive tags."))

def _get_hierarchy_name(self):
self.ensure_one()
names = []
tag = self
while tag:
names.append(tag.name or "")
tag = tag.parent_id
return " / ".join(reversed(names))

@api.depends("parent_id")
def _compute_display_name(self):
for tag in self:
tag.display_name = tag._get_hierarchy_name()

@api.model
def _search_display_name(self, operator, value):
domain = super()._search_display_name(operator, value)
if operator.endswith("like"):
return [("id", "child_of", self._search(list(domain)))]
return domain

@api.model_create_multi
def create(self, vals_list):
"""Be nice when trying to create duplicates"""
Expand Down
34 changes: 34 additions & 0 deletions document_page_tag/tests/test_document_page_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from psycopg2 import IntegrityError

from odoo.exceptions import UserError
from odoo.tests.common import TransactionCase
from odoo.tools.misc import mute_logger

Expand All @@ -20,3 +21,36 @@ def test_document_page_tag(self):
testtag2 = self.env["document.page.tag"].create({"name": "test2"})
testtag2.write({"name": "test"})
testtag2.flush_model()

def test_document_page_tag_recursion(self):
tag_a = self.env["document.page.tag"].create({"name": "tag a"})
tag_b = self.env["document.page.tag"].create(
{"name": "tag b", "parent_id": tag_a.id}
)
# native anti-recursion check from _parent_store
with self.assertRaises(UserError):
tag_a.parent_id = tag_b.id

def test_document_page_tag_display_name_hierarchy(self):
tag_a = self.env["document.page.tag"].create({"name": "tag a"})
tag_b = self.env["document.page.tag"].create(
{"name": "tag b", "parent_id": tag_a.id}
)
self.assertEqual(tag_b.display_name, "tag a / tag b")

def test_document_page_tag_search_display_name(self):
tag_a = self.env["document.page.tag"].create({"name": "tag a"})
tag_b = self.env["document.page.tag"].create(
{"name": "tag b", "parent_id": tag_a.id}
)
found = self.env["document.page.tag"].search(
[("display_name", "ilike", "tag a")]
)
self.assertIn(tag_a, found)
self.assertIn(tag_b, found)

def test_document_page_tag_search_display_name_exact(self):
# covers the non-"like" operator branch (no child_of expansion)
tag_a = self.env["document.page.tag"].create({"name": "tag a"})
found = self.env["document.page.tag"].search([("display_name", "=", "tag a")])
self.assertEqual(found, tag_a)
11 changes: 10 additions & 1 deletion document_page_tag/views/document_page_tag.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
/>
<separator />
<field name="name" filter_domain="[('name', 'ilike', self)]" />
<group string="Group By">
<filter
string="Parent Tag"
name="group_parent_id"
context="{'group_by': 'parent_id'}"
/>
</group>
</search>
</field>
</record>
Expand All @@ -38,6 +45,7 @@
</h1>
</div>
<group name="main">
<field name="parent_id" domain="[('id', '!=', id)]" />
<field name="color" widget="color_picker" />
</group>
</sheet>
Expand All @@ -49,8 +57,9 @@
<field name="name">document.page.tag.tree</field>
<field name="model">document.page.tag</field>
<field name="arch" type="xml">
<list>
<list multi_edit="1">
<field name="name" />
<field name="parent_id" domain="[('id', '!=', id)]" />
<field name="color" widget="color_picker" string="Color" />
</list>
</field>
Expand Down
Loading