From 160c47f80be303b695384c7c90aca098fc417f3e Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Fri, 25 Mar 2011 15:42:27 +0100 Subject: [PATCH] constrain portal name to be unique, and override method copy() bzr revid: rco@openerp.com-20110325144227-08wwe4mq1wn2195p --- addons/portal/portal.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/addons/portal/portal.py b/addons/portal/portal.py index 3b1448f3b2e..9790731eaf3 100644 --- a/addons/portal/portal.py +++ b/addons/portal/portal.py @@ -20,6 +20,7 @@ ############################################################################## from osv import osv, fields +import random class portal(osv.osv): _name = 'res.portal' @@ -36,6 +37,21 @@ class portal(osv.osv): string='Groups', help='Users of this portal automatically belong to those groups'), } + _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 create(self, cr, uid, values, context=None): """ extend create() to assign the portal menu and groups to users """ @@ -94,3 +110,7 @@ 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] +def copy_random(name): + """ return "name [N]" for some random integer N """ + return "%s [%s]" % (name, random.choice(xrange(1000000))) +