rename and improve create_parent_menu()
[odoo/odoo.git] / addons / portal / portal.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2011 Tiny SPRL (<http://tiny.be>).
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 ##############################################################################
21
22 from osv import osv, fields
23 from tools.translate import _
24 import random
25
26 class portal(osv.osv):
27     _name = 'res.portal'
28     _description = 'Portal'
29     _columns = {
30         'name': fields.char(string='Name', size=64, required=True),
31         'group_id': fields.many2one('res.groups', required=True,
32             string='Portal Group',
33             help=_('This group defines the users associated to this portal')),
34         'user_ids': fields.related('group_id', 'users',
35             type='many2many', relation='res.users', store=False,
36             string='Portal Users'),
37         'menu_action_id': fields.many2one('ir.actions.actions', readonly=True,
38             string='Menu Action',
39             help=_("What replaces the standard menu for the portal's users")),
40         'parent_menu_id': fields.many2one('ir.ui.menu',
41             string='Parent Menu',
42             help=_('The menu action opens the submenus of this menu item')),
43     }
44     _sql_constraints = [
45         ('unique_name', 'UNIQUE(name)', _('Portals must have different names.'))
46     ]
47     
48     def copy(self, cr, uid, id, defaults, context=None):
49         """ override copy(): pick a different name and menu_action_id """
50         # find an unused name of the form "name [N]" for some random N
51         old_name = self.browse(cr, uid, id, context).name
52         new_name = copy_random(old_name)
53         while self.search(cr, uid, [('name', '=', new_name)], limit=1, context=context):
54             new_name = copy_random(old_name)
55         
56         defaults['name'] = new_name
57         defaults['menu_action_id'] = None
58         return super(portal, self).copy(cr, uid, id, defaults, context)
59     
60     def create(self, cr, uid, values, context=None):
61         """ extend create() to assign the portal group and menu to users """
62         # first create the 'menu_action_id'
63         assert not values.get('menu_action_id')
64         values['menu_action_id'] = self._create_menu_action(cr, uid, values, context)
65         
66         # set menu action of users
67         user_values = {'menu_id': values['menu_action_id']}
68         # values['user_ids'] should match [(6, 0, IDs)]
69         for id in get_many2many(values['user_ids']):
70             values['user_ids'].append((1, id, user_values))
71         
72         return super(portal, self).create(cr, uid, values, context)
73     
74     def write(self, cr, uid, ids, values, context=None):
75         """ extend write() to reflect menu and groups changes on users """
76         # first apply portal changes
77         super(portal, self).write(cr, uid, ids, values, context)
78         portals = self.browse(cr, uid, ids, context)
79         
80         # if 'menu_action_id' has changed, set menu_id on users
81         if 'menu_action_id' in values:
82             user_values = {'menu_id': values['menu_action_id']}
83             user_ids = [u.id for p in portals for u in p.user_ids]
84             self.pool.get('res.users').write(cr, uid, user_ids, user_values, context)
85         
86         # if parent_menu_id has changed, apply the change on menu_action_id
87         if 'parent_menu_id' in values:
88             act_window_obj = self.pool.get('ir.actions.act_window')
89             action_ids = [p.menu_action_id.id for p in portals]
90             action_values = {'domain': [('parent_id', '=', values['parent_menu_id'])]}
91             act_window_obj.write(cr, uid, action_ids, action_values, context)
92         
93         return True
94     
95     def _create_menu_action(self, cr, uid, values, context=None):
96         # create a menu action that opens the menu items below parent_menu_id
97         actions_obj = self.pool.get('ir.actions.act_window')
98         action_values = {
99             'name': values['name'] + ' Menu',
100             'type': 'ir.actions.act_window',
101             'usage': 'menu',
102             'res_model': 'ir.ui.menu',
103             'view_type': 'tree',
104             'view_id': self._res_xml_id(cr, uid, 'base', 'view_menu'),
105             'domain': [('parent_id', '=', values.get('parent_menu_id', False))],
106         }
107         return actions_obj.create(cr, uid, action_values, context)
108     
109     def do_create_menu(self, cr, uid, ids, context=None):
110         """ create a parent menu for the given portals """
111         menu_obj = self.pool.get('ir.ui.menu')
112         menu_root = self._res_xml_id(cr, uid, 'portal', 'portal_menu')
113         
114         for p in self.browse(cr, uid, ids, context):
115             # create a menuitem under 'portal.portal_menu'
116             menu_values = {
117                 'name': p.name + ' Menu',
118                 'parent_id': menu_root,
119             }
120             menu_id = menu_obj.create(cr, uid, menu_values, context)
121             # set the parent_menu_id to item_id
122             self.write(cr, uid, p.id, {'parent_menu_id': menu_id}, context)
123         
124         return True
125     
126     def onchange_group(self, cr, uid, ids, group_id, context=None):
127         """ update the users list when the group changes """
128         user_ids = False
129         if group_id:
130             group = self.pool.get('res.groups').browse(cr, uid, group_id, context)
131             user_ids = [u.id for u in group.users]
132         return {
133             'value': {'user_ids': user_ids}
134         }
135     
136     def _res_xml_id(self, cr, uid, module, xml_id):
137         """ return the resource id associated to the given xml_id """
138         data_obj = self.pool.get('ir.model.data')
139         data_id = data_obj._get_id(cr, uid, module, xml_id)
140         return data_obj.browse(cr, uid, data_id).res_id
141
142 portal()
143
144 class users(osv.osv):
145     _name = 'res.users'
146     _inherit = 'res.users'
147     
148     def default_get(self, cr, uid, fields, context=None):
149         """ override default value of menu_id for portal users """
150         defs = super(users, self).default_get(cr, uid, fields, context)
151         
152         # the value of 'menu_id' is passed in context by the portal form view
153         if ('menu_id' in context) and ('menu_id' in fields):
154             defs['menu_id'] = context['menu_id']
155         
156         return defs
157
158 users()
159
160
161
162 # utils
163 def get_browse_id(obj):
164     """ return the id of a browse() object, or None """
165     return (obj and obj.id or None)
166
167 def get_browse_ids(objs):
168     """ return the ids of a list of browse() objects """
169     return map(get_browse_id, objs)
170
171 def get_many2many(arg):
172     """ get the list of ids from a many2many 'values' field """
173     assert len(arg) == 1 and arg[0][0] == 6             # arg = [(6, _, IDs)]
174     return arg[0][2]
175
176 def copy_random(name):
177     """ return "name [N]" for some random integer N """
178     return "%s [%s]" % (name, random.choice(xrange(1000000)))
179