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 _description = 'Portal'
28 _rec_name = 'group_id'
30 'group_id': fields.many2one('res.groups', required=True,
31 string='Portal Group',
32 help=_('This group defines the users associated to this portal')),
33 'user_ids': fields.related('group_id', 'users',
34 type='many2many', relation='res.users', store=False,
35 string='Portal Users'),
36 'menu_action_id': fields.many2one('ir.actions.actions', readonly=True,
38 help=_("What replaces the standard menu for the portal's users")),
39 'parent_menu_id': fields.many2one('ir.ui.menu',
41 help=_('The menu action opens the submenus of this menu item')),
42 'widget_ids': fields.one2many('res.portal.widget', 'portal_id',
44 help=_('Widgets assigned to portal users')),
47 ('unique_group', 'UNIQUE(group_id)', _('Portals must have distinct groups.'))
50 def copy(self, cr, uid, id, default={}, context=None):
51 """ override copy(): group_id and menu_action_id must be different """
52 # copy the former group_id
53 groups_obj = self.pool.get('res.groups')
54 group_id = self.browse(cr, uid, id, context).group_id.id
55 default['group_id'] = groups_obj.copy(cr, uid, group_id, {}, context)
56 default['menu_action_id'] = None
57 return super(portal, self).copy(cr, uid, id, default, context)
59 def create(self, cr, uid, values, context=None):
60 """ extend create() to assign the portal group and menu to users """
61 # first create the 'menu_action_id'
62 assert not values.get('menu_action_id')
63 values['menu_action_id'] = self._create_menu_action(cr, uid, values, context)
65 if 'user_ids' in values:
66 # set menu action of users
67 user_values = {'menu_id': values['menu_action_id']}
68 # values['user_ids'] should match [(6, 0, IDs)]
69 for id in get_many2many(values['user_ids']):
70 values['user_ids'].append((1, id, user_values))
73 portal_id = super(portal, self).create(cr, uid, values, context)
75 # assign widgets to users
76 if 'user_ids' in values:
77 self._assign_widgets_to_users(cr, uid, portal_id, context)
81 def name_get(self, cr, uid, ids, context=None):
82 portals = self.browse(cr, uid, ids, context)
83 return [(p.id, p.group_id.name) for p in portals]
85 def name_search(self, cr, uid, name='', args=None, operator='ilike', context=None, limit=100):
86 # first search for group names that match
87 groups_obj = self.pool.get('res.groups')
88 group_names = groups_obj.name_search(cr, uid, name, args, operator, context, limit)
89 # then search for portals that match the groups found so far
90 domain = [('group_id', 'in', [gn[0] for gn in group_names])]
91 ids = self.search(cr, uid, domain, context=context)
92 return self.name_get(cr, uid, ids, context)
94 def write(self, cr, uid, ids, values, context=None):
95 """ extend write() to reflect menu and groups changes on users """
96 # first apply portal changes
97 super(portal, self).write(cr, uid, ids, values, context)
98 portals = self.browse(cr, uid, ids, context)
100 # if 'menu_action_id' has changed, set menu_id on users
101 if 'menu_action_id' in values:
102 user_values = {'menu_id': values['menu_action_id']}
103 user_ids = [u.id for p in portals for u in p.user_ids if u.id != 1]
104 self.pool.get('res.users').write(cr, uid, user_ids, user_values, context)
106 # if parent_menu_id has changed, apply the change on menu_action_id
107 if 'parent_menu_id' in values:
108 act_window_obj = self.pool.get('ir.actions.act_window')
109 action_ids = [p.menu_action_id.id for p in portals]
110 action_values = {'domain': [('parent_id', '=', values['parent_menu_id'])]}
111 act_window_obj.write(cr, uid, action_ids, action_values, context)
113 # assign portal widgets to users, if widgets or users changed
114 if ('user_ids' in values) or ('widget_ids' in values):
115 self._assign_widgets_to_users(cr, uid, ids, context)
119 def _create_menu_action(self, cr, uid, values, context=None):
120 # create a menu action that opens the menu items below parent_menu_id
121 groups_obj = self.pool.get('res.groups')
122 group_name = groups_obj.browse(cr, uid, values['group_id'], context).name
123 actions_obj = self.pool.get('ir.actions.act_window')
125 'name': group_name + ' Menu',
126 'type': 'ir.actions.act_window',
128 'res_model': 'ir.ui.menu',
130 'view_id': self._res_xml_id(cr, uid, 'base', 'view_menu'),
131 'domain': [('parent_id', '=', values.get('parent_menu_id', False))],
133 return actions_obj.create(cr, uid, action_values, context)
135 def do_create_menu(self, cr, uid, ids, context=None):
136 """ create a parent menu for the given portals """
137 menu_obj = self.pool.get('ir.ui.menu')
138 menu_root = self._res_xml_id(cr, uid, 'portal', 'portal_menu')
140 for p in self.browse(cr, uid, ids, context):
141 # create a menuitem under 'portal.portal_menu'
143 'name': p.group_id.name + ' Menu',
144 'parent_id': menu_root,
145 'groups_id': [(6, 0, [p.group_id.id])],
147 menu_id = menu_obj.create(cr, uid, menu_values, context)
148 # set the parent_menu_id to item_id
149 self.write(cr, uid, [p.id], {'parent_menu_id': menu_id}, context)
153 def _assign_widgets_to_users(self, cr, uid, ids, context=None):
154 """ assign portal widgets to users for the given portal ids """
155 widget_user_obj = self.pool.get('res.widget.user')
156 portals = self.browse(cr, uid, ids, context)
158 for w in p.widget_ids:
159 values = {'sequence': w.sequence, 'widget_id': w.widget_id.id}
161 if u.id == 1: continue
162 values['user_id'] = u.id
163 widget_user_obj.create(cr, uid, values, context)
165 def onchange_group(self, cr, uid, ids, group_id, context=None):
166 """ update the users list when the group changes """
169 group = self.pool.get('res.groups').browse(cr, uid, group_id, context)
170 user_ids = [u.id for u in group.users]
172 'value': {'user_ids': user_ids}
175 def _res_xml_id(self, cr, uid, module, xml_id):
176 """ return the resource id associated to the given xml_id """
177 data_obj = self.pool.get('ir.model.data')
178 data_id = data_obj._get_id(cr, uid, module, xml_id)
179 return data_obj.browse(cr, uid, data_id).res_id
185 class users(osv.osv):
187 _inherit = 'res.users'
189 def default_get(self, cr, uid, fields, context=None):
190 """ override default value of menu_id for portal users """
191 defs = super(users, self).default_get(cr, uid, fields, context)
193 # the value of 'menu_id' is passed in context by the portal form view
194 if ('menu_id' in context) and ('menu_id' in fields):
195 defs['menu_id'] = context['menu_id']
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)
234 def get_browse_id(obj):
235 """ return the id of a browse() object, or None """
236 return (obj and obj.id or None)
238 def get_browse_ids(objs):
239 """ return the ids of a list of browse() objects """
240 return map(get_browse_id, objs)
242 def get_many2many(arg):
243 """ get the list of ids from a many2many 'values' field """
244 assert len(arg) == 1 and arg[0][0] == 6 # arg = [(6, _, IDs)]