2011-03-22 14:38:25 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# OpenERP, Open Source Management Solution
|
|
|
|
# Copyright (C) 2004-2011 Tiny SPRL (<http://tiny.be>).
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
from osv import osv, fields
|
2011-03-29 13:51:42 +00:00
|
|
|
from tools.translate import _
|
2011-03-22 14:38:25 +00:00
|
|
|
|
|
|
|
class portal(osv.osv):
|
|
|
|
_name = 'res.portal'
|
2011-03-23 08:16:41 +00:00
|
|
|
_description = 'Portal'
|
2011-04-04 11:30:36 +00:00
|
|
|
_rec_name = 'group_id'
|
2011-03-22 14:38:25 +00:00
|
|
|
_columns = {
|
2011-04-01 07:40:39 +00:00
|
|
|
'group_id': fields.many2one('res.groups', required=True,
|
|
|
|
string='Portal Group',
|
|
|
|
help=_('This group defines the users associated to this portal')),
|
|
|
|
'user_ids': fields.related('group_id', 'users',
|
|
|
|
type='many2many', relation='res.users', store=False,
|
|
|
|
string='Portal Users'),
|
2011-03-30 12:40:07 +00:00
|
|
|
'menu_action_id': fields.many2one('ir.actions.actions', readonly=True,
|
|
|
|
string='Menu Action',
|
|
|
|
help=_("What replaces the standard menu for the portal's users")),
|
|
|
|
'parent_menu_id': fields.many2one('ir.ui.menu',
|
|
|
|
string='Parent Menu',
|
|
|
|
help=_('The menu action opens the submenus of this menu item')),
|
2011-03-22 14:38:25 +00:00
|
|
|
}
|
2011-03-25 14:42:27 +00:00
|
|
|
_sql_constraints = [
|
2011-04-04 11:30:36 +00:00
|
|
|
('unique_group', 'UNIQUE(group_id)', _('Portals must have distinct groups.'))
|
2011-03-25 14:42:27 +00:00
|
|
|
]
|
|
|
|
|
2011-04-04 11:30:36 +00:00
|
|
|
def copy(self, cr, uid, id, default={}, context=None):
|
|
|
|
""" override copy(): group_id and menu_action_id must be different """
|
|
|
|
# copy the former group_id
|
|
|
|
groups_obj = self.pool.get('res.groups')
|
|
|
|
group_id = self.browse(cr, uid, id, context).group_id.id
|
|
|
|
default['group_id'] = groups_obj.copy(cr, uid, group_id, {}, context)
|
|
|
|
default['menu_action_id'] = None
|
|
|
|
return super(portal, self).copy(cr, uid, id, default, context)
|
2011-03-25 11:12:06 +00:00
|
|
|
|
|
|
|
def create(self, cr, uid, values, context=None):
|
2011-04-01 07:40:39 +00:00
|
|
|
""" extend create() to assign the portal group and menu to users """
|
2011-03-30 12:40:07 +00:00
|
|
|
# first create the 'menu_action_id'
|
|
|
|
assert not values.get('menu_action_id')
|
2011-04-01 07:40:39 +00:00
|
|
|
values['menu_action_id'] = self._create_menu_action(cr, uid, values, context)
|
2011-03-30 12:40:07 +00:00
|
|
|
|
2011-04-04 11:30:36 +00:00
|
|
|
if 'user_ids' in values:
|
|
|
|
# set menu action of users
|
|
|
|
user_values = {'menu_id': values['menu_action_id']}
|
|
|
|
# values['user_ids'] should match [(6, 0, IDs)]
|
|
|
|
for id in get_many2many(values['user_ids']):
|
|
|
|
values['user_ids'].append((1, id, user_values))
|
2011-03-25 11:12:06 +00:00
|
|
|
|
|
|
|
return super(portal, self).create(cr, uid, values, context)
|
|
|
|
|
2011-04-05 08:54:09 +00:00
|
|
|
def name_get(self, cr, uid, ids, context=None):
|
|
|
|
portals = self.browse(cr, uid, ids, context)
|
|
|
|
return [(p.id, p.group_id.name) for p in portals]
|
|
|
|
|
|
|
|
def name_search(self, cr, uid, name='', args=None, operator='ilike', context=None, limit=100):
|
|
|
|
# first search for group names that match
|
|
|
|
groups_obj = self.pool.get('res.groups')
|
|
|
|
group_names = groups_obj.name_search(cr, uid, name, args, operator, context, limit)
|
|
|
|
# then search for portals that match the groups found so far
|
|
|
|
domain = [('group_id', 'in', [gn[0] for gn in group_names])]
|
|
|
|
ids = self.search(cr, uid, domain, context=context)
|
|
|
|
return self.name_get(cr, uid, ids, context)
|
|
|
|
|
2011-03-25 11:12:06 +00:00
|
|
|
def write(self, cr, uid, ids, values, context=None):
|
2011-03-28 12:20:29 +00:00
|
|
|
""" extend write() to reflect menu and groups changes on users """
|
|
|
|
# first apply portal changes
|
|
|
|
super(portal, self).write(cr, uid, ids, values, context)
|
2011-04-01 07:40:39 +00:00
|
|
|
portals = self.browse(cr, uid, ids, context)
|
2011-03-28 12:20:29 +00:00
|
|
|
|
2011-04-01 07:40:39 +00:00
|
|
|
# if 'menu_action_id' has changed, set menu_id on users
|
2011-03-30 12:40:07 +00:00
|
|
|
if 'menu_action_id' in values:
|
2011-04-01 07:40:39 +00:00
|
|
|
user_values = {'menu_id': values['menu_action_id']}
|
|
|
|
user_ids = [u.id for p in portals for u in p.user_ids]
|
2011-03-28 12:20:29 +00:00
|
|
|
self.pool.get('res.users').write(cr, uid, user_ids, user_values, context)
|
|
|
|
|
2011-04-01 07:40:39 +00:00
|
|
|
# if parent_menu_id has changed, apply the change on menu_action_id
|
|
|
|
if 'parent_menu_id' in values:
|
|
|
|
act_window_obj = self.pool.get('ir.actions.act_window')
|
|
|
|
action_ids = [p.menu_action_id.id for p in portals]
|
|
|
|
action_values = {'domain': [('parent_id', '=', values['parent_menu_id'])]}
|
|
|
|
act_window_obj.write(cr, uid, action_ids, action_values, context)
|
2011-03-25 11:12:06 +00:00
|
|
|
|
|
|
|
return True
|
2011-03-29 10:24:04 +00:00
|
|
|
|
2011-04-01 07:40:39 +00:00
|
|
|
def _create_menu_action(self, cr, uid, values, context=None):
|
2011-03-30 12:40:07 +00:00
|
|
|
# create a menu action that opens the menu items below parent_menu_id
|
2011-04-04 11:30:36 +00:00
|
|
|
groups_obj = self.pool.get('res.groups')
|
|
|
|
group_name = groups_obj.browse(cr, uid, values['group_id'], context).name
|
2011-03-30 12:40:07 +00:00
|
|
|
actions_obj = self.pool.get('ir.actions.act_window')
|
2011-04-01 07:40:39 +00:00
|
|
|
action_values = {
|
2011-04-04 11:30:36 +00:00
|
|
|
'name': group_name + ' Menu',
|
2011-03-30 12:40:07 +00:00
|
|
|
'type': 'ir.actions.act_window',
|
|
|
|
'usage': 'menu',
|
|
|
|
'res_model': 'ir.ui.menu',
|
|
|
|
'view_type': 'tree',
|
|
|
|
'view_id': self._res_xml_id(cr, uid, 'base', 'view_menu'),
|
2011-04-01 07:40:39 +00:00
|
|
|
'domain': [('parent_id', '=', values.get('parent_menu_id', False))],
|
2011-03-30 12:40:07 +00:00
|
|
|
}
|
2011-04-01 07:40:39 +00:00
|
|
|
return actions_obj.create(cr, uid, action_values, context)
|
2011-03-30 12:40:07 +00:00
|
|
|
|
2011-04-04 08:45:50 +00:00
|
|
|
def do_create_menu(self, cr, uid, ids, context=None):
|
|
|
|
""" create a parent menu for the given portals """
|
|
|
|
menu_obj = self.pool.get('ir.ui.menu')
|
|
|
|
menu_root = self._res_xml_id(cr, uid, 'portal', 'portal_menu')
|
2011-03-29 10:24:04 +00:00
|
|
|
|
2011-04-04 08:45:50 +00:00
|
|
|
for p in self.browse(cr, uid, ids, context):
|
|
|
|
# create a menuitem under 'portal.portal_menu'
|
|
|
|
menu_values = {
|
2011-04-04 11:30:36 +00:00
|
|
|
'name': p.group_id.name + ' Menu',
|
2011-04-04 08:45:50 +00:00
|
|
|
'parent_id': menu_root,
|
2011-04-04 09:05:35 +00:00
|
|
|
'groups_id': [(6, 0, [p.group_id.id])],
|
2011-04-04 08:45:50 +00:00
|
|
|
}
|
|
|
|
menu_id = menu_obj.create(cr, uid, menu_values, context)
|
|
|
|
# set the parent_menu_id to item_id
|
2011-04-04 09:05:35 +00:00
|
|
|
self.write(cr, uid, [p.id], {'parent_menu_id': menu_id}, context)
|
2011-03-29 10:24:04 +00:00
|
|
|
|
2011-04-04 08:45:50 +00:00
|
|
|
return True
|
2011-03-29 10:24:04 +00:00
|
|
|
|
2011-04-01 08:06:20 +00:00
|
|
|
def onchange_group(self, cr, uid, ids, group_id, context=None):
|
|
|
|
""" update the users list when the group changes """
|
|
|
|
user_ids = False
|
|
|
|
if group_id:
|
|
|
|
group = self.pool.get('res.groups').browse(cr, uid, group_id, context)
|
|
|
|
user_ids = [u.id for u in group.users]
|
|
|
|
return {
|
|
|
|
'value': {'user_ids': user_ids}
|
|
|
|
}
|
|
|
|
|
2011-03-30 07:43:31 +00:00
|
|
|
def _res_xml_id(self, cr, uid, module, xml_id):
|
2011-03-29 10:24:04 +00:00
|
|
|
""" return the resource id associated to the given xml_id """
|
2011-03-30 07:43:31 +00:00
|
|
|
data_obj = self.pool.get('ir.model.data')
|
|
|
|
data_id = data_obj._get_id(cr, uid, module, xml_id)
|
|
|
|
return data_obj.browse(cr, uid, data_id).res_id
|
2011-03-22 14:38:25 +00:00
|
|
|
|
2011-03-23 08:16:41 +00:00
|
|
|
portal()
|
|
|
|
|
2011-04-05 08:54:09 +00:00
|
|
|
|
|
|
|
|
2011-03-23 08:16:41 +00:00
|
|
|
class users(osv.osv):
|
|
|
|
_name = 'res.users'
|
|
|
|
_inherit = 'res.users'
|
2011-03-28 14:58:54 +00:00
|
|
|
|
|
|
|
def default_get(self, cr, uid, fields, context=None):
|
2011-04-04 08:14:03 +00:00
|
|
|
""" override default value of menu_id for portal users """
|
2011-03-28 14:58:54 +00:00
|
|
|
defs = super(users, self).default_get(cr, uid, fields, context)
|
2011-04-01 07:40:39 +00:00
|
|
|
|
|
|
|
# the value of 'menu_id' is passed in context by the portal form view
|
|
|
|
if ('menu_id' in context) and ('menu_id' in fields):
|
|
|
|
defs['menu_id'] = context['menu_id']
|
|
|
|
|
2011-03-28 14:58:54 +00:00
|
|
|
return defs
|
2011-03-23 08:16:41 +00:00
|
|
|
|
|
|
|
users()
|
|
|
|
|
2011-04-01 07:40:39 +00:00
|
|
|
|
|
|
|
|
2011-03-25 11:12:06 +00:00
|
|
|
# utils
|
|
|
|
def get_browse_id(obj):
|
|
|
|
""" return the id of a browse() object, or None """
|
|
|
|
return (obj and obj.id or None)
|
|
|
|
|
|
|
|
def get_browse_ids(objs):
|
|
|
|
""" return the ids of a list of browse() objects """
|
2011-03-28 12:20:29 +00:00
|
|
|
return map(get_browse_id, objs)
|
2011-03-25 11:12:06 +00:00
|
|
|
|
2011-03-28 14:58:54 +00:00
|
|
|
def get_many2many(arg):
|
|
|
|
""" get the list of ids from a many2many 'values' field """
|
|
|
|
assert len(arg) == 1 and arg[0][0] == 6 # arg = [(6, _, IDs)]
|
|
|
|
return arg[0][2]
|
|
|
|
|