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')),
44 ('unique_group', 'UNIQUE(group_id)', _('Portals must have distinct groups.'))
47 def copy(self, cr, uid, id, default={}, context=None):
48 """ override copy(): group_id and menu_action_id must be different """
49 # copy the former group_id
50 groups_obj = self.pool.get('res.groups')
51 group_id = self.browse(cr, uid, id, context).group_id.id
52 default['group_id'] = groups_obj.copy(cr, uid, group_id, {}, context)
53 default['menu_action_id'] = None
54 return super(portal, self).copy(cr, uid, id, default, context)
56 def create(self, cr, uid, values, context=None):
57 """ extend create() to assign the portal group and menu to users """
58 # first create the 'menu_action_id'
59 assert not values.get('menu_action_id')
60 values['menu_action_id'] = self._create_menu_action(cr, uid, values, context)
62 if 'user_ids' in values:
63 # set menu action of users
64 user_values = {'menu_id': values['menu_action_id']}
65 # values['user_ids'] should match [(6, 0, IDs)]
66 for id in get_many2many(values['user_ids']):
67 values['user_ids'].append((1, id, user_values))
69 return super(portal, self).create(cr, uid, values, context)
71 def name_get(self, cr, uid, ids, context=None):
72 portals = self.browse(cr, uid, ids, context)
73 return [(p.id, p.group_id.name) for p in portals]
75 def name_search(self, cr, uid, name='', args=None, operator='ilike', context=None, limit=100):
76 # first search for group names that match
77 groups_obj = self.pool.get('res.groups')
78 group_names = groups_obj.name_search(cr, uid, name, args, operator, context, limit)
79 # then search for portals that match the groups found so far
80 domain = [('group_id', 'in', [gn[0] for gn in group_names])]
81 ids = self.search(cr, uid, domain, context=context)
82 return self.name_get(cr, uid, ids, context)
84 def write(self, cr, uid, ids, values, context=None):
85 """ extend write() to reflect menu and groups changes on users """
86 # first apply portal changes
87 super(portal, self).write(cr, uid, ids, values, context)
88 portals = self.browse(cr, uid, ids, context)
90 # if 'menu_action_id' has changed, set menu_id on users
91 if 'menu_action_id' in values:
92 user_values = {'menu_id': values['menu_action_id']}
93 user_ids = [u.id for p in portals for u in p.user_ids]
94 self.pool.get('res.users').write(cr, uid, user_ids, user_values, context)
96 # if parent_menu_id has changed, apply the change on menu_action_id
97 if 'parent_menu_id' in values:
98 act_window_obj = self.pool.get('ir.actions.act_window')
99 action_ids = [p.menu_action_id.id for p in portals]
100 action_values = {'domain': [('parent_id', '=', values['parent_menu_id'])]}
101 act_window_obj.write(cr, uid, action_ids, action_values, context)
105 def _create_menu_action(self, cr, uid, values, context=None):
106 # create a menu action that opens the menu items below parent_menu_id
107 groups_obj = self.pool.get('res.groups')
108 group_name = groups_obj.browse(cr, uid, values['group_id'], context).name
109 actions_obj = self.pool.get('ir.actions.act_window')
111 'name': group_name + ' Menu',
112 'type': 'ir.actions.act_window',
114 'res_model': 'ir.ui.menu',
116 'view_id': self._res_xml_id(cr, uid, 'base', 'view_menu'),
117 'domain': [('parent_id', '=', values.get('parent_menu_id', False))],
119 return actions_obj.create(cr, uid, action_values, context)
121 def do_create_menu(self, cr, uid, ids, context=None):
122 """ create a parent menu for the given portals """
123 menu_obj = self.pool.get('ir.ui.menu')
124 menu_root = self._res_xml_id(cr, uid, 'portal', 'portal_menu')
126 for p in self.browse(cr, uid, ids, context):
127 # create a menuitem under 'portal.portal_menu'
129 'name': p.group_id.name + ' Menu',
130 'parent_id': menu_root,
131 'groups_id': [(6, 0, [p.group_id.id])],
133 menu_id = menu_obj.create(cr, uid, menu_values, context)
134 # set the parent_menu_id to item_id
135 self.write(cr, uid, [p.id], {'parent_menu_id': menu_id}, context)
139 def onchange_group(self, cr, uid, ids, group_id, context=None):
140 """ update the users list when the group changes """
143 group = self.pool.get('res.groups').browse(cr, uid, group_id, context)
144 user_ids = [u.id for u in group.users]
146 'value': {'user_ids': user_ids}
149 def _res_xml_id(self, cr, uid, module, xml_id):
150 """ return the resource id associated to the given xml_id """
151 data_obj = self.pool.get('ir.model.data')
152 data_id = data_obj._get_id(cr, uid, module, xml_id)
153 return data_obj.browse(cr, uid, data_id).res_id
159 class users(osv.osv):
161 _inherit = 'res.users'
163 def default_get(self, cr, uid, fields, context=None):
164 """ override default value of menu_id for portal users """
165 defs = super(users, self).default_get(cr, uid, fields, context)
167 # the value of 'menu_id' is passed in context by the portal form view
168 if ('menu_id' in context) and ('menu_id' in fields):
169 defs['menu_id'] = context['menu_id']
178 def get_browse_id(obj):
179 """ return the id of a browse() object, or None """
180 return (obj and obj.id or None)
182 def get_browse_ids(objs):
183 """ return the ids of a list of browse() objects """
184 return map(get_browse_id, objs)
186 def get_many2many(arg):
187 """ get the list of ids from a many2many 'values' field """
188 assert len(arg) == 1 and arg[0][0] == 6 # arg = [(6, _, IDs)]