Merge commit 'origin/master' into mdv-gpl3-py26
[odoo/odoo.git] / bin / osv / orm.py
index d21a957..6ecb829 100644 (file)
@@ -40,6 +40,7 @@
 #
 
 import time
+import calendar
 import types
 import string
 import netsvc
@@ -62,6 +63,12 @@ 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)
@@ -81,10 +88,10 @@ class browse_null(object):
         self.id = False
 
     def __getitem__(self, name):
-        return False
+        return None
 
     def __getattr__(self, name):
-        return False  # XXX: return self ?
+        return None  # XXX: return self ?
 
     def __int__(self):
         return False
@@ -115,7 +122,7 @@ 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 = {}
@@ -148,7 +155,7 @@ 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):
+            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:
@@ -156,7 +163,7 @@ class browse_record(object):
             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:
@@ -178,9 +185,13 @@ class browse_record(object):
                     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:
@@ -201,6 +212,12 @@ class browse_record(object):
                     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)
                 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):
@@ -281,6 +298,8 @@ def get_pg_type(f):
         f_type = ('float8', 'DOUBLE PRECISION')
     elif isinstance(f, fields.function) and f._type == 'selection':
         f_type = ('text', 'text')
+    elif isinstance(f, fields.function) and f._type == 'char':
+        f_type = ('varchar', 'VARCHAR(%d)' % (f.size))
     else:
         logger = netsvc.Logger()
         logger.notifyChannel("init", netsvc.LOG_WARNING, '%s type not supported!' % (type(f)))
@@ -296,6 +315,7 @@ class orm_template(object):
     _rec_name = 'name'
     _parent_name = 'parent_id'
     _parent_store = False
+    _parent_order = False
     _date_name = 'date'
     _order = 'id'
     _sequence = None
@@ -303,6 +323,8 @@ class orm_template(object):
     _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,))
@@ -440,7 +462,7 @@ class orm_template(object):
                         break
                     i += 1
                 if i == len(f):
-                    data[fpos] = str(r or '')
+                    data[fpos] = tools.ustr(r or '')
         return [data] + lines
 
     def export_data(self, cr, uid, ids, fields, context=None):
@@ -452,8 +474,7 @@ class orm_template(object):
             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, filename=None):
+    def import_data(self, cr, uid, fields, datas, mode='init', current_module=None, noupdate=False, context=None, filename=None):
         if not context:
             context = {}
         fields = map(lambda x: x.split('/'), fields)
@@ -481,7 +502,7 @@ class orm_template(object):
                     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:
@@ -502,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 \
@@ -551,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
@@ -670,7 +693,7 @@ class orm_template(object):
             if not fun(self, cr, uid, ids):
                 translated_msg = trans._get_source(cr, uid, self._name, 'constraint', lng, source=msg) or msg
                 error_msgs.append(
-                        _("Error occured while validating the field(s) %s: %s") % (','.join(fields), translated_msg)
+                        _("Error occurred while validating the field(s) %s: %s") % (','.join(fields), translated_msg)
                 )
                 self._invalids.update(fields)
         if error_msgs:
@@ -712,51 +735,55 @@ class orm_template(object):
         model_access_obj = self.pool.get('ir.model.access')
         for parent in self._inherits:
             res.update(self.pool.get(parent).fields_get(cr, user, fields, context))
-        for f in self._columns.keys():
-            if fields and f not in fields:
-                continue
-            res[f] = {'type': self._columns[f]._type}
-            for arg in ('string', 'readonly', 'states', 'size', 'required',
-                    'change_default', 'translate', 'help', 'select'):
-                if getattr(self._columns[f], arg):
-                    res[f][arg] = getattr(self._columns[f], arg)
-            if not read_access:
-                res[f]['readonly'] = True
-                res[f]['states'] = {}
-            for arg in ('digits', 'invisible','filters'):
-                if hasattr(self._columns[f], arg) \
-                        and getattr(self._columns[f], arg):
-                    res[f][arg] = getattr(self._columns[f], arg)
-
-            res_trans = translation_obj._get_source(cr, user, self._name + ',' + f, 'field', context.get('lang', False) or 'en_US')
-            if res_trans:
-                res[f]['string'] = res_trans
-            help_trans = translation_obj._get_source(cr, user, self._name + ',' + f, 'help', context.get('lang', False) or 'en_US')
-            if help_trans:
-                res[f]['help'] = help_trans
-
-            if hasattr(self._columns[f], 'selection'):
-                if isinstance(self._columns[f].selection, (tuple, list)):
-                    sel = self._columns[f].selection
-                    # translate each selection option
-                    sel2 = []
-                    for (key, val) in sel:
-                        val2 = None
-                        if val:
-                            val2 = translation_obj._get_source(cr, user, self._name + ',' + f, 'selection', context.get('lang', False) or 'en_US', val)
-                        sel2.append((key, val2 or val))
-                    sel = sel2
-                    res[f]['selection'] = sel
-                else:
-                    # call the 'dynamic selection' function
-                    res[f]['selection'] = self._columns[f].selection(self, cr,
-                            user, context)
-            if res[f]['type'] in ('one2many', 'many2many',
-                    'many2one', 'one2one'):
-                res[f]['relation'] = self._columns[f]._obj
-                res[f]['domain'] = self._columns[f]._domain
-                res[f]['context'] = self._columns[f]._context
-
+        
+        if self._columns.keys():
+            for f in self._columns.keys():
+                if fields and f not in fields:
+                    continue
+                res[f] = {'type': self._columns[f]._type}
+                for arg in ('string', 'readonly', 'states', 'size', 'required',
+                        'change_default', 'translate', 'help', 'select'):
+                    if getattr(self._columns[f], arg):
+                        res[f][arg] = getattr(self._columns[f], arg)
+                if not read_access:
+                    res[f]['readonly'] = True
+                    res[f]['states'] = {}
+                for arg in ('digits', 'invisible','filters'):
+                    if hasattr(self._columns[f], arg) \
+                            and getattr(self._columns[f], arg):
+                        res[f][arg] = getattr(self._columns[f], arg)
+    
+                res_trans = translation_obj._get_source(cr, user, self._name + ',' + f, 'field', context.get('lang', False) or 'en_US')
+                if res_trans:
+                    res[f]['string'] = res_trans
+                help_trans = translation_obj._get_source(cr, user, self._name + ',' + f, 'help', context.get('lang', False) or 'en_US')
+                if help_trans:
+                    res[f]['help'] = help_trans
+    
+                if hasattr(self._columns[f], 'selection'):
+                    if isinstance(self._columns[f].selection, (tuple, list)):
+                        sel = self._columns[f].selection
+                        # translate each selection option
+                        sel2 = []
+                        for (key, val) in sel:
+                            val2 = None
+                            if val:
+                                val2 = translation_obj._get_source(cr, user, self._name + ',' + f, 'selection', context.get('lang', False) or 'en_US', val)
+                            sel2.append((key, val2 or val))
+                        sel = sel2
+                        res[f]['selection'] = sel
+                    else:
+                        # call the 'dynamic selection' function
+                        res[f]['selection'] = self._columns[f].selection(self, cr,
+                                user, context)
+                if res[f]['type'] in ('one2many', 'many2many', 'many2one', 'one2one'):
+                    res[f]['relation'] = self._columns[f]._obj
+                    res[f]['domain'] = self._columns[f]._domain
+                    res[f]['context'] = self._columns[f]._context
+        else:
+            #TODO : read the fields from the database 
+            pass
+        
         if fields:
             # filter out fields which aren't in the fields list
             for r in res.keys():
@@ -770,7 +797,7 @@ class orm_template(object):
     def view_header_get(self, cr, user, view_id=None, view_type='form', context=None):
         return False
 
-    def __view_look_dom(self, cr, user, node, context=None):
+    def __view_look_dom(self, cr, user, node, view_id, context=None):
         if not context:
             context = {}
         result = False
@@ -796,7 +823,7 @@ class orm_template(object):
                             node.removeChild(f)
                             ctx = context.copy()
                             ctx['base_model_name'] = self._name
-                            xarch, xfields = self.pool.get(relation).__view_look_dom_arch(cr, user, f, ctx)
+                            xarch, xfields = self.pool.get(relation).__view_look_dom_arch(cr, user, f, view_id, ctx)
                             views[str(f.localName)] = {
                                 'arch': xarch,
                                 'fields': xfields
@@ -846,44 +873,47 @@ class orm_template(object):
 
         if childs:
             for f in node.childNodes:
-                fields.update(self.__view_look_dom(cr, user, f, context))
-
-        if ('state' not in fields) and (('state' in self._columns) or ('state' in self._inherit_fields)):
-            fields['state'] = {}
+                fields.update(self.__view_look_dom(cr, user, f, view_id, context))
 
         return fields
 
-    def __view_look_dom_arch(self, cr, user, node, context=None):
-        fields_def = self.__view_look_dom(cr, user, node, context=context)
+    def __view_look_dom_arch(self, cr, user, node, view_id, context=None):
+        fields_def = self.__view_look_dom(cr, user, node, view_id, context=context)
 
         rolesobj = self.pool.get('res.roles')
         usersobj = self.pool.get('res.users')
 
-        buttons = xpath.Evaluate('//button', node)
-        if buttons:
-            for button in buttons:
-                if button.getAttribute('type') == 'object':
-                    continue
-
-                ok = True
-
-                if user != 1:   # admin user has all roles
-                    user_roles = usersobj.read(cr, user, [user], ['roles_id'])[0]['roles_id']
-                    cr.execute("select role_id from wkf_transition where signal=%s", (button.getAttribute('name'),))
-                    roles = cr.fetchall()
-                    for role in roles:
-                        if role[0]:
-                            ok = ok and rolesobj.check(cr, user, user_roles, role[0])
-
-                if not ok:
-                    button.setAttribute('readonly', '1')
-                else:
-                    button.setAttribute('readonly', '0')
+        buttons = 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 rolesobj.check(cr, user, user_roles, role[0])
+
+            if not ok:
+                button.setAttribute('readonly', '1')
+            else:
+                button.setAttribute('readonly', '0')
 
         arch = node.toxml(encoding="utf-8").replace('\t', '')
         fields = self.fields_get(cr, user, fields_def.keys(), context)
         for field in fields_def:
-            fields[field].update(fields_def[field])
+            if field 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):
@@ -993,7 +1023,7 @@ 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}
@@ -1045,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,)
@@ -1057,20 +1087,26 @@ class orm_template(object):
                 _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)
+                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(encode(result['arch']))
-        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:
@@ -1169,6 +1205,8 @@ class orm_template(object):
                 self.pool.get(table).write_string(cr, uid, id, langs, vals, context)
         return True
 
+    def _check_removed_columns(self, cr, log=False):
+        raise NotImplementedError()
 
 class orm_memory(orm_template):
     _protected = ['read', 'write', 'create', 'default_get', 'perm_read', 'unlink', 'fields_get', 'fields_view_get', 'search', 'name_get', 'distinct_field_get', 'name_search', 'copy', 'import_data', 'search_count']
@@ -1352,21 +1390,26 @@ 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('init', netsvc.LOG_INFO, 'Computing parent left and right for table %s...' % (self._table, ))
+        logger.notifyChannel('orm', netsvc.LOG_INFO, 'Computing parent left and right for table %s...' % (self._table, ))
         def browse_rec(root, pos=0):
 # TODO: set order
             where = self._parent_name+'='+str(root)
             if not root:
                 where = self._parent_name+' IS NULL'
+            if self._parent_order:
+                where += ' order by '+self._parent_order
             cr.execute('SELECT id FROM '+self._table+' WHERE '+where)
             pos2 = pos + 1
             childs = cr.fetchall()
@@ -1374,12 +1417,18 @@ class orm(orm_template):
                 pos2 = browse_rec(id[0], pos2)
             cr.execute('update '+self._table+' set parent_left=%s, parent_right=%s where id=%s', (pos,pos2,root))
             return pos2+1
-        browse_rec(None)
+        query = 'SELECT id FROM '+self._table+' WHERE '+self._parent_name+' IS NULL'
+        if self._parent_order:
+            query += ' order by '+self._parent_order
+        pos = 0
+        cr.execute(query)
+        for (root,) in cr.fetchall():
+            pos = browse_rec(root, pos)
         return True
 
     def _update_store(self, cr, f, k):
         logger = netsvc.Logger()
-        logger.notifyChannel('init', netsvc.LOG_INFO, "storing computed values of fields.function '%s'" % (k,))
+        logger.notifyChannel('orm', netsvc.LOG_INFO, "storing computed values of fields.function '%s'" % (k,))
         ss = self._columns[k]._symbol_set
         update_query = 'UPDATE "%s" SET "%s"=%s WHERE id=%%s' % (self._table, k, ss[0])
         cr.execute('select id from '+self._table)
@@ -1397,6 +1446,26 @@ class orm(orm_template):
                 if (val<>False) or (type(val)<>bool):
                     cr.execute(update_query, (ss[1](val), key))
 
+    def _check_removed_columns(self, cr, log=False):
+        logger = netsvc.Logger()
+        # iterate on the database columns to drop the NOT NULL constraints
+        # of fields which were required but have been removed (or will be added by another module)
+        columns = [c for c in self._columns if not (isinstance(self._columns[c], fields.function) and not self._columns[c].store)]
+        columns += ('id', 'write_uid', 'write_date', 'create_uid', 'create_date') # openerp access columns
+        cr.execute("SELECT a.attname, a.attnotnull"
+                   "  FROM pg_class c, pg_attribute a"
+                   " WHERE c.relname=%%s"
+                   "   AND c.oid=a.attrelid"
+                   "   AND a.attisdropped=%%s"
+                   "   AND pg_catalog.format_type(a.atttypid, a.atttypmod) NOT IN ('cid', 'tid', 'oid', 'xid')"
+                   "   AND a.attname NOT IN (%s)" % ",".join(['%s']*len(columns)), 
+                       [self._table, False] + columns)
+        for column in cr.dictfetchall():
+            if log:
+                logger.notifyChannel("orm", netsvc.LOG_DEBUG, "column %s is in the table %s but not in the corresponding object %s" % (column['attname'], self._table, self._name))
+            if column['attnotnull']:
+                cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" DROP NOT NULL' % (self._table, column['attname']))
+
     def _auto_init(self, cr, context={}):
         store_compute =  False
         logger = netsvc.Logger()
@@ -1416,11 +1485,11 @@ class orm(orm_template):
                     """, (self._table, 'parent_left'))
                 if not cr.rowcount:
                     if 'parent_left' not in self._columns:
-                        logger.notifyChannel('init', netsvc.LOG_ERROR, 'create a column parent_left on object %s: fields.integer(\'Left Parent\', select=1)' % (self._table, ))
+                        logger.notifyChannel('orm', netsvc.LOG_ERROR, 'create a column parent_left on object %s: fields.integer(\'Left Parent\', select=1)' % (self._table, ))
                     if 'parent_right' not in self._columns:
-                        logger.notifyChannel('init', netsvc.LOG_ERROR, 'create a column parent_right on object %s: fields.integer(\'Right Parent\', select=1)' % (self._table, ))
+                        logger.notifyChannel('orm', netsvc.LOG_ERROR, 'create a column parent_right on object %s: fields.integer(\'Right Parent\', select=1)' % (self._table, ))
                     if self._columns[self._parent_name].ondelete<>'cascade':
-                        logger.notifyChannel('init', netsvc.LOG_ERROR, "the columns %s on object must be set as ondelete='cascasde'" % (self._name, self._parent_name))
+                        logger.notifyChannel('orm', netsvc.LOG_ERROR, "the columns %s on object must be set as ondelete='cascasde'" % (self._name, self._parent_name))
                     cr.execute('ALTER TABLE "%s" ADD COLUMN "parent_left" INTEGER' % (self._table,))
                     cr.execute('ALTER TABLE "%s" ADD COLUMN "parent_right" INTEGER' % (self._table,))
                     cr.commit()
@@ -1442,18 +1511,8 @@ class orm(orm_template):
                     if not cr.rowcount:
                         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 = []
@@ -1503,9 +1562,9 @@ class orm(orm_template):
                                 query = 'UPDATE "%s" SET "%s"=%s' % (self._table, k, ss[0])
                                 cr.execute(query, (ss[1](default),))
                                 cr.commit()
-                                logger.notifyChannel('init', netsvc.LOG_DEBUG, 'setting default value of new column %s of table %s'% (k, self._table))
+                                logger.notifyChannel('orm', netsvc.LOG_DEBUG, 'setting default value of new column %s of table %s'% (k, self._table))
                             elif not create:
-                                logger.notifyChannel('init', netsvc.LOG_DEBUG, 'creating new column %s of table %s'% (k, self._table))
+                                logger.notifyChannel('orm', netsvc.LOG_DEBUG, 'creating new column %s of table %s'% (k, self._table))
 
                             if isinstance(f, fields.function):
                                 order = 10
@@ -1530,7 +1589,7 @@ class orm(orm_template):
                                     cr.commit()
                                     cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" SET NOT NULL' % (self._table, k))
                                 except Exception, e:
-                                    logger.notifyChannel('init', netsvc.LOG_WARNING, 'WARNING: unable to set column %s of table %s not null !\nTry to re-run: openerp-server.py --update=module\nIf it doesn\'t work, update records and execute manually:\nALTER TABLE %s ALTER COLUMN %s SET NOT NULL' % (k, self._table, self._table, k))
+                                    logger.notifyChannel('orm', netsvc.LOG_WARNING, 'WARNING: unable to set column %s of table %s not null !\nTry to re-run: openerp-server.py --update=module\nIf it doesn\'t work, update records and execute manually:\nALTER TABLE %s ALTER COLUMN %s SET NOT NULL' % (k, self._table, self._table, k))
                             cr.commit()
                     elif len(res)==1:
                         f_pg_def = res[0]
@@ -1538,7 +1597,7 @@ class orm(orm_template):
                         f_pg_size = f_pg_def['size']
                         f_pg_notnull = f_pg_def['attnotnull']
                         if isinstance(f, fields.function) and not f.store:
-                            logger.notifyChannel('init', netsvc.LOG_INFO, 'column %s (%s) in table %s removed: converted to a function !\n' % (k, f.string, self._table))
+                            logger.notifyChannel('orm', netsvc.LOG_INFO, 'column %s (%s) in table %s removed: converted to a function !\n' % (k, f.string, self._table))
                             cr.execute('ALTER TABLE %s DROP COLUMN %s'% (self._table, k))
                             cr.commit()
                             f_obj_type = None
@@ -1554,7 +1613,7 @@ class orm(orm_template):
                                 ('date', 'datetime', 'TIMESTAMP', '::TIMESTAMP'),
                             ]
                             if f_pg_type == 'varchar' and f._type == 'char' and f_pg_size != f.size:
-                                logger.notifyChannel('init', netsvc.LOG_INFO, "column '%s' in table '%s' changed size" % (k, self._table))
+                                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))
@@ -1562,7 +1621,7 @@ class orm(orm_template):
                                 cr.commit()
                             for c in casts:
                                 if (f_pg_type==c[0]) and (f._type==c[1]):
-                                    logger.notifyChannel('init', netsvc.LOG_INFO, "column '%s' in table '%s' changed type to %s." % (k, self._table, c[1]))
+                                    logger.notifyChannel('orm', netsvc.LOG_INFO, "column '%s' in table '%s' changed type to %s." % (k, self._table, c[1]))
                                     ok = True
                                     cr.execute('ALTER TABLE "%s" RENAME COLUMN "%s" TO temp_change_size' % (self._table, k))
                                     cr.execute('ALTER TABLE "%s" ADD COLUMN "%s" %s' % (self._table, k, c[2]))
@@ -1572,7 +1631,7 @@ class orm(orm_template):
 
                             if f_pg_type != f_obj_type:
                                 if not ok:
-                                    logger.notifyChannel('init', netsvc.LOG_WARNING, "column '%s' in table '%s' has changed type (DB = %s, def = %s) but unable to migrate this change !" % (k, self._table, f_pg_type, f._type))
+                                    logger.notifyChannel('orm', netsvc.LOG_WARNING, "column '%s' in table '%s' has changed type (DB = %s, def = %s) but unable to migrate this change !" % (k, self._table, f_pg_type, f._type))
 
                             # if the field is required and hasn't got a NOT NULL constraint
                             if f.required and f_pg_notnull == 0:
@@ -1589,7 +1648,7 @@ class orm(orm_template):
                                     cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" SET NOT NULL' % (self._table, k))
                                     cr.commit()
                                 except Exception, e:
-                                    logger.notifyChannel('init', netsvc.LOG_WARNING, 'unable to set a NOT NULL constraint on column %s of the %s table !\nIf you want to have it, you should update the records and execute manually:\nALTER TABLE %s ALTER COLUMN %s SET NOT NULL' % (k, self._table, self._table, k))
+                                    logger.notifyChannel('orm', netsvc.LOG_WARNING, 'unable to set a NOT NULL constraint on column %s of the %s table !\nIf you want to have it, you should update the records and execute manually:\nALTER TABLE %s ALTER COLUMN %s SET NOT NULL' % (k, self._table, self._table, k))
                                 cr.commit()
                             elif not f.required and f_pg_notnull == 1:
                                 cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" DROP NOT NULL' % (self._table, k))
@@ -1635,7 +1694,6 @@ class orm(orm_template):
                                             cr.execute('ALTER TABLE "' + self._table + '" ADD FOREIGN KEY ("' + k + '") REFERENCES "' + ref + '" ON DELETE ' + f.ondelete)
                                             cr.commit()
                     else:
-                        logger = netsvc.Logger()
                         logger.notifyChannel('orm', netsvc.LOG_ERROR, "Programming error !")
             for order,f,k in todo_update_store:
                 todo_end.append((order, self._update_store, (f, k)))
@@ -1652,7 +1710,7 @@ class orm(orm_template):
                     cr.execute('alter table "%s" add constraint "%s_%s" %s' % (self._table, self._table, key, con,))
                     cr.commit()
                 except:
-                    logger.notifyChannel('init', netsvc.LOG_WARNING, 'unable to add \'%s\' constraint on table %s !\n If you want to have it, you should update the records and execute manually:\nALTER table %s ADD CONSTRAINT %s_%s %s' % (con, self._table, self._table, self._table, key, con,))
+                    logger.notifyChannel('orm', netsvc.LOG_WARNING, 'unable to add \'%s\' constraint on table %s !\n If you want to have it, you should update the records and execute manually:\nALTER table %s ADD CONSTRAINT %s_%s %s' % (con, self._table, self._table, self._table, key, con,))
 
         if create:
             if hasattr(self, "_sql"):
@@ -1667,6 +1725,11 @@ class orm(orm_template):
 
     def __init__(self, cr):
         super(orm, self).__init__(cr)
+        
+        if not hasattr(self, '_log_access'):
+            # if not access is not specify, it is the same value as _auto
+            self._log_access = not hasattr(self, "_auto") or self._auto
+
         self._columns = self._columns.copy()
         for store_field in self._columns:
             f = self._columns[store_field]
@@ -1861,13 +1924,20 @@ class orm(orm_template):
         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_to_read) + 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):
             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,)
@@ -1892,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)
@@ -2012,26 +2084,32 @@ class orm(orm_template):
             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]
 
         result_store = self._store_get_values(cr, uid, ids, None, context)
-        delta = context.get('read_delta', False)
-        if delta and self._log_access:
-            for i in range(0, len(ids), cr.IN_MAX):
-                sub_ids = ids[i:i+cr.IN_MAX]
-                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'))
+        
+        self._check_concurrency(cr, ids, context)
 
         self.pool.get('ir.model.access').check(cr, uid, self._name, 'unlink')
 
@@ -2069,7 +2147,10 @@ class orm(orm_template):
 
         for order, object, ids, fields in result_store:
             if object<>self._name:
-                self.pool.get(object)._store_set_values(cr, uid, ids, fields, context)
+                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
 
     #
@@ -2112,24 +2193,11 @@ class orm(orm_template):
             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(0, len(ids), cr.IN_MAX):
-                sub_ids = ids[i:i+cr.IN_MAX]
-                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 = []
@@ -2233,53 +2301,61 @@ class orm(orm_template):
             if self.pool._init:
                 self.pool._init_parent[self._name]=True
             else:
-                if vals[self._parent_name]:
-                    cr.execute('select parent_left,parent_right from '+self._table+' where id=%s', (vals[self._parent_name],))
-                else:
-                    cr.execute('SELECT parent_left,parent_right FROM '+self._table+' WHERE id IS NULL')
-                res = cr.fetchone()
-                if res:
-                    pleft,pright = res
-                else:
-                    cr.execute('select max(parent_right),max(parent_right)+1 from '+self._table)
-                    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
+                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:
-                        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))
-
-        if 'read_delta' in context:
-            del context['read_delta']
-
+                        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:
@@ -2312,12 +2388,19 @@ class orm(orm_template):
         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:
@@ -2332,7 +2415,7 @@ class orm(orm_template):
                 (table, col, col_detail) = self._inherit_fields[v]
                 tocreate[table][v] = vals[v]
                 del vals[v]
-        
+
         # 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:        
@@ -2390,8 +2473,17 @@ class orm(orm_template):
             else:
                 parent = vals.get(self._parent_name, False)
                 if parent:
-                    cr.execute('select parent_left from '+self._table+' where id=%s', (parent,))
-                    pleft = cr.fetchone()[0]
+                    cr.execute('select parent_right from '+self._table+' where '+self._parent_name+'=%s order by '+(self._parent_order or self._order), (parent,))
+                    pleft_old = None
+                    result_p = cr.fetchall()
+                    for (pleft,) in result_p:
+                        if not pleft:
+                            break
+                        pleft_old = pleft
+                    if not pleft_old:
+                        cr.execute('select parent_left from '+self._table+' where id=%s', (parent,))
+                        pleft_old = cr.fetchone()[0]
+                    pleft = pleft_old
                 else:
                     cr.execute('select max(parent_right) from '+self._table)
                     pleft = cr.fetchone()[0] or 0
@@ -2579,7 +2671,7 @@ class orm(orm_template):
         res = self.name_get(cr, user, ids, context)
         return res
 
-    def copy(self, cr, uid, id, default=None, context=None):
+    def copy_data(self, cr, uid, id, default=None, context=None):
         if not context:
             context = {}
         if not default:
@@ -2589,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']
 
@@ -2611,14 +2704,15 @@ class orm(orm_template):
                     # the lines are first duplicated using the wrong (old)
                     # parent but then are reassigned to the correct one thanks
                     # to the (4, ...)
-                    res.append((4, rel.copy(cr, uid, rel_id, context=context)))
+                    d,t = rel.copy_data(cr, uid, rel_id, context=context)
+                    res.append((0, 0, d))
+                    trans_data += t
                 data[f] = res
             elif ftype == 'many2many':
                 data[f] = [(6, 0, data[f])]
 
         trans_obj = self.pool.get('ir.translation')
         trans_name=''
-        trans_data=[]
         for f in fields:
             trans_flag=True
             if f in self._columns and self._columns[f].translate:
@@ -2640,14 +2734,15 @@ class orm(orm_template):
 
         for v in self._inherits:
             del data[self._inherits[v]]
+        return data, trans_data
 
+    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):