[IMP] attach the 'Our company' menu items to the portal's root menu
[odoo/odoo.git] / addons / portal / portal.py
index 3b1448f..399377e 100644 (file)
@@ -2,7 +2,7 @@
 ##############################################################################
 #
 #    OpenERP, Open Source Management Solution
-#    Copyright (C) 2004-2011 Tiny SPRL (<http://tiny.be>).
+#    Copyright (C) 2004-2011 OpenERP S.A (<http://www.openerp.com>).
 #
 #    This program is free software: you can redistribute it and/or modify
 #    it under the terms of the GNU Affero General Public License as
 ##############################################################################
 
 from osv import osv, fields
+from tools.translate import _
+
+
 
 class portal(osv.osv):
+    """
+        A portal is a group of users with specific menu, widgets, and typically
+        restricted access rights.
+    """
     _name = 'res.portal'
     _description = 'Portal'
+    _inherits = {'res.groups': 'group_id'}
+    
     _columns = {
-        'name': fields.char(string='Name', size=64, required=True),
-        'menu_id': fields.many2one('ir.actions.actions', required="True",
+        'group_id': fields.many2one('res.groups', required=True, ondelete='cascade',
+            string='Group',
+            help='The group corresponding to this portal'),
+        'url': fields.char('URL', size=64,
+            help="The url where portal users can connect to the server"),
+        'home_action_id': fields.many2one('ir.actions.actions',
+            string='Home Action',
+            help="if set, replaces the standard home action (first screen after loggin) for the portal's users"),
+        'menu_action_id': fields.many2one('ir.actions.act_window', readonly=True,
+            # ISSUE: 'ondelete' constraints do not seem effective on this field...
             string='Menu Action',
-            help="The customized menu of the portal's users"),
-        'user_ids': fields.one2many('res.users', 'portal_id',
-            string='Users',
-            help='Gives the set of users associated to this portal'),
-        'group_ids': fields.many2many('res.groups', 'res_portals_groups_rel', 'pid', 'gid',
-            string='Groups',
-            help='Users of this portal automatically belong to those groups'),
+            help="If set, replaces the standard menu for the portal's users"),
+        'parent_menu_id': fields.many2one('ir.ui.menu', ondelete='restrict',
+            string='Parent Menu',
+            help='The menu action opens the submenus of this menu item'),
+        'widget_ids': fields.one2many('res.portal.widget', 'portal_id',
+            string='Widgets',
+            help='Widgets assigned to portal users'),
     }
     
+    def copy(self, cr, uid, id, values, context=None):
+        """ override copy(): menu_action_id must be different """
+        values['menu_action_id'] = None
+        return super(portal, self).copy(cr, uid, id, values, context)
+    
     def create(self, cr, uid, values, context=None):
-        """ extend create() to assign the portal menu and groups to users """
-        # as 'user_ids' is a many2one relation, values['user_ids'] must be a
-        # list of tuples of the form (0, 0, {values})
-        for op, _, user_values in values['user_ids']:
-            assert op == 0
-            user_values['menu_id'] = values['menu_id']
-            user_values['groups_id'] = values['group_ids']
+        """ extend create() to assign the portal menu to users """
+        if context is None:
+            context = {}
         
-        return super(portal, self).create(cr, uid, values, context)
+        # create portal (admin should not be included)
+        context['noadmin'] = True
+        portal_id = super(portal, self).create(cr, uid, values, context)
+        
+        # assign menu action and widgets to users
+        if values.get('users') or values.get('menu_action_id'):
+            self._assign_menu(cr, uid, [portal_id], context)
+        if values.get('users') or values.get('widget_ids'):
+            self._assign_widgets(cr, uid, [portal_id], context)
+        
+        return portal_id
     
     def write(self, cr, uid, ids, values, context=None):
-        """ extend write() to assign the portal menu and groups to users """
-        user_object = self.pool.get('res.users')
-        
-        # first apply changes on the portals themselves
+        """ extend write() to reflect changes on users """
+        # first apply portal changes
         super(portal, self).write(cr, uid, ids, values, context)
         
-        # then reflect changes on the users of each portal
-        #
-        # PERFORMANCE NOTE.  The loop below performs N write() operations, where
-        # N=len(ids).  This may seem inefficient, but in practice it is not,
-        # because: (1) N is pretty small (typically N=1), and (2) it is too
-        # complex (read: bug-prone) to write those updates as a single batch.
-        #
-        plist = self.browse(cr, uid, ids, context)
-        for p in plist:
-            user_ids  = get_browse_ids(p.user_ids)
-            user_values = {
-                'menu_id': get_browse_id(p.menu_id),
-                'groups_id': [(6, 0, get_browse_ids(p.group_ids))],
-            }
-            user_object.write(cr, uid, user_ids, user_values, context)
+        # assign menu action and widgets to users
+        if values.get('users') or values.get('menu_action_id'):
+            self._assign_menu(cr, uid, ids, context)
+        if values.get('users') or values.get('widget_ids'):
+            self._assign_widgets(cr, uid, ids, context)
+        
+        # if parent_menu_id has changed, apply the change on menu_action_id
+        if 'parent_menu_id' in values:
+            act_window_obj = self.pool.get('ir.actions.act_window')
+            portals = self.browse(cr, uid, ids, context)
+            action_ids = [p.menu_action_id.id for p in portals if p.menu_action_id]
+            if action_ids:
+                action_values = {'domain': [('parent_id', '=', values['parent_menu_id'])]}
+                act_window_obj.write(cr, uid, action_ids, action_values, context)
         
         return True
 
+    def _assign_menu(self, cr, uid, ids, context=None):
+        """ assign portal_menu_settings to users of portals (ids) """
+        user_obj = self.pool.get('res.users')
+        for p in self.browse(cr, uid, ids, context):
+            # user menu action = portal menu action if set in portal
+            if p.menu_action_id:
+                user_ids = [u.id for u in p.users if u.id != 1]
+                user_values = {'menu_id': p.menu_action_id.id}
+                user_obj.write(cr, uid, user_ids, user_values, context)
+
+    def _assign_widgets(self, cr, uid, ids, context=None):
+        """ assign portal widgets to users of portals (ids) """
+        widget_user_obj = self.pool.get('res.widget.user')
+        for p in self.browse(cr, uid, ids, context):
+            for w in p.widget_ids:
+                values = {'sequence': w.sequence, 'widget_id': w.widget_id.id}
+                for u in p.users:
+                    if u.id == 1: continue
+                    values['user_id'] = u.id
+                    widget_user_obj.create(cr, uid, values, context)
+
+    def _res_xml_id(self, cr, uid, module, xml_id):
+        """ return the resource id associated to the given xml_id """
+        data_obj = self.pool.get('ir.model.data')
+        data_id = data_obj._get_id(cr, uid, module, xml_id)
+        return data_obj.browse(cr, uid, data_id).res_id
+
 portal()
 
-class users(osv.osv):
-    _name = 'res.users'
-    _inherit = 'res.users'
+
+
+class portal_override_menu(osv.osv):
+    """
+        extend res.portal with a boolean field 'Override Users Menu', that
+        triggers the creation or removal of menu_action_id
+    """
+    _name = 'res.portal'
+    _inherit = 'res.portal'
+    
+    def _get_override_menu(self, cr, uid, ids, field_name, arg, context=None):
+        assert field_name == 'override_menu'
+        result = {}
+        for p in self.browse(cr, uid, ids, context):
+            result[p.id] = bool(p.menu_action_id)
+        return result
+    
+    def _set_override_menu(self, cr, uid, id, field_name, field_value, arg, context=None):
+        assert field_name == 'override_menu'
+        if field_value:
+            self.create_menu_action(cr, uid, id, context)
+        else:
+            self.write(cr, uid, [id], {'menu_action_id': False}, context)
+    
+    def create_menu_action(self, cr, uid, id, context=None):
+        """ create, if necessary, a menu action that opens the menu items below
+            parent_menu_id """
+        p = self.browse(cr, uid, id, context)
+        if not p.menu_action_id:
+            actions_obj = self.pool.get('ir.actions.act_window')
+            parent_id = p.parent_menu_id.id if p.parent_menu_id else False
+            action_values = {
+                'name': _('%s Menu') % p.name,
+                'type': 'ir.actions.act_window',
+                'usage': 'menu',
+                'res_model': 'ir.ui.menu',
+                'view_type': 'tree',
+                'view_id': self._res_xml_id(cr, uid, 'base', 'view_menu'),
+                'domain': [('parent_id', '=', parent_id)],
+            }
+            action_id = actions_obj.create(cr, uid, action_values, context)
+            self.write(cr, uid, [id], {'menu_action_id': action_id}, context)
+    
+    _columns = {
+        'override_menu': fields.function(
+            _get_override_menu, fnct_inv=_set_override_menu,
+            type='boolean', string='Override Menu Action of Users',
+            help='Enable this option to override the Menu Action of portal users'),
+    }
+
+portal_override_menu()
+
+
+
+class portal_widget(osv.osv):
+    """
+        Similar to res.widget.user (res_widget.py), but with a portal instead.
+        New users in a portal are assigned the portal's widgets.
+    """
+    _name='res.portal.widget'
+    _description = 'Portal Widgets'
+    _order = 'sequence'
     _columns = {
-        'portal_id': fields.many2one('res.portal', string='Portal',
-            help='If given, the portal defines customized menu and access rules'),
+        'sequence': fields.integer('Sequence'),
+        'portal_id': fields.many2one('res.portal', select=1, ondelete='cascade',
+            string='Portal'),
+        'widget_id': fields.many2one('res.widget', required=True, ondelete='cascade',
+            string='Widget'),
     }
 
-users()
+    def create(self, cr, uid, values, context=None):
+        domain = [('portal_id', '=', values.get('portal_id')),
+                  ('widget_id', '=', values.get('widget_id'))]
+        existing = self.search(cr, uid, domain, context=context)
+        if existing:
+            res = existing[0]
+        else:
+            res = super(portal_widget, self).create(cr, uid, values, context=context)
+        return res
+
+portal_widget()
+
 
-# utils
-def get_browse_id(obj):
-    """ return the id of a browse() object, or None """
-    return (obj and obj.id or None)
 
-def get_browse_ids(objs):
-    """ return the ids of a list of browse() objects """
-    return [(obj and obj.id or default) for obj in objs]
 
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: