[IMP] menu loading: simplify/optimize menu loading mechanism and de-duplicate need_action load, use child_of, skip useless calls, update tests
bzr revid: odo@openerp.com-20121212205120-qa8gq86hh6f26bdk
This commit is contained in:
parent
4c1ead2a1c
commit
55419f063c
|
@ -940,24 +940,28 @@ class Menu(openerpweb.Controller):
|
|||
"""
|
||||
Menus = req.session.model('ir.ui.menu')
|
||||
|
||||
fields = ['name', 'sequence', 'parent_id', 'action',
|
||||
'needaction_enabled']
|
||||
menu_roots = Menus.read(self.get_user_roots(req), fields, req.context)
|
||||
fields = ['name', 'sequence', 'parent_id', 'action']
|
||||
menu_root_ids = self.get_user_roots(req)
|
||||
menu_roots = Menus.read(menu_root_ids, fields, req.context) if menu_root_ids else []
|
||||
menu_root = {
|
||||
'id': False,
|
||||
'name': 'root',
|
||||
'parent_id': [-1, ''],
|
||||
'children': menu_roots
|
||||
'children': menu_roots,
|
||||
'all_menu_ids': menu_root_ids,
|
||||
}
|
||||
if not menu_roots:
|
||||
return menu_root
|
||||
|
||||
# menus are loaded fully unlike a regular tree view, cause there are a
|
||||
# limited number of items (752 when all 6.1 addons are installed)
|
||||
menu_ids = Menus.search([], 0, False, False, req.context)
|
||||
menu_ids = Menus.search([('id', 'child_of', menu_root_ids)], 0, False, False, req.context)
|
||||
menu_items = Menus.read(menu_ids, fields, req.context)
|
||||
# adds roots at the end of the sequence, so that they will overwrite
|
||||
# equivalent menu items from full menu read when put into id:item
|
||||
# mapping, resulting in children being correctly set on the roots.
|
||||
menu_items.extend(menu_roots)
|
||||
menu_root['all_menu_ids'] = menu_ids # includes menu_root_ids!
|
||||
|
||||
# make a tree using parent_id
|
||||
menu_items_map = dict(
|
||||
|
@ -979,19 +983,13 @@ class Menu(openerpweb.Controller):
|
|||
return menu_root
|
||||
|
||||
@openerpweb.jsonrequest
|
||||
def load_needaction(self, req, menu_ids=False):
|
||||
""" Loads needaction counters for all or some specific menu ids.
|
||||
def load_needaction(self, req, menu_ids):
|
||||
""" Loads needaction counters for specific menu ids.
|
||||
|
||||
:return: needaction data
|
||||
:rtype: dict(menu_id: {'needaction_enabled': boolean, 'needaction_counter': int})
|
||||
"""
|
||||
Menus = req.session.model('ir.ui.menu')
|
||||
|
||||
if menu_ids == False:
|
||||
menu_ids = Menus.search([('needaction_enabled', '=', True)], context=req.context)
|
||||
|
||||
menu_needaction_data = Menus.get_needaction_data(menu_ids, req.context)
|
||||
return menu_needaction_data
|
||||
return req.session.model('ir.ui.menu').get_needaction_data(menu_ids, req.context)
|
||||
|
||||
@openerpweb.jsonrequest
|
||||
def action(self, req, menu_id):
|
||||
|
|
|
@ -803,11 +803,13 @@ instance.web.Menu = instance.web.Widget.extend({
|
|||
this.has_been_loaded = $.Deferred();
|
||||
this.maximum_visible_links = 'auto'; // # of menu to show. 0 = do not crop, 'auto' = algo
|
||||
this.data = {data:{children:[]}};
|
||||
this.on("menu_loaded", this, function (e) {
|
||||
this.on("menu_loaded", this, function (menu_data) {
|
||||
// launch the fetch of needaction counters, asynchronous
|
||||
this.rpc("/web/menu/load_needaction", {menu_ids: false}).done(function(r) {
|
||||
self.on_needaction_loaded(r);
|
||||
});
|
||||
if (!_.isEmpty(menu_data.all_menu_ids)) {
|
||||
this.rpc("/web/menu/load_needaction", {menu_ids: menu_data.all_menu_ids}).done(function(r) {
|
||||
self.on_needaction_loaded(r);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
},
|
||||
|
|
|
@ -39,12 +39,11 @@ class LoadTest(unittest2.TestCase):
|
|||
root = self.menu.load(self.request)
|
||||
|
||||
self.MockMenus.search.assert_called_with(
|
||||
[], 0, False, False, self.request.context)
|
||||
self.MockMenus.read.assert_called_with(
|
||||
[], ['name', 'sequence', 'parent_id',
|
||||
'action', 'needaction_enabled'],
|
||||
[('parent_id','=', False)], 0, False, False,
|
||||
self.request.context)
|
||||
|
||||
self.assertEqual(root['all_menu_ids'], [])
|
||||
|
||||
self.assertListEqual(
|
||||
root['children'],
|
||||
[])
|
||||
|
@ -59,11 +58,17 @@ class LoadTest(unittest2.TestCase):
|
|||
|
||||
root = self.menu.load(self.request)
|
||||
|
||||
self.MockMenus.search.assert_called_with(
|
||||
[('id','child_of', [1, 2, 3])], 0, False, False,
|
||||
self.request.context)
|
||||
|
||||
self.MockMenus.read.assert_called_with(
|
||||
[1, 2, 3], ['name', 'sequence', 'parent_id',
|
||||
'action', 'needaction_enabled'],
|
||||
'action'],
|
||||
self.request.context)
|
||||
|
||||
self.assertEqual(root['all_menu_ids'], [1, 2, 3])
|
||||
|
||||
self.assertEqual(
|
||||
root['children'],
|
||||
[{
|
||||
|
@ -92,6 +97,12 @@ class LoadTest(unittest2.TestCase):
|
|||
|
||||
root = self.menu.load(self.request)
|
||||
|
||||
self.MockMenus.search.assert_called_with(
|
||||
[('id','child_of', [1])], 0, False, False,
|
||||
self.request.context)
|
||||
|
||||
self.assertEqual(root['all_menu_ids'], [1, 2, 3, 4])
|
||||
|
||||
self.assertEqual(
|
||||
root['children'],
|
||||
[{
|
||||
|
|
Loading…
Reference in New Issue