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