override create() and write() to assign menu and groups to users

bzr revid: rco@openerp.com-20110325111206-q9fy02xw4yep25rd
This commit is contained in:
Raphael Collet 2011-03-25 12:12:06 +01:00
parent bccc450f54
commit 3a791662fe
2 changed files with 48 additions and 3 deletions

View File

@ -26,8 +26,8 @@ class portal(osv.osv):
_description = 'Portal'
_columns = {
'name': fields.char(string='Name', size=64, required=True),
'menu_id': fields.many2one('ir.ui.menu',
string='Portal Menu',
'menu_id': fields.many2one('ir.actions.actions', required="True",
string='Menu Action',
help="The customized menu of the portal's users"),
'user_ids': fields.one2many('res.users', 'portal_id',
string='Users',
@ -36,6 +36,42 @@ class portal(osv.osv):
string='Groups',
help='Users of this portal automatically belong to those groups'),
}
def create(self, cr, uid, values, context=None):
""" extend create() to assign the portal menu and groups to users """
# as 'user_ids' is a many2one relation, values['user_ids'] must be a
# list of tuples of the form (0, 0, {values})
for op, _, user_values in values['user_ids']:
assert op == 0
user_values['menu_id'] = values['menu_id']
user_values['groups_id'] = values['group_ids']
return super(portal, self).create(cr, uid, values, context)
def write(self, cr, uid, ids, values, context=None):
""" extend write() to assign the portal menu and groups to users """
user_object = self.pool.get('res.users')
# first apply changes on the portals themselves
super(portal, self).write(cr, uid, ids, values, context)
# then reflect changes on the users of each portal
#
# PERFORMANCE NOTE. The loop below performs N write() operations, where
# N=len(ids). This may seem inefficient, but in practice it is not,
# because: (1) N is pretty small (typically N=1), and (2) it is too
# complex (read: bug-prone) to write those updates as a single batch.
#
plist = self.browse(cr, uid, ids, context)
for p in plist:
user_ids = get_browse_ids(p.user_ids)
user_values = {
'menu_id': get_browse_id(p.menu_id),
'groups_id': [(6, 0, get_browse_ids(p.group_ids))],
}
user_object.write(cr, uid, user_ids, user_values, context)
return True
portal()
@ -49,3 +85,12 @@ class users(osv.osv):
users()
# 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 """
return [(obj and obj.id or default) for obj in objs]

View File

@ -23,7 +23,7 @@
<field name="arch" type="xml">
<form string="Portal Form">
<field name="name" colspan="4"/>
<field name="menu_id" colspan="2"/>
<field name="menu_id" domain="[('usage','=','menu')]" colspan="2"/>
<separator string="Users and groups" colspan="4"/>
<field name="user_ids" nolabel="1" colspan="2">
<tree string="Users">