From 34ac7f59458b73cb58273e8053876e7d8dc6f2ec Mon Sep 17 00:00:00 2001 From: Christophe Simonis Date: Wed, 10 Jun 2009 13:15:35 +0200 Subject: [PATCH] [FIX] browse_record: raise a better exception when the id doesn't exists [IMP] new method "exists" that verify that a given id exists in database (or in memory for osv_memory objects) bzr revid: christophe@tinyerp.com-20090610111535-54wcw8tw48fvf9z9 --- bin/osv/orm.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/bin/osv/orm.py b/bin/osv/orm.py index fa4559ff491..00ebed90859 100644 --- a/bin/osv/orm.py +++ b/bin/osv/orm.py @@ -80,6 +80,8 @@ class except_orm(Exception): self.value = value self.args = (name, value) +class BrowseRecordError(Exception): + pass # Readonly python database object browser class browse_null(object): @@ -126,7 +128,6 @@ class browse_record(object): ''' if not context: context = {} - assert id and isinstance(id, (int, long,)), _('Wrong ID for the browse record, got %r, expected an integer.') % (id,) self._list_class = list_class or browse_record_list self._cr = cr self._uid = uid @@ -139,6 +140,11 @@ class browse_record(object): cache.setdefault(table._name, {}) self._data = cache[table._name] + if not (id and isinstance(id, (int, long,))): + raise BrowseRecordError(_('Wrong ID for the browse record, got %r, expected an integer.') % (id,)) + if not table.exists(cr, uid, id, context): + raise BrowseRecordError(_('Object %s does not exists') % (self,)) + if id not in self._data: self._data[id] = {'id': id} @@ -1163,6 +1169,9 @@ class orm_template(object): def copy(self, cr, uid, id, default=None, context=None): raise _('The copy method is not implemented on this object !') + def exists(self, cr, uid, id, context=None): + raise _('The exists method is not implemented on this object !') + def read_string(self, cr, uid, id, langs, fields=None, context=None): res = {} res2 = {} @@ -1204,7 +1213,7 @@ class orm_template(object): raise NotImplementedError() class orm_memory(orm_template): - _protected = ['read', 'write', 'create', 'default_get', 'perm_read', 'unlink', 'fields_get', 'fields_view_get', 'search', 'name_get', 'distinct_field_get', 'name_search', 'copy', 'import_data', 'search_count'] + _protected = ['read', 'write', 'create', 'default_get', 'perm_read', 'unlink', 'fields_get', 'fields_view_get', 'search', 'name_get', 'distinct_field_get', 'name_search', 'copy', 'import_data', 'search_count', 'exists'] _inherit_fields = {} _max_count = 200 _max_hours = 1 @@ -1389,11 +1398,14 @@ class orm_memory(orm_template): def _check_removed_columns(self, cr, log=False): # nothing to check in memory... pass + + def exists(self, cr, uid, id, context=None): + return id in self.datas class orm(orm_template): _sql_constraints = [] _table = None - _protected = ['read','write','create','default_get','perm_read','unlink','fields_get','fields_view_get','search','name_get','distinct_field_get','name_search','copy','import_data','search_count'] + _protected = ['read','write','create','default_get','perm_read','unlink','fields_get','fields_view_get','search','name_get','distinct_field_get','name_search','copy','import_data','search_count', 'exists'] def _parent_store_compute(self, cr): logger = netsvc.Logger() @@ -2736,6 +2748,10 @@ class orm(orm_template): trans_obj.create(cr,uid,record) return new_id + def exists(self, cr, uid, id, context=None): + cr.execute('SELECT count(1) FROM "%s" where id=%%s' % (self._table,), (id,)) + return bool(cr.fetchone()[0]) + def check_recursion(self, cr, uid, ids, parent=None): if not parent: parent = self._parent_name