1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2011 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from osv import osv, fields
26 class portal(osv.osv):
28 A portal is a group of users with specific menu, widgets, and typically
29 restricted access rights.
32 _description = 'Portal'
33 _inherits = {'res.groups': 'group_id'}
36 'group_id': fields.many2one('res.groups', required=True, ondelete='cascade',
38 help='The group extended by this portal'),
39 'other_group_ids': fields.many2many('res.groups',
40 'portal_group_rel', 'portal_id', 'group_id',
41 string='Other User Groups',
42 help="Those groups are assigned to the portal's users"),
43 'menu_action_id': fields.many2one('ir.actions.act_window', readonly=True,
44 # ISSUE: 'ondelete' constraints do not seem effective on this field...
46 help="If set, replaces the standard menu for the portal's users"),
47 'parent_menu_id': fields.many2one('ir.ui.menu', ondelete='restrict',
49 help='The menu action opens the submenus of this menu item'),
50 'widget_ids': fields.one2many('res.portal.widget', 'portal_id',
52 help='Widgets assigned to portal users'),
55 def copy(self, cr, uid, id, values, context=None):
56 """ override copy(): menu_action_id must be different """
57 values['menu_action_id'] = None
58 return super(portal, self).copy(cr, uid, id, values, context)
60 def create(self, cr, uid, values, context=None):
61 """ extend create() to assign the portal menu to users """
65 # create portal (admin should not be included)
66 context['noadmin'] = True
67 portal_id = super(portal, self).create(cr, uid, values, context)
69 # assign menu action and widgets to users
70 if values.get('users') or values.get('other_group_ids') or values.get('menu_action_id'):
71 self._assign_menu_and_groups(cr, uid, [portal_id], context)
72 if values.get('users') or values.get('widget_ids'):
73 self._assign_widgets(cr, uid, [portal_id], context)
77 def write(self, cr, uid, ids, values, context=None):
78 """ extend write() to reflect changes on users """
79 # first apply portal changes
80 super(portal, self).write(cr, uid, ids, values, context)
82 # assign menu action and widgets to users
83 if values.get('users') or values.get('other_group_ids') or values.get('menu_action_id'):
84 self._assign_menu_and_groups(cr, uid, ids, context)
85 if values.get('users') or values.get('widget_ids'):
86 self._assign_widgets(cr, uid, ids, context)
88 # if parent_menu_id has changed, apply the change on menu_action_id
89 if 'parent_menu_id' in values:
90 act_window_obj = self.pool.get('ir.actions.act_window')
91 portals = self.browse(cr, uid, ids, context)
92 action_ids = [p.menu_action_id.id for p in portals if p.menu_action_id]
94 action_values = {'domain': [('parent_id', '=', values['parent_menu_id'])]}
95 act_window_obj.write(cr, uid, action_ids, action_values, context)
99 def do_create_menu(self, cr, uid, ids, context=None):
100 """ create a parent menu for the given portals """
101 menu_obj = self.pool.get('ir.ui.menu')
102 menu_root = self._res_xml_id(cr, uid, 'portal', 'portal_menu')
104 for p in self.browse(cr, uid, ids, context):
105 # create a menuitem under 'portal.portal_menu'
107 'name': p.name + ' Menu',
108 'parent_id': menu_root,
109 'groups_id': [(6, 0, [p.group_id.id])],
111 menu_id = menu_obj.create(cr, uid, menu_values, context)
112 # set the parent_menu_id to item_id
113 self.write(cr, uid, [p.id], {'parent_menu_id': menu_id}, context)
117 def _assign_menu_and_groups(self, cr, uid, ids, context=None):
118 """ assign portal menu and other groups to users of portals (ids) """
119 user_obj = self.pool.get('res.users')
120 for p in self.browse(cr, uid, ids, context):
121 # user groups = portal group + other groups
122 group_ids = [p.group_id.id] + [g.id for g in p.other_group_ids]
123 user_values = {'groups_id': [(6, 0, group_ids)]}
124 # user menu action = portal menu action if set in portal
126 user_values['menu_id'] = p.menu_action_id.id
127 user_ids = [u.id for u in p.users if u.id != 1]
128 user_obj.write(cr, uid, user_ids, user_values, context)
130 def _assign_widgets(self, cr, uid, ids, context=None):
131 """ assign portal widgets to users of portals (ids) """
132 widget_user_obj = self.pool.get('res.widget.user')
133 for p in self.browse(cr, uid, ids, context):
134 for w in p.widget_ids:
135 values = {'sequence': w.sequence, 'widget_id': w.widget_id.id}
137 if u.id == 1: continue
138 values['user_id'] = u.id
139 widget_user_obj.create(cr, uid, values, context)
141 def _res_xml_id(self, cr, uid, module, xml_id):
142 """ return the resource id associated to the given xml_id """
143 data_obj = self.pool.get('ir.model.data')
144 data_id = data_obj._get_id(cr, uid, module, xml_id)
145 return data_obj.browse(cr, uid, data_id).res_id
151 class portal_override_menu(osv.osv):
153 extend res.portal with a boolean field 'Override Users Menu', that
154 triggers the creation or removal of menu_action_id
157 _inherit = 'res.portal'
159 def _get_override_menu(self, cr, uid, ids, field_name, arg, context=None):
160 assert field_name == 'override_menu'
162 for p in self.browse(cr, uid, ids, context):
163 result[p.id] = bool(p.menu_action_id)
166 def _set_override_menu(self, cr, uid, id, field_name, field_value, arg, context=None):
167 assert field_name == 'override_menu'
169 self.create_menu_action(cr, uid, id, context)
171 self.write(cr, uid, [id], {'menu_action_id': False}, context)
173 def create_menu_action(self, cr, uid, id, context=None):
174 """ create, if necessary, a menu action that opens the menu items below
176 p = self.browse(cr, uid, id, context)
177 if not p.menu_action_id:
178 actions_obj = self.pool.get('ir.actions.act_window')
179 parent_id = p.parent_menu_id.id if p.parent_menu_id else False
181 'name': p.name + ' Menu',
182 'type': 'ir.actions.act_window',
184 'res_model': 'ir.ui.menu',
186 'view_id': self._res_xml_id(cr, uid, 'base', 'view_menu'),
187 'domain': [('parent_id', '=', parent_id)],
189 action_id = actions_obj.create(cr, uid, action_values, context)
190 self.write(cr, uid, [id], {'menu_action_id': action_id}, context)
193 'override_menu': fields.function(
194 _get_override_menu, fnct_inv=_set_override_menu,
195 type='boolean', method=True, string='Override Users Menu Action',
196 help='Enable this option to create the Menu Action'),
199 portal_override_menu()
203 class portal_widget(osv.osv):
205 Similar to res.widget.user (res_widget.py), but with a portal instead.
206 New users in a portal are assigned the portal's widgets.
208 _name='res.portal.widget'
209 _description = 'Portal Widgets'
212 'sequence': fields.integer('Sequence'),
213 'portal_id': fields.many2one('res.portal', select=1,
215 'widget_id': fields.many2one('res.widget', required=True, ondelete='cascade',
219 def create(self, cr, uid, values, context=None):
220 domain = [('portal_id', '=', values.get('portal_id')),
221 ('widget_id', '=', values.get('widget_id'))]
222 existing = self.search(cr, uid, domain, context=context)
226 res = super(portal_widget, self).create(cr, uid, values, context=context)