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
23 from tools.translate import _
25 class portal(osv.osv):
27 A portal is a group of users with specific menu, widgets, and typically
28 restricted access rights.
31 _description = 'Portal'
32 _inherits = {'res.groups': 'group_id'}
35 'group_id': fields.many2one('res.groups', required=True, ondelete='cascade',
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.act_window', readonly=True,
43 # ISSUE: 'ondelete' constraints do not seem effective on this field...
45 help=_("If set, replaces the standard menu for the portal's users")),
46 'parent_menu_id': fields.many2one('ir.ui.menu', ondelete='restrict',
48 help=_('The menu action opens the submenus of this menu item')),
49 'widget_ids': fields.one2many('res.portal.widget', 'portal_id',
51 help=_('Widgets assigned to portal users')),
54 def copy(self, cr, uid, id, values, context=None):
55 """ override copy(): menu_action_id must be different """
56 values['menu_action_id'] = None
57 return super(portal, self).copy(cr, uid, id, values, context)
59 def create(self, cr, uid, values, context=None):
60 """ extend create() to assign the portal menu to users """
64 # create portal (admin should not be included)
65 context['noadmin'] = True
66 portal_id = super(portal, self).create(cr, uid, values, context)
68 # assign menu action and widgets to users
69 if values.get('users') or values.get('other_group_ids') or values.get('menu_action_id'):
70 self._assign_menu_and_groups(cr, uid, [portal_id], context)
71 if values.get('users') or values.get('widget_ids'):
72 self._assign_widgets(cr, uid, [portal_id], context)
76 def write(self, cr, uid, ids, values, context=None):
77 """ extend write() to reflect changes on users """
78 # first apply portal changes
79 super(portal, self).write(cr, uid, ids, values, context)
81 # assign menu action and widgets to users
82 if values.get('users') or values.get('other_group_ids') or values.get('menu_action_id'):
83 self._assign_menu_and_groups(cr, uid, ids, context)
84 if values.get('users') or values.get('widget_ids'):
85 self._assign_widgets(cr, uid, ids, context)
87 # if parent_menu_id has changed, apply the change on menu_action_id
88 if 'parent_menu_id' in values:
89 act_window_obj = self.pool.get('ir.actions.act_window')
90 portals = self.browse(cr, uid, ids, context)
91 action_ids = [p.menu_action_id.id for p in portals if p.menu_action_id]
93 action_values = {'domain': [('parent_id', '=', values['parent_menu_id'])]}
94 act_window_obj.write(cr, uid, action_ids, action_values, context)
98 def do_create_menu(self, cr, uid, ids, context=None):
99 """ create a parent menu for the given portals """
100 menu_obj = self.pool.get('ir.ui.menu')
101 menu_root = self._res_xml_id(cr, uid, 'portal', 'portal_menu')
103 for p in self.browse(cr, uid, ids, context):
104 # create a menuitem under 'portal.portal_menu'
106 'name': p.name + ' Menu',
107 'parent_id': menu_root,
108 'groups_id': [(6, 0, [p.group_id.id])],
110 menu_id = menu_obj.create(cr, uid, menu_values, context)
111 # set the parent_menu_id to item_id
112 self.write(cr, uid, [p.id], {'parent_menu_id': menu_id}, context)
116 def _assign_menu_and_groups(self, cr, uid, ids, context=None):
117 """ assign portal menu and other groups to users of portals (ids) """
118 user_obj = self.pool.get('res.users')
119 for p in self.browse(cr, uid, ids, context):
120 # user groups = portal group + other groups
121 group_ids = [p.group_id.id] + [g.id for g in p.other_group_ids]
122 user_values = {'groups_id': [(6, 0, group_ids)]}
123 # user menu action = portal menu action if set in portal
125 user_values['menu_id'] = p.menu_action_id.id
126 user_ids = [u.id for u in p.users]
127 user_obj.write(cr, uid, user_ids, user_values, context)
129 def _assign_widgets(self, cr, uid, ids, context=None):
130 """ assign portal widgets to users of portals (ids) """
131 widget_user_obj = self.pool.get('res.widget.user')
132 for p in self.browse(cr, uid, ids, context):
133 for w in p.widget_ids:
134 values = {'sequence': w.sequence, 'widget_id': w.widget_id.id}
136 if u.id == 1: continue
137 values['user_id'] = u.id
138 widget_user_obj.create(cr, uid, values, context)
140 def _res_xml_id(self, cr, uid, module, xml_id):
141 """ return the resource id associated to the given xml_id """
142 data_obj = self.pool.get('ir.model.data')
143 data_id = data_obj._get_id(cr, uid, module, xml_id)
144 return data_obj.browse(cr, uid, data_id).res_id
150 class portal_override_menu(osv.osv):
152 extend res.portal with a boolean field 'Override Users Menu', that
153 triggers the creation or removal of menu_action_id
156 _inherit = 'res.portal'
158 def _get_override_menu(self, cr, uid, ids, field_name, arg, context=None):
159 assert field_name == 'override_menu'
161 for p in self.browse(cr, uid, ids, context):
162 result[p.id] = bool(p.menu_action_id)
165 def _set_override_menu(self, cr, uid, id, field_name, field_value, arg, context=None):
166 assert field_name == 'override_menu'
168 self.create_menu_action(cr, uid, id, context)
170 self.write(cr, uid, [id], {'menu_action_id': False}, context)
172 def create_menu_action(self, cr, uid, id, context=None):
173 """ create, if necessary, a menu action that opens the menu items below
175 p = self.browse(cr, uid, id, context)
176 if not p.menu_action_id:
177 actions_obj = self.pool.get('ir.actions.act_window')
178 parent_id = p.parent_menu_id.id if p.parent_menu_id else False
180 'name': p.name + ' Menu',
181 'type': 'ir.actions.act_window',
183 'res_model': 'ir.ui.menu',
185 'view_id': self._res_xml_id(cr, uid, 'base', 'view_menu'),
186 'domain': [('parent_id', '=', parent_id)],
188 action_id = actions_obj.create(cr, uid, action_values, context)
189 self.write(cr, uid, [id], {'menu_action_id': action_id}, context)
192 'override_menu': fields.function(
193 _get_override_menu, fnct_inv=_set_override_menu,
194 type='boolean', method=True, string='Override Users Menu Action',
195 help=_('Enable this option to create the Menu Action')),
198 portal_override_menu()
202 class portal_widget(osv.osv):
204 Similar to res.widget.user (res_widget.py), but with a portal instead.
205 New users in a portal are assigned the portal's widgets.
207 _name='res.portal.widget'
208 _description = 'Portal Widgets'
211 'sequence': fields.integer('Sequence'),
212 'portal_id': fields.many2one('res.portal', select=1,
214 'widget_id': fields.many2one('res.widget', required=True, ondelete='cascade',
218 def create(self, cr, uid, values, context=None):
219 domain = [('portal_id', '=', values.get('portal_id')),
220 ('widget_id', '=', values.get('widget_id'))]
221 existing = self.search(cr, uid, domain, context=context)
225 res = super(portal_widget, self).create(cr, uid, values, context=context)