[MERGE] from trunk
[odoo/odoo.git] / addons / portal / ir_ui_menu.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2011 OpenERP S.A (<http://www.openerp.com>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 import logging
23
24 from osv import osv, fields
25 from tools.safe_eval import safe_eval
26
27 class portal_menu(osv.osv):
28     """
29         Fix menu class to customize the login search for menus,
30         as web client 6.0 does not support the menu action properly yet
31     """
32     _name = 'ir.ui.menu'
33     _inherit = 'ir.ui.menu'
34
35     def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):
36         if context is None:
37             context = {}
38         
39         # if the user belongs to a portal, we have to rewrite any search on the
40         # top menus to be under the portal's parent menu
41         if not context.get('ir.ui.menu.full_list') and uid != 1 and \
42                 args == [('parent_id', '=', False)]:
43             portal_obj = self.pool.get('res.portal')
44             portal_ids = portal_obj.search(cr, uid, [('users', 'in', uid)])
45             if portal_ids:
46                 if len(portal_ids) > 1:
47                     log = logging.getLogger('ir.ui.menu')
48                     log.warning('User %s belongs to several portals', str(uid))
49                 p = portal_obj.browse(cr, uid, portal_ids[0])
50                 # if the portal overrides the menu, use its domain
51                 if p.menu_action_id:
52                     args = safe_eval(p.menu_action_id.domain)
53         
54         return super(portal_menu, self).search(cr, uid, args, offset=offset,
55                     limit=limit, order=order, context=context, count=count)
56
57 portal_menu()
58