[IMP] introduce i18n
[odoo/odoo.git] / addons / portal / portal.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2011 OpenERP S.A (<http://www.openerp.com>).
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
26
27 class portal(osv.osv):
28     """
29         A portal is a group of users with specific menu, widgets, and typically
30         restricted access rights.
31     """
32     _name = 'res.portal'
33     _description = 'Portal'
34     _inherits = {'res.groups': 'group_id'}
35     
36     _columns = {
37         'group_id': fields.many2one('res.groups', required=True, ondelete='cascade',
38             string='Group',
39             help='The group extended by this portal'),
40         'other_group_ids': fields.many2many('res.groups',
41             'portal_group_rel', 'portal_id', 'group_id',
42             string='Other User Groups',
43             help="Those groups are assigned to the portal's users"),
44         'url': fields.char('URL', size=64,
45             help="The url where portal users can connect to the server"),
46         'menu_action_id': fields.many2one('ir.actions.act_window', readonly=True,
47             # ISSUE: 'ondelete' constraints do not seem effective on this field...
48             string='Menu Action',
49             help="If set, replaces the standard menu for the portal's users"),
50         'parent_menu_id': fields.many2one('ir.ui.menu', ondelete='restrict',
51             string='Parent Menu',
52             help='The menu action opens the submenus of this menu item'),
53         'widget_ids': fields.one2many('res.portal.widget', 'portal_id',
54             string='Widgets',
55             help='Widgets assigned to portal users'),
56     }
57     
58     def copy(self, cr, uid, id, values, context=None):
59         """ override copy(): menu_action_id must be different """
60         values['menu_action_id'] = None
61         return super(portal, self).copy(cr, uid, id, values, context)
62     
63     def create(self, cr, uid, values, context=None):
64         """ extend create() to assign the portal menu to users """
65         if context is None:
66             context = {}
67         
68         # create portal (admin should not be included)
69         context['noadmin'] = True
70         portal_id = super(portal, self).create(cr, uid, values, context)
71         
72         # assign menu action and widgets to users
73         if values.get('users') or values.get('other_group_ids') or values.get('menu_action_id'):
74             self._assign_menu_and_groups(cr, uid, [portal_id], context)
75         if values.get('users') or values.get('widget_ids'):
76             self._assign_widgets(cr, uid, [portal_id], context)
77         
78         return portal_id
79     
80     def write(self, cr, uid, ids, values, context=None):
81         """ extend write() to reflect changes on users """
82         # first apply portal changes
83         super(portal, self).write(cr, uid, ids, values, context)
84         
85         # assign menu action and widgets to users
86         if values.get('users') or values.get('other_group_ids') or values.get('menu_action_id'):
87             self._assign_menu_and_groups(cr, uid, ids, context)
88         if values.get('users') or values.get('widget_ids'):
89             self._assign_widgets(cr, uid, ids, context)
90         
91         # if parent_menu_id has changed, apply the change on menu_action_id
92         if 'parent_menu_id' in values:
93             act_window_obj = self.pool.get('ir.actions.act_window')
94             portals = self.browse(cr, uid, ids, context)
95             action_ids = [p.menu_action_id.id for p in portals if p.menu_action_id]
96             if action_ids:
97                 action_values = {'domain': [('parent_id', '=', values['parent_menu_id'])]}
98                 act_window_obj.write(cr, uid, action_ids, action_values, context)
99         
100         return True
101     
102     def do_create_menu(self, cr, uid, ids, context=None):
103         """ create a parent menu for the given portals """
104         menu_obj = self.pool.get('ir.ui.menu')
105         menu_root = self._res_xml_id(cr, uid, 'portal', 'portal_menu')
106         
107         for p in self.browse(cr, uid, ids, context):
108             # create a menuitem under 'portal.portal_menu'
109             menu_values = {
110                 'name': _('%s Menu') % p.name,
111                 'parent_id': menu_root,
112                 'groups_id': [(6, 0, [p.group_id.id])],
113             }
114             menu_id = menu_obj.create(cr, uid, menu_values, context)
115             # set the parent_menu_id to item_id
116             self.write(cr, uid, [p.id], {'parent_menu_id': menu_id}, context)
117         
118         return True
119
120     def _assign_menu_and_groups(self, cr, uid, ids, context=None):
121         """ assign portal menu and other groups to users of portals (ids) """
122         user_obj = self.pool.get('res.users')
123         for p in self.browse(cr, uid, ids, context):
124             # user groups = portal group + other groups
125             group_ids = [p.group_id.id] + [g.id for g in p.other_group_ids]
126             user_values = {'groups_id': [(6, 0, group_ids)]}
127             # user menu action = portal menu action if set in portal
128             if p.menu_action_id:
129                 user_values['menu_id'] = p.menu_action_id.id
130             user_ids = [u.id for u in p.users if u.id != 1]
131             user_obj.write(cr, uid, user_ids, user_values, context)
132
133     def _assign_widgets(self, cr, uid, ids, context=None):
134         """ assign portal widgets to users of portals (ids) """
135         widget_user_obj = self.pool.get('res.widget.user')
136         for p in self.browse(cr, uid, ids, context):
137             for w in p.widget_ids:
138                 values = {'sequence': w.sequence, 'widget_id': w.widget_id.id}
139                 for u in p.users:
140                     if u.id == 1: continue
141                     values['user_id'] = u.id
142                     widget_user_obj.create(cr, uid, values, context)
143
144     def _res_xml_id(self, cr, uid, module, xml_id):
145         """ return the resource id associated to the given xml_id """
146         data_obj = self.pool.get('ir.model.data')
147         data_id = data_obj._get_id(cr, uid, module, xml_id)
148         return data_obj.browse(cr, uid, data_id).res_id
149
150 portal()
151
152
153
154 class portal_override_menu(osv.osv):
155     """
156         extend res.portal with a boolean field 'Override Users Menu', that
157         triggers the creation or removal of menu_action_id
158     """
159     _name = 'res.portal'
160     _inherit = 'res.portal'
161     
162     def _get_override_menu(self, cr, uid, ids, field_name, arg, context=None):
163         assert field_name == 'override_menu'
164         result = {}
165         for p in self.browse(cr, uid, ids, context):
166             result[p.id] = bool(p.menu_action_id)
167         return result
168     
169     def _set_override_menu(self, cr, uid, id, field_name, field_value, arg, context=None):
170         assert field_name == 'override_menu'
171         if field_value:
172             self.create_menu_action(cr, uid, id, context)
173         else:
174             self.write(cr, uid, [id], {'menu_action_id': False}, context)
175     
176     def create_menu_action(self, cr, uid, id, context=None):
177         """ create, if necessary, a menu action that opens the menu items below
178             parent_menu_id """
179         p = self.browse(cr, uid, id, context)
180         if not p.menu_action_id:
181             actions_obj = self.pool.get('ir.actions.act_window')
182             parent_id = p.parent_menu_id.id if p.parent_menu_id else False
183             action_values = {
184                 'name': _('%s Menu') % p.name,
185                 'type': 'ir.actions.act_window',
186                 'usage': 'menu',
187                 'res_model': 'ir.ui.menu',
188                 'view_type': 'tree',
189                 'view_id': self._res_xml_id(cr, uid, 'base', 'view_menu'),
190                 'domain': [('parent_id', '=', parent_id)],
191             }
192             action_id = actions_obj.create(cr, uid, action_values, context)
193             self.write(cr, uid, [id], {'menu_action_id': action_id}, context)
194     
195     _columns = {
196         'override_menu': fields.function(
197             _get_override_menu, fnct_inv=_set_override_menu,
198             type='boolean', string='Override Menu Action of Users',
199             help='Enable this option to override the Menu Action of portal users'),
200     }
201
202 portal_override_menu()
203
204
205
206 class portal_widget(osv.osv):
207     """
208         Similar to res.widget.user (res_widget.py), but with a portal instead.
209         New users in a portal are assigned the portal's widgets.
210     """
211     _name='res.portal.widget'
212     _description = 'Portal Widgets'
213     _order = 'sequence'
214     _columns = {
215         'sequence': fields.integer('Sequence'),
216         'portal_id': fields.many2one('res.portal', select=1, ondelete='cascade',
217             string='Portal'),
218         'widget_id': fields.many2one('res.widget', required=True, ondelete='cascade',
219             string='Widget'),
220     }
221
222     def create(self, cr, uid, values, context=None):
223         domain = [('portal_id', '=', values.get('portal_id')),
224                   ('widget_id', '=', values.get('widget_id'))]
225         existing = self.search(cr, uid, domain, context=context)
226         if existing:
227             res = existing[0]
228         else:
229             res = super(portal_widget, self).create(cr, uid, values, context=context)
230         return res
231
232 portal_widget()
233
234
235