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