28
28
from osv import fields, osv
31
class portal_portal(osv.osv):
32
_name = "portal.portal"
33
_description = "Portal"
35
'name': fields.char('Portal Name',size=64,required=True),
36
'group_id': fields.many2one('res.groups', 'Associated Group',required=True),
37
'menu_id': fields.many2one('ir.ui.menu','Main Menu', required=True),
38
'menu_action_id': fields.many2one('ir.actions.act_window', 'User Menu Action', readonly=True,
39
help='''Default main menu for the users of the portal. This field is auto-completed at creation. '''),
40
'home_action_id': fields.many2one('ir.actions.act_window', 'User Home Action', help='Complete this field to provide a Home menu different from the Main menu.'),
41
'company_id': fields.many2one('res.company', 'Company', required=True),
44
def create_action_menu(self,cr,uid,action_id,action_name,context):
46
Create default menu for the users of the portal.
48
menu_action_id = self.pool.get('ir.actions.act_window').create(cr,uid, {
49
'name': action_name+ ' menu action',
51
'type':'ir.actions.act_window',
52
'res_model': "ir.ui.menu",
53
'domain': "[('parent_id','=',"+str(action_id)+")]",
55
'view_id': self.pool.get('ir.model.data')._get_id(cr, uid, 'base', 'view_menu')
60
def create(self, cr, user, vals, context={}):
61
if not vals.get('menu_action_id') :
62
vals['menu_action_id']= self.create_action_menu(cr,user,vals['menu_id'], vals['name'],context)
63
return super(portal_portal, self).create(cr, user, vals, context)
65
def create_menu(self, cr, uid,portal_id, portal_model_id, menu_name, action_id,parent_menu_id=None,context=None):
67
Create a menuitem for the given portal and model whith the given name and action.
70
assert portal_id and portal_model_id and menu_name and action_id, "Create_menu does not accept null values"
72
portal= self.pool.get('portal.portal').browse(cr,uid,portal_id)
73
model= self.pool.get('portal.model').browse(cr,uid,portal_model_id)
74
action= self.pool.get('ir.actions.act_window').browse(cr,uid,action_id)
76
menu_id=self.pool.get('ir.ui.menu').create(cr, uid, {
78
'parent_id': parent_menu_id or portal.menu_id.id,
82
for view in model.view_ids:
83
available_view[view.type]= view.id
87
for view in self.pool.get('ir.actions.act_window').browse(cr,uid,action_id).views:
90
'view_id': available_view.get(view[1], view[0]),
95
## Duplicate the action and give the fetched views to the new one:
96
action_id = action.copy(cr,uid, action.id,{
102
## Create the values:
103
value_id = self.pool.get('ir.values').create(cr, uid, {
104
'name': 'AutoGenerated by portal module',
105
'key2': 'tree_but_open',
106
'model': 'ir.ui.menu',
108
'value': 'ir.actions.act_window,%d'%action_id,
111
## add the rule_group to the user
112
portal.group_id.write(cr,uid,[portal.group_id.id],{'rule_groups': [(4,model.rule_group_id.id)]})
31
118
class portal_model(osv.osv):
32
119
_name = "portal.model"
33
120
_description = "Portal Model"
35
122
'name': fields.char('Name',size=64,),
36
123
'model_id': fields.many2one('ir.model','Model',required=True),
37
'view_id': fields.many2one('ir.ui.view','View',required=True),
38
'rule_id': fields.many2one('ir.rule','Rule', help="Rule uses to restrict acces to the associated document.", required=True),
39
'access_id': fields.many2one('ir.model.access','Model access', required=True),
124
'view_ids': fields.many2many('ir.ui.view','portal_model_view_rel','model_id','view_id','Views'),
125
'rule_group_id': fields.many2one('ir.rule.group','Rule group', required=True),
43
class portal_factory(osv.osv):
45
Object associating a model to several restricted views (at least
46
one tree or one form), to a menuotem and to a access rule.
48
The resulting table will contain all the available models for a
49
portal. When a user want to add a menu item in the portal an
50
'activate' button enable the model with his setup : menu, view and
51
access (Some combinaison way not be meaningful).
53
- restricted views because we don't want the customer to see all
56
- the access controls will prevent the user from editing stuff and
57
the ir_rules will prevent them to see data of other customers.
60
_name = "portal.factory"
61
_description = "Portal Factory"
63
'name': fields.char('Name',size=64, required=True,states={'enabled':[('readonly',True)]}),
64
'pmodel_id': fields.many2one('portal.model','Portal Model',required=True,states={'enabled':[('readonly',True)]}),
65
'view_id': fields.many2one('ir.ui.view','View',states={'enabled':[('readonly',True)]}),
66
'menu_name': fields.char('Menu Name',size=64, required=True,states={'enabled':[('readonly',True)]}),
67
'parentmenu_id': fields.many2one('ir.ui.menu','Parent Menu',states={'enabled':[('readonly',True)]}, required=True),
68
'template_action_id': fields.many2one('ir.actions.act_window','Action',states={'enabled':[('readonly',True)]}, required=True),
69
'state': fields.selection([('enabled','Enabled'),('disabled','Disabled')],'State', readonly=True),
70
'group_id': fields.many2one('res.groups', 'Group',states={'enabled':[('readonly',True)]}),
72
'created_menu_id': fields.many2one('ir.ui.menu','Created Menu', readonly=True),
73
'created_action_id': fields.many2one('ir.actions.act_window','Action',states={'enabled':[('readonly',True)]}),
74
'created_value_id': fields.many2one('ir.values','Value',states={'enabled':[('readonly',True)]}),
79
'state': lambda *a: 'disabled',
82
def enable(self,cr,uid,ids,context):
83
factories= self.pool.get('portal.factory').browse(cr,uid,ids)
85
m_id = self.pool.get('ir.model').search(cr,uid,[('model','=',f.template_action_id.res_model)])
86
if f.pmodel_id.model_id.id not in m_id:
87
raise osv.except_osv('Error','Model type mismatch: The choosen action and model are not compatible.')
89
menu_id=self.pool.get('ir.ui.menu').create(cr, uid, {
91
'parent_id': f.parentmenu_id.id,
95
action_id = self.pool.get('ir.actions.act_window').create(cr,uid, {
96
'name': f.template_action_id.name,
97
'res_model': f.pmodel_id.model_id.model,
98
'domain': f.template_action_id.domain,
100
'view_mode': f.template_action_id.view_mode,
101
'view_id': f.view_id.id,
104
value_id = self.pool.get('ir.values').create(cr, uid, {
105
'name': 'AutoGenerated by portal module',
106
'key2': 'tree_but_open',
107
'model': 'ir.ui.menu',
109
'value': 'ir.actions.act_window,%d'%action_id,
114
f.group_id.write(cr,uid,[f.group_id.id],{'rule_ids': [(4,f.pmodel_id.rule_id.id)]})
115
f.pmodel_id.access_id.write(cr,uid,[f.pmodel_id.access_id.id],{'group_id': f.group_id.id})
117
return self.write(cr,uid,ids,{'state':'enabled',"created_menu_id": menu_id,"created_action_id": action_id,"created_value_id": value_id})
119
def disable(self,cr,uid,ids,context):
120
factories= self.pool.get('portal.factory').browse(cr,uid,ids)
122
self.pool.get('ir.ui.menu').unlink(cr,uid,[f.created_menu_id])
123
self.pool.get('ir.actions.act_window').unlink(cr,uid,[f.created_action_id])
124
self.pool.get('ir.values').unlink(cr,uid,[f.created_value_id])
126
f.group_id.write(cr,uid,[f.group_id.id],{'rule_ids': [(3,f.pmodel_id.rule_id.id)]})
127
f.pmodel_id.access_id.write(cr,uid,[f.pmodel_id.access_id.id],{'group_id': False})
128
return self.write(cr,uid,ids,{'state':'disabled',})