~openerp-commiter/openobject-addons/trunk-extra-addons

« back to all changes in this revision

Viewing changes to portal/portal.py

  • Committer: bch
  • Date: 2007-06-08 14:37:39 UTC
  • Revision ID: bch-5fa00360dc58501dfb40471fce55f5c1f0923d8a
PORTAL: imporvements and bugfix.
* Several corrections due to the new version of the ir_rules.
* Smarter wizard giving more info to the user and providing more options.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
from osv import fields, osv
29
29
 
30
30
 
 
31
class portal_portal(osv.osv):
 
32
        _name = "portal.portal"
 
33
        _description = "Portal"
 
34
        _columns = {
 
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),
 
42
                }
 
43
 
 
44
        def create_action_menu(self,cr,uid,action_id,action_name,context):
 
45
                """
 
46
                Create default menu for the users of the portal.
 
47
                """
 
48
                menu_action_id = self.pool.get('ir.actions.act_window').create(cr,uid, { 
 
49
                        'name': action_name+ ' menu action',
 
50
                        'usage': 'menu',
 
51
                        'type':'ir.actions.act_window',
 
52
                        'res_model': "ir.ui.menu",
 
53
                        'domain': "[('parent_id','=',"+str(action_id)+")]",
 
54
                        'view_type': 'tree',
 
55
                        'view_id': self.pool.get('ir.model.data')._get_id(cr, uid, 'base', 'view_menu')
 
56
                        })
 
57
                return menu_action_id
 
58
 
 
59
        
 
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)
 
64
 
 
65
        def create_menu(self, cr, uid,portal_id, portal_model_id, menu_name, action_id,parent_menu_id=None,context=None):
 
66
                """
 
67
                Create a menuitem for the given portal and model whith the given name and action.
 
68
                """
 
69
                
 
70
                assert portal_id and portal_model_id and menu_name and action_id, "Create_menu does not accept null values"
 
71
                
 
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)
 
75
                ## Create the menu:
 
76
                menu_id=self.pool.get('ir.ui.menu').create(cr, uid, {
 
77
                        'name': menu_name,
 
78
                        'parent_id': parent_menu_id or portal.menu_id.id,
 
79
                        'icon': 'STOCK_NEW'
 
80
                        })
 
81
                available_view={}
 
82
                for view in model.view_ids:
 
83
                        available_view[view.type]= view.id
 
84
                vids = []
 
85
                i = 0
 
86
                ## Fetch the views:
 
87
                for view in self.pool.get('ir.actions.act_window').browse(cr,uid,action_id).views:
 
88
                        vids.append((0,0, {
 
89
                                'sequence':i,
 
90
                                'view_id': available_view.get(view[1], view[0]),
 
91
                                'view_mode': view[1]
 
92
                        }))
 
93
                        i+=1
 
94
 
 
95
                ## Duplicate the action and give the fetched views to the new one:
 
96
                action_id = action.copy(cr,uid, action.id,{
 
97
                        'name': menu_name,
 
98
                        'view_ids': vids
 
99
                        })
 
100
 
 
101
 
 
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',
 
107
                        'res_id': menu_id,
 
108
                        'value': 'ir.actions.act_window,%d'%action_id,
 
109
                        'object': True
 
110
                        })
 
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)]})
 
113
                return action_id
 
114
        
 
115
portal_portal()
 
116
 
 
117
        
31
118
class portal_model(osv.osv):
32
119
        _name = "portal.model"
33
120
        _description = "Portal Model"
34
121
        _columns = {
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),
40
126
                }
41
127
portal_model()
42
128
 
43
 
class portal_factory(osv.osv):
44
 
        """
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.
47
 
        
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).
52
 
 
53
 
        - restricted views because we don't want the customer to see all
54
 
      the details.
55
 
        
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.
58
 
        """
59
 
        
60
 
        _name = "portal.factory"
61
 
        _description = "Portal Factory"
62
 
        _columns = {
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)]}),
71
 
 
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)]}),
75
 
                
76
 
        }
77
 
 
78
 
        _defaults={
79
 
                'state': lambda *a: 'disabled',
80
 
                }
81
 
 
82
 
        def enable(self,cr,uid,ids,context):
83
 
                factories= self.pool.get('portal.factory').browse(cr,uid,ids)
84
 
                for f in factories:
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.')
88
 
 
89
 
                        menu_id=self.pool.get('ir.ui.menu').create(cr, uid, {
90
 
                                'name': f.menu_name,
91
 
                                'parent_id': f.parentmenu_id.id,
92
 
                                'icon': 'STOCK_NEW'
93
 
                                })
94
 
                        
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,
99
 
                                'view_type': 'form',
100
 
                                'view_mode': f.template_action_id.view_mode,
101
 
                                'view_id': f.view_id.id,
102
 
                                })
103
 
 
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',
108
 
                                'res_id': menu_id,
109
 
                                'value': 'ir.actions.act_window,%d'%action_id,
110
 
                                'object': True
111
 
                                })
112
 
 
113
 
                        if f.group_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})
116
 
 
117
 
                return self.write(cr,uid,ids,{'state':'enabled',"created_menu_id": menu_id,"created_action_id": action_id,"created_value_id": value_id})
118
 
 
119
 
        def disable(self,cr,uid,ids,context):
120
 
                factories= self.pool.get('portal.factory').browse(cr,uid,ids)
121
 
                for f in factories:
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])
125
 
                        if f.group_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',})
129
 
                
130
 
portal_factory()
 
129