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

[FIX] partner_contact_department: really order by name #2009

Open
wants to merge 1 commit into
base: 16.0
Choose a base branch
from
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
26 changes: 23 additions & 3 deletions partner_contact_department/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# © 2016 Tecnativa S.L. - Vicent Cubells
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

from odoo import fields, models
from odoo import api, fields, models


class ResPartner(models.Model):
Expand All @@ -13,16 +13,36 @@ class ResPartner(models.Model):

class ResPartnerDepartment(models.Model):
_name = "res.partner.department"
_order = "parent_path, name"
_parent_order = "name"
_order = "display_name"
_parent_store = True
_description = "Department"

name = fields.Char(required=True, translate=True)
display_name = fields.Char(compute="_compute_display_name", store=True, index=True)
parent_id = fields.Many2one(
"res.partner.department", "Parent department", ondelete="restrict"
)
child_ids = fields.One2many(
"res.partner.department", "parent_id", "Child departments"
)
parent_path = fields.Char(index=True, unaccent=False)

@api.depends("parent_path", "name")
def _compute_display_name(self):
return super()._compute_display_name()

def name_get(self):
"""Prepend parent name to department name."""
all_ids = set(
map(int, "/".join(rec.parent_path.strip("/") for rec in self).split("/"))
)
names = {rec.id: rec.name for rec in self.browse(all_ids)}
return [
(
rec.id,
" / ".join(
names[int(id)] for id in rec.parent_path.strip("/").split("/")
),
)
for rec in self
]
14 changes: 14 additions & 0 deletions partner_contact_department/tests/test_recursion.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,17 @@ def test_recursion(self):
# Creating a parent's child department using dpt1.
with self.assertRaises(UserError):
self.dpt1.write(vals={"parent_id": self.dpt3.id})

def test_order(self):
dpt3 = self.env["res.partner.department"].create({"name": "A"})
dpts = self.env["res.partner.department"].search(
[("id", "in", (self.dpt1 | self.dpt2 | dpt3).ids)]
)
self.assertRecordValues(
dpts,
[
{"name": "A", "display_name": "A"},
{"name": "Dpt. 1", "display_name": "Dpt. 1"},
{"name": "Dep. 2", "display_name": "Dpt. 1 / Dep. 2"},
],
)