From 8e189081bdde788d576832c428c75e27020fe479 Mon Sep 17 00:00:00 2001 From: Christophe Simonis Date: Thu, 30 Jun 2011 14:19:45 +0200 Subject: [PATCH] [FIX] document: _check_duplication is now a real _constraints + remove unneeded commit()s bzr revid: chs@openerp.com-20110630121945-qw42hfnhipsevri6 --- addons/document/document.py | 45 ++++++++++++++----------------------- 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/addons/document/document.py b/addons/document/document.py index 50769ea0fda..2a27b4d7253 100644 --- a/addons/document/document.py +++ b/addons/document/document.py @@ -150,32 +150,27 @@ class document_file(osv.osv): 'parent_id': __get_def_directory } _sql_constraints = [ - # filename_uniq is not possible in pure SQL + # filename_uniq is not possible in pure SQL # ?? ] - def _check_duplication(self, cr, uid, vals, ids=[], op='create'): - name = vals.get('name', False) - parent_id = vals.get('parent_id', False) - res_model = vals.get('res_model', False) - res_id = vals.get('res_id', 0) - if op == 'write': - for file in self.browse(cr, uid, ids): # FIXME fields_only - if not name: - name = file.name - if not parent_id: - parent_id = file.parent_id and file.parent_id.id or False - if not res_model: - res_model = file.res_model and file.res_model or False - if not res_id: - res_id = file.res_id and file.res_id or 0 - res = self.search(cr, uid, [('id', '<>', file.id), ('name', '=', name), ('parent_id', '=', parent_id), ('res_model', '=', res_model), ('res_id', '=', res_id)]) - if len(res): - return False - if op == 'create': - res = self.search(cr, uid, [('name', '=', name), ('parent_id', '=', parent_id), ('res_id', '=', res_id), ('res_model', '=', res_model)]) - if len(res): + def _check_duplication(self, cr, uid, ids, context=None): + # FIXME can be a SQL constraint: unique(name,parent_id,res_model,res_id) + for attach in self.browse(cr, uid, ids, context): + domain = [('id', '!=', attach.id), + ('name', '=', attach.name), + ('parent_id', '=', attach.parent_id.id), + ('res_model', '=', attach.res_model), + ('res_id', '=', attach.res_id), + ] + tools.debug(domain) + if self.search(cr, uid, domain, context=context): # XXX search_count always return 0 !?! return False return True + _constraints = [ + (_check_duplication, 'File name must be unique!', ['name', 'parent_id', 'res_model', 'res_id']) + ] + + def check(self, cr, uid, ids, mode, context=None, values=None): """Check access wrt. res_model, relax the rule of ir.attachment parent @@ -222,8 +217,6 @@ class document_file(osv.osv): res = self.search(cr, uid, [('id', 'in', ids)]) if not len(res): return False - if not self._check_duplication(cr, uid, vals, ids, 'write'): - raise osv.except_osv(_('ValidateError'), _('File name must be unique!')) # if nodes call this write(), they must skip the code below from_node = context and context.get('__from_node', False) @@ -260,7 +253,6 @@ class document_file(osv.osv): del vals['file_size'] if len(ids) and len(vals): result = super(document_file,self).write(cr, uid, ids, vals, context=context) - cr.commit() # ? return result def create(self, cr, uid, vals, context=None): @@ -290,10 +282,7 @@ class document_file(osv.osv): else: if vals.get('file_size'): del vals['file_size'] - if not self._check_duplication(cr, uid, vals): - raise osv.except_osv(_('ValidateError'), _('File name must be unique!')) result = super(document_file, self).create(cr, uid, vals, context) - cr.commit() # ? return result def __get_partner_id(self, cr, uid, res_model, res_id, context=None):