Merge commit 'origin/master' into mdv-gpl3-py26
[odoo/odoo.git] / bin / osv / orm.py
index 9c03a23..6ecb829 100644 (file)
@@ -1,31 +1,24 @@
 # -*- encoding: utf-8 -*-
 ##############################################################################
 #
-# Copyright (c) 2004-2008 Tiny SPRL (http://tiny.be) All Rights Reserved.
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
+#    $Id$
 #
-# $Id$
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation, either version 3 of the License, or
+#    (at your option) any later version.
 #
-# WARNING: This program as such is intended to be used by professional
-# programmers who take the whole responsability of assessing all potential
-# consequences resulting from its eventual inadequacies and bugs
-# End users who are looking for a ready-to-use solution with commercial
-# garantees and support are strongly adviced to contract a Free Software
-# Service Company
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
 #
-# This program is Free Software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
+#    You should have received a copy of the GNU General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
-###############################################################################
+##############################################################################
 
 #
 # Object relationnal mapping to postgresql module
 #
 
 import time
+import calendar
 import types
-from xml import dom, xpath
-from xml.parsers import expat
 import string
-import pooler
-import psycopg
 import netsvc
 import re
 
+import pickle
+
 import fields
-import ir
 import tools
 
-from tools.config import config
+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)
 
-prof = 0
-ID_MAX = 1000
+from tools.config import config
 
 regex_order = re.compile('^([a-zA-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)
 
@@ -77,14 +80,18 @@ class except_orm(Exception):
         self.value = value
         self.args = (name, value)
 
+
 # Readonly python database object browser
 class browse_null(object):
 
     def __init__(self):
-        self.id=False
+        self.id = False
 
     def __getitem__(self, name):
-        return False
+        return None
+
+    def __getattr__(self, name):
+        return None  # XXX: return self ?
 
     def __int__(self):
         return False
@@ -94,6 +101,10 @@ class browse_null(object):
 
     def __nonzero__(self):
         return False
+    
+    def __unicode__(self):
+        return u''
+
 
 #
 # TODO: execute an object method on browse_record_list
@@ -111,11 +122,11 @@ 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, _('Wrong ID for the browse record, got %s, expected an integer.') % str(id)
+        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
@@ -127,14 +138,16 @@ class browse_record(object):
 
         cache.setdefault(table._name, {})
         self._data = cache[table._name]
-        if not id in self._data:
-            self._data[id] = {'id':id}
+
+        if id not in self._data:
+            self._data[id] = {'id': id}
+
         self._cache = cache
 
     def __getitem__(self, name):
         if name == 'id':
             return self._id
-        if not self._data[self._id].has_key(name):
+        if name not in self._data[self._id]:
             # build the list of fields we will fetch
 
             # fetch the definition of the field which was asked for
@@ -142,15 +155,15 @@ class browse_record(object):
                 col = self._table._columns[name]
             elif name in self._table._inherit_fields:
                 col = self._table._inherit_fields[name][2]
-            elif hasattr(self._table, name):
-                if isinstance( getattr(self._table, name), (types.MethodType,types.LambdaType,types.FunctionType)):
+            elif hasattr(self._table, str(name)):
+                if isinstance(getattr(self._table, name), (types.MethodType, types.LambdaType, types.FunctionType)):
                     return lambda *args, **argv: getattr(self._table, name)(self._cr, self._uid, [self._id], *args, **argv)
                 else:
                     return getattr(self._table, name)
             else:
                 logger = netsvc.Logger()
                 logger.notifyChannel('orm', netsvc.LOG_ERROR, "Programming error: field '%s' does not exist in object '%s' !" % (name, self._table._name))
-                return False
+                return None
 
             # if the field is a classic one or a many2one, we'll fetch all classic and many2one fields
             if col._classic_write:
@@ -162,27 +175,31 @@ class browse_record(object):
                 ffields += filter(lambda x: x[1]._classic_write, inherits)
             # otherwise we fetch only that field
             else:
-                ffields = [(name,col)]
-            ids = filter(lambda id: not self._data[id].has_key(name), self._data.keys())
+                ffields = [(name, col)]
+            ids = filter(lambda id: name not in self._data[id], self._data.keys())
             # read the data
             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:
-                for n,f in ffields:
+                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]:
+                                d[n].set_value(self._cr, self._uid, d[n], self, f)
 
 
+           if not datas:
+                   # Where did those ids come from? Perhaps old entries in ir_model_data?
+                   raise except_orm('NoDataError', 'Field %s in %s%s'%(name,self._table_name,str(ids)))
             # create browse records for 'remote' objects
             for data in datas:
-                for n,f in ffields:
+                for n, f in ffields:
                     if f._type in ('many2one', 'one2one'):
                         if data[n]:
                             obj = self._table.pool.get(f._obj)
-                            compids=False
-                            if not f._classic_write:
+                            compids = False
+                            if type(data[n]) in (type([]),type( (1,) )):
                                 ids2 = data[n][0]
                             else:
                                 ids2 = data[n]
@@ -193,8 +210,14 @@ class browse_record(object):
                         else:
                             data[n] = browse_null()
                     elif f._type in ('one2many', 'many2many') and len(data[n]):
-                        data[n] = self._list_class([browse_record(self._cr,self._uid,id,self._table.pool.get(f._obj),self._cache, context=self._context, list_class=self._list_class, fields_process=self._fields_process) for id in data[n]], self._context)
+                        data[n] = self._list_class([browse_record(self._cr, self._uid, id, self._table.pool.get(f._obj), self._cache, context=self._context, list_class=self._list_class, fields_process=self._fields_process) for id in data[n]], self._context)
                 self._data[data['id']].update(data)
+       if not name in self._data[self._id]:
+               #how did this happen?
+               logger = netsvc.Logger()
+               logger.notifyChannel("browse_record", netsvc.LOG_ERROR,"Ffields: %s, datas: %s"%(str(fffields),str(datas)))
+               logger.notifyChannel("browse_record", netsvc.LOG_ERROR,"Data: %s, Table: %s"%(str(self._data[self._id]),str(self._table)))
+               raise AttributeError(_('Unknown attribute %s in %s ') % (str(name),self._table_name))
         return self._data[self._id][name]
 
     def __getattr__(self, name):
@@ -237,49 +260,53 @@ def get_pg_type(f):
     '''
 
     type_dict = {
-            fields.boolean:'bool',
-            fields.integer:'int4',
-            fields.text:'text',
-            fields.date:'date',
-            fields.time:'time',
-            fields.datetime:'timestamp',
-            fields.binary:'bytea',
-            fields.many2one:'int4',
+            fields.boolean: 'bool',
+            fields.integer: 'int4',
+            fields.integer_big: 'int8',
+            fields.text: 'text',
+            fields.date: 'date',
+            fields.time: 'time',
+            fields.datetime: 'timestamp',
+            fields.binary: 'bytea',
+            fields.many2one: 'int4',
             }
-    if type_dict.has_key(type(f)):
+    if type(f) in type_dict:
         f_type = (type_dict[type(f)], type_dict[type(f)])
     elif isinstance(f, fields.float):
         if f.digits:
-            f_type = ('numeric', 'NUMERIC(%d,%d)' % (f.digits[0],f.digits[1]))
+            f_type = ('numeric', 'NUMERIC(%d,%d)' % (f.digits[0], f.digits[1]))
         else:
             f_type = ('float8', 'DOUBLE PRECISION')
     elif isinstance(f, (fields.char, fields.reference)):
         f_type = ('varchar', 'VARCHAR(%d)' % (f.size,))
     elif isinstance(f, fields.selection):
         if isinstance(f.selection, list) and isinstance(f.selection[0][0], (str, unicode)):
-            f_size = reduce(lambda x,y: max(x,len(y[0])), f.selection, f.size or 16)
+            f_size = reduce(lambda x, y: max(x, len(y[0])), f.selection, f.size or 16)
         elif isinstance(f.selection, list) and isinstance(f.selection[0][0], int):
             f_size = -1
         else:
-            f_size = (hasattr(f,'size') and f.size) or 16
+            f_size = (hasattr(f, 'size') and f.size) or 16
 
         if f_size == -1:
             f_type = ('int4', 'INTEGER')
         else:
             f_type = ('varchar', 'VARCHAR(%d)' % f_size)
-    elif isinstance(f, fields.function) and type_dict.has_key(eval('fields.'+(f._type))):
-        t=eval('fields.'+(f._type))
+    elif isinstance(f, fields.function) and eval('fields.'+(f._type)) in type_dict:
+        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')
     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)))
         f_type = None
     return f_type
 
+
 class orm_template(object):
     _name = None
     _columns = {}
@@ -287,24 +314,34 @@ class orm_template(object):
     _defaults = {}
     _rec_name = 'name'
     _parent_name = 'parent_id'
+    _parent_store = False
+    _parent_order = False
     _date_name = 'date'
     _order = 'id'
     _sequence = None
     _description = None
     _inherits = {}
     _table = None
+    _invalids = set()
+    
+    CONCURRENCY_CHECK_FIELD = '__last_update'
 
     def _field_create(self, cr, context={}):
-        cr.execute("SELECT id FROM ir_model WHERE model='%s'" % self._name)
+        cr.execute("SELECT id FROM ir_model WHERE model=%s", (self._name,))
         if not cr.rowcount:
-            # reference model in order to have a description of its fonctionnality in custom_report
             cr.execute('SELECT nextval(%s)', ('ir_model_id_seq',))
-            id = cr.fetchone()[0]
-            cr.execute("INSERT INTO ir_model (id,model, name, info) VALUES (%s, %s, %s, %s)", (id,self._name, self._description, self.__doc__))
-            if 'module' in context:
+            model_id = cr.fetchone()[0]
+            cr.execute("INSERT INTO ir_model (id,model, name, info,state) VALUES (%s, %s, %s, %s, %s)", (model_id, self._name, self._description, self.__doc__, 'base'))
+        else:
+            model_id = cr.fetchone()[0]
+        if 'module' in context:
+            name_id = 'model_'+self._name.replace('.','_')
+            cr.execute('select * from ir_model_data where name=%s and res_id=%s', (name_id,model_id))
+            if not cr.rowcount:
                 cr.execute("INSERT INTO ir_model_data (name,date_init,date_update,module,model,res_id) VALUES (%s, now(), now(), %s, %s, %s)", \
-                    ( 'model_'+self._table, context['module'], 'ir.model', id)
+                    (name_id, context['module'], 'ir.model', model_id)
                 )
+
         cr.commit()
 
         cr.execute("SELECT * FROM ir_model_fields WHERE model=%s", (self._name,))
@@ -312,54 +349,50 @@ class orm_template(object):
         for rec in cr.dictfetchall():
             cols[rec['name']] = rec
 
-        cr.execute("SELECT id FROM ir_model WHERE model='%s'" % self._name)
-        model_id = cr.fetchone()[0]
-
-        for (k,f) in self._columns.items():
+        for (k, f) in self._columns.items():
             vals = {
-                'model_id':model_id,
-                'model':self._name,
-                'name':k,
-                'field_description':f.string.replace("'", " "),
-                'ttype':f._type,
-                'relate':(f.relate and 1) or 0,
-                'relation':f._obj or 'NULL',
-                'group_name':f.group_name or '',
-                'view_load':(f.view_load and 1) or 0,
-                'select_level':str(f.select or 0)
+                'model_id': model_id,
+                'model': self._name,
+                'name': k,
+                'field_description': f.string.replace("'", " "),
+                'ttype': f._type,
+                'relation': f._obj or 'NULL',
+                'view_load': (f.view_load and 1) or 0,
+                'select_level': str(f.select or 0),
+                'readonly':(f.readonly and 1) or 0,
+                'required':(f.required and 1) or 0,
             }
             if k not in cols:
                 cr.execute('select nextval(%s)', ('ir_model_fields_id_seq',))
                 id = cr.fetchone()[0]
                 vals['id'] = id
                 cr.execute("""INSERT INTO ir_model_fields (
-                    id, model_id, model, name, field_description, ttype, 
-                    relate,relation,group_name,view_load,state,select_level
+                    id, model_id, model, name, field_description, ttype,
+                    relation,view_load,state,select_level
                 ) VALUES (
-                    %d,%s,%s,%s,%s,%s, %s,%s,%s,%s,%s, %s
+                    %s,%s,%s,%s,%s,%s,%s,%s,%s,%s
                 )""", (
                     id, vals['model_id'], vals['model'], vals['name'], vals['field_description'], vals['ttype'],
-                    bool(vals['relate']), vals['relation'], vals['group_name'], bool(vals['view_load']), 'base',
+                     vals['relation'], bool(vals['view_load']), 'base',
                     vals['select_level']
                 ))
                 if 'module' in context:
                     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)
+                        (('field_'+self._table+'_'+k)[:64], context['module'], 'ir.model.fields', id)
                     )
             else:
-                for key,val in vals.items():
-                    if cols[k][key]<>vals[key]:
-                        print 'Different', cols[k][key], vals[key], k, key, self._name
-                        cr.execute('update ir_model_fields set field_description=%s where model=%s and name=%s', (vals['field_description'],vals['model'],vals['name']))
+                for key, val in vals.items():
+                    if cols[k][key] != vals[key]:
+                        cr.execute('update ir_model_fields set field_description=%s where model=%s and name=%s', (vals['field_description'], vals['model'], vals['name']))
                         cr.commit()
                         cr.execute("""UPDATE ir_model_fields SET
-                            model_id=%s, field_description=%s, ttype=%s, relate=%s, relation=%s,
-                            group_name=%s, view_load=%s, select_level=%s
+                            model_id=%s, field_description=%s, ttype=%s, relation=%s,
+                            view_load=%s, select_level=%s, readonly=%s ,required=%s
                         WHERE
                             model=%s AND name=%s""", (
-                                vals['model_id'],vals['field_description'],vals['ttype'],bool(vals['relate']),
-                                vals['relation'], vals['group_name'], bool(vals['view_load']), 
-                                vals['select_level'], vals['model'], vals['name']
+                                vals['model_id'], vals['field_description'], vals['ttype'],
+                                vals['relation'], bool(vals['view_load']),
+                                vals['select_level'], bool(vals['readonly']),bool(vals['required']), vals['model'], vals['name']
                             ))
                         continue
         cr.commit()
@@ -368,22 +401,30 @@ class orm_template(object):
         self._field_create(cr, context)
 
     def __init__(self, cr):
+        if not self._name and not hasattr(self, '_inherit'):
+            name = type(self).__name__.split('.')[0]
+            msg = "The class %s has to have a _name attribute" % name
+
+            logger = netsvc.Logger()
+            logger.notifyChannel('orm', netsvc.LOG_ERROR, msg )
+            raise except_orm('ValueError', msg )
+
         if not self._description:
             self._description = self._name
         if not self._table:
-            self._table=self._name.replace('.','_')
+            self._table = self._name.replace('.', '_')
 
     def browse(self, cr, uid, select, context=None, list_class=None, fields_process={}):
         if not context:
-            context={}
+            context = {}
         self._list_class = list_class or browse_record_list
         cache = {}
         # need to accepts ints and longs because ids coming from a method
         # launched by button in the interface have a type long...
         if isinstance(select, (int, long)):
-            return browse_record(cr,uid,select,self,cache, context=context, list_class=self._list_class, fields_process=fields_process)
-        elif isinstance(select,list):
-            return self._list_class([browse_record(cr,uid,id,self,cache, context=context, list_class=self._list_class, fields_process=fields_process) for id in select], context)
+            return browse_record(cr, uid, select, self, cache, context=context, list_class=self._list_class, fields_process=fields_process)
+        elif isinstance(select, list):
+            return self._list_class([browse_record(cr, uid, id, self, cache, context=context, list_class=self._list_class, fields_process=fields_process) for id in select], context)
         else:
             return browse_null()
 
@@ -396,7 +437,7 @@ class orm_template(object):
             if f:
                 r = row
                 i = 0
-                while i<len(f):
+                while i < len(f):
                     r = r[f[i]]
                     if not r:
                         break
@@ -414,54 +455,54 @@ class orm_template(object):
                                 for fpos2 in range(len(fields)):
                                     if lines2 and lines2[0][fpos2]:
                                         data[fpos2] = lines2[0][fpos2]
-                                lines+= lines2[1:]
+                                lines += lines2[1:]
                                 first = False
                             else:
-                                lines+= lines2
+                                lines += lines2
                         break
-                    i+=1
-                if i==len(f):
-                    data[fpos] = str(r or '')
+                    i += 1
+                if i == len(f):
+                    data[fpos] = tools.ustr(r or '')
         return [data] + lines
 
     def export_data(self, cr, uid, ids, fields, context=None):
         if not context:
-            context={}
+            context = {}
         fields = map(lambda x: x.split('/'), fields)
         datas = []
         for row in self.browse(cr, uid, ids, context):
             datas += self.__export_row(cr, uid, row, fields, context)
         return datas
 
-    def import_data(self, cr, uid, fields, datas, mode='init',
-            current_module=None, noupdate=False, context=None):
+    def import_data(self, cr, uid, fields, datas, mode='init', current_module=None, noupdate=False, context=None, filename=None):
         if not context:
-            context={}
+            context = {}
         fields = map(lambda x: x.split('/'), fields)
         logger = netsvc.Logger()
+
         def process_liness(self, datas, prefix, fields_def, position=0):
             line = datas[position]
             row = {}
             translate = {}
             todo = []
             warning = ''
-            data_id= False
+            data_id = False
             #
             # 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),)
+                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]
+                    data_id = line[i]
                     continue
                 if (len(field)==len(prefix)+1) and field[len(prefix)].endswith(':id'):
                     res_id = False
                     if line[i]:
                         if fields_def[field[len(prefix)][:-3]]['type']=='many2many':
                             res_id = []
-                            for word in line[i].split(','):
+                            for word in line[i].split(config.get('csv_internal_sep')):
                                 if '.' in word:
                                     module, xml_id = word.rsplit('.', 1)
                                 else:
@@ -474,7 +515,7 @@ class orm_template(object):
                                 if res_id2:
                                     res_id.append(res_id2)
                             if len(res_id):
-                                res_id=[(6,0,res_id)]
+                                res_id = [(6, 0, res_id)]
                         else:
                             if '.' in line[i]:
                                 module, xml_id = line[i].rsplit('.', 1)
@@ -482,8 +523,10 @@ class orm_template(object):
                                 module, xml_id = current_module, line[i]
                             ir_model_data_obj = self.pool.get('ir.model.data')
                             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']
+                           res_res_id = ir_model_data_obj.read(cr, uid, [id],
+                                    ['res_id'])
+                           if res_res_id:
+                                   res_id = res_res_id[0]['res_id']
                     row[field[0][:-3]] = res_id or False
                     continue
                 if (len(field) == len(prefix)+1) and \
@@ -494,9 +537,11 @@ class orm_template(object):
                 if (len(field) == len(prefix)+1) and \
                         (prefix == field[0:len(prefix)]):
                     if fields_def[field[len(prefix)]]['type'] == 'integer':
-                        res =line[i] and int(line[i])
+                        res = line[i] and int(line[i])
+                    elif fields_def[field[len(prefix)]]['type'] == 'boolean':
+                        res = line[i] and eval(line[i])
                     elif fields_def[field[len(prefix)]]['type'] == 'float':
-                        res =line[i] and float(line[i])
+                        res = line[i] and float(line[i])
                     elif fields_def[field[len(prefix)]]['type'] == 'selection':
                         res = False
                         if isinstance(fields_def[field[len(prefix)]]['selection'],
@@ -529,7 +574,7 @@ class orm_template(object):
                         res = []
                         if line[i]:
                             relation = fields_def[field[len(prefix)]]['relation']
-                            for word in line[i].split(','):
+                            for word in line[i].split(config.get('csv_internal_sep')):
                                 res2 = self.pool.get(relation).name_search(cr,
                                         uid, word, [], operator='=')
                                 res3 = (res2 and res2[0][0]) or False
@@ -543,7 +588,7 @@ class orm_template(object):
                                 else:
                                     res.append(res3)
                             if len(res):
-                                res= [(6,0,res)]
+                                res = [(6, 0, res)]
                     else:
                         res = line[i] or False
                     row[field[len(prefix)]] = res
@@ -561,9 +606,9 @@ class orm_template(object):
                 (newrow, max2, w2, translate2, data_id2) = res
                 nbrmax = max(nbrmax, max2)
                 warning = warning + w2
-                reduce(lambda x,y: x and y, newrow)
+                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
@@ -577,13 +622,13 @@ class orm_template(object):
                     (newrow, max2, w2, translate2, data_id2) = process_liness(
                             self, datas, prefix+[field], newfd, position+i)
                     warning = warning+w2
-                    if reduce(lambda x,y: x or y, newrow.values()):
-                        row[field].append((0,0,newrow))
-                    i+=max2
+                    if reduce(lambda x, y: x or y, newrow.values()):
+                        row[field].append((0, 0, newrow))
+                    i += max2
                     nbrmax = max(nbrmax, i)
 
             if len(prefix)==0:
-                for i in range(max(nbrmax,1)):
+                for i in range(max(nbrmax, 1)):
                     #if datas:
                     datas.pop(0)
             result = (row, nbrmax, warning, translate, data_id)
@@ -592,30 +637,40 @@ class orm_template(object):
         fields_def = self.fields_get(cr, uid, context=context)
         done = 0
 
+        initial_size = len(datas)
+        if config.get('import_partial', False) and filename:
+            data = pickle.load(file(config.get('import_partial')))
+            original_value =  data.get(filename, 0)
+        counter = 0
         while len(datas):
+            counter += 1
             res = {}
-            try:
-                (res, other, warning, translate, data_id) = \
-                        process_liness(self, datas, [], fields_def)
-                if 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)
-                for lang in translate:
-                    context2=context.copy()
-                    context2['lang']=lang
-                    self.write(cr, uid, [id], translate[lang], context2)
-                if config.get('commit_mode', False):
-                    cr.commit()
-            except Exception, e:
-                logger.notifyChannel("import",netsvc.LOG_ERROR, e)
+            #try:
+            (res, other, warning, translate, data_id) = \
+                    process_liness(self, datas, [], fields_def)
+            if warning:
                 cr.rollback()
-                try:
-                    return (-1, res, e[0], warning)
-                except:
-                    return (-1, res, e[0], '')
+                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)
+            for lang in translate:
+                context2 = context.copy()
+                context2['lang'] = lang
+                self.write(cr, uid, [id], translate[lang], context2)
+            if config.get('import_partial', False) and filename and (not (counter%100)) :
+                data = pickle.load(file(config.get('import_partial')))
+                data[filename] = initial_size - len(datas) + original_value
+                pickle.dump(data, file(config.get('import_partial'),'wb'))
+                cr.commit()
+
+            #except Exception, e:
+            #    logger.notifyChannel("import", netsvc.LOG_ERROR, e)
+            #    cr.rollback()
+            #    try:
+            #        return (-1, res, e[0], warning)
+            #    except:
+            #        return (-1, res, e[0], '')
             done += 1
         #
         # TODO: Send a request with the result and multi-thread !
@@ -625,20 +680,27 @@ class orm_template(object):
     def read(self, cr, user, ids, fields=None, context=None, load='_classic_read'):
         raise _('The read method is not implemented on this object !')
 
+    def get_invalid_fields(self,cr,uid):
+        return list(self._invalids)
+
     def _validate(self, cr, uid, ids, context=None):
         context = context or {}
         lng = context.get('lang', False) or 'en_US'
         trans = self.pool.get('ir.translation')
-        field_error = []
-        field_err_str = []
+        error_msgs = []
         for constraint in self._constraints:
             fun, msg, fields = constraint
             if not fun(self, cr, uid, ids):
-                field_error += fields
-                field_err_str.append( trans._get_source(cr, uid, self._name, 'constraint', lng, source=msg) or msg )
-        if len(field_err_str):
+                translated_msg = trans._get_source(cr, uid, self._name, 'constraint', lng, source=msg) or msg
+                error_msgs.append(
+                        _("Error occurred while validating the field(s) %s: %s") % (','.join(fields), translated_msg)
+                )
+                self._invalids.update(fields)
+        if error_msgs:
             cr.rollback()
-            raise except_orm('ValidateError', ('\n'.join(field_err_str), ','.join(field_error)))
+            raise except_orm('ValidateError', '\n'.join(error_msgs))
+        else:
+            self._invalids.clear()
 
     def default_get(self, cr, uid, fields_list, context=None):
         return {}
@@ -657,6 +719,14 @@ class orm_template(object):
 
     # returns the definition of each field in the object
     # the optional fields parameter can limit the result to some fields
+    def fields_get_keys(self, cr, user, context=None, read_access=True):
+        if context is None:
+            context = {}
+        res = self._columns.keys()
+        for parent in self._inherits:
+            res.extend(self.pool.get(parent).fields_get_keys(cr, user, fields, context))
+        return res
+
     def fields_get(self, cr, user, fields=None, context=None, read_access=True):
         if context is None:
             context = {}
@@ -664,54 +734,56 @@ class orm_template(object):
         translation_obj = self.pool.get('ir.translation')
         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():
-            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'):
-                if hasattr(self._columns[f], arg) \
-                        and getattr(self._columns[f], arg):
-                    res[f][arg] = getattr(self._columns[f], arg)
-
-            # translate the field label
-            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 = 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
-
+            res.update(self.pool.get(parent).fields_get(cr, user, fields, 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
             for r in res.keys():
@@ -725,14 +797,14 @@ 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={}
+            context = {}
         result = False
         fields = {}
         childs = True
 
-        if node.nodeType==node.ELEMENT_NODE and node.localName=='field':
+        if node.nodeType == node.ELEMENT_NODE and node.localName == 'field':
             if node.hasAttribute('name'):
                 attrs = {}
                 try:
@@ -747,111 +819,102 @@ class orm_template(object):
                     childs = False
                     views = {}
                     for f in node.childNodes:
-                        if f.nodeType==f.ELEMENT_NODE and f.localName in ('form','tree','graph'):
+                        if f.nodeType == f.ELEMENT_NODE and f.localName in ('form', 'tree', 'graph'):
                             node.removeChild(f)
-                            xarch,xfields = self.pool.get(relation).__view_look_dom_arch(cr, user, f, context)
+                            ctx = context.copy()
+                            ctx['base_model_name'] = self._name
+                            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):
+                            attrs['selection'].append((False,''))
                 fields[node.getAttribute('name')] = attrs
 
-        elif node.nodeType==node.ELEMENT_NODE and node.localName in ('form','tree'):
+        elif node.nodeType==node.ELEMENT_NODE and node.localName in ('form', 'tree'):
             result = self.view_header_get(cr, user, False, node.localName, context)
             if result:
-                node.setAttribute('string', result.decode('utf-8'))
-        if node.nodeType==node.ELEMENT_NODE and node.hasAttribute('groups'):
-            groups = None
-            group_str = node.getAttribute('groups')
-            if ',' in group_str:
-                groups = group_str.split(',');
+                node.setAttribute('string', result)
+
+        elif node.nodeType==node.ELEMENT_NODE and node.localName == 'calendar':
+            for additional_field in ('date_start', 'date_delay', 'date_stop', 'color'):
+                if node.hasAttribute(additional_field) and node.getAttribute(additional_field):
+                    fields[node.getAttribute(additional_field)] = {}
+
+        if node.nodeType == node.ELEMENT_NODE and node.hasAttribute('groups'):
+            if node.getAttribute('groups'):
+                groups = node.getAttribute('groups').split(',')
                 readonly = False
                 access_pool = self.pool.get('ir.model.access')
                 for group in groups:
-                    readonly = readonly and access_pool.check_groups(cr, user, group)
-
-                if readonly:
-                    parent = node.parentNode
-                    parent.removeChild(node)
-
-            else:
-                if not self.pool.get('ir.model.access').check_groups(cr, user, group_str):
-                    parent = node.parentNode
-                    parent.removeChild(node)
+                    readonly = readonly or access_pool.check_groups(cr, user, group)
+                if not readonly:
+                    node.setAttribute('invisible', '1')
+            node.removeAttribute('groups')
 
         if node.nodeType == node.ELEMENT_NODE:
             # translate view
             if ('lang' in context) and not result:
                 if node.hasAttribute('string') and node.getAttribute('string'):
-                    trans = tools.translate(cr, self._name, 'view', context['lang'], node.getAttribute('string').encode('utf8'))
+                    trans = self.pool.get('ir.translation')._get_source(cr, user, self._name, 'view', context['lang'], node.getAttribute('string').encode('utf8'))
+                    if not trans and ('base_model_name' in context):
+                        trans = self.pool.get('ir.translation')._get_source(cr, user, context['base_model_name'], 'view', context['lang'], node.getAttribute('string').encode('utf8'))
                     if trans:
-                        node.setAttribute('string', trans.decode('utf8'))
+                        node.setAttribute('string', trans)
                 if node.hasAttribute('sum') and node.getAttribute('sum'):
-                    trans = tools.translate(cr, self._name, 'view', context['lang'], node.getAttribute('sum').encode('utf8'))
+                    trans = self.pool.get('ir.translation')._get_source(cr, user, self._name, 'view', context['lang'], node.getAttribute('sum').encode('utf8'))
                     if trans:
-                        node.setAttribute('sum', trans.decode('utf8'))
-            #
-            # Add view for properties !
-            #
-            if node.localName=='properties':
-                parent = node.parentNode
-                doc = node.ownerDocument
-                models = map(lambda x: "'"+x+"'", [self._name] + self._inherits.keys())
-                cr.execute('select id,name,group_name from ir_model_fields where model in ('+','.join(models)+') and view_load order by group_name, id')
-                oldgroup = None
-                res = cr.fetchall()
-                for id, fname, gname in res:
-                    if oldgroup != gname:
-                        child = doc.createElement('separator')
-                        child.setAttribute('string', gname.decode('utf-8'))
-                        child.setAttribute('colspan', "4")
-                        oldgroup = gname
-                        parent.insertBefore(child, node)
-
-                    child = doc.createElement('field')
-                    child.setAttribute('name', fname.decode('utf-8'))
-                    parent.insertBefore(child, node)
-                parent.removeChild(node)
+                        node.setAttribute('sum', trans)
 
         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):
-        if not context:
-            context={}
-        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)
 
-        buttons = xpath.Evaluate('//button', node)
-        if buttons:
-            for button in buttons:
-                if button.getAttribute('type') == 'object':
-                    continue
+        rolesobj = self.pool.get('res.roles')
+        usersobj = self.pool.get('res.users')
 
-                ok = True
-
-                serv = netsvc.LocalService('object_proxy')
-                user_roles = serv.execute_cr(cr, user, 'res.users', 'read', [user], ['roles_id'])[0]['roles_id']
-                cr.execute("select role_id from wkf_transition where signal='%s'" % button.getAttribute('name'))
+        buttons = xpath.Evaluate("//button[@type != 'object']", node)
+        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 serv.execute_cr(cr, user, 'res.roles', 'check', user_roles, 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')
+            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])
-        return arch, fields
+            if 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):
         """Generate a default calendar view (For internal use only).
@@ -889,18 +952,23 @@ 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={}
+            context = {}
+        
+        def encode(s):
+            if isinstance(s, unicode):
+                return s.encode('utf8')
+            return s 
+
         def _inherit_apply(src, inherit):
             def _find(node, node2):
-                # Check if xpath query or normal inherit (with field matching)
-                if node2.nodeType==node2.ELEMENT_NODE and node2.localName=='xpath':
+                if node2.nodeType == node2.ELEMENT_NODE and node2.localName == 'xpath':
                     res = xpath.Evaluate(node2.getAttribute('expr'), node)
                     return res and res[0]
                 else:
-                    if node.nodeType==node.ELEMENT_NODE and node.localName==node2.localName:
+                    if node.nodeType == node.ELEMENT_NODE and node.localName == node2.localName:
                         res = True
                         for attr in node2.attributes.keys():
-                            if attr=='position':
+                            if attr == 'position':
                                 continue
                             if node.hasAttribute(attr):
                                 if node.getAttribute(attr)==node2.getAttribute(attr):
@@ -910,17 +978,19 @@ class orm_template(object):
                             return node
                     for child in node.childNodes:
                         res = _find(child, node2)
-                        if res: return res
+                        if res:
+                            return res
                 return None
+            
 
-            doc_src = dom.minidom.parseString(src)
-            doc_dest = dom.minidom.parseString(inherit)
+            doc_src = dom.minidom.parseString(encode(src))
+            doc_dest = dom.minidom.parseString(encode(inherit))
             toparse = doc_dest.childNodes
             while len(toparse):
                 node2 = toparse.pop(0)
-                if not node2.nodeType==node2.ELEMENT_NODE:
+                if not node2.nodeType == node2.ELEMENT_NODE:
                     continue
-                if node2.localName=='data':
+                if node2.localName == 'data':
                     toparse += node2.childNodes
                     continue
                 node = _find(doc_src, node2)
@@ -928,27 +998,24 @@ class orm_template(object):
                     pos = 'inside'
                     if node2.hasAttribute('position'):
                         pos = node2.getAttribute('position')
-                    if pos=='replace':
+                    if pos == 'replace':
                         parent = node.parentNode
                         for child in node2.childNodes:
-                            if child.nodeType==child.ELEMENT_NODE:
+                            if child.nodeType == child.ELEMENT_NODE:
                                 parent.insertBefore(child, node)
                         parent.removeChild(node)
                     else:
+                        sib = node.nextSibling
                         for child in node2.childNodes:
-                            if child.nodeType==child.ELEMENT_NODE:
-                                if pos=='inside':
+                            if child.nodeType == child.ELEMENT_NODE:
+                                if pos == 'inside':
                                     node.appendChild(child)
-                                elif pos=='after':
-                                    sib = node.nextSibling
-                                    if sib:
-                                        node.parentNode.insertBefore(child, sib)
-                                    else:
-                                        node.parentNode.appendChild(child)
+                                elif pos == 'after':
+                                    node.parentNode.insertBefore(child, sib)
                                 elif pos=='before':
                                     node.parentNode.insertBefore(child, node)
                                 else:
-                                    raise AttributeError, _('Unknown position in inherited view %s !') % pos
+                                    raise AttributeError(_('Unknown position in inherited view %s !') % pos)
                 else:
                     attrs = ''.join([
                         ' %s="%s"' % (attr, node2.getAttribute(attr))
@@ -956,10 +1023,10 @@ class orm_template(object):
                         if attr != 'position'
                     ])
                     tag = "<%s%s>" % (node2.localName, attrs)
-                    raise AttributeError, _("Couldn't find tag '%s' in parent view !") % tag
+                    raise AttributeError(_("Couldn't find tag '%s' in parent view !\n%s") % (tag,src))
             return doc_src.toxml(encoding="utf-8").replace('\t', '')
 
-        result = {'type':view_type, 'model':self._name}
+        result = {'type': view_type, 'model': self._name}
 
         ok = True
         model = True
@@ -967,9 +1034,17 @@ class orm_template(object):
         while ok:
             if view_id:
                 where = (model and (" and model='%s'" % (self._name,))) or ''
-                cr.execute('SELECT arch,name,field_parent,id,type,inherit_id FROM ir_ui_view WHERE id=%d'+where, (view_id,))
+                cr.execute('SELECT arch,name,field_parent,id,type,inherit_id FROM ir_ui_view WHERE id=%s'+where, (view_id,))
             else:
-                cr.execute('SELECT arch,name,field_parent,id,type,inherit_id FROM ir_ui_view WHERE model=%s AND type=%s ORDER BY priority', (self._name,view_type))
+                cr.execute('''SELECT
+                        arch,name,field_parent,id,type,inherit_id
+                    FROM
+                        ir_ui_view
+                    WHERE
+                        model=%s AND
+                        type=%s AND
+                        inherit_id IS NULL
+                    ORDER BY priority''', (self._name, view_type))
             sql_res = cr.fetchone()
             if not sql_res:
                 break
@@ -985,9 +1060,9 @@ class orm_template(object):
 
             def _inherit_apply_rec(result, inherit_id):
                 # get all views which inherit from (ie modify) this view
-                cr.execute('select arch,id from ir_ui_view where inherit_id=%d and model=%s order by priority', (inherit_id, self._name))
+                cr.execute('select arch,id from ir_ui_view where inherit_id=%s and model=%s order by priority', (inherit_id, self._name))
                 sql_inherit = cr.fetchall()
-                for (inherit,id) in sql_inherit:
+                for (inherit, id) in sql_inherit:
                     result = _inherit_apply(result, inherit)
                     result = _inherit_apply_rec(result, id)
                 return result
@@ -1000,8 +1075,8 @@ class orm_template(object):
             # otherwise, build some kind of default view
             if view_type == 'form':
                 res = self.fields_get(cr, user, context=context)
-                xml = '''<?xml version="1.0" encoding="utf-8"?>''' \
-                '''<form string="%s">''' % (self._description,)
+                xml = '<?xml version="1.0" encoding="utf-8"?> ' \
+                     '<form string="%s">' % (self._description,)
                 for x in res:
                     if res[x]['type'] not in ('one2many', 'many2many'):
                         xml += '<field name="%s"/>' % (x,)
@@ -1009,20 +1084,29 @@ class orm_template(object):
                             xml += "<newline/>"
                 xml += "</form>"
             elif view_type == 'tree':
-                xml = '''<?xml version="1.0" encoding="utf-8"?>''' \
-                '''<tree string="%s"><field name="%s"/></tree>''' \
-                % (self._description, self._rec_name)
+                _rec_name = self._rec_name
+                if _rec_name not in self._columns:
+                    _rec_name = self._columns.keys()[0]
+                xml = '<?xml version="1.0" encoding="utf-8"?>' \
+                       '<tree string="%s"><field name="%s"/></tree>' \
+                       % (self._description, self._rec_name)
             elif view_type == 'calendar':
                 xml = self.__get_default_calendar_view()
             else:
-                xml = ''
+                xml = '<?xml version="1.0"?>'  # what happens here, graph case?
             result['arch'] = xml
             result['name'] = 'default'
             result['field_parent'] = False
             result['view_id'] = 0
 
-        doc = dom.minidom.parseString(result['arch'].encode('utf-8'))
-        xarch, xfields = self.__view_look_dom_arch(cr, user, doc, context=context)
+       try:
+               doc = dom.minidom.parseString(encode(result['arch']))
+       except Exception, ex:
+               logger = netsvc.Logger()
+               logger.notifyChannel('init', netsvc.LOG_DEBUG, 'Wrong arch in %s (%s):\n %s' % (result['name'], view_type, result['arch'] ))
+               raise except_orm('Error',
+                        ('Invalid xml in view %s(%d) of %s: %s' % (result['name'], result['view_id'], self._name, str(ex))))
+        xarch, xfields = self.__view_look_dom_arch(cr, user, doc, view_id, context=context)
         result['arch'] = xarch
         result['fields'] = xfields
         if toolbar:
@@ -1047,9 +1131,9 @@ class orm_template(object):
                     context)
             resprint = map(clean, resprint)
             resaction = map(clean, resaction)
-            resaction = filter(lambda x: not x.get('multi',False), resaction)
-            resprint = filter(lambda x: not x.get('multi',False), resprint)
-            resrelate = map(lambda x:x[2], resrelate)
+            resaction = filter(lambda x: not x.get('multi', False), resaction)
+            resprint = filter(lambda x: not x.get('multi', False), resprint)
+            resrelate = map(lambda x: x[2], resrelate)
 
             for x in resprint+resaction+resrelate:
                 x['string'] = x['name']
@@ -1061,7 +1145,7 @@ class orm_template(object):
             }
         return result
 
-    _view_look_dom_arch=__view_look_dom_arch
+    _view_look_dom_arch = __view_look_dom_arch
 
     def search_count(self, cr, user, args, context=None):
         if not context:
@@ -1078,18 +1162,59 @@ class orm_template(object):
     def name_get(self, cr, user, ids, context=None):
         raise _('The name_get method is not implemented on this object !')
 
-    def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=80):
+    def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=None):
         raise _('The name_search method is not implemented on this object !')
 
     def copy(self, cr, uid, id, default=None, context=None):
         raise _('The copy 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')
+        if not fields:
+            fields = self._columns.keys() + self._inherit_fields.keys()
+        for lang in langs:
+            res[lang] = {'code': lang}
+            for f in fields:
+                if f in self._columns:
+                    res_trans = self.pool.get('ir.translation')._get_source(cr, uid, self._name+','+f, 'field', lang)
+                    if res_trans:
+                        res[lang][f] = res_trans
+                    else:
+                        res[lang][f] = self._columns[f].string
+        for table in self._inherits:
+            cols = intersect(self._inherit_fields.keys(), fields)
+            res2 = self.pool.get(table).read_string(cr, uid, id, langs, cols, context)
+        for lang in res2:
+            if lang in res:
+                res[lang]['code'] = lang
+            for f in res2[lang]:
+                res[lang][f] = res2[lang][f]
+        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')
+        for lang in langs:
+            for field in vals:
+                if field in self._columns:
+                    self.pool.get('ir.translation')._set_ids(cr, uid, self._name+','+field, 'field', lang, [0], vals[field])
+        for table in self._inherits:
+            cols = intersect(self._inherit_fields.keys(), vals)
+            if cols:
+                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']
     _inherit_fields = {}
     _max_count = 200
     _max_hours = 1
     _check_time = 20
+
     def __init__(self, cr):
         super(orm_memory, self).__init__(cr)
         self.datas = {}
@@ -1097,8 +1222,8 @@ class orm_memory(orm_template):
         self.check_id = 0
         cr.execute('delete from wkf_instance where res_type=%s', (self._name,))
 
-    def clear(self):
-        self.check_id+=1
+    def vaccum(self, cr, uid):
+        self.check_id += 1
         if self.check_id % self._check_time:
             return True
         tounlink = []
@@ -1106,29 +1231,40 @@ class orm_memory(orm_template):
         for id in self.datas:
             if self.datas[id]['internal.date_access'] < max:
                 tounlink.append(id)
-        self.unlink(tounlink)
+        self.unlink(cr, uid, tounlink)
         if len(self.datas)>self._max_count:
             sorted = map(lambda x: (x[1]['internal.date_access'], x[0]), self.datas.items())
             sorted.sort()
             ids = map(lambda x: x[1], sorted[:len(self.datas)-self._max_count])
-            self.unlink(ids)
+            self.unlink(cr, uid, ids)
         return True
 
-    def read(self, cr, user, ids, fields=None, context=None, load='_classic_read'):
-        if not fields:
-            fields = self._columns.keys()
+    def read(self, cr, user, ids, fields_to_read=None, context=None, load='_classic_read'):
+        if not context:
+            context = {}
+        if not fields_to_read:
+            fields_to_read = self._columns.keys()
         result = []
-        for id in ids:
-            r = {'id': id}
-            for f in fields:
-                r[f] = self.datas[id].get(f, False)
-            result.append(r)
-            self.datas[id]['internal.date_access'] = time.time()
-        fields_post = filter(lambda x: x in self._columns and not getattr(self._columns[x], load), fields)
-        for f in fields_post:
-            res2 = self._columns[f].get_memory(cr, self, ids, f, user, context=context, values=False)
-            for record in result:
-                record[f] = res2[record['id']]
+        if self.datas:
+            if isinstance(ids, (int, long)):
+                ids = [ids]
+            for id in ids:
+                r = {'id': id}
+                for f in fields_to_read:
+                    if id in self.datas:
+                        r[f] = self.datas[id].get(f, False)
+                        if r[f] and isinstance(self._columns[f], fields.binary) and context.get('bin_size', False):
+                            r[f] = len(r[f])
+                result.append(r)
+                if id in self.datas:
+                    self.datas[id]['internal.date_access'] = time.time()
+            fields_post = filter(lambda x: x in self._columns and not getattr(self._columns[x], load), fields_to_read)
+            for f in fields_post:
+                res2 = self._columns[f].get_memory(cr, self, ids, f, user, context=context, values=result)
+                for record in result:
+                    record[f] = res2[record['id']]
+            if isinstance(ids, (int, long)):
+                return result[0]
         return result
 
     def write(self, cr, user, ids, vals, context=None):
@@ -1147,7 +1283,7 @@ 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.clear()
+        self.vaccum(cr, user)
         return id_new
 
     def create(self, cr, user, vals, context=None):
@@ -1168,24 +1304,25 @@ class orm_memory(orm_template):
                 upd_todo.append(field)
         self.datas[id_new] = vals2
         self.datas[id_new]['internal.date_access'] = time.time()
+
         for field in upd_todo:
             self._columns[field].set_memory(cr, self, id_new, field, vals[field], user, context)
         self._validate(cr, user, [id_new], context)
         wf_service = netsvc.LocalService("workflow")
         wf_service.trg_create(user, self._name, id_new, cr)
-        self.clear()
+        self.vaccum(cr, user)
         return id_new
 
     def default_get(self, cr, uid, fields_list, context=None):
         if not context:
-            context={}
+            context = {}
         value = {}
         # get the default values for the inherited fields
         for f in fields_list:
             if f in self._defaults:
                 value[f] = self._defaults[f](self, cr, uid, context)
             fld_def = ((f in self._columns) and self._columns[f]) \
-                    or ((f in self._inherit_fields ) and self._inherit_fields[f][2]) \
+                    or ((f in self._inherit_fields) and self._inherit_fields[f][2]) \
                     or False
 
         # get the default values set by the user and override the default
@@ -1194,7 +1331,7 @@ class orm_memory(orm_template):
         res = ir_values_obj.get(cr, uid, 'default', False, [self._name])
         for id, field, field_value in res:
             if field in fields_list:
-                fld_def = (field in self._columns)
+                fld_def = (field in self._columns) and self._columns[field] or self._inherit_fields[field][2]
                 if fld_def._type in ('many2one', 'one2one'):
                     obj = self.pool.get(fld_def._obj)
                     if not obj.search(cr, uid, [('id', '=', field_value)]):
@@ -1223,6 +1360,11 @@ class orm_memory(orm_template):
                             field_value2[i][field2] = field_value[i][field2]
                     field_value = field_value2
                 value[field] = field_value
+
+        # get the default values from the context
+        for key in context or {}:
+            if key.startswith('default_'):
+                value[key[8:]] = context[key]
         return value
 
     def search(self, cr, user, args, offset=0, limit=None, order=None,
@@ -1234,7 +1376,7 @@ class orm_memory(orm_template):
             if id in self.datas:
                 del self.datas[id]
         if len(ids):
-            cr.execute('delete from wkf_instance where res_type=%s and res_id in ('+','.join(map(str,ids))+')', (self._name, ))
+            cr.execute('delete from wkf_instance where res_type=%s and res_id in ('+','.join(map(str, ids))+')', (self._name, ))
         return True
 
     def perm_read(self, cr, user, ids, context=None, details=True):
@@ -1248,17 +1390,87 @@ class orm_memory(orm_template):
                 'id': id
             })
         return result
+    
+    def _check_removed_columns(self, cr, log=False):
+        # nothing to check in memory...
+        pass
 
 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']
+
+    def _parent_store_compute(self, cr):
+        logger = netsvc.Logger()
+        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()
+            for id in childs:
+                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
+        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('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)
+        ids_lst = map(lambda x: x[0], cr.fetchall())
+        while ids_lst:
+            iids = ids_lst[:40]
+            ids_lst = ids_lst[40:]
+            res = f.get(cr, self, iids, k, 1, {})
+            for key,val in res.items():
+                if f._multi:
+                    val = val[k]
+                # if val is a many2one, just write the ID
+                if type(val)==tuple:
+                    val = val[0]
+                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()
         create = False
+        todo_end = []
         self._field_create(cr, context=context)
         if not hasattr(self, "_auto") or self._auto:
             cr.execute("SELECT relname FROM pg_class WHERE relkind in ('r','v') AND relname='%s'" % self._table)
@@ -1266,6 +1478,23 @@ class orm(orm_template):
                 cr.execute("CREATE TABLE \"%s\" (id SERIAL NOT NULL, PRIMARY KEY(id)) WITH OIDS" % self._table)
                 create = True
             cr.commit()
+            if self._parent_store:
+                cr.execute("""SELECT c.relname
+                    FROM pg_class c, pg_attribute a
+                    WHERE c.relname=%s AND a.attname=%s AND c.oid=a.attrelid
+                    """, (self._table, 'parent_left'))
+                if not cr.rowcount:
+                    if 'parent_left' not in self._columns:
+                        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('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('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()
+                    store_compute = True
+
             if self._log_access:
                 logs = {
                     'create_uid': 'INTEGER REFERENCES res_users ON DELETE SET NULL',
@@ -1274,32 +1503,21 @@ class orm(orm_template):
                     'write_date': 'TIMESTAMP'
                 }
                 for k in logs:
-                    cr.execute(
-                        """
+                    cr.execute("""
                         SELECT c.relname
-                        FROM pg_class c, pg_attribute a
-                        WHERE c.relname='%s' AND a.attname='%s' AND c.oid=a.attrelid
-                        """ % (self._table, k))
+                          FROM pg_class c, pg_attribute a
+                         WHERE c.relname=%s AND a.attname=%s AND c.oid=a.attrelid
+                        """, (self._table, k))
                     if not cr.rowcount:
-                        cr.execute("ALTER TABLE \"%s\" ADD COLUMN \"%s\" %s" %
-                            (self._table, k, logs[k]))
+                        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 = []
             for k in self._columns:
-                if k in ('id','write_uid','write_date','create_uid','create_date'):
+                if k in ('id', 'write_uid', 'write_date', 'create_uid', 'create_date'):
                     continue
                     #raise _('Can not define a column %s. Reserved keyword !') % (k,)
                 f = self._columns[k]
@@ -1307,10 +1525,10 @@ class orm(orm_template):
                 if isinstance(f, fields.one2many):
                     cr.execute("SELECT relname FROM pg_class WHERE relkind='r' AND relname=%s", (f._obj,))
                     if cr.fetchone():
-                        cr.execute("SELECT count(*) as c FROM pg_class c,pg_attribute a WHERE c.relname=%s AND a.attname=%s AND c.oid=a.attrelid", (f._obj,f._fields_id))
+                        cr.execute("SELECT count(1) as c FROM pg_class c,pg_attribute a WHERE c.relname=%s AND a.attname=%s AND c.oid=a.attrelid", (f._obj, f._fields_id))
                         res = cr.fetchone()[0]
                         if not res:
-                            cr.execute("ALTER TABLE \"%s\" ADD FOREIGN KEY (%s) REFERENCES \"%s\" ON DELETE SET NULL" % (self._obj, f._fields_id, f._table))
+                            cr.execute('ALTER TABLE "%s" ADD FOREIGN KEY (%s) REFERENCES "%s" ON DELETE SET NULL' % (self._obj, f._fields_id, f._table))
                 elif isinstance(f, fields.many2many):
                     cr.execute("SELECT relname FROM pg_class WHERE relkind in ('r','v') AND relname=%s", (f._rel,))
                     if not cr.dictfetchall():
@@ -1318,36 +1536,41 @@ class orm(orm_template):
                         try:
                             ref = self.pool.get(f._obj)._table
                         except AttributeError:
-                            ref = f._obj.replace('.','_')
-                        cr.execute("CREATE TABLE \"%s\" (\"%s\" INTEGER NOT NULL REFERENCES \"%s\" ON DELETE CASCADE, \"%s\" INTEGER NOT NULL REFERENCES \"%s\" ON DELETE CASCADE) WITH OIDS"%(f._rel,f._id1,self._table,f._id2,ref))
-                        cr.execute("CREATE INDEX \"%s_%s_index\" ON \"%s\" (\"%s\")" % (f._rel,f._id1,f._rel,f._id1))
-                        cr.execute("CREATE INDEX \"%s_%s_index\" ON \"%s\" (\"%s\")" % (f._rel,f._id2,f._rel,f._id2))
+                            ref = f._obj.replace('.', '_')
+                        cr.execute('CREATE TABLE "%s" ("%s" INTEGER NOT NULL REFERENCES "%s" ON DELETE CASCADE, "%s" INTEGER NOT NULL REFERENCES "%s" ON DELETE CASCADE) WITH OIDS' % (f._rel, f._id1, self._table, f._id2, ref))
+                        cr.execute('CREATE INDEX "%s_%s_index" ON "%s" ("%s")' % (f._rel, f._id1, f._rel, f._id1))
+                        cr.execute('CREATE INDEX "%s_%s_index" ON "%s" ("%s")' % (f._rel, f._id2, f._rel, f._id2))
                         cr.commit()
                 else:
-                    cr.execute("SELECT c.relname,a.attname,a.attlen,a.atttypmod,a.attnotnull,a.atthasdef,t.typname,CASE WHEN a.attlen=-1 THEN a.atttypmod-4 ELSE a.attlen END as size FROM pg_class c,pg_attribute a,pg_type t WHERE c.relname=%s AND a.attname=%s AND c.oid=a.attrelid AND a.atttypid=t.oid", (self._table, k))
+                    cr.execute("SELECT c.relname,a.attname,a.attlen,a.atttypmod,a.attnotnull,a.atthasdef,t.typname,CASE WHEN a.attlen=-1 THEN a.atttypmod-4 ELSE a.attlen END as size " \
+                               "FROM pg_class c,pg_attribute a,pg_type t " \
+                               "WHERE c.relname=%s " \
+                               "AND a.attname=%s " \
+                               "AND c.oid=a.attrelid " \
+                               "AND a.atttypid=t.oid", (self._table, k))
                     res = cr.dictfetchall()
                     if not res:
-                        if not isinstance(f,fields.function) or f.store:
+                        if not isinstance(f, fields.function) or f.store:
 
                             # add the missing field
-                            cr.execute("ALTER TABLE \"%s\" ADD COLUMN \"%s\" %s" % (self._table, k, get_pg_type(f)[1]))
+                            cr.execute('ALTER TABLE "%s" ADD COLUMN "%s" %s' % (self._table, k, get_pg_type(f)[1]))
 
                             # initialize it
                             if not create and k in self._defaults:
                                 default = self._defaults[k](self, cr, 1, {})
-                                if not default:
-                                    cr.execute("UPDATE \"%s\" SET \"%s\"=NULL" % (self._table, k))
-                                else:
-                                    cr.execute("UPDATE \"%s\" SET \"%s\"='%s'" % (self._table, k, default))
-                            if isinstance(f,fields.function):
-                                cr.execute('select id from '+self._table)
-                                ids_lst = map(lambda x: x[0], cr.fetchall())
-                                while ids_lst:
-                                    iids = ids_lst[:40]
-                                    ids_lst = ids_lst[40:]
-                                    res = f.get(cr, self, iids, k, 1, {})
-                                    for r in res.items():
-                                        cr.execute("UPDATE \"%s\" SET \"%s\"='%s' where id=%d"% (self._table, k, r[1],r[0]))
+                                ss = self._columns[k]._symbol_set
+                                query = 'UPDATE "%s" SET "%s"=%s' % (self._table, k, ss[0])
+                                cr.execute(query, (ss[1](default),))
+                                cr.commit()
+                                logger.notifyChannel('orm', netsvc.LOG_DEBUG, 'setting default value of new column %s of table %s'% (k, self._table))
+                            elif not create:
+                                logger.notifyChannel('orm', netsvc.LOG_DEBUG, 'creating new column %s of table %s'% (k, self._table))
+
+                            if isinstance(f, fields.function):
+                                order = 10
+                                if f.store is not True:
+                                    order = f.store[f.store.keys()[0]][2]
+                                todo_update_store.append((order, f,k))
 
                             # and add constraints if needed
                             if isinstance(f, fields.many2one):
@@ -1355,18 +1578,18 @@ class orm(orm_template):
                                 try:
                                     ref = self.pool.get(f._obj)._table
                                 except AttributeError:
-                                    ref = f._obj.replace('.','_')
+                                    ref = f._obj.replace('.', '_')
                                 # ir_actions is inherited so foreign key doesn't work on it
-                                if ref <> 'ir_actions':
-                                    cr.execute("ALTER TABLE \"%s\" ADD FOREIGN KEY (\"%s\") REFERENCES \"%s\" ON DELETE %s" % (self._table, k, ref, f.ondelete))
+                                if ref != 'ir_actions':
+                                    cr.execute('ALTER TABLE "%s" ADD FOREIGN KEY ("%s") REFERENCES "%s" ON DELETE %s' % (self._table, k, ref, f.ondelete))
                             if f.select:
-                                cr.execute("CREATE INDEX \"%s_%s_index\" ON \"%s\" (\"%s\")" % (self._table, k, self._table, k))
+                                cr.execute('CREATE INDEX "%s_%s_index" ON "%s" ("%s")' % (self._table, k, self._table, k))
                             if f.required:
-                                cr.commit()
                                 try:
-                                    cr.execute("ALTER TABLE \"%s\" ALTER COLUMN \"%s\" SET NOT NULL" % (self._table, k))
-                                except:
-                                    logger.notifyChannel('init', netsvc.LOG_WARNING, 'WARNING: unable to set column %s of table %s not null !\nTry to re-run: tinyerp-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()
+                                    cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" SET NOT NULL' % (self._table, k))
+                                except Exception, e:
+                                    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]
@@ -1374,72 +1597,89 @@ 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_WARNING, 'column %s (%s) in table %s was converted to a function !\nYou should remove this column from your database.' % (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
                         else:
                             f_obj_type = get_pg_type(f) and get_pg_type(f)[0]
 
                         if f_obj_type:
-                            if f_pg_type != f_obj_type:
-                                logger.notifyChannel('init', netsvc.LOG_WARNING, "column '%s' in table '%s' has changed type (DB = %s, def = %s) !" % (k, self._table, f_pg_type, f._type))
+                            ok = False
+                            casts = [
+                                ('text', 'char', 'VARCHAR(%d)' % (f.size or 0,), '::VARCHAR(%d)'%(f.size or 0,)),
+                                ('varchar', 'text', 'TEXT', ''),
+                                ('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:
-                                # columns with the name 'type' cannot be changed for an unknown reason?!
-                                if k != 'type':
-                                    if f_pg_size > f.size:
-                                        logger.notifyChannel('init', netsvc.LOG_WARNING, "column '%s' in table '%s' has changed size (DB = %d, def = %d), DB size will be kept !" % (k, self._table, f_pg_size, f.size))
-                                    # If actual DB size is < than new
-                                    # We update varchar size, otherwise, we keep DB size
-                                    # to avoid truncated string...
-                                    if f_pg_size < f.size:
-                                        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))
-                                        cr.execute("ALTER TABLE \"%s\" DROP COLUMN temp_change_size" % (self._table,))
-                                        cr.commit()
+                                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))
+                                cr.execute('ALTER TABLE "%s" DROP COLUMN temp_change_size' % (self._table,))
+                                cr.commit()
+                            for c in casts:
+                                if (f_pg_type==c[0]) and (f._type==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]))
+                                    cr.execute(('UPDATE "%s" SET "%s"=temp_change_size'+c[3]) % (self._table, k))
+                                    cr.execute('ALTER TABLE "%s" DROP COLUMN temp_change_size CASCADE' % (self._table,))
+                                    cr.commit()
+
+                            if f_pg_type != f_obj_type:
+                                if not ok:
+                                    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:
                                 # set the field to the default value if any
-                                if self._defaults.has_key(k):
+                                if k in self._defaults:
                                     default = self._defaults[k](self, cr, 1, {})
-                                    if not (default is False):
-                                        cr.execute("UPDATE \"%s\" SET \"%s\"='%s' WHERE %s is NULL" % (self._table, k, default, k))
-                                        cr.commit()
+                                    if (default is not None):
+                                        ss = self._columns[k]._symbol_set
+                                        query = 'UPDATE "%s" SET "%s"=%s WHERE %s is NULL' % (self._table, k, ss[0], k)
+                                        cr.execute(query, (ss[1](default),))
                                 # add the NOT NULL constraint
+                                cr.commit()
                                 try:
-                                    cr.execute("ALTER TABLE \"%s\" ALTER COLUMN \"%s\" SET NOT NULL" % (self._table, k))
+                                    cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" SET NOT NULL' % (self._table, k))
                                     cr.commit()
-                                except:
-                                    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))
+                                except Exception, e:
+                                    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))
+                                cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" DROP NOT NULL' % (self._table, k))
                                 cr.commit()
-                            cr.execute("SELECT indexname FROM pg_indexes WHERE indexname = '%s_%s_index' and tablename = '%s'" % (self._table, k, self._table))
+                            indexname = '%s_%s_index' % (self._table, k)
+                            cr.execute("SELECT indexname FROM pg_indexes WHERE indexname = %s and tablename = %s", (indexname, self._table))
                             res = cr.dictfetchall()
                             if not res and f.select:
-                                cr.execute("CREATE INDEX \"%s_%s_index\" ON \"%s\" (\"%s\")" % (self._table, k, self._table, k))
+                                cr.execute('CREATE INDEX "%s_%s_index" ON "%s" ("%s")' % (self._table, k, self._table, k))
                                 cr.commit()
                             if res and not f.select:
-                                cr.execute("DROP INDEX \"%s_%s_index\"" % (self._table, k))
+                                cr.execute('DROP INDEX "%s_%s_index"' % (self._table, k))
                                 cr.commit()
                             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 ' \
-                                                'AND con.contype = \'f\'', (self._table, ref, k, 'id'))
+                                    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:
                                         confdeltype = {
@@ -1454,62 +1694,76 @@ class orm(orm_template):
                                             cr.execute('ALTER TABLE "' + self._table + '" ADD FOREIGN KEY ("' + k + '") REFERENCES "' + ref + '" ON DELETE ' + f.ondelete)
                                             cr.commit()
                     else:
-                        print "ERROR"
+                        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)))
+
         else:
-            cr.execute("SELECT relname FROM pg_class WHERE relkind in ('r','v') AND relname='%s'" % self._table)
+            cr.execute("SELECT relname FROM pg_class WHERE relkind in ('r','v') AND relname=%s", (self._table,))
             create = not bool(cr.fetchone())
 
-        for (key,con,_) in self._sql_constraints:
-            cr.execute("SELECT conname FROM pg_constraint where conname='%s_%s'" % (self._table, key))
+        for (key, con, _) in self._sql_constraints:
+            conname = '%s_%s' % (self._table, key)
+            cr.execute("SELECT conname FROM pg_constraint where conname=%s", (conname,))
             if not cr.dictfetchall():
                 try:
-                    cr.execute('alter table \"%s\" add constraint \"%s_%s\" %s' % (self._table,self._table,key, con,))
+                    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"):
+            if hasattr(self, "_sql"):
                 for line in self._sql.split(';'):
-                    line2 = line.replace('\n','').strip()
+                    line2 = line.replace('\n', '').strip()
                     if line2:
                         cr.execute(line2)
                         cr.commit()
+        if store_compute:
+            self._parent_store_compute(cr)
+        return todo_end
 
     def __init__(self, cr):
         super(orm, self).__init__(cr)
-        f=filter(lambda a: isinstance(self._columns[a], fields.function) and self._columns[a].store, self._columns)
-        if f:
-            list_store = []
-            tuple_store = ()
-            tuple_fn = ()
-            for store_field in f:
-                if not self._columns[store_field].store == True:
-                    dict_store = self._columns[store_field].store
-                    key = dict_store.keys()
-                    list_data=[]
-                    for i in key:
-                        tuple_store=self._name,store_field,self._columns[store_field]._fnct.__name__,tuple(dict_store[i][0]),dict_store[i][1],i
-                        list_data.append(tuple_store)
-                    #tuple_store=self._name,store_field,self._columns[store_field]._fnct.__name__,tuple(dict_store[key[0]][0]),dict_store[key[0]][1]
-                    for l in list_data:
-                        list_store=[]
-                        if l[5] in self.pool._store_function.keys():
-                            self.pool._store_function[l[5]].append(l)
-                            temp_list = list(set(self.pool._store_function[l[5]]))
-                            self.pool._store_function[l[5]]=temp_list
-                        else:
-                            list_store.append(l)
-                            self.pool._store_function[l[5]]=list_store
-
-        for (key,_,msg) in self._sql_constraints:
+        
+        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]
+            if not isinstance(f, fields.function):
+                continue
+            if not f.store:
+                continue
+            if self._columns[store_field].store is True:
+                sm = {self._name:(lambda self,cr, uid, ids, c={}: ids, None, 10)}
+            else:
+                sm = self._columns[store_field].store
+            for object, aa in sm.items():
+                if len(aa)==3:
+                    (fnct,fields2,order)=aa
+                else:
+                    raise except_orm('Error',
+                        ('Invalid function definition %s in object %s !\nYou must use the definition: store={object:(fnct, fields, priority)}.' % (store_field, self._name)))
+                self.pool._store_function.setdefault(object, [])
+                ok = True
+                for x,y,z,e,f in self.pool._store_function[object]:
+                    if (x==self._name) and (y==store_field) and (e==fields2):
+                        ok = False
+                if ok:
+                    self.pool._store_function[object].append( (self._name, store_field, fnct, fields2, order))
+                    self.pool._store_function[object].sort(lambda x,y: cmp(x[4],y[4]))
+
+        for (key, _, msg) in self._sql_constraints:
             self.pool._sql_error[self._table+'_'+key] = msg
 
         # Load manual fields
 
-        cr.execute("SELECT id FROM ir_model_fields WHERE name=%s AND model=%s", ('state','ir.model.fields'))
+        cr.execute("SELECT id FROM ir_model_fields WHERE name=%s AND model=%s", ('state', 'ir.model.fields'))
         if cr.fetchone():
-            cr.execute('SELECT * FROM ir_model_fields WHERE model=%s AND state=%s', (self._name,'manual'))
+            cr.execute('SELECT * FROM ir_model_fields WHERE model=%s AND state=%s', (self._name, 'manual'))
             for field in cr.dictfetchall():
                 if field['name'] in self._columns:
                     continue
@@ -1523,12 +1777,21 @@ class orm(orm_template):
                     'translate': (field['translate']),
                     #'select': int(field['select_level'])
                 }
-                #if field['relation']:
-                #   attrs['relation'] = field['relation']
+
                 if field['ttype'] == 'selection':
                     self._columns[field['name']] = getattr(fields, field['ttype'])(eval(field['selection']), **attrs)
+                elif field['ttype'] == 'reference':
+                    self._columns[field['name']] = getattr(fields, field['ttype'])(selection=eval(field['selection']), **attrs)
                 elif field['ttype'] == 'many2one':
                     self._columns[field['name']] = getattr(fields, field['ttype'])(field['relation'], **attrs)
+                elif field['ttype'] == 'one2many':
+                    self._columns[field['name']] = getattr(fields, field['ttype'])(field['relation'], field['relation_field'], **attrs)
+                elif field['ttype'] == 'many2many':
+                    import random
+                    _rel1 = field['relation'].replace('.', '_')
+                    _rel2 = field['model'].replace('.', '_')
+                    _rel_name = 'x_%s_%s_%s_rel' %(_rel1, _rel2, random.randint(0, 10000))
+                    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)
 
@@ -1542,7 +1805,7 @@ class orm(orm_template):
 
     def default_get(self, cr, uid, fields_list, context=None):
         if not context:
-            context={}
+            context = {}
         value = {}
         # get the default values for the inherited fields
         for t in self._inherits.keys():
@@ -1554,7 +1817,7 @@ class orm(orm_template):
             if f in self._defaults:
                 value[f] = self._defaults[f](self, cr, uid, context)
             fld_def = ((f in self._columns) and self._columns[f]) \
-                    or ((f in self._inherit_fields ) and self._inherit_fields[f][2]) \
+                    or ((f in self._inherit_fields) and self._inherit_fields[f][2]) \
                     or False
             if isinstance(fld_def, fields.property):
                 property_obj = self.pool.get('ir.property')
@@ -1602,9 +1865,11 @@ class orm(orm_template):
                             field_value2[i][field2] = field_value[i][field2]
                     field_value = field_value2
                 value[field] = field_value
+        for key in context or {}:
+            if key.startswith('default_'):
+                value[key[8:]] = context[key]
         return value
 
-
     #
     # Update objects that uses this one to update their _inherits fields
     #
@@ -1618,59 +1883,72 @@ class orm(orm_template):
         for table in self._inherits:
             res.update(self.pool.get(table)._inherit_fields)
             for col in self.pool.get(table)._columns.keys():
-                res[col]=(table, self._inherits[table], self.pool.get(table)._columns[col])
+                res[col] = (table, self._inherits[table], self.pool.get(table)._columns[col])
             for col in self.pool.get(table)._inherit_fields.keys():
-                res[col]=(table, self._inherits[table], self.pool.get(table)._inherit_fields[col][2])
-        self._inherit_fields=res
+                res[col] = (table, self._inherits[table], self.pool.get(table)._inherit_fields[col][2])
+        self._inherit_fields = res
         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)
+        read_access = self.pool.get('ir.model.access').check(cr, user, self._name, 'write', raise_exception=False)
         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={}
+            context = {}
         self.pool.get('ir.model.access').check(cr, user, self._name, 'read')
         if not fields:
             fields = self._columns.keys() + self._inherit_fields.keys()
         select = ids
         if isinstance(ids, (int, long)):
             select = [ids]
-        result =  self._read_flat(cr, user, select, fields, context, load)
+        result = self._read_flat(cr, user, select, fields, context, load)
         for r in result:
-            for key,v in r.items():
+            for key, v in r.items():
                 if v == None:
-                    r[key]=False
+                    r[key] = False
         if isinstance(ids, (int, long)):
-            return result[0]
+            return result and result[0] or False
         return result
 
-    def _read_flat(self, cr, user, ids, fields, context=None, load='_classic_read'):
+    def _read_flat(self, cr, user, ids, fields_to_read, context=None, load='_classic_read'):
         if not context:
-            context={}
+            context = {}
         if not ids:
             return []
 
-        if fields==None:
-            fields = self._columns.keys()
+        if fields_to_read == None:
+            fields_to_read = self._columns.keys()
 
         # construct a clause for the rules :
         d1, d2 = self.pool.get('ir.rule').domain_get(cr, user, self._name)
 
         # all inherited fields + all non inherited fields for which the attribute whose name is in load is True
-        fields_pre = filter(lambda x: x in self._columns and getattr(self._columns[x],'_classic_write'), fields) + self._inherits.values()
+        fields_pre = [f for f in fields_to_read if
+                           f == self.CONCURRENCY_CHECK_FIELD 
+                        or (f in self._columns and getattr(self._columns[f], '_classic_write'))
+                     ] + self._inherits.values()
 
         res = []
-        if len(fields_pre) :
-            fields_pre2 = map(lambda x: (x in ('create_date', 'write_date')) and ('date_trunc(\'second\', '+x+') as '+x) or '"'+x+'"', fields_pre)
-            for i in range((len(ids) / ID_MAX) + ((len(ids) % ID_MAX) and 1 or 0)):
-                sub_ids = ids[ID_MAX * i:ID_MAX * (i + 1)]
+        if len(fields_pre):
+            def convert_field(f):
+                if f in ('create_date', 'write_date'):
+                    return "date_trunc('second', %s) as %s" % (f, f)
+                if f == self.CONCURRENCY_CHECK_FIELD:
+                    if self._log_access:
+                        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 '"%s"' % (f,)
+            fields_pre2 = map(convert_field, fields_pre)
+            for i in range(0, len(ids), cr.IN_MAX):
+                sub_ids = ids[i:i+cr.IN_MAX]
                 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)
+                                self._order), 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)
@@ -1684,6 +1962,8 @@ class orm(orm_template):
             res = map(lambda x: {'id': x}, ids)
 
         for f in fields_pre:
+            if f == self.CONCURRENCY_CHECK_FIELD:
+                continue
             if self._columns[f].translate:
                 ids = map(lambda x: x['id'], res)
                 res_trans = self.pool.get('ir.translation')._get_ids(cr, user, self._name+','+f, 'model', context.get('lang', False) or 'en_US', ids)
@@ -1692,7 +1972,7 @@ class orm(orm_template):
 
         for table in self._inherits:
             col = self._inherits[table]
-            cols = intersect(self._inherit_fields.keys(), fields)
+            cols = intersect(self._inherit_fields.keys(), fields_to_read)
             if not cols:
                 continue
             res2 = self.pool.get(table).read(cr, user, [x[col] for x in res], cols, context, load)
@@ -1704,26 +1984,44 @@ class orm(orm_template):
 
             for record in res:
                 record.update(res3[record[col]])
-                if col not in fields:
+                if col not in fields_to_read:
                     del record[col]
 
         # all fields which need to be post-processed by a simple function (symbol_get)
-        fields_post = filter(lambda x: x in self._columns and self._columns[x]._symbol_get, fields)
+        fields_post = filter(lambda x: x in self._columns and self._columns[x]._symbol_get, fields_to_read)
         if fields_post:
             # maybe it would be faster to iterate on the fields then on res, so that we wouldn't need
             # to get the _symbol_get in each occurence
             for r in res:
                 for f in fields_post:
-                    r[f] = self.columns[f]._symbol_get(r[f])
+                    r[f] = self._columns[f]._symbol_get(r[f])
         ids = map(lambda x: x['id'], res)
 
         # all non inherited fields for which the attribute whose name is in load is False
-        fields_post = filter(lambda x: x in self._columns and not getattr(self._columns[x], load), fields)
+        fields_post = filter(lambda x: x in self._columns and not getattr(self._columns[x], load), fields_to_read)
+
+        # Compute POST fields
+        todo = {}
         for f in fields_post:
-            # get the value of that field for all records/ids
-            res2 = self._columns[f].get(cr, self, ids, f, user, context=context, values=res)
-            for record in res:
-                record[f] = res2[record['id']]
+            todo.setdefault(self._columns[f]._multi, [])
+            todo[self._columns[f]._multi].append(f)
+        for key,val in todo.items():
+            if key:
+                res2 = self._columns[val[0]].get(cr, self, ids, val, user, context=context, values=res)
+                for pos in val:
+                    for record in res:
+                        record[pos] = res2[record['id']][pos]
+            else:
+                for f in val:
+                    res2 = self._columns[f].get(cr, self, ids, f, user, context=context, values=res)
+                    for record in res:
+                        record[f] = res2[record['id']]
+
+#for f in fields_post:
+#    # get the value of that field for all records/ids
+#    res2 = self._columns[f].get(cr, self, ids, f, user, context=context, values=res)
+#    for record in res:
+#        record[f] = res2[record['id']]
 
         readonly = None
         for vals in res:
@@ -1764,58 +2062,54 @@ class orm(orm_template):
 
     def perm_read(self, cr, user, ids, context=None, details=True):
         if not context:
-            context={}
+            context = {}
         if not ids:
             return []
         fields = ''
         if self._log_access:
-            fields =', u.create_uid, u.create_date, u.write_uid, u.write_date'
+            fields = ', u.create_uid, u.create_date, u.write_uid, u.write_date'
         if isinstance(ids, (int, long)):
             ids_str = str(ids)
         else:
-            ids_str = string.join(map(lambda x:str(x), ids),',')
+            ids_str = string.join(map(lambda x: str(x), ids), ',')
         cr.execute('select u.id'+fields+' from "'+self._table+'" u where u.id in ('+ids_str+')')
         res = cr.dictfetchall()
         for r in res:
             for key in r:
                 r[key] = r[key] or False
-                if key in ('write_uid','create_uid','uid') and details:
+                if key in ('write_uid', 'create_uid', 'uid') and details:
                     if r[key]:
                         r[key] = self.pool.get('res.users').name_get(cr, user, [r[key]])[0]
         if isinstance(ids, (int, long)):
             return res[ids]
         return res
 
-    def unlink(self, cr, uid, ids, context=None):
+    def _check_concurrency(self, cr, ids, context):
         if not context:
-            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(((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'))
+
+    def unlink(self, cr, uid, ids, context=None):
         if not ids:
             return True
         if isinstance(ids, (int, long)):
             ids = [ids]
 
-        fn_list = []
-        if self._name in self.pool._store_function.keys():
-            list_store = self.pool._store_function[self._name]
-            fn_data = ()
-            id_change = []
-            for tuple_fn in list_store:
-                for id in ids:
-                    id_change.append(self._store_get_ids(cr, uid, id,tuple_fn, context)[0])
-                fn_data = id_change,tuple_fn
-                fn_list.append(fn_data)
-
-        delta= context.get('read_delta',False)
-        if delta and self._log_access:
-            for i in range((len(ids) / ID_MAX) + ((len(ids) % ID_MAX) and 1 or 0)):
-                sub_ids = ids[ID_MAX * i:ID_MAX * (i + 1)]
-                cr.execute("select  (now()  - min(write_date)) <= '%s'::interval " \
-                        "from \"%s\" where id in (%s)" %
-                        (delta, self._table, ",".join(map(str, sub_ids))) )
-            res= cr.fetchone()
-            if res and res[0]:
-                raise except_orm(_('ConcurrencyException'),
-                        _('This record was modified in the meanwhile'))
+        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')
 
@@ -1833,9 +2127,9 @@ class orm(orm_template):
         if d1:
             d1 = ' AND '+d1
 
-        for i in range((len(ids) / ID_MAX) + ((len(ids) % ID_MAX) and 1 or 0)):
-            sub_ids = ids[ID_MAX * i:ID_MAX * (i + 1)]
-            str_d = string.join(('%d',)*len(sub_ids),',')
+        for i in range(0, len(ids), cr.IN_MAX):
+            sub_ids = ids[i:i+cr.IN_MAX]
+            str_d = string.join(('%s',)*len(sub_ids), ',')
             if d1:
                 cr.execute('SELECT id FROM "'+self._table+'" ' \
                         'WHERE id IN ('+str_d+')'+d1, sub_ids+d2)
@@ -1850,10 +2144,13 @@ class orm(orm_template):
             else:
                 cr.execute('delete from "'+self._table+'" ' \
                         'where id in ('+str_d+')', sub_ids)
-        if fn_list:
-            for ids,tuple_fn in fn_list:
-                self._store_set_values(cr, uid, ids,tuple_fn,id_change, context)
 
+        for order, object, ids, fields in result_store:
+            if object<>self._name:
+                cr.execute('select id from '+self._table+' where id in ('+','.join(map(str, ids))+')')
+                ids = map(lambda x: x[0], cr.fetchall())
+                if ids:
+                    self.pool.get(object)._store_set_values(cr, uid, ids, fields, context)
         return True
 
     #
@@ -1891,38 +2188,25 @@ class orm(orm_template):
                     vals.pop(field)
 
         if not context:
-            context={}
+            context = {}
         if not ids:
             return True
         if isinstance(ids, (int, long)):
             ids = [ids]
-        delta= context.get('read_delta',False)
-        if delta and self._log_access:
-            for i in range((len(ids) / ID_MAX) + ((len(ids) % ID_MAX) and 1 or 0)):
-                sub_ids = ids[ID_MAX * i:ID_MAX * (i + 1)]
-                cr.execute("select  (now()  - min(write_date)) <= '%s'::interval " \
-                        "from %s where id in (%s)" %
-                        (delta,self._table, ",".join(map(str, sub_ids))))
-                res= cr.fetchone()
-                if res and res[0]:
-                    for field in vals:
-                        if field in self._columns and self._columns[field]._classic_write:
-                            raise except_orm(_('ConcurrencyException'),
-                                    _('This record was modified in the meanwhile'))
+
+        self._check_concurrency(cr, ids, context)
 
         self.pool.get('ir.model.access').check(cr, user, self._name, 'write')
 
-        #for v in self._inherits.values():
-        #   assert v not in vals, (v, vals)
-        upd0=[]
-        upd1=[]
-        upd_todo=[]
-        updend=[]
+        upd0 = []
+        upd1 = []
+        upd_todo = []
+        updend = []
         direct = []
         totranslate = context.get('lang', False) and (context['lang'] != 'en_US')
         for field in vals:
             if field in self._columns:
-                if self._columns[field]._classic_write:
+                if self._columns[field]._classic_write and not (hasattr(self._columns[field], '_fnct_inv')):
                     if (not totranslate) or not self._columns[field].translate:
                         upd0.append('"'+field+'"='+self._columns[field]._symbol_set[0])
                         upd1.append(self._columns[field]._symbol_set[1](vals[field]))
@@ -1951,7 +2235,7 @@ class orm(orm_template):
                                 % (vals[field], field))
 
         if self._log_access:
-            upd0.append('write_uid=%d')
+            upd0.append('write_uid=%s')
             upd0.append('write_date=now()')
             upd1.append(user)
 
@@ -1961,9 +2245,9 @@ class orm(orm_template):
             if d1:
                 d1 = ' and '+d1
 
-            for i in range((len(ids) / ID_MAX) + ((len(ids) % ID_MAX) and 1 or 0)):
-                sub_ids = ids[ID_MAX * i:ID_MAX * (i + 1)]
-                ids_str = string.join(map(str, sub_ids),',')
+            for i in range(0, len(ids), cr.IN_MAX):
+                sub_ids = ids[i:i+cr.IN_MAX]
+                ids_str = string.join(map(str, sub_ids), ',')
                 if d1:
                     cr.execute('SELECT id FROM "'+self._table+'" ' \
                             'WHERE id IN ('+ids_str+')'+d1, d2)
@@ -1978,19 +2262,19 @@ class orm(orm_template):
                                 _('You try to write on an record that doesn\'t exist ' \
                                         '(Document type: %s).') % self._description)
                 if d1:
-                    cr.execute('update "'+self._table+'" set '+string.join(upd0,',')+' ' \
+                    cr.execute('update "'+self._table+'" set '+string.join(upd0, ',')+' ' \
                             'where id in ('+ids_str+')'+d1, upd1+ d2)
                 else:
-                    cr.execute('update "'+self._table+'" set '+string.join(upd0,',')+' ' \
+                    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])
+                        self.pool.get('ir.translation')._set_ids(cr, user, self._name+','+f, 'model', context['lang'], ids, vals[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)
+        upd_todo.sort(lambda x, y: self._columns[x].priority-self._columns[y].priority)
         for field in upd_todo:
             for id in ids:
                 self._columns[field].set(cr, self, id, field, vals[field], user, context=context)
@@ -1998,43 +2282,88 @@ class orm(orm_template):
         for table in self._inherits:
             col = self._inherits[table]
             nids = []
-            for i in range((len(ids) / ID_MAX) + ((len(ids) % ID_MAX) and 1 or 0)):
-                sub_ids = ids[ID_MAX * i:ID_MAX * (i + 1)]
-                ids_str = string.join(map(str, sub_ids),',')
+            for i in range(0, len(ids), cr.IN_MAX):
+                sub_ids = ids[i:i+cr.IN_MAX]
+                ids_str = string.join(map(str, sub_ids), ',')
                 cr.execute('select distinct "'+col+'" from "'+self._table+'" ' \
                         'where id in ('+ids_str+')', upd1)
                 nids.extend([x[0] for x in cr.fetchall()])
 
             v = {}
             for val in updend:
-                if self._inherit_fields[val][0]==table:
-                    v[val]=vals[val]
+                if self._inherit_fields[val][0] == table:
+                    v[val] = vals[val]
             self.pool.get(table).write(cr, user, nids, v, context)
 
         self._validate(cr, user, ids, context)
-
-        if context.has_key('read_delta'):
-            del context['read_delta']
+# TODO: use _order to set dest at the right position and not first node of parent
+        if self._parent_store and (self._parent_name in vals):
+            if self.pool._init:
+                self.pool._init_parent[self._name]=True
+            else:
+                for id in ids:
+                    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],))
+                        pleft_old = pright_old = None
+                        result_p = cr.fetchall()
+                        for (pleft,pright,pid) in result_p:
+                            if pid == id:
+                                break
+                            pleft_old = pleft
+                            pright_old = pright
+                        if not pleft_old:
+                            cr.execute('select parent_left,parent_right from '+self._table+' where id=%s', (vals[self._parent_name],))
+                            pleft_old,pright_old = cr.fetchone()
+                        res = (pleft_old, pright_old)
+                    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)
+                        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
+                        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))
+
+        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)
 
         wf_service = netsvc.LocalService("workflow")
         for id in ids:
             wf_service.trg_write(user, self._name, id, cr)
-        self._update_function_stored(cr, user, ids, context=context)
-
-        if self._name in self.pool._store_function.keys():
-            list_store = self.pool._store_function[self._name]
-            for tuple_fn in list_store:
-                flag = False
-                if not tuple_fn[3]:
-                    flag = True
-                for field in tuple_fn[3]:
-                    if field in vals.keys():
-                        flag = True
-                        break
-                if flag:
-                    id_change = self._store_get_ids(cr, user, ids[0],tuple_fn, context)
-                    self._store_set_values(cr, user, ids[0],tuple_fn,id_change, context)
-
         return True
 
     #
@@ -2047,24 +2376,31 @@ class orm(orm_template):
         vals = dictionary of the form {'field_name':field_value, ...}
         """
         if not context:
-            context={}
+            context = {}
         self.pool.get('ir.model.access').check(cr, user, self._name, 'create')
 
         default = []
 
         avoid_table = []
-        for (t,c) in self._inherits.items():
+        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:
                 default.append(f)
+
         for f in self._inherit_fields.keys():
-            if (not f in vals) and (not self._inherit_fields[f][0] in avoid_table):
+            if (not f in vals) and (self._inherit_fields[f][0] not in avoid_table):
                 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:
@@ -2076,22 +2412,29 @@ class orm(orm_template):
 
         for v in vals.keys():
             if v in self._inherit_fields:
-                (table,col,col_detail) = self._inherit_fields[v]
+                (table, col, col_detail) = self._inherit_fields[v]
                 tocreate[table][v] = vals[v]
                 del vals[v]
 
-        cr.execute("SELECT nextval('"+self._sequence+"')")
+        # 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:        
+            cr.execute("SELECT nextval('"+self._sequence+"')")
+        except:
+            raise except_orm(_('UserError'),
+                        _('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 += ',%d'
+            upd1 += ',%s'
             upd2.append(id)
 
         for field in vals:
             if self._columns[field]._classic_write:
-                upd0=upd0+',"'+field+'"'
-                upd1=upd1+','+self._columns[field]._symbol_set[0]
+                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)
@@ -2115,62 +2458,107 @@ class orm(orm_template):
                                 % (vals[field], field))
         if self._log_access:
             upd0 += ',create_uid,create_date'
-            upd1 += ',%d,now()'
+            upd1 += ',%s,now()'
             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)
+        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:
+                self.pool._init_parent[self._name]=True
+            else:
+                parent = vals.get(self._parent_name, False)
+                if parent:
+                    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))
+
+        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)
+
         wf_service = netsvc.LocalService("workflow")
         wf_service.trg_create(user, self._name, id_new, cr)
-        self._update_function_stored(cr, user, [id_new], context=context)
-        if self._name in self.pool._store_function.keys():
-            list_store = self.pool._store_function[self._name]
-            for tuple_fn in list_store:
-                id_change = self._store_get_ids(cr, user, id_new,tuple_fn, context)
-                self._store_set_values(cr, user, id_new,tuple_fn,id_change, context)
-
         return id_new
 
-    def _store_get_ids(self,cr, uid, ids,tuple_fn, context):
-        parent_id=getattr(self.pool.get(tuple_fn[0]),tuple_fn[4].func_name)(cr,uid,[ids])
-        return parent_id
-
-    def _store_set_values(self,cr,uid,ids,tuple_fn,parent_id,context):
-        name = tuple_fn[1]
-        table = tuple_fn[0]
-        args={}
-        vals_tot=getattr(self.pool.get(table),tuple_fn[2])(cr, uid, parent_id ,name, args, context)
-        write_dict = {}
-        for id in vals_tot.keys():
-            write_dict[name]=vals_tot[id]
-            self.pool.get(table).write(cr,uid,[id],write_dict)
-        return True
+    def _store_get_values(self, cr, uid, ids, fields, context):
+        result = {}
+        fncts = self.pool._store_function.get(self._name, [])
+        for fnct in range(len(fncts)):
+            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 = []
+        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()
+        return result2
+
+    def _store_set_values(self, cr, uid, ids, fields, context):
+        todo = {}
+        keys = []
+        for f in fields:
+            if self._columns[f]._multi not in keys:
+                keys.append(self._columns[f]._multi)
+            todo.setdefault(self._columns[f]._multi, [])
+            todo[self._columns[f]._multi].append(f)
+        for key in keys:
+            val = todo[key]
+            if key:
+                result = self._columns[val[0]].get(cr, self, ids, val, uid, context=context)
+                for id,value in result.items():
+                    upd0 = []
+                    upd1 = []
+                    for v in value:
+                        if v not in val:
+                            continue
+                        if self._columns[v]._type in ('many2one', 'one2one'):
+                            try:
+                                value[v] = value[v][0]
+                            except:
+                                pass
+                        upd0.append('"'+v+'"='+self._columns[v]._symbol_set[0])
+                        upd1.append(self._columns[v]._symbol_set[1](value[v]))
+                    upd1.append(id)
+                    cr.execute('update "' + self._table + '" set ' + \
+                        string.join(upd0, ',') + ' where id = %s', upd1)
 
-    def _update_function_stored(self, cr, user, ids, context=None):
-        if not context:
-            context = {}
-        f = filter(lambda a: isinstance(self._columns[a], fields.function) \
-                and self._columns[a].store, self._columns)
-        if f:
-            result=self.read(cr, user, ids, fields=f, context=context)
-            for res in result:
-                upd0=[]
-                upd1=[]
-                for field in res:
-                    if field not in f:
-                        continue
-                    value = res[field]
-                    if self._columns[field]._type in ('many2one', 'one2one'):
-                        value = res[field][0]
-                    upd0.append('"'+field+'"='+self._columns[field]._symbol_set[0])
-                    upd1.append(self._columns[field]._symbol_set[1](value))
-                upd1.append(res['id'])
-                cr.execute('update "' + self._table + '" set ' + \
-                        string.join(upd0, ',') + ' where id = %d', upd1)
+            else:
+                for f in val:
+                    result = self._columns[f].get(cr, self, ids, f, uid, context=context)
+                    for id,value in result.items():
+                        if self._columns[f]._type in ('many2one', 'one2one'):
+                            try:
+                                value = value[0]
+                            except:
+                                pass
+                        cr.execute('update "' + self._table + '" set ' + \
+                            '"'+f+'"='+self._columns[f]._symbol_set[0] + ' where id = %s', (self._columns[f]._symbol_set[1](value),id))
         return True
 
     #
@@ -2182,246 +2570,32 @@ class orm(orm_template):
     # TODO: ameliorer avec NULL
     def _where_calc(self, cr, user, args, active_test=True, context=None):
         if not context:
-            context={}
+            context = {}
         args = args[:]
         # if the object has a field named 'active', filter out all inactive
         # records unless they were explicitely asked for
         if 'active' in self._columns and (active_test and context.get('active_test', True)):
-            active_found = reduce( lambda x, y: x or y == 'active', args, False )
-            if not active_found:
-                args.append(('active', '=', 1))
-
-        tables=['"'+self._table+'"']
-        joins=[]
-        for i, argument in zip(range(len(args)), args):
-            table=self
-            assert argument[1] in ('like','!=','ilike','=like', 'not like', 'not ilike', 'not in','inselect','child_of','in','=','<>','<','>','>=','<='), _('Error ! Bad clause operand "%s".') % (argument[1],)
-            if argument[1] == 'inselect':
-                raise except_orm(_('ValidateError'),
-                        _("The clause 'inselect' can not be used outside the orm!"))
-            if argument[0] in self._inherit_fields:
-                table=self.pool.get(self._inherit_fields[argument[0]][0])
-                if ('"'+table._table+'"' not in tables):
-                    tables.append('"'+table._table+'"')
-                    joins.append(('id', 'join', '%s.%s' % (self._table, self._inherits[table._name]), table))
-            fargs = argument[0].split('.', 1)
-            field = table._columns.get(fargs[0],False)
-            if not field:
-                if argument[0] == 'id' and argument[1] == 'child_of':
-                    ids2 = argument[2]
-                    def _rec_get(ids, table, parent):
-                        if not ids:
-                            return []
-                        ids2 = table.search(cr, user, [(parent, 'in', ids)], context=context)
-                        return ids + _rec_get(ids2, table, parent)
-                    args[i] = (argument[0], 'in', ids2 + _rec_get(ids2, table, table._parent_name), table)
-                continue
-            if len(fargs) > 1:
-                if field._type == 'many2one':
-                    args[i] = (fargs[0], 'in', self.pool.get(field._obj).search(cr, user, [(fargs[1], argument[1], argument[2])], context=context))
-                continue
-            if field._properties:
-                arg = [args.pop(i)]
-                j = i
-                while j<len(args):
-                    if args[j][0]==arg[0][0]:
-                        arg.append(args.pop(j))
-                    else:
-                        j+=1
-                if field._fnct_search:
-                    args.extend(field.search(cr, user, self, arg[0][0], arg))
-                if field.store:
-                    args.extend(arg)
-            elif field._type=='one2many':
-                field_obj = self.pool.get(field._obj)
-
-                if isinstance(argument[2], basestring):
-                    # get the ids of the records of the "distant" resource
-                    ids2 = [x[0] for x in field_obj.name_search(cr, user, argument[2], [], argument[1])]
-                else:
-                    ids2 = argument[2]
-                if not ids2:
-                    args[i] = ('id','=','0')
-                else:
-                    ids3 = []
-                    for j in range((len(ids2) / ID_MAX) + (len(ids2) % ID_MAX)):
-                        sub_ids2 = ids2[ID_MAX * j:ID_MAX * (j + 1)]
-                        if sub_ids2:
-                            cr.execute('SELECT "'+field._fields_id+'" ' \
-                                    'FROM "'+field_obj._table+'" ' \
-                                    'WHERE id in ('+','.join(map(str,sub_ids2))+')')
-                            ids3.extend([x[0] for x in cr.fetchall()])
-
-                    args[i] = ('id', 'in', ids3)
-
-            elif field._type=='many2many':
-                #FIXME
-                if argument[1]=='child_of':
-                    if isinstance(argument[2], basestring):
-                        ids2 = [x[0] for x in self.pool.get(field._obj).name_search(cr, user, argument[2], [], 'like')]
-                    else:
-                        ids2 = argument[2]
-                    def _rec_get(ids, table, parent):
-                        if not ids:
-                            return []
-                        ids2 = table.search(cr, user, [(parent, 'in', ids)], context=context)
-                        return ids + _rec_get(ids2, table, parent)
-                    def _rec_convert(ids):
-                        if self.pool.get(field._obj)==self:
-                            return ids
-                        if not len(ids): return []
-                        cr.execute('select "'+field._id1+'" from "'+field._rel+'" where "'+field._id2+'" in ('+','.join(map(str,ids))+')')
-                        ids = [x[0] for x in cr.fetchall()]
-                        return ids
-                    args[i] = ('id','in',_rec_convert(ids2+_rec_get(ids2, self.pool.get(field._obj), table._parent_name)))
-                else:
-                    if isinstance(argument[2], basestring):
-                        res_ids = [x[0] for x in self.pool.get(field._obj).name_search(cr, user, argument[2], [], argument[1])]
-                    else:
-                        res_ids = argument[2]
-                    if not len(res_ids):
-                        args[i] = ('id', 'in', [0])
-                    else:
-                        cr.execute('select "'+field._id1+'" from "'+field._rel+'" where "'+field._id2+'" in ('+','.join(map(str, res_ids))+')')
-                        args[i] = ('id', 'in', map(lambda x: x[0], cr.fetchall()))
-
-            elif field._type=='many2one':
-                if argument[1]=='child_of':
-                    if isinstance(argument[2], basestring):
-                        ids2 = [x[0] for x in self.pool.get(field._obj).name_search(cr, user, argument[2], [], 'like')]
-                    else:
-                        ids2 = argument[2]
-                    def _rec_get(ids, table, parent):
-                        if not ids:
-                            return []
-                        ids2 = table.search(cr, user, [(parent, 'in', ids)], context=context)
-                        return ids + _rec_get(ids2, table, parent)
-                    if field._obj <> table._name:
-                        args[i] = (argument[0],'in',ids2+_rec_get(ids2, self.pool.get(field._obj), table._parent_name), table)
-                    else:
-                        args[i] = ('id','in',ids2+_rec_get(ids2, table, argument[0]), table)
-                else:
-                    if isinstance(argument[2], basestring):
-                        res_ids = self.pool.get(field._obj).name_search(cr, user, argument[2], [], argument[1])
-                        args[i] = (argument[0],'in',map(lambda x: x[0], res_ids), table)
-                    else:
-                        args[i] += (table,)
+            if args:
+                active_in_args = False
+                for a in args:
+                    if a[0] == 'active':
+                        active_in_args = True
+                if not active_in_args:
+                    args.insert(0, ('active', '=', 1))
             else:
-                if field.translate:
-                    if argument[1] in ('like', 'ilike', 'not like', 'not ilike'):
-                        args[i] = (argument[0], argument[1], '%%%s%%' % argument[2])
-                    query1 = '(SELECT res_id FROM ir_translation ' \
-                            'WHERE name = %s AND lang = %s ' \
-                                'AND type = %s ' \
-                                'AND VALUE ' + argument[1] + ' %s)'
-                    query2 = [table._name + ',' + argument[0],
-                            context.get('lang', False) or 'en_US',
-                            'model',
-                            argument[2]]
-                    query1 += ' UNION '
-                    query1 += '(SELECT id FROM "' + table._table + '" ' \
-                            'WHERE "' + argument[0] + '" ' + argument[1] + ' %s)'
-                    query2 += [argument[2]]
-                    args[i] = ('id', 'inselect', (query1, query2), table)
-                else:
-                    args[i] += (table,)
-        args.extend(joins)
-
-        qu1, qu2 = [], []
-        for x in args:
-            table=self
-            if len(x) > 3:
-                table=x[3]
-            if x[1] == 'inselect':
-                qu1.append('(%s.%s in (%s))' % (table._table, x[0], x[2][0]))
-                qu2 += x[2][1]
-            elif x[1]=='not in':
-                    if len(x[2])>0:
-                        todel = []
-                        for xitem in range(len(x[2])):
-                            if x[2][xitem]==False and isinstance(x[2][xitem],bool):
-                                todel.append(xitem)
-                        for xitem in todel[::-1]:
-                            del x[2][xitem]
-                        if x[0]=='id':
-                            qu1.append('(%s.id not in (%s))' % (table._table, ','.join(['%d'] * len(x[2])),))
-                        else:
-                            qu1.append('(%s.%s not in (%s))' % (table._table, x[0], ','.join([table._columns[x[0]]._symbol_set[0]]*len(x[2]))))
-                        if todel:
-                            qu1[-1] = '('+qu1[-1]+' or '+x[0]+' is null)'
-                        qu2+=x[2]
-                    else:
-                        qu1.append(' (1=0)')
-            elif x[1] != 'in':
-#FIXME: this replace all (..., '=', False) values with 'is null' and this is
-# not what we want for real boolean fields. The problem is, we can't change it
-# easily because we use False everywhere instead of None
-# NOTE FAB: we can't use None becStay tunes ! ause it is not accepted by XML-RPC, that's why
-# boolean (0-1), None -> False
-# Ged> boolean fields are not always = 0 or 1
-                if (x[2] is False) and (x[1]=='='):
-                    qu1.append(x[0]+' is null')
-                elif (x[2] is False) and (x[1]=='<>' or x[1]=='!='):
-                    qu1.append(x[0]+' is not null')
-                else:
-                    if x[0]=='id':
-                        if x[1]=='join':
-                            qu1.append('(%s.%s = %s)' % (table._table, x[0], x[2]))
-                        else:
-                            qu1.append('(%s.%s %s %%s)' % (table._table, x[0], x[1]))
-                            qu2.append(x[2])
-                    else:
-                        add_null = False
-                        if x[1] in ('like', 'ilike', 'not like', 'not ilike'):
-                            if isinstance(x[2], str):
-                                str_utf8 = x[2]
-                            elif isinstance(x[2], unicode):
-                                str_utf8 = x[2].encode('utf-8')
-                            else:
-                                str_utf8 = str(x[2])
-                            qu2.append('%%%s%%' % str_utf8)
-                            if not str_utf8:
-                                add_null = True
-                        else:
-                            if x[0] in table._columns:
-                                qu2.append(table._columns[x[0]]._symbol_set[1](x[2]))
-                        if x[1]=='=like':
-                            x1 = 'like'
-                        else:
-                            x1 = x[1]
-                        if x[0] in table._columns:
-                            if x[1] in ('like', 'ilike', 'not like', 'not ilike'):
+                args = [('active', '=', 1)]
+
+        if args:
+            import expression
+            e = expression.expression(args)
+            e.parse(cr, user, self, context)
+            tables = e.get_tables()
+            qu1, qu2 = e.to_sql()
+            qu1 = qu1 and [qu1] or []
+        else:
+            qu1, qu2, tables = [], [], ['"%s"' % self._table]
 
-                                qu1.append('(%s.%s %s %s)' % (table._table,
-                                    x[0], x1, '%s'))
-                            else:
-                                qu1.append('(%s.%s %s %s)' % (table._table,
-                                    x[0], x1,
-                                    table._columns[x[0]]._symbol_set[0]))
-                        else:
-                            qu1.append('(%s.%s %s \'%s\')' % (table._table, x[0], x1, x[2]))
-
-                        if add_null:
-                            qu1[-1] = '('+qu1[-1]+' or '+x[0]+' is null)'
-            elif x[1]=='in':
-                if len(x[2])>0:
-                    todel = []
-                    for xitem in range(len(x[2])):
-                        if x[2][xitem]==False and isinstance(x[2][xitem],bool):
-                            todel.append(xitem)
-                    for xitem in todel[::-1]:
-                        del x[2][xitem]
-                    #TODO fix max_stack_depth
-                    if x[0]=='id':
-                        qu1.append('(%s.id in (%s))' % (table._table, ','.join(['%d'] * len(x[2])),))
-                    else:
-                        qu1.append('(%s.%s in (%s))' % (table._table, x[0], ','.join([table._columns[x[0]]._symbol_set[0]]*len(x[2]))))
-                    if todel:
-                        qu1[-1] = '('+qu1[-1]+' or '+x[0]+' is null)'
-                    qu2+=x[2]
-                else:
-                    qu1.append(' (1=0)')
-        return (qu1,qu2,tables)
+        return (qu1, qu2, tables)
 
     def _check_qorder(self, word):
         if not regex_order.match(word):
@@ -2433,10 +2607,10 @@ class orm(orm_template):
         if not context:
             context = {}
         # compute the where, order by, limit and offset clauses
-        (qu1,qu2,tables) = self._where_calc(cr, user, args, context=context)
+        (qu1, qu2, tables) = self._where_calc(cr, user, args, context=context)
 
         if len(qu1):
-            qu1 = ' where '+string.join(qu1,' and ')
+            qu1 = ' where '+string.join(qu1, ' and ')
         else:
             qu1 = ''
 
@@ -2469,37 +2643,37 @@ class orm(orm_template):
     # a char field
     def distinct_field_get(self, cr, uid, field, value, args=None, offset=0, limit=None):
         if not args:
-            args=[]
+            args = []
         if field in self._inherit_fields:
-            return self.pool.get(self._inherit_fields[field][0]).distinct_field_get(cr, uid,field,value,args,offset,limit)
+            return self.pool.get(self._inherit_fields[field][0]).distinct_field_get(cr, uid, field, value, args, offset, limit)
         else:
             return self._columns[field].search(cr, self, args, field, value, offset, limit, uid)
 
     def name_get(self, cr, user, ids, context=None):
         if not context:
-            context={}
+            context = {}
         if not ids:
             return []
         if isinstance(ids, (int, long)):
             ids = [ids]
-        return [(r['id'], str(r[self._rec_name])) for r in self.read(cr, user, ids,
+        return [(r['id'], tools.ustr(r[self._rec_name])) for r in self.read(cr, user, ids,
             [self._rec_name], context, load='_classic_write')]
 
-    def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=80):
+    def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=None):
         if not args:
-            args=[]
+            args = []
         if not context:
-            context={}
-        args=args[:]
+            context = {}
+        args = args[:]
         if name:
-            args += [(self._rec_name,operator,name)]
+            args += [(self._rec_name, operator, name)]
         ids = self.search(cr, user, args, limit=limit, context=context)
         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={}
+            context = {}
         if not default:
             default = {}
         if 'state' not in default:
@@ -2507,6 +2681,7 @@ class orm(orm_template):
                 default['state'] = self._defaults['state'](self, cr, uid, context)
         data = self.read(cr, uid, [id], context=context)[0]
         fields = self.fields_get(cr, uid)
+        trans_data=[]
         for f in fields:
             ftype = fields[f]['type']
 
@@ -2529,55 +2704,46 @@ 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=''
+        for f in fields:
+            trans_flag=True
+            if f in self._columns and self._columns[f].translate:
+                trans_name=self._name+","+f
+            elif f in self._inherit_fields and self._inherit_fields[f][2].translate:
+                trans_name=self._inherit_fields[f][0]+","+f
+            else:
+                trans_flag=False
+
+            if trans_flag:
+                trans_ids = trans_obj.search(cr, uid, [
+                        ('name', '=', trans_name),
+                        ('res_id','=',data['id'])
+                    ])
+
+                trans_data.extend(trans_obj.read(cr,uid,trans_ids,context=context))
+
         del data['id']
+
         for v in self._inherits:
             del data[self._inherits[v]]
-        return self.create(cr, uid, data)
+        return data, trans_data
 
-    def read_string(self, cr, uid, id, langs, fields=None, context=None):
-        if not context:
-            context={}
-        res = {}
-        res2 = {}
-        self.pool.get('ir.model.access').check(cr, uid, 'ir.translation', 'read')
-        if not fields:
-            fields = self._columns.keys() + self._inherit_fields.keys()
-        for lang in langs:
-            res[lang] = {'code': lang}
-            for f in fields:
-                if f in self._columns:
-                    res_trans = self.pool.get('ir.translation')._get_source(cr, uid, self._name+','+f, 'field', lang)
-                    if res_trans:
-                        res[lang][f]=res_trans
-                    else:
-                        res[lang][f]=self._columns[f].string
-        for table in self._inherits:
-            cols = intersect(self._inherit_fields.keys(), fields)
-            res2 = self.pool.get(table).read_string(cr, uid, id, langs, cols, context)
-        for lang in res2:
-            if lang in res:
-                res[lang]={'code': lang}
-            for f in res2[lang]:
-                res[lang][f]=res2[lang][f]
-        return res
-
-    def write_string(self, cr, uid, id, langs, vals, context=None):
-        if not context:
-            context={}
-        self.pool.get('ir.model.access').check(cr, uid, 'ir.translation', 'write')
-        for lang in langs:
-            for field in vals:
-                if field in self._columns:
-                    self.pool.get('ir.translation')._set_ids(cr, uid, self._name+','+field, 'field', lang, [0], vals[field])
-        for table in self._inherits:
-            cols = intersect(self._inherit_fields.keys(), vals)
-            if cols:
-                self.pool.get(table).write_string(cr, uid, id, langs, vals, context)
-        return True
+    def copy(self, cr, uid, id, default=None, context=None):
+        data, trans_data = self.copy_data(cr, uid, id, default, context)
+        new_id=self.create(cr, uid, data)
+        for record in trans_data:
+            del record['id']
+            record['res_id']=new_id
+            trans_obj.create(cr,uid,record)
+        return new_id
 
     def check_recursion(self, cr, uid, ids, parent=None):
         if not parent:
@@ -2585,8 +2751,8 @@ class orm(orm_template):
         ids_parent = ids[:]
         while len(ids_parent):
             ids_parent2 = []
-            for i in range((len(ids) / ID_MAX) + ((len(ids) % ID_MAX) and 1 or 0)):
-                sub_ids_parent = ids_parent[ID_MAX * i:ID_MAX * (i + 1)]
+            for i in range(0, len(ids), cr.IN_MAX):
+                sub_ids_parent = ids_parent[i:i+cr.IN_MAX]
                 cr.execute('SELECT distinct "'+parent+'"'+
                     ' FROM "'+self._table+'" ' \
                     'WHERE id in ('+','.join(map(str, sub_ids_parent))+')')