[IMP] attach the 'Our company' menu items to the portal's root menu
[odoo/odoo.git] / addons / portal / portal.py
index a04521c..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 _
-import random
+
+
 
 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=_("What replaces the standard menu for the portal's users")),
-        'user_ids': fields.one2many('res.users', 'portal_id',
-            string='Users',
-            help=_('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'),
     }
-    _sql_constraints = [
-        ('unique_name', 'UNIQUE(name)', _('Portals must have different names.'))
-    ]
     
-    def copy(self, cr, uid, id, defaults, context=None):
-        """ override copy() to not copy the portal users """
-        # find an unused name of the form "old_name [N]" for some random N
-        old_name = self.browse(cr, uid, id, context).name
-        new_name = copy_random(old_name)
-        while self.search(cr, uid, [('name', '=', new_name)], limit=1, context=context):
-            new_name = copy_random(old_name)
-        
-        defaults['name'] = new_name
-        defaults['user_ids'] = []
-        return super(portal, self).copy(cr, uid, id, defaults, context)
+    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)
-    
-    def write(self, cr, uid, ids, values, context=None):
-        """ extend write() to reflect menu and groups changes on users """
+        # create portal (admin should not be included)
+        context['noadmin'] = True
+        portal_id = super(portal, self).create(cr, uid, values, context)
         
-        # analyse groups changes, and determine how to change users
-        groups_diff = []
-        for change in values.get('group_ids', []):
-            if change[0] in [0, 5, 6]:          # change creates or sets groups,
-                groups_diff = None              # must compute per-portal diff
-                break
-            if change[0] in [3, 4]:             # change add or remove group,
-                groups_diff.append(change)      # add or remove group on users
+        # 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)
         
-        if groups_diff is None:
-            return self._write_compute_diff(cr, uid, ids, values, context)
-        else:
-            return self._write_diff(cr, uid, ids, values, groups_diff, context)
+        return portal_id
     
-    def _write_diff(self, cr, uid, ids, values, groups_diff, context=None):
-        """ perform write() and apply groups_diff on users """
+    def write(self, cr, uid, ids, values, context=None):
+        """ extend write() to reflect changes on users """
         # first apply portal changes
         super(portal, self).write(cr, uid, ids, values, context)
         
-        # then apply menu and group changes on their users
-        user_values = {}
-        if 'menu_id' in values:
-            user_values['menu_id'] = values['menu_id']
-        if groups_diff:
-            user_values['groups_id'] = groups_diff
+        # 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 user_values:
-            user_ids = []
-            for p in self.browse(cr, uid, ids, context):
-                user_ids += get_browse_ids(p.user_ids)
-            self.pool.get('res.users').write(cr, uid, user_ids, user_values, 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 _write_compute_diff(self, cr, uid, ids, values, context=None):
-        """ perform write(), then compute and apply groups_diff on each portal """
-        # read group_ids before write() to compute groups_diff
-        old_group_ids = {}
+
+    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):
-            old_group_ids[p.id] = get_browse_ids(p.group_ids)
-        
-        # apply portal changes
-        super(portal, self).write(cr, uid, ids, values, context)
-        
-        # the changes to apply on users
-        user_object = self.pool.get('res.users')
-        user_values = {}
-        if 'menu_id' in values:
-            user_values['menu_id'] = values['menu_id']
-        
-        # compute groups_diff on each portal, and apply them on users
+            # 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):
-            old_groups = set(old_group_ids[p.id])
-            new_groups = set(get_browse_ids(p.group_ids))
-            # groups_diff: [(3, UNLINKED_ID), ..., (4, LINKED_ID), ...]
-            user_values['groups_id'] = \
-                [(3, g) for g in (old_groups - new_groups)] + \
-                [(4, g) for g in (new_groups - old_groups)]
-            user_ids = get_browse_ids(p.user_ids)
-            user_object.write(cr, uid, user_ids, user_values, context)
-        
-        return True
-    
-    def action_create_menu(self, cr, uid, ids, context=None):
-        """ create a menu for this portal """
-        if len(ids) != 1:
-            raise ValueError("portal.action_create_menu() applies to one portal only")
-        portal_name = self.browse(cr, uid, ids[0], context).name
-        
-        # create a menuitem under 'portal.portal_menu_tree'
-        item_data = {
-            'name': portal_name,
-            'parent_id': self._get_res_id(cr, uid, 'portal', 'portal_menu_tree'),
-        }
-        item_id = self.pool.get('ir.ui.menu').create(cr, uid, item_data, context)
-        
-        # create an action to open the menuitems under item_id
-        action_data = {
-            'name': portal_name,
-            'type': 'ir.actions.act_window',
-            'usage': 'menu',
-            'res_model': 'ir.ui.menu',
-            'view_type': 'tree',
-            'view_id': self._get_res_id(cr, uid, 'base', 'view_menu'),
-            'domain': [('parent_id', '=', item_id)],
-        }
-        action_id = self.pool.get('ir.actions.act_window').create(cr, uid, action_data, context)
-        
-        # set the portal menu_id to action_id
-        return self.write(cr, uid, ids, {'menu_id': action_id}, context)
-    
-    def _get_res_id(self, cr, uid, module, xml_id):
+            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 """
-        ir_model_data_obj = self.pool.get('ir.model.data')
-        record_id = ir_model_data_obj._get_id(cr, uid, module, xml_id)
-        record_data = ir_model_data_obj.read(cr, uid, [record_id], ['res_id'])
-        assert (len(record_data) == 1) and ('res_id' in record_data[0])
-        return record_data[0]['res_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 = {
-        'portal_id': fields.many2one('res.portal', readonly=True,
-            string='Portal',
-            help=_('If given, the portal defines customized menu and access rules')),
+        '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'),
     }
-    
-    def default_get(self, cr, uid, fields, context=None):
-        """ override default values of menu_id and groups_id for portal users """
-        others = {}
-        # How it works: the values of 'menu_id' and 'groups_id' are passed in
-        # context by the portal form view
-        if ('menu_id' in context) and ('menu_id' in fields):
-            fields.remove('menu_id')
-            others['menu_id'] = context['menu_id']
-        if ('groups_id' in context) and ('groups_id' in fields):
-            fields.remove('groups_id')
-            others['groups_id'] = get_many2many(context['groups_id'])
-        # the remaining fields use inherited defaults
-        defs = super(users, self).default_get(cr, uid, fields, context)
-        defs.update(others)
-        return defs
-
-users()
-
-# 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 map(get_browse_id, objs)
-
-def get_many2many(arg):
-    """ get the list of ids from a many2many 'values' field """
-    assert len(arg) == 1 and arg[0][0] == 6             # arg = [(6, _, IDs)]
-    return arg[0][2]
-
-def copy_random(name):
-    """ return "name [N]" for some random integer N """
-    return "%s [%s]" % (name, random.choice(xrange(1000000)))
 
+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 = {
+        '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'),
+    }
+
+    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()
+
+
+
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: