remove portal name, and use the group name instead
[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
25 class portal(osv.osv):
26     _name = 'res.portal'
27     _description = 'Portal'
28     _rec_name = 'group_id'
29     _columns = {
30         'group_id': fields.many2one('res.groups', required=True,
31             string='Portal Group',
32             help=_('This group defines the users associated to this portal')),
33         'user_ids': fields.related('group_id', 'users',
34             type='many2many', relation='res.users', store=False,
35             string='Portal Users'),
36         'menu_action_id': fields.many2one('ir.actions.actions', readonly=True,
37             string='Menu Action',
38             help=_("What replaces the standard menu for the portal's users")),
39         'parent_menu_id': fields.many2one('ir.ui.menu',
40             string='Parent Menu',
41             help=_('The menu action opens the submenus of this menu item')),
42     }
43     _sql_constraints = [
44         ('unique_group', 'UNIQUE(group_id)', _('Portals must have distinct groups.'))
45     ]
46     
47     def copy(self, cr, uid, id, default={}, context=None):
48         """ override copy(): group_id and menu_action_id must be different """
49         # copy the former group_id
50         groups_obj = self.pool.get('res.groups')
51         group_id = self.browse(cr, uid, id, context).group_id.id
52         default['group_id'] = groups_obj.copy(cr, uid, group_id, {}, context)
53         default['menu_action_id'] = None
54         return super(portal, self).copy(cr, uid, id, default, context)
55     
56     def create(self, cr, uid, values, context=None):
57         """ extend create() to assign the portal group and menu to users """
58         # first create the 'menu_action_id'
59         assert not values.get('menu_action_id')
60         values['menu_action_id'] = self._create_menu_action(cr, uid, values, context)
61         
62         if 'user_ids' in values:
63             # set menu action of users
64             user_values = {'menu_id': values['menu_action_id']}
65             # values['user_ids'] should match [(6, 0, IDs)]
66             for id in get_many2many(values['user_ids']):
67                 values['user_ids'].append((1, id, user_values))
68         
69         return super(portal, self).create(cr, uid, values, context)
70     
71     def write(self, cr, uid, ids, values, context=None):
72         """ extend write() to reflect menu and groups changes on users """
73         # first apply portal changes
74         super(portal, self).write(cr, uid, ids, values, context)
75         portals = self.browse(cr, uid, ids, context)
76         
77         # if 'menu_action_id' has changed, set menu_id on users
78         if 'menu_action_id' in values:
79             user_values = {'menu_id': values['menu_action_id']}
80             user_ids = [u.id for p in portals for u in p.user_ids]
81             self.pool.get('res.users').write(cr, uid, user_ids, user_values, context)
82         
83         # if parent_menu_id has changed, apply the change on menu_action_id
84         if 'parent_menu_id' in values:
85             act_window_obj = self.pool.get('ir.actions.act_window')
86             action_ids = [p.menu_action_id.id for p in portals]
87             action_values = {'domain': [('parent_id', '=', values['parent_menu_id'])]}
88             act_window_obj.write(cr, uid, action_ids, action_values, context)
89         
90         return True
91     
92     def _create_menu_action(self, cr, uid, values, context=None):
93         # create a menu action that opens the menu items below parent_menu_id
94         groups_obj = self.pool.get('res.groups')
95         group_name = groups_obj.browse(cr, uid, values['group_id'], context).name
96         actions_obj = self.pool.get('ir.actions.act_window')
97         action_values = {
98             'name': group_name + ' Menu',
99             'type': 'ir.actions.act_window',
100             'usage': 'menu',
101             'res_model': 'ir.ui.menu',
102             'view_type': 'tree',
103             'view_id': self._res_xml_id(cr, uid, 'base', 'view_menu'),
104             'domain': [('parent_id', '=', values.get('parent_menu_id', False))],
105         }
106         return actions_obj.create(cr, uid, action_values, context)
107     
108     def do_create_menu(self, cr, uid, ids, context=None):
109         """ create a parent menu for the given portals """
110         menu_obj = self.pool.get('ir.ui.menu')
111         menu_root = self._res_xml_id(cr, uid, 'portal', 'portal_menu')
112         
113         for p in self.browse(cr, uid, ids, context):
114             # create a menuitem under 'portal.portal_menu'
115             menu_values = {
116                 'name': p.group_id.name + ' Menu',
117                 'parent_id': menu_root,
118                 'groups_id': [(6, 0, [p.group_id.id])],
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