Skip to content
Permalink
Browse files

wip on counts for deep hierarchy

  • Loading branch information
Polymorphe57 committed Mar 25, 2020
1 parent badb2cb commit bedceb0eb68a7071564ec947d8ceb26d995dbec6
Showing with 102 additions and 19 deletions.
  1. +43 −18 addons/web/models/models.py
  2. +59 −1 odoo/addons/test_search_panel/tests/test_search_panel_select_range.py
@@ -9,6 +9,7 @@
from odoo.tools import lazy
from odoo.tools.misc import get_lang
from odoo.exceptions import UserError
from collections import defaultdict


class IrActionsActWindowView(models.Model):
@@ -242,41 +243,65 @@ def search_panel_select_range(self, field_name, **kwargs):

# retrieve all possible values, and return them with their label and counter
if field.type == 'many2one':
Comodel = self.env[field.comodel_name]
parent_name = Comodel._parent_name if Comodel._parent_name in Comodel._fields else False
field_names = ['display_name', parent_name] if parent_name else ['display_name']

records = Comodel.with_context(hierarchical_naming=False).search_read([], field_names)

# make sure the value obtained for a many2one is always a couple
def group_id_name(value):
return value or (False, _("Not Set"))

# get counter
counters = {}
#############################################################################################
# get counters

counters = defaultdict(lambda: 0)
if not disable_counters:
# get local counters
groups = self.read_group(search_domain, [field_name], [field_name])
counters = {
group_id_name(group[field_name])[0]: group[field_name + '_count']
for group in groups
}
for group in groups:
group_id = group_id_name(group[field_name])[0]
counters[group_id] = group[field_name + '_count']

Comodel = self.env[field.comodel_name]
parent_name = Comodel._parent_name if Comodel._parent_name in Comodel._fields else False
field_names = ['display_name', parent_name] if parent_name else ['display_name']

records = Comodel.with_context(hierarchical_naming=False).search_read([], field_names)
if parent_name:
# get global counters
forest = defaultdict(lambda: {
'is_root': True,
'children_ids': []
})
for record in records:
record_id = record['id']
parent_id = group_id_name(record[parent_name])[0]
if parent_id:
forest[parent_id]['children_ids'].append(record_id)
forest[record_id]['is_root'] = False

root_ids = [rec[0] for rec in forest.items() if rec[1]['is_root']]
forest['adjoint_id']['children_ids'] = root_ids

def compute_global_count(id):
counts = sum([compute_global_count(child_id) for child_id in forest[id]['children_ids']])
counters[id] += counts
return counters[id]

compute_global_count('adjoint_id')

#############################################################################################

for record in records:
record_id = record['id']
values = {
'id': record_id,
'display_name': record['display_name'],
'count': counters[record_id]
}
if parent_name:
parent = record[parent_name]
if parent:
parent_id = parent[0]
parent_id = group_id_name(record[parent_name])[0]
if parent_id:
values['parent_id'] = parent_id
counters[parent_id] = counters.get(parent_id, 0) + counters.get(record_id, 0)
category_values.append(values)

for values in category_values:
values['count'] = counters.get(values['id'], 0)

return {
'parent_field': parent_name,
'values': category_values,
@@ -2,7 +2,7 @@
import odoo.tests


@odoo.tests.tagged('post_install', '-at_install')
@odoo.tests.tagged('post_install', '-at_install', 'dam')
class TestSelectRange(odoo.tests.TransactionCase):

def setUp(self):
@@ -113,6 +113,64 @@ def test_many2one(self):
]
)

def test_many2one_deep_hierarchy(self):
folders_level_0 = self.TargetModel.create([
{'name': 'Folder 1', },
{'name': 'Folder 2', },
{'name': 'Folder 3', },
])

f1_id, f2_id, f3_id = folders_level_0.ids

folders_level_1 = self.TargetModel.create([
{'name': 'Folder 4', 'parent_id': f1_id, },
{'name': 'Folder 5', 'parent_id': f2_id, },
{'name': 'Folder 6', 'parent_id': f2_id, },
])

f4_id, f5_id, f6_id = folders_level_1.ids

folders_level_2 = self.TargetModel.create([
{'name': 'Folder 7', 'parent_id': f4_id, },
{'name': 'Folder 8', 'parent_id': f6_id, },
])

f7_id, f8_id = folders_level_2.ids

folders_level_3 = self.TargetModel.create([
{'name': 'Folder 9', 'parent_id': f8_id, },
{'name': 'Folder 10', 'parent_id': f8_id, },
])

f9_id, f10_id = folders_level_3.ids

records = self.SourceModel.create([
{'name': 'Rec 1', 'folder_id': f1_id, },
{'name': 'Rec 2', 'folder_id': f6_id, },
{'name': 'Rec 3', 'folder_id': f7_id, },
{'name': 'Rec 4', 'folder_id': f7_id, },
{'name': 'Rec 5', 'folder_id': f9_id, },
{'name': 'Rec 6', 'folder_id': f10_id, },
])

# counters
result = self.SourceModel.search_panel_select_range('folder_id')
self.assertEqual(
result['values'],
[
{'count': 3, 'display_name': 'Folder 1', 'id': f1_id, },
{'count': 1, 'display_name': 'Folder 10', 'id': f10_id, 'parent_id': f8_id, },
{'count': 3, 'display_name': 'Folder 2', 'id': f2_id, },
{'count': 0, 'display_name': 'Folder 3', 'id': f3_id, },
{'count': 2, 'display_name': 'Folder 4', 'id': f4_id, 'parent_id': f1_id, },
{'count': 0, 'display_name': 'Folder 5', 'id': f5_id, 'parent_id': f2_id, },
{'count': 3, 'display_name': 'Folder 6', 'id': f6_id, 'parent_id': f2_id, },
{'count': 2, 'display_name': 'Folder 7', 'id': f7_id, 'parent_id': f4_id, },
{'count': 2, 'display_name': 'Folder 8', 'id': f8_id, 'parent_id': f6_id, },
{'count': 1, 'display_name': 'Folder 9', 'id': f9_id, 'parent_id': f8_id, },
]
)

# Many2one no parent name

def test_many2one_empty_no_parent_name(self):

0 comments on commit bedceb0

Please sign in to comment.
You can’t perform that action at this time.