[IMP] add tooltip to override_menu
[odoo/odoo.git] / addons / portal / portal.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2011 Tiny SPRL (<http://tiny.be>).
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 from osv import osv, fields
23 from tools.translate import _
24
25 class portal(osv.osv):
26     """
27         A portal is a group of users with specific menu, widgets, and typically
28         restricted access rights.
29     """
30     _name = 'res.portal'
31     _description = 'Portal'
32     _inherits = {'res.groups': 'group_id'}
33     
34     _columns = {
35         'group_id': fields.many2one('res.groups', required=True, ondelete='cascade',
36             string='Group',
37             help=_('The group extended by this portal')),
38         'other_group_ids': fields.many2many('res.groups',
39             'portal_group_rel', 'portal_id', 'group_id',
40             string='Other User Groups',
41             help=_("Those groups are assigned to the portal's users")),
42         'menu_action_id': fields.many2one('ir.actions.actions', readonly=True,
43             string='Menu Action',
44             help=_("If set, replaces the standard menu for the portal's users")),
45         'parent_menu_id': fields.many2one('ir.ui.menu',
46             string='Parent Menu',
47             help=_('The menu action opens the submenus of this menu item')),
48         'widget_ids': fields.one2many('res.portal.widget', 'portal_id',
49             string='Widgets',
50             help=_('Widgets assigned to portal users')),
51     }
52     
53     def copy(self, cr, uid, id, values, context=None):
54         """ override copy(): menu_action_id must be different """
55         values['menu_action_id'] = None
56         return super(portal, self).copy(cr, uid, id, values, context)
57     
58     def create(self, cr, uid, values, context=None):
59         """ extend create() to assign the portal menu to users """
60         if context is None:
61             context = {}
62         
63         # create portal (admin should not be included)
64         context['noadmin'] = True
65         portal_id = super(portal, self).create(cr, uid, values, context)
66         
67         # assign menu action and widgets to users
68         if values.get('users') or values.get('other_group_ids') or values.get('menu_action_id'):
69             self._assign_menu_and_groups(cr, uid, [portal_id], context)
70         if values.get('users') or values.get('widget_ids'):
71             self._assign_widgets(cr, uid, [portal_id], context)
72         
73         return portal_id
74     
75     def write(self, cr, uid, ids, values, context=None):
76         """ extend write() to reflect changes on users """
77         # first apply portal changes
78         super(portal, self).write(cr, uid, ids, values, context)
79         
80         # assign menu action and widgets to users
81         if values.get('users') or values.get('other_group_ids') or values.get('menu_action_id'):
82             self._assign_menu_and_groups(cr, uid, ids, context)
83         if values.get('users') or values.get('widget_ids'):
84             self._assign_widgets(cr, uid, ids, context)
85         
86         # if parent_menu_id has changed, apply the change on menu_action_id
87         if 'parent_menu_id' in values:
88             act_window_obj = self.pool.get('ir.actions.act_window')
89             portals = self.browse(cr, uid, ids, context)
90             action_ids = [p.menu_action_id.id for p in portals if p.menu_action_id]
91             if action_ids:
92                 action_values = {'domain': [('parent_id', '=', values['parent_menu_id'])]}
93                 act_window_obj.write(cr, uid, action_ids, action_values, context)
94         
95         return True
96     
97     def do_create_menu(self, cr, uid, ids, context=None):
98         """ create a parent menu for the given portals """
99         menu_obj = self.pool.get('ir.ui.menu')
100         menu_root = self._res_xml_id(cr, uid, 'portal', 'portal_menu')
101         
102         for p in self.browse(cr, uid, ids, context):
103             # create a menuitem under 'portal.portal_menu'
104             menu_values = {
105                 'name': p.name + ' Menu',
106                 'parent_id': menu_root,
107                 'groups_id': [(6, 0, [p.group_id.id])],
108             }
109             menu_id = menu_obj.create(cr, uid, menu_values, context)
110             # set the parent_menu_id to item_id
111             self.write(cr, uid, [p.id], {'parent_menu_id': menu_id}, context)
112         
113         return True
114
115     def _assign_menu_and_groups(self, cr, uid, ids, context=None):
116         """ assign portal menu and other groups to users of portals (ids) """
117         user_obj = self.pool.get('res.users')
118         for p in self.browse(cr, uid, ids, context):
119             # user groups = portal group + other groups
120             group_ids = [p.group_id.id] + [g.id for g in p.other_group_ids]
121             user_values = {'groups_id': [(6, 0, group_ids)]}
122             # user menu action = portal menu action if set in portal
123             if p.menu_action_id:
124                 user_values['menu_id'] = p.menu_action_id.id
125             user_ids = [u.id for u in p.users]
126             user_obj.write(cr, uid, user_ids, user_values, context)
127
128     def _assign_widgets(self, cr, uid, ids, context=None):
129         """ assign portal widgets to users of portals (ids) """
130         widget_user_obj = self.pool.get('res.widget.user')
131         for p in self.browse(cr, uid, ids, context):
132             for w in p.widget_ids:
133                 values = {'sequence': w.sequence, 'widget_id': w.widget_id.id}
134                 for u in p.user_ids:
135                     if u.id == 1: continue
136                     values['user_id'] = u.id
137                     widget_user_obj.create(cr, uid, values, context)
138
139     def _res_xml_id(self, cr, uid, module, xml_id):
140         """ return the resource id associated to the given xml_id """
141         data_obj = self.pool.get('ir.model.data')
142         data_id = data_obj._get_id(cr, uid, module, xml_id)
143         return data_obj.browse(cr, uid, data_id).res_id
144
145 portal()
146
147
148
149 class portal_override_menu(osv.osv):
150     """
151         extend res.portal with a boolean field 'Override Users Menu', that
152         triggers the creation or removal of menu_action_id
153     """
154     _name = 'res.portal'
155     _inherit = 'res.portal'
156     
157     def _get_override_menu(self, cr, uid, ids, field_name, arg, context=None):
158         assert field_name == 'override_menu'
159         result = {}
160         for p in self.browse(cr, uid, ids, context):
161             result[p.id] = bool(p.menu_action_id)
162         return result
163     
164     def _set_override_menu(self, cr, uid, id, field_name, field_value, arg, context=None):
165         assert field_name == 'override_menu'
166         if field_value:
167             self.create_menu_action(cr, uid, id, context)
168         else:
169             self.write(cr, uid, [id], {'menu_action_id': False}, context)
170     
171     def create_menu_action(self, cr, uid, id, context=None):
172         """ create, if necessary, a menu action that opens the menu items below
173             parent_menu_id """
174         p = self.browse(cr, uid, id, context)
175         if not p.menu_action_id:
176             actions_obj = self.pool.get('ir.actions.act_window')
177             parent_id = p.parent_menu_id.id if p.parent_menu_id else False
178             action_values = {
179                 'name': p.name + ' Menu',
180                 'type': 'ir.actions.act_window',
181                 'usage': 'menu',
182                 'res_model': 'ir.ui.menu',
183                 'view_type': 'tree',
184                 'view_id': self._res_xml_id(cr, uid, 'base', 'view_menu'),
185                 'domain': [('parent_id', '=', parent_id)],
186             }
187             action_id = actions_obj.create(cr, uid, action_values, context)
188             self.write(cr, uid, [id], {'menu_action_id': action_id}, context)
189     
190     _columns = {
191         'override_menu': fields.function(
192             _get_override_menu, fnct_inv=_set_override_menu,
193             type='boolean', method=True, string='Override Users Menu Action',
194             help=_('Enable this option to create the Menu Action')),
195     }
196
197 portal_override_menu()
198
199
200
201 class portal_widget(osv.osv):
202     """
203         Similar to res.widget.user (res_widget.py), but with a portal instead.
204         New users in a portal are assigned the portal's widgets.
205     """
206     _name='res.portal.widget'
207     _description = 'Portal Widgets'
208     _order = 'sequence'
209     _columns = {
210         'sequence': fields.integer('Sequence'),
211         'portal_id': fields.many2one('res.portal', select=1,
212             string='Portal'),
213         'widget_id': fields.many2one('res.widget', required=True, ondelete='cascade',
214             string='Widget'),
215     }
216
217     def create(self, cr, uid, values, context=None):
218         domain = [('portal_id', '=', values.get('portal_id')),
219                   ('widget_id', '=', values.get('widget_id'))]
220         existing = self.search(cr, uid, domain, context=context)
221         if existing:
222             res = existing[0]
223         else:
224             res = super(portal_widget, self).create(cr, uid, values, context=context)
225         return res
226
227 portal_widget()
228
229
230