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