[FIX] Price Accuracy : rounding made to be based on --price_accuracy option
[odoo/odoo.git] / bin / osv / orm.py
index 66860d3..26afced 100644 (file)
@@ -40,6 +40,7 @@
 #
 
 import time
+import calendar
 import types
 import string
 import netsvc
@@ -51,17 +52,25 @@ import fields
 import tools
 
 import sys
+
 try:
     from xml import dom, xpath
 except ImportError:
     sys.stderr.write("ERROR: Import xpath module\n")
     sys.stderr.write("ERROR: Try to install the old python-xml package\n")
-    sys.exit(2)
+    sys.stderr.write('On Ubuntu Jaunty, try this: sudo cp /usr/lib/python2.6/dist-packages/oldxml/_xmlplus/utils/boolean.so /usr/lib/python2.5/site-packages/oldxml/_xmlplus/utils\n')
+    raise
 
 from tools.config import config
 
-regex_order = re.compile('^([a-zA-Z0-9_]+( desc)?( asc)?,?)+$', re.I)
+regex_order = re.compile('^([a-z0-9_]+( *desc| *asc)?( *, *|))+$', re.I)
 
+def last_day_of_current_month():
+    import datetime
+    import calendar
+    today = datetime.date.today()
+    last_day = str(calendar.monthrange(today.year, today.month)[1])
+    return time.strftime('%Y-%m-' + last_day)
 
 def intersect(la, lb):
     return filter(lambda x: x in lb, la)
@@ -73,6 +82,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):
@@ -94,7 +105,7 @@ class browse_null(object):
 
     def __nonzero__(self):
         return False
-    
+
     def __unicode__(self):
         return u''
 
@@ -115,11 +126,10 @@ class browse_record(object):
     def __init__(self, cr, uid, id, table, cache, context=None, list_class = None, fields_process={}):
         '''
         table : the object (inherited from orm)
-        context : a dictionnary with an optionnal context
+        context : a dictionary with an optional context
         '''
         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
@@ -132,6 +142,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}
 
@@ -159,7 +174,7 @@ class browse_record(object):
                 return False
 
             # if the field is a classic one or a many2one, we'll fetch all classic and many2one fields
-            if col._classic_write:
+            if col._prefetch:
                 # gen the list of "local" (ie not inherited) fields which are classic or many2one
                 ffields = filter(lambda x: x[1]._classic_write, self._table._columns.items())
                 # gen the list of inherited fields
@@ -174,11 +189,17 @@ class browse_record(object):
             fffields = map(lambda x: x[0], ffields)
             datas = self._table.read(self._cr, self._uid, ids, fffields, context=self._context, load="_classic_write")
             if self._fields_process:
+                lang = self._context.get('lang', 'en_US') or 'en_US'
+                lang_obj_ids = self.pool.get('res.lang').search(self._cr, self._uid,[('code','=',lang)])
+                if not lang_obj_ids:
+                    raise Exception(_('Language with code "%s" is not defined in your system !\nDefine it through the Administration menu.') % (lang,))
+                lang_obj = self.pool.get('res.lang').browse(self._cr, self._uid,lang_obj_ids[0])
                 for n, f in ffields:
                     if f._type in self._fields_process:
                         for d in datas:
                             d[n] = self._fields_process[f._type](d[n])
-                            d[n].set_value(d[n], self, f)
+                            if (d[n] is not None) and (d[n] is not False):
+                                d[n].set_value(self._cr, self._uid, d[n], self, f, lang_obj)
 
 
             # create browse records for 'remote' objects
@@ -278,9 +299,14 @@ def get_pg_type(f):
         t = eval('fields.'+(f._type))
         f_type = (type_dict[t], type_dict[t])
     elif isinstance(f, fields.function) and f._type == 'float':
-        f_type = ('float8', 'DOUBLE PRECISION')
+        if f.digits:
+            f_type = ('numeric', 'NUMERIC(%d,%d)' % (f.digits[0], f.digits[1]))
+        else:
+            f_type = ('float8', 'DOUBLE PRECISION')
     elif isinstance(f, fields.function) and f._type == 'selection':
         f_type = ('text', 'text')
+    elif isinstance(f, fields.function) and f._type == 'char':
+        f_type = ('varchar', 'VARCHAR(%d)' % (f.size))
     else:
         logger = netsvc.Logger()
         logger.notifyChannel("init", netsvc.LOG_WARNING, '%s type not supported!' % (type(f)))
@@ -296,6 +322,7 @@ class orm_template(object):
     _rec_name = 'name'
     _parent_name = 'parent_id'
     _parent_store = False
+    _parent_order = False
     _date_name = 'date'
     _order = 'id'
     _sequence = None
@@ -303,7 +330,7 @@ class orm_template(object):
     _inherits = {}
     _table = None
     _invalids = set()
-    
+
     CONCURRENCY_CHECK_FIELD = '__last_update'
 
     def _field_create(self, cr, context={}):
@@ -357,8 +384,12 @@ class orm_template(object):
                     vals['select_level']
                 ))
                 if 'module' in context:
+                    name1 = 'field_' + self._table + '_' + k
+                    cr.execute("select name from ir_model_data where name=%s", (name1,))
+                    if cr.fetchone():
+                        name1 = name1 + "_" + str(id)
                     cr.execute("INSERT INTO ir_model_data (name,date_init,date_update,module,model,res_id) VALUES (%s, now(), now(), %s, %s, %s)", \
-                        (('field_'+self._table+'_'+k)[:64], context['module'], 'ir.model.fields', id)
+                        (name1, context['module'], 'ir.model.fields', id)
                     )
             else:
                 for key, val in vals.items():
@@ -409,17 +440,46 @@ class orm_template(object):
             return browse_null()
 
     def __export_row(self, cr, uid, row, fields, context=None):
+        
+        def check_type(field_type):
+            if field_type == 'float':
+                return 0.0
+            elif field_type == 'integer':
+                return 0
+            elif field_type == 'boolean':   
+                return False                                
+            return ''
+        
         lines = []
         data = map(lambda x: '', range(len(fields)))
         done = []
         for fpos in range(len(fields)):
-            f = fields[fpos]
+            f = fields[fpos]            
             if f:
                 r = row
                 i = 0
                 while i < len(f):
-                    r = r[f[i]]
+                    if f[i] == 'db_id':
+                        r = r['id']                        
+                    elif f[i] == 'id':                        
+                        model_data = self.pool.get('ir.model.data')
+                        data_ids = model_data.search(cr, uid, [('model','=',r._table_name),('res_id','=',r['id'])])
+                        if len(data_ids):
+                            d = model_data.read(cr, uid, data_ids, ['name','module'])[0]
+                            if d['module']:
+                                r = '%s.%s'%(d['module'],d['name'])
+                            else:
+                                r = d['name']
+                        else:
+                            break
+                    else:
+                        r = r[f[i]]                   
                     if not r:
+                        if f[i] in self._columns: 
+                            r = check_type(self._columns[f[i]]._type)
+                        elif f[i] in self._inherit_fields:
+                            r = check_type(self._inherit_fields[f[i]][2]._type)                        
+                        data[fpos] = r                        
                         break
                     if isinstance(r, (browse_record_list, list)):
                         first = True
@@ -427,57 +487,127 @@ class orm_template(object):
                                 or [], fields)
                         if fields2 in done:
                             break
-                        done.append(fields2)
+                        done.append(fields2)                        
                         for row2 in r:
                             lines2 = self.__export_row(cr, uid, row2, fields2,
-                                    context)
+                                    context)                            
                             if first:
                                 for fpos2 in range(len(fields)):
                                     if lines2 and lines2[0][fpos2]:
                                         data[fpos2] = lines2[0][fpos2]
+                                if not data[fpos]:
+                                    dt = ''
+                                    for rr in r :
+                                        if isinstance(rr.name, browse_record):
+                                            rr = rr.name
+                                        dt += rr.name or '' + ','
+                                    data[fpos] = dt[:-1]
+                                    break
                                 lines += lines2[1:]
                                 first = False
                             else:
-                                lines += lines2
+                                lines += lines2                            
                         break
                     i += 1
                 if i == len(f):
-                    data[fpos] = str(r or '')
+                    if isinstance(r, browse_record):
+                        r = r.name
+                    data[fpos] = tools.ustr(r or '')
         return [data] + lines
 
-    def export_data(self, cr, uid, ids, fields, context=None):
+    def export_data(self, cr, uid, ids, fields_to_export, context=None):
         if not context:
             context = {}
-        fields = map(lambda x: x.split('/'), fields)
+        imp_comp = context.get('import_comp',False)        
+        cols = self._columns.copy()
+        for f in self._inherit_fields:
+            cols.update({f: self._inherit_fields[f][2]})        
+        fields_to_export = map(lambda x: x.split('/'), fields_to_export)
+        fields_export = fields_to_export+[]        
+        warning = ''  
+        warning_fields = []      
+        for field in fields_export:
+            if imp_comp and len(field)>1:
+                warning_fields.append('/'.join(map(lambda x:x in cols and cols[x].string or x,field)))
+            elif len (field) <=1:
+                if imp_comp and cols.get(field and field[0],False):
+                    if ((isinstance(cols[field[0]], fields.function) and not cols[field[0]].store) \
+                                     or isinstance(cols[field[0]], fields.related)\
+                                     or isinstance(cols[field[0]], fields.one2many)):                        
+                        warning_fields.append('/'.join(map(lambda x:x in cols and cols[x].string or x,field)))
         datas = []
+        if imp_comp and len(warning_fields):
+            warning = 'Following columns cannot be exported since you select to be import compatible.\n%s' %('\n'.join(warning_fields))        
+            cr.rollback()
+            return {'warning' : warning}
         for row in self.browse(cr, uid, ids, context):
-            datas += self.__export_row(cr, uid, row, fields, context)
-        return datas
+            datas += self.__export_row(cr, uid, row, fields_to_export, context)
+        return {'datas':datas}
 
-    def import_data(self, cr, uid, fields, datas, mode='init',
-            current_module=None, noupdate=False, context=None, filename=None):
+    def import_data(self, cr, uid, fields, datas, mode='init', current_module='', noupdate=False, context=None, filename=None):
         if not context:
             context = {}
         fields = map(lambda x: x.split('/'), fields)
         logger = netsvc.Logger()
-
-        def process_liness(self, datas, prefix, fields_def, position=0):
+        ir_model_data_obj = self.pool.get('ir.model.data')
+        
+        def _check_db_id(self, model_name, db_id):
+            obj_model = self.pool.get(model_name)
+            ids = obj_model.search(cr, uid, [('id','=',int(db_id))])
+            if not len(ids):
+                raise Exception(_("Database ID doesn't exist: %s : %s") %(model_name, db_id))
+            return True
+            
+        def process_liness(self, datas, prefix, current_module, model_name, fields_def, position=0):
             line = datas[position]
             row = {}
             translate = {}
             todo = []
-            warning = ''
+            warning = []
             data_id = False
+            data_res_id = False
+            is_xml_id = False
+            is_db_id = False
+            ir_model_data_obj = self.pool.get('ir.model.data')
             #
             # Import normal fields
             #
             for i in range(len(fields)):
                 if i >= len(line):
                     raise Exception(_('Please check that all your lines have %d columns.') % (len(fields),))
-                field = fields[i]
-                if field == ["id"]:
-                    data_id = line[i]
+                if not line[i]:
                     continue
+                field = fields[i]
+                if (len(field)==len(prefix)+1) and field[len(prefix)].endswith(':db_id'):
+                        # Database ID
+                        res = False
+                        if line[i]:
+                            field_name = field[0].split(':')[0]
+                            model_rel =  fields_def[field_name]['relation']                            
+                            
+                            if fields_def[field[len(prefix)][:-6]]['type']=='many2many':
+                                res_id = []
+                                for db_id in line[i].split(config.get('csv_internal_sep')):
+                                    try:
+                                        _check_db_id(self, model_rel, db_id)
+                                        res_id.append(db_id)
+                                    except Exception,e:                                    
+                                        warning += [tools.exception_to_unicode(e)]
+                                        logger.notifyChannel("import", netsvc.LOG_ERROR,
+                                                  tools.exception_to_unicode(e))
+                                if len(res_id):
+                                    res = [(6, 0, res_id)]
+                            else:
+                                try:
+                                    _check_db_id(self, model_rel, line[i])
+                                    res = line[i]
+                                except Exception,e:                                    
+                                    warning += [tools.exception_to_unicode(e)]
+                                    logger.notifyChannel("import", netsvc.LOG_ERROR,
+                                              tools.exception_to_unicode(e))                        
+                        row[field_name] = res or False
+                        continue
+
                 if (len(field)==len(prefix)+1) and field[len(prefix)].endswith(':id'):
                     res_id = False
                     if line[i]:
@@ -487,8 +617,7 @@ class orm_template(object):
                                 if '.' in word:
                                     module, xml_id = word.rsplit('.', 1)
                                 else:
-                                    module, xml_id = current_module, word
-                                ir_model_data_obj = self.pool.get('ir.model.data')
+                                    module, xml_id = current_module, word                                
                                 id = ir_model_data_obj._get_id(cr, uid, module,
                                         xml_id)
                                 res_id2 = ir_model_data_obj.read(cr, uid, [id],
@@ -501,8 +630,7 @@ class orm_template(object):
                             if '.' in line[i]:
                                 module, xml_id = line[i].rsplit('.', 1)
                             else:
-                                module, xml_id = current_module, line[i]
-                            ir_model_data_obj = self.pool.get('ir.model.data')
+                                module, xml_id = current_module, line[i]                            
                             id = ir_model_data_obj._get_id(cr, uid, module, xml_id)
                             res_id = ir_model_data_obj.read(cr, uid, [id],
                                     ['res_id'])[0]['res_id']
@@ -515,10 +643,58 @@ class orm_template(object):
                     continue
                 if (len(field) == len(prefix)+1) and \
                         (prefix == field[0:len(prefix)]):
+                    if field[len(prefix)] == "id":  
+                        # XML ID                         
+                        db_id = False                 
+                        is_xml_id = data_id = line[i] 
+                        d =  data_id.split('.')
+                        module = len(d)>1 and d[0] or ''
+                        name = len(d)>1 and d[1] or d[0] 
+                        data_ids = ir_model_data_obj.search(cr, uid, [('module','=',module),('model','=',model_name),('name','=',name)])                    
+                        if len(data_ids):
+                            d = ir_model_data_obj.read(cr, uid, data_ids, ['res_id'])[0]                                                
+                            db_id = d['res_id']                       
+                        if is_db_id and not db_id:
+                           data_ids = ir_model_data_obj.search(cr, uid, [('module','=',module),('model','=',model_name),('res_id','=',is_db_id)])                     
+                           if not len(data_ids):
+                               ir_model_data_obj.create(cr, uid, {'module':module, 'model':model_name, 'name':name, 'res_id':is_db_id}) 
+                               db_id = is_db_id 
+                        if is_db_id and int(db_id) != int(is_db_id):                        
+                            warning += [_("Id is not the same than existing one: %s")%(is_db_id)]
+                            logger.notifyChannel("import", netsvc.LOG_ERROR,
+                                    _("Id is not the same than existing one: %s")%(is_db_id))
+                        continue
+
+                    if field[len(prefix)] == "db_id":
+                        # Database ID                        
+                        try:                            
+                            _check_db_id(self, model_name, line[i])
+                            data_res_id = is_db_id = int(line[i])
+                        except Exception,e:                                    
+                            warning += [tools.exception_to_unicode(e)]
+                            logger.notifyChannel("import", netsvc.LOG_ERROR,
+                                      tools.exception_to_unicode(e))
+                            continue
+                        data_ids = ir_model_data_obj.search(cr, uid, [('model','=',model_name),('res_id','=',line[i])])
+                        if len(data_ids):
+                            d = ir_model_data_obj.read(cr, uid, data_ids, ['name','module'])[0]                                                
+                            data_id = d['name']       
+                            if d['module']:
+                                data_id = '%s.%s'%(d['module'],d['name'])
+                            else:
+                                data_id = d['name']
+                        if is_xml_id and not data_id:
+                            data_id = is_xml_id                                     
+                        if is_xml_id and is_xml_id!=data_id:  
+                            warning += [_("Id is not the same than existing one: %s")%(line[i])]
+                            logger.notifyChannel("import", netsvc.LOG_ERROR,
+                                    _("Id is not the same than existing one: %s")%(line[i]))
+                                                           
+                        continue
                     if fields_def[field[len(prefix)]]['type'] == 'integer':
                         res = line[i] and int(line[i])
                     elif fields_def[field[len(prefix)]]['type'] == 'boolean':
-                        res = line[i] and eval(line[i])
+                        res = line[i].lower() not in ('0', 'false', 'off')
                     elif fields_def[field[len(prefix)]]['type'] == 'float':
                         res = line[i] and float(line[i])
                     elif fields_def[field[len(prefix)]]['type'] == 'selection':
@@ -530,40 +706,40 @@ class orm_template(object):
                             sel = fields_def[field[len(prefix)]]['selection'](self,
                                     cr, uid, context)
                         for key, val in sel:
-                            if str(key) == line[i]:
+                            if line[i] in [tools.ustr(key),tools.ustr(val)]: #Acepting key or value for selection field
                                 res = key
+                                break
                         if line[i] and not res:
                             logger.notifyChannel("import", netsvc.LOG_WARNING,
-                                    "key '%s' not found in selection field '%s'" % \
+                                    _("key '%s' not found in selection field '%s'") % \
                                             (line[i], field[len(prefix)]))
+                            
+                            warning += [_("Key/value '%s' not found in selection field '%s'")%(line[i],field[len(prefix)])]
+                            
                     elif fields_def[field[len(prefix)]]['type']=='many2one':
                         res = False
                         if line[i]:
                             relation = fields_def[field[len(prefix)]]['relation']
                             res2 = self.pool.get(relation).name_search(cr, uid,
-                                    line[i], [], operator='=')
+                                    line[i], [], operator='=', context=context)
                             res = (res2 and res2[0][0]) or False
                             if not res:
-                                warning += ('Relation not found: ' + line[i] + \
-                                        ' on ' + relation + ' !\n')
+                                warning += [_("Relation not found: %s on '%s'")%(line[i],relation)]
                                 logger.notifyChannel("import", netsvc.LOG_WARNING,
-                                        'Relation not found: ' + line[i] + \
-                                                ' on ' + relation + ' !\n')
+                                        _("Relation not found: %s on '%s'")%(line[i],relation))
                     elif fields_def[field[len(prefix)]]['type']=='many2many':
                         res = []
                         if line[i]:
                             relation = fields_def[field[len(prefix)]]['relation']
                             for word in line[i].split(config.get('csv_internal_sep')):
                                 res2 = self.pool.get(relation).name_search(cr,
-                                        uid, word, [], operator='=')
+                                        uid, word, [], operator='=', context=context)
                                 res3 = (res2 and res2[0][0]) or False
                                 if not res3:
-                                    warning += ('Relation not found: ' + \
-                                            line[i] + ' on '+relation + ' !\n')
+                                    warning += [_("Relation not found: %s on '%s'")%(line[i],relation)]
                                     logger.notifyChannel("import",
                                             netsvc.LOG_WARNING,
-                                            'Relation not found: ' + line[i] + \
-                                                    ' on '+relation + ' !\n')
+                                            _("Relation not found: %s on '%s'")%(line[i],relation))
                                 else:
                                     res.append(res3)
                             if len(res):
@@ -575,19 +751,20 @@ class orm_template(object):
                     if field[0] not in todo:
                         todo.append(field[len(prefix)])
             #
-            # Import one2many fields
+            # Import one2many, many2many fields
             #
             nbrmax = 1
             for field in todo:
-                newfd = self.pool.get(fields_def[field]['relation']).fields_get(
+                relation_obj = self.pool.get(fields_def[field]['relation'])
+                newfd = relation_obj.fields_get(
                         cr, uid, context=context)
-                res = process_liness(self, datas, prefix + [field], newfd, position)
-                (newrow, max2, w2, translate2, data_id2) = res
+                res = process_liness(self, datas, prefix + [field], current_module, relation_obj._name, newfd, position)                              
+                (newrow, max2, w2, translate2, data_id2, data_res_id2) = res                  
                 nbrmax = max(nbrmax, max2)
-                warning = warning + w2
-                reduce(lambda x, y: x and y, newrow)
+                warning = warning + w2         
+                reduce(lambda x, y: x and y, newrow)       
                 row[field] = (reduce(lambda x, y: x or y, newrow.values()) and \
-                        [(0, 0, newrow)]) or []
+                        [(0, 0, newrow)]) or []                
                 i = max2
                 while (position+i)<len(datas):
                     ok = True
@@ -598,11 +775,11 @@ class orm_template(object):
                     if not ok:
                         break
 
-                    (newrow, max2, w2, translate2, data_id2) = process_liness(
-                            self, datas, prefix+[field], newfd, position+i)
+                    (newrow, max2, w2, translate2, data_id2, data_res_id2) = process_liness(
+                            self, datas, prefix+[field], current_module, relation_obj._name, newfd, position+i)
                     warning = warning+w2
                     if reduce(lambda x, y: x or y, newrow.values()):
-                        row[field].append((0, 0, newrow))
+                        row[field].append((0, 0, newrow))                    
                     i += max2
                     nbrmax = max(nbrmax, i)
 
@@ -610,7 +787,7 @@ class orm_template(object):
                 for i in range(max(nbrmax, 1)):
                     #if datas:
                     datas.pop(0)
-            result = (row, nbrmax, warning, translate, data_id)
+            result = (row, nbrmax, warning, translate, data_id, data_res_id)
             return result
 
         fields_def = self.fields_get(cr, uid, context=context)
@@ -625,14 +802,26 @@ class orm_template(object):
             counter += 1
             res = {}
             #try:
-            (res, other, warning, translate, data_id) = \
-                    process_liness(self, datas, [], fields_def)
-            if warning:
+            (res, other, warning, translate, data_id, res_id) = \
+                    process_liness(self, datas, [], current_module, self._name, fields_def)
+            if len(warning):                        
                 cr.rollback()
-                return (-1, res, warning, '')
-            id = self.pool.get('ir.model.data')._update(cr, uid, self._name,
-                    current_module, res, xml_id=data_id, mode=mode,
-                    noupdate=noupdate)
+                return (-1, res, 'Line ' + str(counter) +' : ' + '!\n'.join(warning), '')
+            
+            try:
+                id = ir_model_data_obj._update(cr, uid, self._name,
+                     current_module, res, xml_id=data_id, mode=mode,
+                     noupdate=noupdate, res_id=res_id)
+            except Exception, e:
+                import psycopg2
+                if isinstance(e,psycopg2.IntegrityError):
+                    msg= _('Insertion Failed!')
+                    for key in self.pool._sql_error.keys():
+                        if key in e[0]:
+                            msg = self.pool._sql_error[key]
+                            break
+                    return (-1, res,'Line ' + str(counter) +' : ' + msg,'' )
+                
             for lang in translate:
                 context2 = context.copy()
                 context2['lang'] = lang
@@ -672,7 +861,7 @@ class orm_template(object):
             if not fun(self, cr, uid, ids):
                 translated_msg = trans._get_source(cr, uid, self._name, 'constraint', lng, source=msg) or msg
                 error_msgs.append(
-                        _("Error occured while validating the field(s) %s: %s") % (','.join(fields), translated_msg)
+                        _("Error occurred while validating the field(s) %s: %s") % (','.join(fields), translated_msg)
                 )
                 self._invalids.update(fields)
         if error_msgs:
@@ -714,50 +903,54 @@ class orm_template(object):
         model_access_obj = self.pool.get('ir.model.access')
         for parent in self._inherits:
             res.update(self.pool.get(parent).fields_get(cr, user, fields, context))
-        for f in self._columns.keys():
-            if fields and f not in fields:
-                continue
-            res[f] = {'type': self._columns[f]._type}
-            for arg in ('string', 'readonly', 'states', 'size', 'required',
-                    'change_default', 'translate', 'help', 'select'):
-                if getattr(self._columns[f], arg):
-                    res[f][arg] = getattr(self._columns[f], arg)
-            if not read_access:
-                res[f]['readonly'] = True
-                res[f]['states'] = {}
-            for arg in ('digits', 'invisible','filters'):
-                if hasattr(self._columns[f], arg) \
-                        and getattr(self._columns[f], arg):
-                    res[f][arg] = getattr(self._columns[f], arg)
-
-            res_trans = translation_obj._get_source(cr, user, self._name + ',' + f, 'field', context.get('lang', False) or 'en_US')
-            if res_trans:
-                res[f]['string'] = res_trans
-            help_trans = translation_obj._get_source(cr, user, self._name + ',' + f, 'help', context.get('lang', False) or 'en_US')
-            if help_trans:
-                res[f]['help'] = help_trans
-
-            if hasattr(self._columns[f], 'selection'):
-                if isinstance(self._columns[f].selection, (tuple, list)):
-                    sel = self._columns[f].selection
-                    # translate each selection option
-                    sel2 = []
-                    for (key, val) in sel:
-                        val2 = None
-                        if val:
-                            val2 = translation_obj._get_source(cr, user, self._name + ',' + f, 'selection', context.get('lang', False) or 'en_US', val)
-                        sel2.append((key, val2 or val))
-                    sel = sel2
-                    res[f]['selection'] = sel
-                else:
-                    # call the 'dynamic selection' function
-                    res[f]['selection'] = self._columns[f].selection(self, cr,
-                            user, context)
-            if res[f]['type'] in ('one2many', 'many2many',
-                    'many2one', 'one2one'):
-                res[f]['relation'] = self._columns[f]._obj
-                res[f]['domain'] = self._columns[f]._domain
-                res[f]['context'] = self._columns[f]._context
+
+        if self._columns.keys():
+            for f in self._columns.keys():
+                if fields and f not in fields:
+                    continue
+                res[f] = {'type': self._columns[f]._type}
+                for arg in ('string', 'readonly', 'states', 'size', 'required',
+                        'change_default', 'translate', 'help', 'select'):
+                    if getattr(self._columns[f], arg):
+                        res[f][arg] = getattr(self._columns[f], arg)
+                if not read_access:
+                    res[f]['readonly'] = True
+                    res[f]['states'] = {}
+                for arg in ('digits', 'invisible','filters'):
+                    if hasattr(self._columns[f], arg) \
+                            and getattr(self._columns[f], arg):
+                        res[f][arg] = getattr(self._columns[f], arg)
+
+                res_trans = translation_obj._get_source(cr, user, self._name + ',' + f, 'field', context.get('lang', False) or 'en_US')
+                if res_trans:
+                    res[f]['string'] = res_trans
+                help_trans = translation_obj._get_source(cr, user, self._name + ',' + f, 'help', context.get('lang', False) or 'en_US')
+                if help_trans:
+                    res[f]['help'] = help_trans
+
+                if hasattr(self._columns[f], 'selection'):
+                    if isinstance(self._columns[f].selection, (tuple, list)):
+                        sel = self._columns[f].selection
+                        # translate each selection option
+                        sel2 = []
+                        for (key, val) in sel:
+                            val2 = None
+                            if val:
+                                val2 = translation_obj._get_source(cr, user, self._name + ',' + f, 'selection', context.get('lang', False) or 'en_US', val)
+                            sel2.append((key, val2 or val))
+                        sel = sel2
+                        res[f]['selection'] = sel
+                    else:
+                        # call the 'dynamic selection' function
+                        res[f]['selection'] = self._columns[f].selection(self, cr,
+                                user, context)
+                if res[f]['type'] in ('one2many', 'many2many', 'many2one', 'one2one'):
+                    res[f]['relation'] = self._columns[f]._obj
+                    res[f]['domain'] = self._columns[f]._domain
+                    res[f]['context'] = self._columns[f]._context
+        else:
+            #TODO : read the fields from the database
+            pass
 
         if fields:
             # filter out fields which aren't in the fields list
@@ -772,7 +965,7 @@ class orm_template(object):
     def view_header_get(self, cr, user, view_id=None, view_type='form', context=None):
         return False
 
-    def __view_look_dom(self, cr, user, node, context=None):
+    def __view_look_dom(self, cr, user, node, view_id, context=None):
         if not context:
             context = {}
         result = False
@@ -784,13 +977,14 @@ class orm_template(object):
                 attrs = {}
                 try:
                     if node.getAttribute('name') in self._columns:
-                        relation = self._columns[node.getAttribute('name')]._obj
+                        column = self._columns[node.getAttribute('name')]
                     else:
-                        relation = self._inherit_fields[node.getAttribute('name')][2]._obj
+                        column = self._inherit_fields[node.getAttribute('name')][2]
                 except:
-                    relation = False
+                    column = False
 
-                if relation:
+                if column:
+                    relation = column._obj
                     childs = False
                     views = {}
                     for f in node.childNodes:
@@ -798,16 +992,20 @@ class orm_template(object):
                             node.removeChild(f)
                             ctx = context.copy()
                             ctx['base_model_name'] = self._name
-                            xarch, xfields = self.pool.get(relation).__view_look_dom_arch(cr, user, f, ctx)
+                            xarch, xfields = self.pool.get(relation).__view_look_dom_arch(cr, user, f, view_id, ctx)
                             views[str(f.localName)] = {
                                 'arch': xarch,
                                 'fields': xfields
                             }
                     attrs = {'views': views}
                     if node.hasAttribute('widget') and node.getAttribute('widget')=='selection':
-                        # We can not use the domain has it is defined according to the record !
-                        attrs['selection'] = self.pool.get(relation).name_search(cr, user, '', context=context)
-                        if not attrs.get('required',False):
+                        # We can not use the 'string' domain has it is defined according to the record !
+                        dom = []
+                        if column._domain and not isinstance(column._domain, (str, unicode)):
+                            dom = column._domain
+                        
+                        attrs['selection'] = self.pool.get(relation).name_search(cr, user, '', dom, context=context)
+                        if (node.hasAttribute('required') and not int(node.getAttribute('required'))) or not column.required:
                             attrs['selection'].append((False,''))
                 fields[node.getAttribute('name')] = attrs
 
@@ -848,41 +1046,50 @@ class orm_template(object):
 
         if childs:
             for f in node.childNodes:
-                fields.update(self.__view_look_dom(cr, user, f, context))
+                fields.update(self.__view_look_dom(cr, user, f, view_id, context))
 
         return fields
 
-    def __view_look_dom_arch(self, cr, user, node, context=None):
-        fields_def = self.__view_look_dom(cr, user, node, context=context)
+    def __view_look_dom_arch(self, cr, user, node, view_id, context=None):
+        fields_def = self.__view_look_dom(cr, user, node, view_id, context=context)
 
         rolesobj = self.pool.get('res.roles')
         usersobj = self.pool.get('res.users')
 
-        buttons = xpath.Evaluate('//button', node)
-        if buttons:
-            for button in buttons:
-                if button.getAttribute('type') == 'object':
-                    continue
-
-                ok = True
-
-                if user != 1:   # admin user has all roles
-                    user_roles = usersobj.read(cr, user, [user], ['roles_id'])[0]['roles_id']
-                    cr.execute("select role_id from wkf_transition where signal=%s", (button.getAttribute('name'),))
-                    roles = cr.fetchall()
-                    for role in roles:
-                        if role[0]:
-                            ok = ok and rolesobj.check(cr, user, user_roles, role[0])
-
-                if not ok:
-                    button.setAttribute('readonly', '1')
-                else:
-                    button.setAttribute('readonly', '0')
+        buttons = (n for n in node.getElementsByTagName('button') if n.getAttribute('type') != 'object')
+        for button in buttons:
+            ok = True
+            if user != 1:   # admin user has all roles
+                user_roles = usersobj.read(cr, user, [user], ['roles_id'])[0]['roles_id']
+                cr.execute("select role_id from wkf_transition where signal=%s", (button.getAttribute('name'),))
+                roles = cr.fetchall()
+                for role in roles:
+                    if role[0]:
+                        ok = ok and rolesobj.check(cr, user, user_roles, role[0])
+
+            if not ok:
+                button.setAttribute('readonly', '1')
+            else:
+                button.setAttribute('readonly', '0')
 
         arch = node.toxml(encoding="utf-8").replace('\t', '')
         fields = self.fields_get(cr, user, fields_def.keys(), context)
         for field in fields_def:
-            fields[field].update(fields_def[field])
+            if field == 'id':
+                # sometime, the view may containt the (invisible) field 'id' needed for a domain (when 2 objects have cross references)
+                fields['id'] = {'readonly': True, 'type': 'integer', 'string': 'ID'}
+            elif field in fields:
+                fields[field].update(fields_def[field])
+            else:
+                cr.execute('select name, model from ir_ui_view where (id=%s or inherit_id=%s) and arch like %s', (view_id, view_id, '%%%s%%' % field))
+                res = cr.fetchall()[:]
+                model = res[0][1]
+                res.insert(0, ("Can't find field '%s' in the following view parts composing the view of object model '%s':" % (field, model), None))
+                msg = "\n * ".join([r[0] for r in res])
+                msg += "\n\nEither you wrongly customised this view, or some modules bringing those views are not compatible with your current data model"
+                netsvc.Logger().notifyChannel('orm', netsvc.LOG_ERROR, msg)
+                raise except_orm('View error', msg)
+
         return arch, fields
 
     def __get_default_calendar_view(self):
@@ -922,11 +1129,11 @@ class orm_template(object):
     def fields_view_get(self, cr, user, view_id=None, view_type='form', context=None, toolbar=False):
         if not context:
             context = {}
-        
+
         def encode(s):
             if isinstance(s, unicode):
                 return s.encode('utf8')
-            return s 
+            return s
 
         def _inherit_apply(src, inherit):
             def _find(node, node2):
@@ -950,7 +1157,7 @@ class orm_template(object):
                         if res:
                             return res
                 return None
-            
+
 
             doc_src = dom.minidom.parseString(encode(src))
             doc_dest = dom.minidom.parseString(encode(inherit))
@@ -1069,7 +1276,7 @@ class orm_template(object):
             result['view_id'] = 0
 
         doc = dom.minidom.parseString(encode(result['arch']))
-        xarch, xfields = self.__view_look_dom_arch(cr, user, doc, context=context)
+        xarch, xfields = self.__view_look_dom_arch(cr, user, doc, view_id, context=context)
         result['arch'] = xarch
         result['fields'] = xfields
         if toolbar:
@@ -1131,10 +1338,13 @@ 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 = {}
-        self.pool.get('ir.model.access').check(cr, uid, 'ir.translation', 'read')
+        self.pool.get('ir.model.access').check(cr, uid, 'ir.translation', 'read', context=context)
         if not fields:
             fields = self._columns.keys() + self._inherit_fields.keys()
         for lang in langs:
@@ -1157,7 +1367,7 @@ class orm_template(object):
         return res
 
     def write_string(self, cr, uid, id, langs, vals, context=None):
-        self.pool.get('ir.model.access').check(cr, uid, 'ir.translation', 'write')
+        self.pool.get('ir.model.access').check(cr, uid, 'ir.translation', 'write', context=context)
         for lang in langs:
             for field in vals:
                 if field in self._columns:
@@ -1168,9 +1378,11 @@ class orm_template(object):
                 self.pool.get(table).write_string(cr, uid, id, langs, vals, context)
         return True
 
+    def _check_removed_columns(self, cr, log=False):
+        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
@@ -1244,10 +1456,10 @@ class orm_memory(orm_template):
         self._validate(cr, user, [id_new], context)
         wf_service = netsvc.LocalService("workflow")
         wf_service.trg_write(user, self._name, id_new, cr)
-        self.vaccum(cr, user)
         return id_new
 
     def create(self, cr, user, vals, context=None):
+        self.vaccum(cr, user)
         self.next_id += 1
         id_new = self.next_id
         default = []
@@ -1271,7 +1483,6 @@ class orm_memory(orm_template):
         self._validate(cr, user, [id_new], context)
         wf_service = netsvc.LocalService("workflow")
         wf_service.trg_create(user, self._name, id_new, cr)
-        self.vaccum(cr, user)
         return id_new
 
     def default_get(self, cr, uid, fields_list, context=None):
@@ -1352,20 +1563,28 @@ class orm_memory(orm_template):
             })
         return result
 
+    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 = []
-    _log_access = True
     _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()
-        logger.notifyChannel('init', netsvc.LOG_INFO, 'Computing parent left and right for table %s...' % (self._table, ))
+        logger.notifyChannel('orm', netsvc.LOG_INFO, 'Computing parent left and right for table %s...' % (self._table, ))
         def browse_rec(root, pos=0):
 # TODO: set order
             where = self._parent_name+'='+str(root)
             if not root:
                 where = self._parent_name+' IS NULL'
+            if self._parent_order:
+                where += ' order by '+self._parent_order
             cr.execute('SELECT id FROM '+self._table+' WHERE '+where)
             pos2 = pos + 1
             childs = cr.fetchall()
@@ -1373,12 +1592,18 @@ class orm(orm_template):
                 pos2 = browse_rec(id[0], pos2)
             cr.execute('update '+self._table+' set parent_left=%s, parent_right=%s where id=%s', (pos,pos2,root))
             return pos2+1
-        browse_rec(None)
+        query = 'SELECT id FROM '+self._table+' WHERE '+self._parent_name+' IS NULL'
+        if self._parent_order:
+            query += ' order by '+self._parent_order
+        pos = 0
+        cr.execute(query)
+        for (root,) in cr.fetchall():
+            pos = browse_rec(root, pos)
         return True
 
     def _update_store(self, cr, f, k):
         logger = netsvc.Logger()
-        logger.notifyChannel('init', netsvc.LOG_INFO, "storing computed values of fields.function '%s'" % (k,))
+        logger.notifyChannel('orm', netsvc.LOG_INFO, "storing computed values of fields.function '%s'" % (k,))
         ss = self._columns[k]._symbol_set
         update_query = 'UPDATE "%s" SET "%s"=%s WHERE id=%%s' % (self._table, k, ss[0])
         cr.execute('select id from '+self._table)
@@ -1396,6 +1621,26 @@ class orm(orm_template):
                 if (val<>False) or (type(val)<>bool):
                     cr.execute(update_query, (ss[1](val), key))
 
+    def _check_removed_columns(self, cr, log=False):
+        logger = netsvc.Logger()
+        # iterate on the database columns to drop the NOT NULL constraints
+        # of fields which were required but have been removed (or will be added by another module)
+        columns = [c for c in self._columns if not (isinstance(self._columns[c], fields.function) and not self._columns[c].store)]
+        columns += ('id', 'write_uid', 'write_date', 'create_uid', 'create_date') # openerp access columns
+        cr.execute("SELECT a.attname, a.attnotnull"
+                   "  FROM pg_class c, pg_attribute a"
+                   " WHERE c.relname=%%s"
+                   "   AND c.oid=a.attrelid"
+                   "   AND a.attisdropped=%%s"
+                   "   AND pg_catalog.format_type(a.atttypid, a.atttypmod) NOT IN ('cid', 'tid', 'oid', 'xid')"
+                   "   AND a.attname NOT IN (%s)" % ",".join(['%s']*len(columns)),
+                       [self._table, False] + columns)
+        for column in cr.dictfetchall():
+            if log:
+                logger.notifyChannel("orm", netsvc.LOG_DEBUG, "column %s is in the table %s but not in the corresponding object %s" % (column['attname'], self._table, self._name))
+            if column['attnotnull']:
+                cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" DROP NOT NULL' % (self._table, column['attname']))
+
     def _auto_init(self, cr, context={}):
         store_compute =  False
         logger = netsvc.Logger()
@@ -1415,11 +1660,11 @@ class orm(orm_template):
                     """, (self._table, 'parent_left'))
                 if not cr.rowcount:
                     if 'parent_left' not in self._columns:
-                        logger.notifyChannel('init', netsvc.LOG_ERROR, 'create a column parent_left on object %s: fields.integer(\'Left Parent\', select=1)' % (self._table, ))
+                        logger.notifyChannel('orm', netsvc.LOG_ERROR, 'create a column parent_left on object %s: fields.integer(\'Left Parent\', select=1)' % (self._table, ))
                     if 'parent_right' not in self._columns:
-                        logger.notifyChannel('init', netsvc.LOG_ERROR, 'create a column parent_right on object %s: fields.integer(\'Right Parent\', select=1)' % (self._table, ))
+                        logger.notifyChannel('orm', netsvc.LOG_ERROR, 'create a column parent_right on object %s: fields.integer(\'Right Parent\', select=1)' % (self._table, ))
                     if self._columns[self._parent_name].ondelete<>'cascade':
-                        logger.notifyChannel('init', netsvc.LOG_ERROR, "the columns %s on object must be set as ondelete='cascasde'" % (self._name, self._parent_name))
+                        logger.notifyChannel('orm', netsvc.LOG_ERROR, "the columns %s on object must be set as ondelete='cascasde'" % (self._name, self._parent_name))
                     cr.execute('ALTER TABLE "%s" ADD COLUMN "parent_left" INTEGER' % (self._table,))
                     cr.execute('ALTER TABLE "%s" ADD COLUMN "parent_right" INTEGER' % (self._table,))
                     cr.commit()
@@ -1442,17 +1687,7 @@ class orm(orm_template):
                         cr.execute('ALTER TABLE "%s" ADD COLUMN "%s" %s' % (self._table, k, logs[k]))
                         cr.commit()
 
-            # iterate on the database columns to drop the NOT NULL constraints
-            # of fields which were required but have been removed
-            cr.execute(
-                "SELECT a.attname, a.attnotnull "\
-                "FROM pg_class c, pg_attribute a "\
-                "WHERE c.oid=a.attrelid AND c.relname=%s", (self._table,))
-            db_columns = cr.dictfetchall()
-            for column in db_columns:
-                if column['attname'] not in ('id', 'oid', 'tableoid', 'ctid', 'xmin', 'xmax', 'cmin', 'cmax'):
-                    if column['attnotnull'] and column['attname'] not in self._columns:
-                        cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" DROP NOT NULL' % (self._table, column['attname']))
+            self._check_removed_columns(cr, log=False)
 
             # iterate on the "object columns"
             todo_update_store = []
@@ -1502,9 +1737,9 @@ class orm(orm_template):
                                 query = 'UPDATE "%s" SET "%s"=%s' % (self._table, k, ss[0])
                                 cr.execute(query, (ss[1](default),))
                                 cr.commit()
-                                logger.notifyChannel('init', netsvc.LOG_DEBUG, 'setting default value of new column %s of table %s'% (k, self._table))
+                                logger.notifyChannel('orm', netsvc.LOG_DEBUG, 'setting default value of new column %s of table %s'% (k, self._table))
                             elif not create:
-                                logger.notifyChannel('init', netsvc.LOG_DEBUG, 'creating new column %s of table %s'% (k, self._table))
+                                logger.notifyChannel('orm', netsvc.LOG_DEBUG, 'creating new column %s of table %s'% (k, self._table))
 
                             if isinstance(f, fields.function):
                                 order = 10
@@ -1529,7 +1764,7 @@ class orm(orm_template):
                                     cr.commit()
                                     cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" SET NOT NULL' % (self._table, k))
                                 except Exception, e:
-                                    logger.notifyChannel('init', netsvc.LOG_WARNING, 'WARNING: unable to set column %s of table %s not null !\nTry to re-run: openerp-server.py --update=module\nIf it doesn\'t work, update records and execute manually:\nALTER TABLE %s ALTER COLUMN %s SET NOT NULL' % (k, self._table, self._table, k))
+                                    logger.notifyChannel('orm', netsvc.LOG_WARNING, 'WARNING: unable to set column %s of table %s not null !\nTry to re-run: openerp-server.py --update=module\nIf it doesn\'t work, update records and execute manually:\nALTER TABLE %s ALTER COLUMN %s SET NOT NULL' % (k, self._table, self._table, k))
                             cr.commit()
                     elif len(res)==1:
                         f_pg_def = res[0]
@@ -1537,7 +1772,7 @@ class orm(orm_template):
                         f_pg_size = f_pg_def['size']
                         f_pg_notnull = f_pg_def['attnotnull']
                         if isinstance(f, fields.function) and not f.store:
-                            logger.notifyChannel('init', netsvc.LOG_INFO, 'column %s (%s) in table %s removed: converted to a function !\n' % (k, f.string, self._table))
+                            logger.notifyChannel('orm', netsvc.LOG_INFO, 'column %s (%s) in table %s removed: converted to a function !\n' % (k, f.string, self._table))
                             cr.execute('ALTER TABLE %s DROP COLUMN %s'% (self._table, k))
                             cr.commit()
                             f_obj_type = None
@@ -1552,8 +1787,10 @@ class orm(orm_template):
                                 ('int4', 'float', get_pg_type(f)[1], '::'+get_pg_type(f)[1]),
                                 ('date', 'datetime', 'TIMESTAMP', '::TIMESTAMP'),
                             ]
-                            if f_pg_type == 'varchar' and f._type == 'char' and f_pg_size != f.size:
-                                logger.notifyChannel('init', netsvc.LOG_INFO, "column '%s' in table '%s' changed size" % (k, self._table))
+                            # !!! Avoid reduction of varchar field !!!
+                            if f_pg_type == 'varchar' and f._type == 'char' and f_pg_size < f.size:
+                            # if f_pg_type == 'varchar' and f._type == 'char' and f_pg_size != f.size:
+                                logger.notifyChannel('orm', netsvc.LOG_INFO, "column '%s' in table '%s' changed size" % (k, self._table))
                                 cr.execute('ALTER TABLE "%s" RENAME COLUMN "%s" TO temp_change_size' % (self._table, k))
                                 cr.execute('ALTER TABLE "%s" ADD COLUMN "%s" VARCHAR(%d)' % (self._table, k, f.size))
                                 cr.execute('UPDATE "%s" SET "%s"=temp_change_size::VARCHAR(%d)' % (self._table, k, f.size))
@@ -1561,7 +1798,7 @@ class orm(orm_template):
                                 cr.commit()
                             for c in casts:
                                 if (f_pg_type==c[0]) and (f._type==c[1]):
-                                    logger.notifyChannel('init', netsvc.LOG_INFO, "column '%s' in table '%s' changed type to %s." % (k, self._table, c[1]))
+                                    logger.notifyChannel('orm', netsvc.LOG_INFO, "column '%s' in table '%s' changed type to %s." % (k, self._table, c[1]))
                                     ok = True
                                     cr.execute('ALTER TABLE "%s" RENAME COLUMN "%s" TO temp_change_size' % (self._table, k))
                                     cr.execute('ALTER TABLE "%s" ADD COLUMN "%s" %s' % (self._table, k, c[2]))
@@ -1571,7 +1808,7 @@ class orm(orm_template):
 
                             if f_pg_type != f_obj_type:
                                 if not ok:
-                                    logger.notifyChannel('init', netsvc.LOG_WARNING, "column '%s' in table '%s' has changed type (DB = %s, def = %s) but unable to migrate this change !" % (k, self._table, f_pg_type, f._type))
+                                    logger.notifyChannel('orm', netsvc.LOG_WARNING, "column '%s' in table '%s' has changed type (DB = %s, def = %s) but unable to migrate this change !" % (k, self._table, f_pg_type, f._type))
 
                             # if the field is required and hasn't got a NOT NULL constraint
                             if f.required and f_pg_notnull == 0:
@@ -1588,7 +1825,7 @@ class orm(orm_template):
                                     cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" SET NOT NULL' % (self._table, k))
                                     cr.commit()
                                 except Exception, e:
-                                    logger.notifyChannel('init', netsvc.LOG_WARNING, 'unable to set a NOT NULL constraint on column %s of the %s table !\nIf you want to have it, you should update the records and execute manually:\nALTER TABLE %s ALTER COLUMN %s SET NOT NULL' % (k, self._table, self._table, k))
+                                    logger.notifyChannel('orm', netsvc.LOG_WARNING, 'unable to set a NOT NULL constraint on column %s of the %s table !\nIf you want to have it, you should update the records and execute manually:\nALTER TABLE %s ALTER COLUMN %s SET NOT NULL' % (k, self._table, self._table, k))
                                 cr.commit()
                             elif not f.required and f_pg_notnull == 1:
                                 cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" DROP NOT NULL' % (self._table, k))
@@ -1605,20 +1842,20 @@ class orm(orm_template):
                             if isinstance(f, fields.many2one):
                                 ref = self.pool.get(f._obj)._table
                                 if ref != 'ir_actions':
-                                    cr.execute('SELECT confdeltype, conname FROM pg_constraint as con, pg_class as cl1, pg_class as cl2, ' 
-                                                'pg_attribute as att1, pg_attribute as att2 ' 
-                                            'WHERE con.conrelid = cl1.oid ' 
-                                                'AND cl1.relname = %s ' 
-                                                'AND con.confrelid = cl2.oid ' 
-                                                'AND cl2.relname = %s ' 
-                                                'AND array_lower(con.conkey, 1) = 1 ' 
-                                                'AND con.conkey[1] = att1.attnum ' 
-                                                'AND att1.attrelid = cl1.oid ' 
-                                                'AND att1.attname = %s ' 
-                                                'AND array_lower(con.confkey, 1) = 1 ' 
-                                                'AND con.confkey[1] = att2.attnum ' 
-                                                'AND att2.attrelid = cl2.oid ' 
-                                                'AND att2.attname = %s ' 
+                                    cr.execute('SELECT confdeltype, conname FROM pg_constraint as con, pg_class as cl1, pg_class as cl2, '
+                                                'pg_attribute as att1, pg_attribute as att2 '
+                                            'WHERE con.conrelid = cl1.oid '
+                                                'AND cl1.relname = %s '
+                                                'AND con.confrelid = cl2.oid '
+                                                'AND cl2.relname = %s '
+                                                'AND array_lower(con.conkey, 1) = 1 '
+                                                'AND con.conkey[1] = att1.attnum '
+                                                'AND att1.attrelid = cl1.oid '
+                                                'AND att1.attname = %s '
+                                                'AND array_lower(con.confkey, 1) = 1 '
+                                                'AND con.confkey[1] = att2.attnum '
+                                                'AND att2.attrelid = cl2.oid '
+                                                'AND att2.attname = %s '
                                                 "AND con.contype = 'f'", (self._table, ref, k, 'id'))
                                     res = cr.dictfetchall()
                                     if res:
@@ -1634,7 +1871,6 @@ class orm(orm_template):
                                             cr.execute('ALTER TABLE "' + self._table + '" ADD FOREIGN KEY ("' + k + '") REFERENCES "' + ref + '" ON DELETE ' + f.ondelete)
                                             cr.commit()
                     else:
-                        logger = netsvc.Logger()
                         logger.notifyChannel('orm', netsvc.LOG_ERROR, "Programming error !")
             for order,f,k in todo_update_store:
                 todo_end.append((order, self._update_store, (f, k)))
@@ -1651,7 +1887,7 @@ class orm(orm_template):
                     cr.execute('alter table "%s" add constraint "%s_%s" %s' % (self._table, self._table, key, con,))
                     cr.commit()
                 except:
-                    logger.notifyChannel('init', netsvc.LOG_WARNING, 'unable to add \'%s\' constraint on table %s !\n If you want to have it, you should update the records and execute manually:\nALTER table %s ADD CONSTRAINT %s_%s %s' % (con, self._table, self._table, self._table, key, con,))
+                    logger.notifyChannel('orm', netsvc.LOG_WARNING, 'unable to add \'%s\' constraint on table %s !\n If you want to have it, you should update the records and execute manually:\nALTER table %s ADD CONSTRAINT %s_%s %s' % (con, self._table, self._table, self._table, key, con,))
 
         if create:
             if hasattr(self, "_sql"):
@@ -1666,6 +1902,11 @@ class orm(orm_template):
 
     def __init__(self, cr):
         super(orm, self).__init__(cr)
+
+        if not hasattr(self, '_log_access'):
+            # if not access is not specify, it is the same value as _auto
+            self._log_access = not hasattr(self, "_auto") or self._auto
+
         self._columns = self._columns.copy()
         for store_field in self._columns:
             f = self._columns[store_field]
@@ -1726,7 +1967,7 @@ class orm(orm_template):
                     import random
                     _rel1 = field['relation'].replace('.', '_')
                     _rel2 = field['model'].replace('.', '_')
-                    _rel_name = 'x_%s_%s_%s_rel' %(_rel1, _rel2, random.randint(0, 10000))
+                    _rel_name = 'x_%s_%s_%s_rel' %(_rel1, _rel2, field['name'])
                     self._columns[field['name']] = getattr(fields, field['ttype'])(field['relation'], _rel_name, 'id1', 'id2', **attrs)
                 else:
                     self._columns[field['name']] = getattr(fields, field['ttype'])(**attrs)
@@ -1826,13 +2067,15 @@ class orm(orm_template):
         self._inherits_reload_src()
 
     def fields_get(self, cr, user, fields=None, context=None):
-        read_access = self.pool.get('ir.model.access').check(cr, user, self._name, 'write', raise_exception=False)
+        ira = self.pool.get('ir.model.access')
+        read_access = ira.check(cr, user, self._name, 'write', raise_exception=False, context=context) or \
+                      ira.check(cr, user, self._name, 'create', raise_exception=False, context=context)
         return super(orm, self).fields_get(cr, user, fields, context, read_access)
 
     def read(self, cr, user, ids, fields=None, context=None, load='_classic_read'):
         if not context:
             context = {}
-        self.pool.get('ir.model.access').check(cr, user, self._name, 'read')
+        self.pool.get('ir.model.access').check(cr, user, self._name, 'read', context=context)
         if not fields:
             fields = self._columns.keys() + self._inherit_fields.keys()
         select = ids
@@ -1861,7 +2104,7 @@ class orm(orm_template):
 
         # all inherited fields + all non inherited fields for which the attribute whose name is in load is True
         fields_pre = [f for f in fields_to_read if
-                           f == self.CONCURRENCY_CHECK_FIELD 
+                           f == self.CONCURRENCY_CHECK_FIELD
                         or (f in self._columns and getattr(self._columns[f], '_classic_write'))
                      ] + self._inherits.values()
 
@@ -1875,7 +2118,7 @@ class orm(orm_template):
                         return "COALESCE(write_date, create_date, now())::timestamp AS %s" % (f,)
                     return "now()::timestamp AS %s" % (f,)
                 if isinstance(self._columns[f], fields.binary) and context.get('bin_size', False):
-                    return "length(%s) as %s" % (f,f)
+                    return 'length("%s") as "%s"' % (f, f)
                 return '"%s"' % (f,)
             fields_pre2 = map(convert_field, fields_pre)
             for i in range(0, len(ids), cr.IN_MAX):
@@ -1883,16 +2126,16 @@ class orm(orm_template):
                 if d1:
                     cr.execute('SELECT %s FROM \"%s\" WHERE id IN (%s) AND %s ORDER BY %s' % \
                             (','.join(fields_pre2 + ['id']), self._table,
-                                ','.join([str(x) for x in sub_ids]), d1,
-                                self._order), d2)
+                                ','.join(['%s' for x in sub_ids]), d1,
+                                self._order),sub_ids + d2)
                     if not cr.rowcount == len({}.fromkeys(sub_ids)):
                         raise except_orm(_('AccessError'),
                                 _('You try to bypass an access rule (Document type: %s).') % self._description)
                 else:
                     cr.execute('SELECT %s FROM \"%s\" WHERE id IN (%s) ORDER BY %s' % \
                             (','.join(fields_pre2 + ['id']), self._table,
-                                ','.join([str(x) for x in sub_ids]),
-                                self._order))
+                                ','.join(['%s' for x in sub_ids]),
+                                self._order), sub_ids)
                 res.extend(cr.dictfetchall())
         else:
             res = map(lambda x: {'id': x}, ids)
@@ -2024,20 +2267,19 @@ class orm(orm_template):
         if not context:
             return
         if context.get(self.CONCURRENCY_CHECK_FIELD) and self._log_access:
+            def key(oid):
+                return "%s,%s" % (self._name, oid)
             santa = "(id = %s AND %s < COALESCE(write_date, create_date, now())::timestamp)"
             for i in range(0, len(ids), cr.IN_MAX):
-                sub_ids = tools.flatten(((i, context[self.CONCURRENCY_CHECK_FIELD][str(i)]) 
-                                          for i in ids[i:i+cr.IN_MAX] 
-                                          if str(i) in context[self.CONCURRENCY_CHECK_FIELD]))
+                sub_ids = tools.flatten(((oid, context[self.CONCURRENCY_CHECK_FIELD][key(oid)])
+                                          for oid in ids[i:i+cr.IN_MAX]
+                                          if key(oid) in context[self.CONCURRENCY_CHECK_FIELD]))
                 if sub_ids:
                     cr.execute("SELECT count(1) FROM %s WHERE %s" % (self._table, " OR ".join([santa]*(len(sub_ids)/2))), sub_ids)
                     res = cr.fetchone()
                     if res and res[0]:
                         raise except_orm('ConcurrencyException', _('Records were modified in the meanwhile'))
 
-            del context[self.CONCURRENCY_CHECK_FIELD]
-
-
     def unlink(self, cr, uid, ids, context=None):
         if not ids:
             return True
@@ -2045,14 +2287,21 @@ class orm(orm_template):
             ids = [ids]
 
         result_store = self._store_get_values(cr, uid, ids, None, context)
-        
+
         self._check_concurrency(cr, ids, context)
 
-        self.pool.get('ir.model.access').check(cr, uid, self._name, 'unlink')
+        self.pool.get('ir.model.access').check(cr, uid, self._name, 'unlink', context=context)
+
+        properties = self.pool.get('ir.property')
+        domain = [('res_id', '=', False), 
+                  ('value', 'in', ['%s,%s' % (self._name, i) for i in ids]), 
+                 ]
+        if properties.search(cr, uid, domain, context=context):
+            raise except_orm(_('Error'), _('Unable to delete this document because it is used as a default property'))
 
         wf_service = netsvc.LocalService("workflow")
-        for id in ids:
-            wf_service.trg_delete(uid, self._name, id, cr)
+        for oid in ids:
+            wf_service.trg_delete(uid, self._name, oid, cr)
 
         #cr.execute('select * from '+self._table+' where id in ('+str_d+')', ids)
         #res = cr.dictfetchall()
@@ -2070,7 +2319,7 @@ class orm(orm_template):
             if d1:
                 cr.execute('SELECT id FROM "'+self._table+'" ' \
                         'WHERE id IN ('+str_d+')'+d1, sub_ids+d2)
-                if not cr.rowcount == len({}.fromkeys(ids)):
+                if not cr.rowcount == len(sub_ids):
                     raise except_orm(_('AccessError'),
                             _('You try to bypass an access rule (Document type: %s).') % \
                                     self._description)
@@ -2082,9 +2331,13 @@ class orm(orm_template):
                 cr.execute('delete from "'+self._table+'" ' \
                         'where id in ('+str_d+')', sub_ids)
 
-        for order, object, ids, fields in result_store:
+        for order, object, store_ids, fields in result_store:
             if object<>self._name:
-                self.pool.get(object)._store_set_values(cr, uid, ids, fields, context)
+                obj =  self.pool.get(object)
+                cr.execute('select id from '+obj._table+' where id in ('+','.join(map(str, store_ids))+')')
+                rids = map(lambda x: x[0], cr.fetchall())
+                if rids:
+                    obj._store_set_values(cr, uid, rids, fields, context)
         return True
 
     #
@@ -2121,6 +2374,7 @@ class orm(orm_template):
                 if not edit:
                     vals.pop(field)
 
+
         if not context:
             context = {}
         if not ids:
@@ -2130,7 +2384,8 @@ class orm(orm_template):
 
         self._check_concurrency(cr, ids, context)
 
-        self.pool.get('ir.model.access').check(cr, user, self._name, 'write')
+        self.pool.get('ir.model.access').check(cr, user, self._name, 'write', context=context)
+
 
         upd0 = []
         upd1 = []
@@ -2201,17 +2456,27 @@ class orm(orm_template):
                 else:
                     cr.execute('update "'+self._table+'" set '+string.join(upd0, ',')+' ' \
                             'where id in ('+ids_str+')', upd1)
-
+            
             if totranslate:
                 for f in direct:
                     if self._columns[f].translate:
-                        self.pool.get('ir.translation')._set_ids(cr, user, self._name+','+f, 'model', context['lang'], ids, vals[f])
+                        src_trans = self.pool.get(self._name).read(cr,user,ids,[f])
+                        self.pool.get('ir.translation')._set_ids(cr, user, self._name+','+f, 'model', context['lang'], ids, vals[f], src_trans[0][f])
+
 
         # call the 'set' method of fields which are not classic_write
         upd_todo.sort(lambda x, y: self._columns[x].priority-self._columns[y].priority)
+
+        # default element in context must be removed when call a one2many or many2many
+        rel_context = context.copy()
+        for c in context.items():
+            if c[0].startswith('default_'):
+                del rel_context[c[0]]
+
+        result = []
         for field in upd_todo:
             for id in ids:
-                self._columns[field].set(cr, self, id, field, vals[field], user, context=context)
+                result += self._columns[field].set(cr, self, id, field, vals[field], user, context=rel_context) or []
 
         for table in self._inherits:
             col = self._inherits[table]
@@ -2235,51 +2500,45 @@ class orm(orm_template):
             if self.pool._init:
                 self.pool._init_parent[self._name]=True
             else:
-                if vals[self._parent_name]:
-                    cr.execute('select parent_left,parent_right from '+self._table+' where id=%s', (vals[self._parent_name],))
-                else:
-                    cr.execute('SELECT parent_left,parent_right FROM '+self._table+' WHERE id IS NULL')
-                res = cr.fetchone()
-                if res:
-                    pleft,pright = res
-                else:
-                    cr.execute('select max(parent_right),max(parent_right)+1 from '+self._table)
+                for id in ids:
+                    # Find Position of the element
+                    if vals[self._parent_name]:
+                        cr.execute('select parent_left,parent_right,id from '+self._table+' where '+self._parent_name+'=%s order by '+(self._parent_order or self._order), (vals[self._parent_name],))
+                    else:
+                        cr.execute('select parent_left,parent_right,id from '+self._table+' where '+self._parent_name+' is null order by '+(self._parent_order or self._order))
+                    result_p = cr.fetchall()
+                    position = None
+                    for (pleft,pright,pid) in result_p:
+                        if pid == id:
+                            break
+                        position = pright+1
+
+                    # It's the first node of the parent: position = parent_left+1
+                    if not position:
+                        if not vals[self._parent_name]:
+                            position = 1
+                        else:
+                            cr.execute('select parent_left from '+self._table+' where id=%s', (vals[self._parent_name],))
+                            position = cr.fetchone()[0]+1
+
+                    # We have the new position !
+                    cr.execute('select parent_left,parent_right from '+self._table+' where id=%s', (id,))
                     pleft,pright = cr.fetchone()
-                cr.execute('select parent_left,parent_right,id from '+self._table+' where id in ('+','.join(map(lambda x:'%s',ids))+')', ids)
-                dest = pleft + 1
-                for cleft,cright,cid in cr.fetchall():
-                    if cleft > pleft:
-                        treeshift  = pleft - cleft + 1
-                        leftbound  = pleft+1
-                        rightbound = cleft-1
-                        cwidth     = cright-cleft+1
-                        leftrange = cright
-                        rightrange  = pleft
+                    distance = pright - pleft + 1
+
+                    if position>pleft and position<=pright:
+                        raise except_orm(_('UserError'), _('Recursivity Detected.'))
+
+                    if pleft<position:
+                        cr.execute('update '+self._table+' set parent_left=parent_left+%s where parent_left>=%s', (distance, position))
+                        cr.execute('update '+self._table+' set parent_right=parent_right+%s where parent_right>=%s', (distance, position))
+                        cr.execute('update '+self._table+' set parent_left=parent_left+%s, parent_right=parent_right+%s where parent_left>=%s and parent_left<%s', (position-pleft,position-pleft, pleft, pright))
                     else:
-                        treeshift  = pleft - cright
-                        leftbound  = cright + 1
-                        rightbound = pleft
-                        cwidth     = cleft-cright-1
-                        leftrange  = pleft+1
-                        rightrange = cleft
-                    cr.execute('UPDATE '+self._table+'''
-                        SET
-                            parent_left = CASE
-                                WHEN parent_left BETWEEN %s AND %s THEN parent_left + %s
-                                WHEN parent_left BETWEEN %s AND %s THEN parent_left + %s
-                                ELSE parent_left
-                            END,
-                            parent_right = CASE
-                                WHEN parent_right BETWEEN %s AND %s THEN parent_right + %s
-                                WHEN parent_right BETWEEN %s AND %s THEN parent_right + %s
-                                ELSE parent_right
-                            END
-                        WHERE
-                            parent_left<%s OR parent_right>%s;
-                    ''', (leftbound,rightbound,cwidth,cleft,cright,treeshift,leftbound,rightbound,
-                        cwidth,cleft,cright,treeshift,leftrange,rightrange))
+                        cr.execute('update '+self._table+' set parent_left=parent_left+%s where parent_left>=%s', (distance, position))
+                        cr.execute('update '+self._table+' set parent_right=parent_right+%s where parent_right>=%s', (distance, position))
+                        cr.execute('update '+self._table+' set parent_left=parent_left-%s, parent_right=parent_right-%s where parent_left>=%s and parent_left<%s', (pleft-position+distance,pleft-position+distance, pleft+distance, pright+distance))
 
-        result = self._store_get_values(cr, user, ids, vals.keys(), context)
+        result += self._store_get_values(cr, user, ids, vals.keys(), context)
         for order, object, ids, fields in result:
             self.pool.get(object)._store_set_values(cr, user, ids, fields, context)
 
@@ -2299,7 +2558,7 @@ class orm(orm_template):
         """
         if not context:
             context = {}
-        self.pool.get('ir.model.access').check(cr, user, self._name, 'create')
+        self.pool.get('ir.model.access').check(cr, user, self._name, 'create', context=context)
 
         default = []
 
@@ -2307,16 +2566,21 @@ class orm(orm_template):
         for (t, c) in self._inherits.items():
             if c in vals:
                 avoid_table.append(t)
-        for f in self._columns.keys(): # + self._inherit_fields.keys():
-            if not f in vals:
+        for f in self._columns.keys():
+            if (not f in vals) and (not isinstance(self._columns[f], fields.property)):
                 default.append(f)
 
         for f in self._inherit_fields.keys():
-            if (not f in vals) and (self._inherit_fields[f][0] not in avoid_table):
+            if (not f in vals) and (self._inherit_fields[f][0] not in avoid_table) and (not isinstance(self._inherit_fields[f][2], fields.property)):
                 default.append(f)
 
         if len(default):
-            vals.update(self.default_get(cr, user, default, context))
+            default_values = self.default_get(cr, user, default, context)
+            for dv in default_values:
+                if dv in self._columns and self._columns[dv]._type == 'many2many':
+                    if default_values[dv] and isinstance(default_values[dv][0], (int, long)):
+                        default_values[dv] = [(6, 0, default_values[dv])]
+            vals.update(default_values)
 
         tocreate = {}
         for v in self._inherits:
@@ -2334,26 +2598,35 @@ class orm(orm_template):
 
         # Try-except added to filter the creation of those records whose filds are readonly.
         # Example : any dashboard which has all the fields readonly.(due to Views(database views))
-        try:        
+        try:
             cr.execute("SELECT nextval('"+self._sequence+"')")
         except:
             raise except_orm(_('UserError'),
-                        _('You cannot perform this operation.'))    
-        
+                        _('You cannot perform this operation.'))
+
         id_new = cr.fetchone()[0]
         for table in tocreate:
             id = self.pool.get(table).create(cr, user, tocreate[table])
             upd0 += ','+self._inherits[table]
             upd1 += ',%s'
             upd2.append(id)
-
+        
+        #Start : Set bool fields to be False if they are not touched(to make search more powerful) 
+        bool_fields = [x for x in self._columns.keys() if self._columns[x]._type=='boolean']
+        
+        for bool_field in bool_fields:
+            if bool_field not in vals:
+                vals[bool_field] = False
+        #End
+        
         for field in vals:
-            if self._columns[field]._classic_write:
-                upd0 = upd0 + ',"' + field + '"'
-                upd1 = upd1 + ',' + self._columns[field]._symbol_set[0]
-                upd2.append(self._columns[field]._symbol_set[1](vals[field]))
-            else:
-                upd_todo.append(field)
+            if field in self._columns:
+                if self._columns[field]._classic_write:
+                    upd0 = upd0 + ',"' + field + '"'
+                    upd1 = upd1 + ',' + self._columns[field]._symbol_set[0]
+                    upd2.append(self._columns[field]._symbol_set[1](vals[field]))
+                else:
+                    upd_todo.append(field)
             if field in self._columns \
                     and hasattr(self._columns[field], 'selection') \
                     and vals[field]:
@@ -2378,10 +2651,6 @@ class orm(orm_template):
             upd2.append(user)
         cr.execute('insert into "'+self._table+'" (id'+upd0+") values ("+str(id_new)+upd1+')', tuple(upd2))
         upd_todo.sort(lambda x, y: self._columns[x].priority-self._columns[y].priority)
-        for field in upd_todo:
-            self._columns[field].set(cr, self, id_new, field, vals[field], user, context)
-
-        self._validate(cr, user, [id_new], context)
 
         if self._parent_store:
             if self.pool._init:
@@ -2389,18 +2658,43 @@ class orm(orm_template):
             else:
                 parent = vals.get(self._parent_name, False)
                 if parent:
-                    cr.execute('select parent_left from '+self._table+' where id=%s', (parent,))
-                    pleft = cr.fetchone()[0]
+                    cr.execute('select parent_right from '+self._table+' where '+self._parent_name+'=%s order by '+(self._parent_order or self._order), (parent,))
+                    pleft_old = None
+                    result_p = cr.fetchall()
+                    for (pleft,) in result_p:
+                        if not pleft:
+                            break
+                        pleft_old = pleft
+                    if not pleft_old:
+                        cr.execute('select parent_left from '+self._table+' where id=%s', (parent,))
+                        pleft_old = cr.fetchone()[0]
+                    pleft = pleft_old
                 else:
                     cr.execute('select max(parent_right) from '+self._table)
                     pleft = cr.fetchone()[0] or 0
                 cr.execute('update '+self._table+' set parent_left=parent_left+2 where parent_left>%s', (pleft,))
                 cr.execute('update '+self._table+' set parent_right=parent_right+2 where parent_right>%s', (pleft,))
                 cr.execute('update '+self._table+' set parent_left=%s,parent_right=%s where id=%s', (pleft+1,pleft+2,id_new))
+                
+        # default element in context must be removed when call a one2many or many2many
+        rel_context = context.copy()
+        for c in context.items():
+            if c[0].startswith('default_'):
+                del rel_context[c[0]]
+        
+        result = []
+        for field in upd_todo:
+            result += self._columns[field].set(cr, self, id_new, field, vals[field], user, rel_context) or []
+        self._validate(cr, user, [id_new], context)
 
-        result = self._store_get_values(cr, user, [id_new], vals.keys(), context)
-        for order, object, ids, fields in result:
-            self.pool.get(object)._store_set_values(cr, user, ids, fields, context)
+        if not context.get('no_store_function', False):
+            result += self._store_get_values(cr, user, [id_new], vals.keys(), context)
+            result.sort()
+            done = []
+            for order, object, ids, fields2 in result:
+                if not (object, ids, fields2) in done:
+                    self.pool.get(object)._store_set_values(cr, user, ids, fields2, context)
+                    done.append((object, ids, fields2))
 
         wf_service = netsvc.LocalService("workflow")
         wf_service.trg_create(user, self._name, id_new, cr)
@@ -2410,20 +2704,34 @@ class orm(orm_template):
         result = {}
         fncts = self.pool._store_function.get(self._name, [])
         for fnct in range(len(fncts)):
+            if fncts[fnct][3]:
+                ok = False
+                for f in (fields or []):
+                    if f in fncts[fnct][3]:
+                        ok = True
+                        break
+                if not ok:
+                    continue
+
             result.setdefault(fncts[fnct][0], {})
             ids2 = fncts[fnct][2](self,cr, uid, ids, context)
             for id in filter(None, ids2):
                 result[fncts[fnct][0]].setdefault(id, [])
                 result[fncts[fnct][0]][id].append(fnct)
-        result2 = []
+        dict = {}
         for object in result:
             k2 = {}
             for id,fnct in result[object].items():
                 k2.setdefault(tuple(fnct), [])
                 k2[tuple(fnct)].append(id)
             for fnct,id in k2.items():
-                result2.append((fncts[fnct[0]][4],object,id,map(lambda x: fncts[x][1], fnct)))
-        result2.sort()
+                dict.setdefault(fncts[fnct[0]][4],[])
+                dict[fncts[fnct[0]][4]].append((fncts[fnct[0]][4],object,id,map(lambda x: fncts[x][1], fnct)))
+        result2 = []
+        tmp = dict.keys()
+        tmp.sort()
+        for k in tmp:
+            result2+=dict[k]
         return result2
 
     def _store_set_values(self, cr, uid, ids, fields, context):
@@ -2578,7 +2886,7 @@ class orm(orm_template):
         res = self.name_get(cr, user, ids, context)
         return res
 
-    def copy(self, cr, uid, id, default=None, context=None):
+    def copy_data(self, cr, uid, id, default=None, context=None):
         if not context:
             context = {}
         if not default:
@@ -2587,7 +2895,8 @@ class orm(orm_template):
             if 'state' in self._defaults:
                 default['state'] = self._defaults['state'](self, cr, uid, context)
         data = self.read(cr, uid, [id], context=context)[0]
-        fields = self.fields_get(cr, uid)
+        fields = self.fields_get(cr, uid, context=context)
+        trans_data=[]
         for f in fields:
             ftype = fields[f]['type']
 
@@ -2610,14 +2919,15 @@ class orm(orm_template):
                     # the lines are first duplicated using the wrong (old)
                     # parent but then are reassigned to the correct one thanks
                     # to the (4, ...)
-                    res.append((4, rel.copy(cr, uid, rel_id, context=context)))
+                    d,t = rel.copy_data(cr, uid, rel_id, context=context)
+                    res.append((0, 0, d))
+                    trans_data += t
                 data[f] = res
             elif ftype == 'many2many':
                 data[f] = [(6, 0, data[f])]
 
         trans_obj = self.pool.get('ir.translation')
         trans_name=''
-        trans_data=[]
         for f in fields:
             trans_flag=True
             if f in self._columns and self._columns[f].translate:
@@ -2639,16 +2949,22 @@ class orm(orm_template):
 
         for v in self._inherits:
             del data[self._inherits[v]]
+        return data, trans_data
 
-        new_id=self.create(cr, uid, data)
-
+    def copy(self, cr, uid, id, default=None, context=None):
+        trans_obj = self.pool.get('ir.translation')
+        data, trans_data = self.copy_data(cr, uid, id, default, context)
+        new_id = self.create(cr, uid, data, context)
         for record in trans_data:
             del record['id']
-            record['res_id']=new_id
-            trans_obj.create(cr,uid,record)
-
+            record['res_id'] = new_id
+            trans_obj.create(cr, uid, record, context)
         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