diff --git a/bin/osv/orm.py b/bin/osv/orm.py index 3c578cb0fb1..46f66ce6555 100644 --- a/bin/osv/orm.py +++ b/bin/osv/orm.py @@ -199,10 +199,10 @@ class browse_record(object): field_values = self._table.read(self._cr, self._uid, ids, field_names, context=self._context, load="_classic_write") if self._fields_process: lang = self._context.get('lang', 'en_US') or 'en_US' - lang_obj_ids = self.pool.get('res.lang').search(self._cr, self._uid,[('code','=',lang)]) + lang_obj_ids = self.pool.get('res.lang').search(self._cr, self._uid, [('code', '=', lang)]) if not lang_obj_ids: raise Exception(_('Language with code "%s" is not defined in your system !\nDefine it through the Administration menu.') % (lang,)) - lang_obj = self.pool.get('res.lang').browse(self._cr, self._uid,lang_obj_ids[0]) + lang_obj = self.pool.get('res.lang').browse(self._cr, self._uid, lang_obj_ids[0]) for field_name, field_column in fields_to_fetch: if field_column._type in self._fields_process: @@ -214,7 +214,7 @@ class browse_record(object): if not field_values: # Where did those ids come from? Perhaps old entries in ir_model_dat? self.__logger.warn("No field_values found for ids %s in %s", ids, self) - raise KeyError('Field %s not found in %s'%(name,self)) + raise KeyError('Field %s not found in %s'%(name, self)) # create browse records for 'remote' objects for result_line in field_values: new_data = {} @@ -222,7 +222,7 @@ class browse_record(object): if field_column._type in ('many2one', 'one2one'): if result_line[field_name]: obj = self._table.pool.get(field_column._obj) - if isinstance(result_line[field_name], (list,tuple)): + if isinstance(result_line[field_name], (list, tuple)): value = result_line[field_name][0] else: value = result_line[field_name] @@ -346,7 +346,7 @@ def get_pg_type(f): f_type = ('int4', 'INTEGER') else: f_type = ('varchar', 'VARCHAR(%d)' % f_size) - elif isinstance(f, fields.function) and eval('fields.'+(f._type),globals()) in type_dict: + elif isinstance(f, fields.function) and eval('fields.'+(f._type), globals()) in type_dict: t = eval('fields.'+(f._type), globals()) f_type = (type_dict[t], type_dict[t]) elif isinstance(f, fields.function) and f._type == 'float': @@ -393,7 +393,7 @@ class orm_template(object): context=context ) - def view_init(self, cr , uid , fields_list, context=None): + def view_init(self, cr, uid, fields_list, context=None): """Override this method to do specific things when a view on the object is opened.""" pass @@ -409,8 +409,8 @@ class orm_template(object): 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 and module=%s', (name_id,model_id,context['module'])) + name_id = 'model_'+self._name.replace('.', '_') + cr.execute('select * from ir_model_data where name=%s and res_id=%s and module=%s', (name_id, model_id, context['module'])) 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)", \ (name_id, context['module'], 'ir.model', model_id) @@ -433,15 +433,15 @@ class orm_template(object): 'relation': f._obj or '', 'view_load': (f.view_load and 1) or 0, 'select_level': tools.ustr(f.select or 0), - 'readonly':(f.readonly and 1) or 0, - 'required':(f.required and 1) or 0, - 'selectable' : (f.selectable and 1) or 0, - 'relation_field': (f._type=='one2many' and isinstance(f,fields.one2many)) and f._fields_id or '', + 'readonly': (f.readonly and 1) or 0, + 'required': (f.required and 1) or 0, + 'selectable': (f.selectable and 1) or 0, + 'relation_field': (f._type=='one2many' and isinstance(f, fields.one2many)) and f._fields_id or '', } # When its a custom field,it does not contain f.select - if context.get('field_state','base') == 'manual': - if context.get('field_name','') == k: - vals['select_level'] = context.get('select','0') + if context.get('field_state', 'base') == 'manual': + if context.get('field_name', '') == k: + vals['select_level'] = context.get('select', '0') #setting value to let the problem NOT occur next time else: vals['select_level'] = cols[k]['select_level'] @@ -458,7 +458,7 @@ class orm_template(object): )""", ( id, vals['model_id'], vals['model'], vals['name'], vals['field_description'], vals['ttype'], vals['relation'], bool(vals['view_load']), 'base', - vals['select_level'],vals['relation_field'] + vals['select_level'], vals['relation_field'] )) if 'module' in context: name1 = 'field_' + self._table + '_' + k @@ -480,7 +480,7 @@ class orm_template(object): model=%s AND name=%s""", ( vals['model_id'], vals['field_description'], vals['ttype'], vals['relation'], bool(vals['view_load']), - vals['select_level'], bool(vals['readonly']),bool(vals['required']),bool(vals['selectable']),vals['relation_field'],vals['model'], vals['name'] + vals['select_level'], bool(vals['readonly']), bool(vals['required']), bool(vals['selectable']), vals['relation_field'], vals['model'], vals['name'] )) continue cr.commit() @@ -494,8 +494,8 @@ class orm_template(object): 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 ) + logger.notifyChannel('orm', netsvc.LOG_ERROR, msg) + raise except_orm('ValueError', msg) if not self._description: self._description = self._name @@ -562,11 +562,11 @@ class orm_template(object): r = r['id'] elif f[i] == 'id': model_data = self.pool.get('ir.model.data') - data_ids = model_data.search(cr, uid, [('model','=',r._table_name),('res_id','=',r['id'])]) + data_ids = model_data.search(cr, uid, [('model', '=', r._table_name), ('res_id', '=', r['id'])]) if len(data_ids): - d = model_data.read(cr, uid, data_ids, ['name','module'])[0] + d = model_data.read(cr, uid, data_ids, ['name', 'module'])[0] if d['module']: - r = '%s.%s'%(d['module'],d['name']) + r = '%s.%s' % (d['module'], d['name']) else: r = d['name'] else: @@ -574,7 +574,7 @@ class orm_template(object): else: r = r[f[i]] # To display external name of selection field when its exported - if not context.get('import_comp',False):# Allow external name only if its not import compatible + if not context.get('import_comp', False):# Allow external name only if its not import compatible cols = False if f[i] in self._columns.keys(): cols = self._columns[f[i]] @@ -608,7 +608,7 @@ class orm_template(object): data[fpos2] = lines2[0][fpos2] if not data[fpos]: dt = '' - for rr in r : + for rr in r: if isinstance(rr.name, browse_record): rr = rr.name rr_name = self.pool.get(rr._table_name).name_get(cr, uid, [rr.id], context=context) @@ -645,31 +645,31 @@ class orm_template(object): """ if context is None: context = {} - imp_comp = context.get('import_comp',False) + imp_comp = context.get('import_comp', False) cols = self._columns.copy() for f in self._inherit_fields: cols.update({f: self._inherit_fields[f][2]}) fields_to_export = map(lambda x: x.split('/'), fields_to_export) - fields_export = fields_to_export+[] + fields_export = fields_to_export + [] warning = '' warning_fields = [] for field in fields_export: - if imp_comp and len(field)>1: - warning_fields.append('/'.join(map(lambda x:x in cols and cols[x].string or x,field))) - elif len (field) <=1: - if imp_comp and cols.get(field and field[0],False): + if imp_comp and len(field) > 1: + warning_fields.append('/'.join(map(lambda x: x in cols and cols[x].string or x,field))) + elif len (field) <= 1: + if imp_comp and cols.get(field and field[0], False): if ((isinstance(cols[field[0]], fields.function) and not cols[field[0]].store) \ or isinstance(cols[field[0]], fields.related)\ or isinstance(cols[field[0]], fields.one2many)): - warning_fields.append('/'.join(map(lambda x:x in cols and cols[x].string or x,field))) + warning_fields.append('/'.join(map(lambda x: x in cols and cols[x].string or x,field))) datas = [] if imp_comp and len(warning_fields): - warning = 'Following columns cannot be exported since you select to be import compatible.\n%s' %('\n'.join(warning_fields)) + warning = 'Following columns cannot be exported since you select to be import compatible.\n%s' % ('\n'.join(warning_fields)) cr.rollback() - return {'warning' : warning} + return {'warning': warning} for row in self.browse(cr, uid, ids, context): datas += self.__export_row(cr, uid, row, fields_to_export, context) - return {'datas':datas} + return {'datas': datas} def import_data(self, cr, uid, fields, datas, mode='init', current_module='', noupdate=False, context=None, filename=None): """ @@ -698,7 +698,7 @@ class orm_template(object): def _check_db_id(self, model_name, db_id): obj_model = self.pool.get(model_name) - ids = obj_model.search(cr, uid, [('id','=',int(db_id))]) + ids = obj_model.search(cr, uid, [('id', '=', int(db_id))]) if not len(ids): raise Exception(_("Database ID doesn't exist: %s : %s") %(model_name, db_id)) return True @@ -732,15 +732,15 @@ class orm_template(object): res = False if line[i]: field_name = field[0].split(':')[0] - model_rel = fields_def[field_name]['relation'] + model_rel = fields_def[field_name]['relation'] - if fields_def[field[len(prefix)][:-6]]['type']=='many2many': + if fields_def[field[len(prefix)][:-6]]['type'] == 'many2many': res_id = [] for db_id in line[i].split(config.get('csv_internal_sep')): try: _check_db_id(self, model_rel, db_id) res_id.append(db_id) - except Exception,e: + except Exception, e: warning += [tools.exception_to_unicode(e)] logger.notifyChannel("import", netsvc.LOG_ERROR, tools.exception_to_unicode(e)) @@ -750,7 +750,7 @@ class orm_template(object): try: _check_db_id(self, model_rel, line[i]) res = line[i] - except Exception,e: + except Exception, e: warning += [tools.exception_to_unicode(e)] logger.notifyChannel("import", netsvc.LOG_ERROR, tools.exception_to_unicode(e)) @@ -760,7 +760,7 @@ class orm_template(object): 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': + if fields_def[field[len(prefix)][:-3]]['type'] == 'many2many': res_id = [] for word in line[i].split(config.get('csv_internal_sep')): if '.' in word: @@ -791,7 +791,7 @@ class orm_template(object): if (len(field) == len(prefix)+1) and \ len(field[len(prefix)].split(':lang=')) == 2: f, lang = field[len(prefix)].split(':lang=') - translate.setdefault(lang, {})[f]=line[i] or False + translate.setdefault(lang, {})[f] = line[i] or False continue if (len(field) == len(prefix)+1) and \ (prefix == field[0:len(prefix)]): @@ -799,22 +799,22 @@ class orm_template(object): # XML ID db_id = False is_xml_id = data_id = line[i] - d = data_id.split('.') - module = len(d)>1 and d[0] or '' - name = len(d)>1 and d[1] or d[0] - data_ids = ir_model_data_obj.search(cr, uid, [('module','=',module),('model','=',model_name),('name','=',name)]) + d = data_id.split('.') + module = len(d) > 1 and d[0] or '' + name = len(d) > 1 and d[1] or d[0] + data_ids = ir_model_data_obj.search(cr, uid, [('module', '=', module), ('model', '=', model_name), ('name', '=', name)]) if len(data_ids): d = ir_model_data_obj.read(cr, uid, data_ids, ['res_id'])[0] db_id = d['res_id'] if is_db_id and not db_id: - data_ids = ir_model_data_obj.search(cr, uid, [('module','=',module),('model','=',model_name),('res_id','=',is_db_id)]) + data_ids = ir_model_data_obj.search(cr, uid, [('module', '=', module), ('model', '=', model_name), ('res_id', '=', is_db_id)]) if not len(data_ids): - ir_model_data_obj.create(cr, uid, {'module':module, 'model':model_name, 'name':name, 'res_id':is_db_id}) + ir_model_data_obj.create(cr, uid, {'module': module, 'model': model_name, 'name': name, 'res_id': is_db_id}) db_id = is_db_id if is_db_id and int(db_id) != int(is_db_id): - warning += [_("Id is not the same than existing one: %s")%(is_db_id)] + warning += [_("Id is not the same than existing one: %s") % (is_db_id)] logger.notifyChannel("import", netsvc.LOG_ERROR, - _("Id is not the same than existing one: %s")%(is_db_id)) + _("Id is not the same than existing one: %s") % (is_db_id)) continue if field[len(prefix)] == "db_id": @@ -822,25 +822,25 @@ class orm_template(object): try: _check_db_id(self, model_name, line[i]) data_res_id = is_db_id = int(line[i]) - except Exception,e: + except Exception, e: warning += [tools.exception_to_unicode(e)] logger.notifyChannel("import", netsvc.LOG_ERROR, tools.exception_to_unicode(e)) continue - data_ids = ir_model_data_obj.search(cr, uid, [('model','=',model_name),('res_id','=',line[i])]) + data_ids = ir_model_data_obj.search(cr, uid, [('model', '=', model_name), ('res_id', '=', line[i])]) if len(data_ids): - d = ir_model_data_obj.read(cr, uid, data_ids, ['name','module'])[0] + d = ir_model_data_obj.read(cr, uid, data_ids, ['name', 'module'])[0] data_id = d['name'] if d['module']: - data_id = '%s.%s'%(d['module'],d['name']) + data_id = '%s.%s' % (d['module'], d['name']) else: data_id = d['name'] if is_xml_id and not data_id: data_id = is_xml_id - if is_xml_id and is_xml_id!=data_id: - warning += [_("Id is not the same than existing one: %s")%(line[i])] + if is_xml_id and is_xml_id != data_id: + warning += [_("Id is not the same than existing one: %s") % (line[i])] logger.notifyChannel("import", netsvc.LOG_ERROR, - _("Id is not the same than existing one: %s")%(line[i])) + _("Id is not the same than existing one: %s") % (line[i])) continue if fields_def[field[len(prefix)]]['type'] == 'integer': @@ -858,7 +858,7 @@ class orm_template(object): sel = fields_def[field[len(prefix)]]['selection'](self, cr, uid, context) for key, val in sel: - if line[i] in [tools.ustr(key),tools.ustr(val)]: #Acepting key or value for selection field + if line[i] in [tools.ustr(key), tools.ustr(val)]: #Acepting key or value for selection field res = key break if line[i] and not res: @@ -866,9 +866,9 @@ class orm_template(object): _("key '%s' not found in selection field '%s'") % \ (line[i], field[len(prefix)])) - warning += [_("Key/value '%s' not found in selection field '%s'")%(line[i],field[len(prefix)])] + warning += [_("Key/value '%s' not found in selection field '%s'") % (line[i], field[len(prefix)])] - elif fields_def[field[len(prefix)]]['type']=='many2one': + elif fields_def[field[len(prefix)]]['type'] == 'many2one': res = False if line[i]: relation = fields_def[field[len(prefix)]]['relation'] @@ -876,10 +876,10 @@ class orm_template(object): line[i], [], operator='=', context=context) res = (res2 and res2[0][0]) or False if not res: - warning += [_("Relation not found: %s on '%s'")%(line[i],relation)] + warning += [_("Relation not found: %s on '%s'") % (line[i], relation)] logger.notifyChannel("import", netsvc.LOG_WARNING, - _("Relation not found: %s on '%s'")%(line[i],relation)) - elif fields_def[field[len(prefix)]]['type']=='many2many': + _("Relation not found: %s on '%s'") % (line[i], relation)) + elif fields_def[field[len(prefix)]]['type'] == 'many2many': res = [] if line[i]: relation = fields_def[field[len(prefix)]]['relation'] @@ -888,10 +888,10 @@ class orm_template(object): uid, word, [], operator='=', context=context) res3 = (res2 and res2[0][0]) or False if not res3: - warning += [_("Relation not found: %s on '%s'")%(line[i],relation)] + warning += [_("Relation not found: %s on '%s'") % (line[i], relation)] logger.notifyChannel("import", netsvc.LOG_WARNING, - _("Relation not found: %s on '%s'")%(line[i],relation)) + _("Relation not found: %s on '%s'") % (line[i], relation)) else: res.append(res3) if len(res): @@ -918,7 +918,7 @@ class orm_template(object): row[field] = (reduce(lambda x, y: x or y, newrow.values()) and \ [(0, 0, newrow)]) or [] i = max2 - while (position+i)1: + if result['type'] == 'form' and result['arch'].count("default_focus") > 1: msg = "Form View contain more than one default_focus attribute" netsvc.Logger().notifyChannel('orm', netsvc.LOG_ERROR, msg) - raise except_orm('View Error !',msg) + raise except_orm('View Error !', msg) return result _view_look_dom_arch = __view_look_dom_arch @@ -1980,7 +1980,7 @@ class orm_memory(orm_template): if self.datas[id]['internal.date_access'] < max: tounlink.append(id) self.unlink(cr, 1, tounlink) - if len(self.datas)>self._max_count: + 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]) @@ -2074,7 +2074,7 @@ class orm_memory(orm_template): if not context: context = {} args = args[:] - res=[] + res = [] # 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)): @@ -2105,16 +2105,16 @@ class orm_memory(orm_template): args.insert(0, ('internal.create_uid', '=', user)) result = self._where_calc(cr, user, args, context=context) - if result==[]: + if result == []: return self.datas.keys() - res=[] - counter=0 + res = [] + counter = 0 #Find the value of dict - f=False + f = False if result: for id, data in self.datas.items(): - counter=counter+1 + counter = counter + 1 data['id'] = id if limit and (counter > int(limit)): break @@ -2122,7 +2122,7 @@ class orm_memory(orm_template): for arg in result: if arg[1] == '=': val = eval('data[arg[0]]'+'==' +' arg[2]', locals()) - elif arg[1] in ['<','>','in','not in','<=','>=','<>']: + elif arg[1] in ['<', '>', 'in', 'not in', '<=', '>=', '<>']: val = eval('data[arg[0]]'+arg[1] +' arg[2]', locals()) elif arg[1] in ['ilike']: val = (str(data[arg[0]]).find(str(arg[2]))!=-1) @@ -2168,7 +2168,7 @@ class orm_memory(orm_template): class orm(orm_template): _sql_constraints = [] _table = None - _protected = ['read','write','create','default_get','perm_read','unlink','fields_get','fields_view_get','search','name_get','distinct_field_get','name_search','copy','import_data','search_count', 'exists'] + _protected = ['read', 'write', 'create', 'default_get', 'perm_read', 'unlink', 'fields_get', 'fields_view_get', 'search', 'name_get', 'distinct_field_get', 'name_search', 'copy', 'import_data', 'search_count', 'exists'] def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None): """ @@ -2219,7 +2219,7 @@ class orm(orm_template): if groupby: if groupby and isinstance(groupby, list): groupby = groupby[0] - tables, where_clause, qfield = self._inherits_join_calc(groupby,tables,where_clause) + tables, where_clause, qfield = self._inherits_join_calc(groupby, tables, where_clause) if len(where_clause): where_clause = ' where ' + ' and '.join(where_clause) @@ -2229,15 +2229,15 @@ class orm(orm_template): offset_str = offset and ' offset %d' % offset or '' fget = self.fields_get(cr, uid, fields) - float_int_fields = filter(lambda x: fget[x]['type'] in ('float','integer'), fields) + float_int_fields = filter(lambda x: fget[x]['type'] in ('float', 'integer'), fields) sum = {} flist = '' group_by = groupby if groupby: - if fget.get(groupby,False) and fget[groupby]['type'] in ('date','datetime'): - flist = "to_char(%s,'yyyy-mm') as %s "%(groupby,groupby) - groupby = "to_char(%s,'yyyy-mm')"%(groupby) + if fget.get(groupby, False) and fget[groupby]['type'] in ('date', 'datetime'): + flist = "to_char(%s,'yyyy-mm') as %s " % (groupby, groupby) + groupby = "to_char(%s,'yyyy-mm')" % (groupby) else: flist = groupby @@ -2246,8 +2246,8 @@ class orm(orm_template): f == self.CONCURRENCY_CHECK_FIELD or (f in self._columns and getattr(self._columns[f], '_classic_write'))] for f in fields_pre: - if f not in ['id','sequence']: - operator = fget[f].get('group_operator','sum') + if f not in ['id', 'sequence']: + operator = fget[f].get('group_operator', 'sum') if flist: flist += ',' flist += operator+'('+f+') as '+f @@ -2260,25 +2260,25 @@ class orm(orm_template): alldata = {} groupby = group_by for r in cr.dictfetchall(): - for fld,val in r.items(): - if val == None:r[fld] = False + for fld, val in r.items(): + if val == None: r[fld] = False alldata[r['id']] = r del r['id'] data = self.read(cr, uid, alldata.keys(), groupby and [groupby] or ['id'], context=context) for d in data: if groupby: - d['__domain'] = [(groupby,'=',alldata[d['id']][groupby] or False)] + domain - if not isinstance(groupby_list,(str, unicode)): + d['__domain'] = [(groupby, '=', alldata[d['id']][groupby] or False)] + domain + if not isinstance(groupby_list, (str, unicode)): if groupby or not context.get('group_by_no_leaf', False): - d['__context'] = {'group_by':groupby_list[1:]} + d['__context'] = {'group_by': groupby_list[1:]} if groupby and groupby in fget: - if d[groupby] and fget[groupby]['type'] in ('date','datetime'): - dt = datetime.datetime.strptime(alldata[d['id']][groupby][:7],'%Y-%m') + if d[groupby] and fget[groupby]['type'] in ('date', 'datetime'): + dt = datetime.datetime.strptime(alldata[d['id']][groupby][:7], '%Y-%m') days = calendar.monthrange(dt.year, dt.month)[1] - d[groupby] = datetime.datetime.strptime(d[groupby][:10],'%Y-%m-%d').strftime('%B %Y') - d['__domain'] = [(groupby,'>=',alldata[d['id']][groupby] and datetime.datetime.strptime(alldata[d['id']][groupby][:7] + '-01','%Y-%m-%d').strftime('%Y-%m-%d') or False),\ - (groupby,'<=',alldata[d['id']][groupby] and datetime.datetime.strptime(alldata[d['id']][groupby][:7] + '-' + str(days),'%Y-%m-%d').strftime('%Y-%m-%d') or False)] + domain + d[groupby] = datetime.datetime.strptime(d[groupby][:10], '%Y-%m-%d').strftime('%B %Y') + d['__domain'] = [(groupby, '>=', alldata[d['id']][groupby] and datetime.datetime.strptime(alldata[d['id']][groupby][:7] + '-01', '%Y-%m-%d').strftime('%Y-%m-%d') or False),\ + (groupby, '<=', alldata[d['id']][groupby] and datetime.datetime.strptime(alldata[d['id']][groupby][:7] + '-' + str(days), '%Y-%m-%d').strftime('%Y-%m-%d') or False)] + domain del alldata[d['id']][groupby] d.update(alldata[d['id']]) del d['id'] @@ -2326,7 +2326,7 @@ class orm(orm_template): if not self._parent_store: return logger = netsvc.Logger() - logger.notifyChannel('orm', netsvc.LOG_INFO, 'Computing parent left and right for table %s...' % (self._table, )) + logger.notifyChannel('data', 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) @@ -2339,11 +2339,11 @@ class orm(orm_template): 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 + 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 + query += ' order by ' + self._parent_order pos = 0 cr.execute(query) for (root,) in cr.fetchall(): @@ -2352,7 +2352,7 @@ class orm(orm_template): def _update_store(self, cr, f, k): logger = netsvc.Logger() - logger.notifyChannel('orm', netsvc.LOG_INFO, "storing computed values of fields.function '%s'" % (k,)) + logger.notifyChannel('data', 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) @@ -2361,11 +2361,11 @@ class orm(orm_template): iids = ids_lst[:40] ids_lst = ids_lst[40:] res = f.get(cr, self, iids, k, 1, {}) - for key,val in res.items(): + 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: + if type(val) == tuple: val = val[0] if (val<>False) or (type(val)<>bool): cr.execute(update_query, (ss[1](val), key)) @@ -2382,26 +2382,30 @@ class orm(orm_template): " 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" ,(self._table, False, tuple(columns))), + " AND a.attname NOT IN %s", (self._table, False, tuple(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'])) + logging.getLogger('schema').info("Table '%s': column '%s': dropped NOT NULL constraint" % (self._table, column['attname'])) def _auto_init(self, cr, context={}): - store_compute = False + store_compute = False logger = netsvc.Logger() create = False todo_end = [] self._field_create(cr, context=context) if getattr(self, '_auto', True): - 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,)) if not cr.rowcount: cr.execute('CREATE TABLE "%s" (id SERIAL NOT NULL, PRIMARY KEY(id)) WITHOUT OIDS' % (self._table,)) - cr.execute("COMMENT ON TABLE \"%s\" IS '%s'" % (self._table, self._description.replace("'","''"))) + cr.execute("COMMENT ON TABLE \"%s\" IS '%s'" % (self._table, self._description.replace("'", "''"))) create = True + + logging.getLogger('schema').info("Table '%s': created" % (self._table, )) + cr.commit() if self._parent_store: cr.execute("""SELECT c.relname @@ -2409,14 +2413,17 @@ class orm(orm_template): 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 column %s on object %s must be set as ondelete='cascade'" % (self._parent_name, self._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,)) + 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, )) + logging.getLogger('schema').info("Table '%s': added column '%s' with definition=%s" % (self._table, 'parent_left', 'INTEGER')) + 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, )) + logging.getLogger('schema').info("Table '%s': added column '%s' with definition=%s" % (self._table, 'parent_right', 'INTEGER')) + if self._columns[self._parent_name].ondelete != 'cascade': + logger.notifyChannel('orm', netsvc.LOG_ERROR, "The column %s on object %s must be set as ondelete='cascade'" % (self._parent_name, self._name)) + cr.commit() store_compute = True @@ -2436,6 +2443,7 @@ class orm(orm_template): if not cr.rowcount: cr.execute('ALTER TABLE "%s" ADD COLUMN "%s" %s' % (self._table, k, logs[k])) cr.commit() + logging.getLogger('schema').info("Table '%s': added column '%s' with definition=%s" % (self._table, k, logs[k])) self._check_removed_columns(cr, log=False) @@ -2457,13 +2465,16 @@ class orm(orm_template): if self.pool.get(f._obj): if f._fields_id not in self.pool.get(f._obj)._columns.keys(): if not self.pool.get(f._obj)._inherits or (f._fields_id not in self.pool.get(f._obj)._inherit_fields.keys()): - raise except_orm('Programming Error', ("There is no reference field '%s' found for '%s'") % (f._fields_id,f._obj,)) + raise except_orm('Programming Error', ("There is no reference field '%s' found for '%s'") % (f._fields_id, f._obj,)) if cr.fetchone(): 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)) + logging.getLogger('schema').info("Table '%s': added foreign key '%s' with definition=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(): @@ -2476,6 +2487,7 @@ class orm(orm_template): cr.execute('CREATE INDEX "%s_%s_index" ON "%s" ("%s")' % (f._rel, f._id2, f._rel, f._id2)) cr.execute("COMMENT ON TABLE \"%s\" IS 'RELATION BETWEEN %s AND %s'" % (f._rel, self._table, ref)) cr.commit() + logging.getLogger('schema').info("Create table '%s': relation between '%s' and '%s'" % (f._rel, self._table, ref)) 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 " \ @@ -2484,7 +2496,7 @@ class orm(orm_template): "AND c.oid=a.attrelid " \ "AND a.atttypid=t.oid", (self._table, k)) res = cr.dictfetchall() - if not res and hasattr(f,'oldname'): + if not res and hasattr(f, 'oldname'): 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 " \ @@ -2492,14 +2504,13 @@ class orm(orm_template): "AND c.oid=a.attrelid " \ "AND a.atttypid=t.oid", (self._table, f.oldname)) res_old = cr.dictfetchall() - logger.notifyChannel('orm', netsvc.LOG_DEBUG, 'trying to rename %s(%s) to %s'% (self._table, f.oldname, k)) - if res_old and len(res_old)==1: - cr.execute('ALTER TABLE "%s" RENAME "%s" TO "%s"' % ( self._table,f.oldname, k)) + if res_old and len(res_old) == 1: + cr.execute('ALTER TABLE "%s" RENAME "%s" TO "%s"' % (self._table, f.oldname, k)) res = res_old res[0]['attname'] = k + logging.getLogger('schema').info("Table '%s': renamed column '%s' to '%s'" % (self._table, f.oldname, k)) - - if len(res)==1: + if len(res) == 1: f_pg_def = res[0] f_pg_type = f_pg_def['typname'] f_pg_size = f_pg_def['size'] @@ -2507,8 +2518,9 @@ class orm(orm_template): if isinstance(f, fields.function) and not f.store and\ not getattr(f, 'nodrop', False): 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" CASCADE'% (self._table, k)) + cr.execute('ALTER TABLE "%s" DROP COLUMN "%s" CASCADE' % (self._table, k)) cr.commit() + logging.getLogger('schema').info("Table '%s': dropped column '%s' with cascade" % (self._table, k)) f_obj_type = None else: f_obj_type = get_pg_type(f) and get_pg_type(f)[0] @@ -2525,23 +2537,22 @@ class orm(orm_template): ('float8', 'float', get_pg_type(f)[1], '::'+get_pg_type(f)[1]), ] if f_pg_type == 'varchar' and f._type == 'char' and f_pg_size < f.size: - logger.notifyChannel('orm', netsvc.LOG_INFO, "column '%s' in table '%s' changed size" % (k, self._table)) cr.execute('ALTER TABLE "%s" RENAME COLUMN "%s" TO temp_change_size' % (self._table, k)) cr.execute('ALTER TABLE "%s" ADD COLUMN "%s" VARCHAR(%d)' % (self._table, k, f.size)) cr.execute('UPDATE "%s" SET "%s"=temp_change_size::VARCHAR(%d)' % (self._table, k, f.size)) cr.execute('ALTER TABLE "%s" DROP COLUMN temp_change_size CASCADE' % (self._table,)) cr.commit() + logging.getLogger('schema').info("Table '%s': column '%s' (type varchar) changed size from %s to %s" % (self._table, k, f_pg_size, f.size)) for c in casts: if (f_pg_type==c[0]) and (f._type==c[1]): if f_pg_type != f_obj_type: - if f_pg_type != f_obj_type: - 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() + logging.getLogger('schema').info("Table '%s': column '%s' changed type from %s to %s" % (self._table, k, c[0], c[1])) break if f_pg_type != f_obj_type: @@ -2555,13 +2566,15 @@ class orm(orm_template): "AND c.oid=a.attrelid ", (self._table, newname)) if not cr.fetchone()[0]: break - i+=1 - logger.notifyChannel('orm', netsvc.LOG_WARNING, "column '%s' in table '%s' has changed type (DB=%s, def=%s), data moved to table %s !" % (k, self._table, f_pg_type, f._type, newname)) + i += 1 if f_pg_notnull: cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" DROP NOT NULL' % (self._table, k)) cr.execute('ALTER TABLE "%s" RENAME COLUMN "%s" TO "%s"' % (self._table, k, newname)) cr.execute('ALTER TABLE "%s" ADD COLUMN "%s" %s' % (self._table, k, get_pg_type(f)[1])) - cr.execute("COMMENT ON COLUMN %s.%s IS '%s'" % (self._table, k, f.string.replace("'","''"))) + cr.execute("COMMENT ON COLUMN %s.%s IS '%s'" % (self._table, k, f.string.replace("'", "''"))) + logging.getLogger('schema').info("Table '%s': column '%s' has changed type (DB=%s, def=%s), data moved to column %s !" % ( + self._table, k, f_pg_type, f._type, newname) + ) # if the field is required and hasn't got a NOT NULL constraint if f.required and f_pg_notnull == 0: @@ -2581,13 +2594,18 @@ class orm(orm_template): try: cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" SET NOT NULL' % (self._table, k)) cr.commit() + logging.getLogger('schema').info("Table '%s': column '%s': added NOT NULL constraint" % (self._table, k)) except Exception: - 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)) + msg = "Table '%s': unable to set a NOT NULL constraint on column '%s' !\n"\ + "If you want to have it, you should update the records and execute manually:\n"\ + "ALTER TABLE %s ALTER COLUMN %s SET NOT NULL" % ( + self._table, k, self._table, k) + logger.notifyChannel('schema', netsvc.LOG_WARNING, msg) 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.commit() - + logging.getLogger('schema').info("Table '%s': column '%s': dropped NOT NULL constraint" % (self._table, k)) # Verify index indexname = '%s_%s_index' % (self._table, k) cr.execute("SELECT indexname FROM pg_indexes WHERE indexname = %s and tablename = %s", (indexname, self._table)) @@ -2597,13 +2615,16 @@ class orm(orm_template): cr.commit() if f._type == 'text': # FIXME: for fields.text columns we should try creating GIN indexes instead (seems most suitable for an ERP context) - logger.notifyChannel('orm', netsvc.LOG_WARNING, "Adding (b-tree) index for text column '%s' in table '%s'."\ - "This is probably useless (does not work for fulltext search) and prevents INSERTs of long texts because there is a length limit for indexable btree values!\n"\ - "Use a search view instead if you simply want to make the field searchable." % (k, f._type, self._table)) + msg = "Table '%s': Adding (b-tree) index for text column '%s'."\ + "This is probably useless (does not work for fulltext search) and prevents INSERTs of long texts"\ + " because there is a length limit for indexable btree values!\n"\ + "Use a search view instead if you simply want to make the field searchable." % (self._table, k, f._type) + logger.notifyChannel('schema', netsvc.LOG_WARNING, msg) if res2 and not f.select: cr.execute('DROP INDEX "%s_%s_index"' % (self._table, k)) cr.commit() - logger.notifyChannel('orm', netsvc.LOG_WARNING, "Dropping index for column '%s' of type '%s' in table '%s' as it is not required anymore" % (k, f._type, self._table)) + msg = "Table '%s': dropping index for column '%s' of type '%s' as it is not required anymore" % (self._table, k, f._type) + logger.notifyChannel('schema', netsvc.LOG_WARNING, msg) if isinstance(f, fields.many2one): ref = self.pool.get(f._obj)._table @@ -2629,14 +2650,15 @@ class orm(orm_template): cr.execute('ALTER TABLE "' + self._table + '" DROP CONSTRAINT "' + res2[0]['conname'] + '"') cr.execute('ALTER TABLE "' + self._table + '" ADD FOREIGN KEY ("' + k + '") REFERENCES "' + ref + '" ON DELETE ' + f.ondelete) cr.commit() - elif len(res)>1: - logger.notifyChannel('orm', netsvc.LOG_ERROR, "Programming error, column %s->%s has multiple instances !"%(self._table,k)) + logging.getLogger('schema').info("Table '%s': column '%s': XXX" % (self._table, k)) + elif len(res) > 1: + logger.notifyChannel('orm', netsvc.LOG_ERROR, "Programming error, column %s->%s has multiple instances !" % (self._table, k)) if not res: 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("COMMENT ON COLUMN %s.%s IS '%s'" % (self._table, k, f.string.replace("'","''"))) + cr.execute("COMMENT ON COLUMN %s.%s IS '%s'" % (self._table, k, f.string.replace("'", "''"))) + logging.getLogger('schema').info("Table '%s': added column '%s' with definition=%s" % (self._table, k, get_pg_type(f)[1])) # initialize it if not create and k in self._defaults: @@ -2649,15 +2671,13 @@ 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('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)) + logger.notifyChannel('data', netsvc.LOG_DEBUG, "Table '%s': setting default value of new column %s" % (self._table, k)) 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)) + todo_update_store.append((order, f, k)) # and add constraints if needed if isinstance(f, fields.many2one): @@ -2668,16 +2688,23 @@ class orm(orm_template): # 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)) + logging.getLogger('schema').info("Table '%s': added foreign key '%s' with definition=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)) if f.required: try: cr.commit() cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" SET NOT NULL' % (self._table, k)) + logging.getLogger('schema').info("Table '%s': column '%s': added a NOT NULL constraint" % (self._table, k)) except Exception: - 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)) + msg = "WARNING: unable to set column %s of table %s not null !\n"\ + "Try to re-run: openerp-server.py --update=module\n"\ + "If it doesn't work, update records and execute manually:\n"\ + "ALTER TABLE %s ALTER COLUMN %s SET NOT NULL" % (k, self._table, self._table, k) + logger.notifyChannel('orm', netsvc.LOG_WARNING, msg) cr.commit() - for order,f,k in todo_update_store: + for order, f, k in todo_update_store: todo_end.append((order, self._update_store, (f, k))) else: @@ -2694,8 +2721,11 @@ class orm(orm_template): try: cr.execute(query) cr.commit() + logging.getLogger('schema').info("Table '%s': added constraint '%s' with definition=%s" % (self._table, conname, con)) except: - 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:\n%s' % (con, self._table, query)) + msg = "Table '%s': unable to add \'%s\' constraint !\n If you want to have it, you should update the records and execute manually:\n%s" % ( + self._table, con, query) + logger.notifyChannel('schema', netsvc.LOG_WARNING, msg) cr.rollback() if create: @@ -2727,27 +2757,27 @@ class orm(orm_template): 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, None)} + sm = {self._name: (lambda self, cr, uid, ids, c={}: ids, None, 10, None)} else: sm = self._columns[store_field].store for object, aa in sm.items(): - if len(aa)==4: - (fnct,fields2,order,length)=aa - elif len(aa)==3: - (fnct,fields2,order)=aa + if len(aa) == 4: + (fnct, fields2, order, length) = aa + elif len(aa) == 3: + (fnct, fields2, order) = aa length = None else: raise except_orm('Error', ('Invalid function definition %s in object %s !\nYou must use the definition: store={object:(fnct, fields, priority, time length)}.' % (store_field, self._name))) self.pool._store_function.setdefault(object, []) ok = True - for x,y,z,e,f,l in self.pool._store_function[object]: + for x, y, z, e, f, l in self.pool._store_function[object]: if (x==self._name) and (y==store_field) and (e==fields2): - if f==order: + if f == order: ok = False if ok: self.pool._store_function[object].append( (self._name, store_field, fnct, fields2, order, length)) - self.pool._store_function[object].sort(lambda x,y: cmp(x[4],y[4])) + 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 @@ -2782,14 +2812,14 @@ class orm(orm_template): elif field['ttype'] == 'many2many': _rel1 = field['relation'].replace('.', '_') _rel2 = field['model'].replace('.', '_') - _rel_name = 'x_%s_%s_%s_rel' %(_rel1, _rel2, field['name']) + _rel_name = 'x_%s_%s_%s_rel' % (_rel1, _rel2, field['name']) self._columns[field['name']] = getattr(fields, field['ttype'])(field['relation'], _rel_name, 'id1', 'id2', **attrs) else: self._columns[field['name']] = getattr(fields, field['ttype'])(**attrs) self._inherits_check() self._inherits_reload() if not self._sequence: - self._sequence = self._table+'_id_seq' + self._sequence = self._table + '_id_seq' for k in self._defaults: assert (k in self._columns) or (k in self._inherit_fields), 'Default function defined in %s but field %s does not exist !' % (self._name, k,) for f in self._columns: @@ -2819,7 +2849,7 @@ class orm(orm_template): for table, field_name in self._inherits.items(): if field_name not in self._columns: logging.getLogger('init').info('Missing many2one field definition for _inherits reference "%s" in "%s", using default one.' % (field_name, self._name)) - self._columns[field_name] = fields.many2one(table, string="Automatically created field to link to parent %s" % table, + self._columns[field_name] = fields.many2one(table, string="Automatically created field to link to parent %s" % table, required=True, ondelete="cascade") elif not self._columns[field_name].required or self._columns[field_name].ondelete.lower() != "cascade": logging.getLogger('init').warning('Field definition for _inherits reference "%s" in "%s" must be marked as "required" with ondelete="cascade", forcing it.' % (field_name, self._name)) @@ -2894,7 +2924,7 @@ class orm(orm_template): select = [ids] else: select = ids - select = map(lambda x: isinstance(x,dict) and x['id'] or x, select) + select = map(lambda x: isinstance(x, dict) and x['id'] or x, select) result = self._read_flat(cr, user, select, fields, context, load) for r in result: @@ -3000,7 +3030,7 @@ class orm(orm_template): del r['id'] for record in res: - if not record[col]:# if the record is deleted from _inherits table? + if not record[col]: # if the record is deleted from _inherits table? continue record.update(res3[record[col]]) if col not in fields_to_read: @@ -3022,12 +3052,12 @@ class orm(orm_template): for f in fields_post: todo.setdefault(self._columns[f]._multi, []) todo[self._columns[f]._multi].append(f) - for key,val in todo.items(): + 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: - if isinstance(res2[record['id']], str):res2[record['id']] = eval(res2[record['id']]) #TOCHECK : why got string instend of dict in python2.6 + if isinstance(res2[record['id']], str): res2[record['id']] = eval(res2[record['id']]) #TOCHECK : why got string instend of dict in python2.6 record[pos] = res2[record['id']][pos] else: for f in val: @@ -3197,8 +3227,8 @@ class orm(orm_template): 'where id IN %s', (sub_ids,)) for order, object, store_ids, fields in result_store: if object != self._name: - obj = self.pool.get(object) - cr.execute('select id from '+obj._table+' where id IN %s',(tuple(store_ids),)) + obj = self.pool.get(object) + cr.execute('select id from '+obj._table+' where id IN %s', (tuple(store_ids),)) rids = map(lambda x: x[0], cr.fetchall()) if rids: obj._store_set_values(cr, uid, rids, fields, context) @@ -3368,11 +3398,11 @@ class orm(orm_template): # TODO: optimize for f in direct: if self._columns[f].translate: - src_trans = self.pool.get(self._name).read(cr,user,ids,[f])[0][f] + src_trans = self.pool.get(self._name).read(cr, user, ids, [f])[0][f] if not src_trans: src_trans = vals[f] # Inserting value to DB - self.write(cr, user, ids, {f:vals[f]}) + self.write(cr, user, ids, {f: vals[f]}) self.pool.get('ir.translation')._set_ids(cr, user, self._name+','+f, 'model', context['lang'], ids, vals[f], src_trans) @@ -3411,7 +3441,7 @@ class orm(orm_template): # parent_left/right (via a child_of domain) if parents_changed: if self.pool._init: - self.pool._init_parent[self._name]=True + self.pool._init_parent[self._name] = True else: order = self._parent_order or self._order parent_val = vals[self._parent_name] @@ -3432,7 +3462,7 @@ class orm(orm_template): for (parent_pright, parent_id) in parents: if parent_id == id: break - position = parent_pright+1 + position = parent_pright + 1 # It's the first node of the parent if not position: @@ -3440,7 +3470,7 @@ class orm(orm_template): position = 1 else: cr.execute('select parent_left from '+self._table+' where id=%s', (parent_val,)) - position = cr.fetchone()[0]+1 + position = cr.fetchone()[0] + 1 if pleft < position <= pright: raise except_orm(_('UserError'), _('Recursivity Detected.')) @@ -3448,18 +3478,18 @@ class orm(orm_template): if pleft < position: cr.execute('update '+self._table+' set parent_left=parent_left+%s where parent_left>=%s', (distance, position)) cr.execute('update '+self._table+' set parent_right=parent_right+%s where parent_right>=%s', (distance, position)) - cr.execute('update '+self._table+' set parent_left=parent_left+%s, parent_right=parent_right+%s where parent_left>=%s and parent_left<%s', (position-pleft,position-pleft, pleft, pright)) + cr.execute('update '+self._table+' set parent_left=parent_left+%s, parent_right=parent_right+%s where parent_left>=%s and parent_left<%s', (position-pleft, position-pleft, pleft, pright)) else: cr.execute('update '+self._table+' set parent_left=parent_left+%s where parent_left>=%s', (distance, position)) cr.execute('update '+self._table+' set parent_right=parent_right+%s where parent_right>=%s', (distance, position)) - cr.execute('update '+self._table+' set parent_left=parent_left-%s, parent_right=parent_right-%s where parent_left>=%s and parent_left<%s', (pleft-position+distance,pleft-position+distance, pleft+distance, pright+distance)) + cr.execute('update '+self._table+' set parent_left=parent_left-%s, parent_right=parent_right-%s where parent_left>=%s and parent_left<%s', (pleft-position+distance, pleft-position+distance, pleft+distance, pright+distance)) result += self._store_get_values(cr, user, ids, vals.keys(), context) result.sort() done = {} for order, object, ids, fields in result: - key = (object,tuple(fields)) + key = (object, tuple(fields)) done.setdefault(key, {}) # avoid to do several times the same computation todo = [] @@ -3510,7 +3540,7 @@ class orm(orm_template): if self._inherits[v] not in vals: tocreate[v] = {} else: - tocreate[v] = {'id' : vals[self._inherits[v]]} + tocreate[v] = {'id': vals[self._inherits[v]]} (upd0, upd1, upd2) = ('', '', []) upd_todo = [] for v in vals.keys(): @@ -3542,7 +3572,7 @@ class orm(orm_template): else: self.pool.get(table).write(cr, user, [record_id], tocreate[table], context=context) - upd0 += ','+self._inherits[table] + upd0 += ',' + self._inherits[table] upd1 += ',%s' upd2.append(record_id) @@ -3616,7 +3646,7 @@ class orm(orm_template): if self._parent_store and not context.get('defer_parent_store_computation'): if self.pool._init: - self.pool._init_parent[self._name]=True + self.pool._init_parent[self._name] = True else: parent = vals.get(self._parent_name, False) if parent: @@ -3636,7 +3666,7 @@ class orm(orm_template): 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)) + cr.execute('update '+self._table+' set parent_left=%s,parent_right=%s where id=%s', (pleft+1, pleft+2, id_new)) # default element in context must be remove when call a one2many or many2many rel_context = context.copy() @@ -3662,7 +3692,7 @@ class orm(orm_template): message = self._description + \ " '" + \ self.name_get(cr, user, [id_new], context=context)[0][1] + \ - "' "+ _("created.") + "' " + _("created.") self.log(cr, user, id_new, message, True, context=context) wf_service = netsvc.LocalService("workflow") wf_service.trg_create(user, self._name, id_new, cr) @@ -3686,24 +3716,24 @@ class orm(orm_template): result.setdefault(fncts[fnct][0], {}) # uid == 1 for accessing objects having rules defined on store fields - ids2 = fncts[fnct][2](self,cr, 1, ids, context) + ids2 = fncts[fnct][2](self, cr, 1, ids, context) for id in filter(None, ids2): result[fncts[fnct][0]].setdefault(id, []) result[fncts[fnct][0]][id].append(fnct) dict = {} for object in result: k2 = {} - for id,fnct in result[object].items(): + for id, fnct in result[object].items(): k2.setdefault(tuple(fnct), []) k2[tuple(fnct)].append(id) - for fnct,id in k2.items(): - dict.setdefault(fncts[fnct[0]][4],[]) - dict[fncts[fnct[0]][4]].append((fncts[fnct[0]][4],object,id,map(lambda x: fncts[x][1], fnct))) + for fnct, id in k2.items(): + dict.setdefault(fncts[fnct[0]][4], []) + dict[fncts[fnct[0]][4]].append((fncts[fnct[0]][4], object, id, map(lambda x: fncts[x][1], fnct))) result2 = [] tmp = dict.keys() tmp.sort() for k in tmp: - result2+=dict[k] + result2 += dict[k] return result2 def _store_set_values(self, cr, uid, ids, fields, context): @@ -3712,7 +3742,7 @@ class orm(orm_template): field_flag = False field_dict = {} if self._log_access: - cr.execute('select id,write_date from '+self._table+' where id IN %s',(tuple(ids),)) + cr.execute('select id,write_date from '+self._table+' where id IN %s', (tuple(ids),)) res = cr.fetchall() for r in res: if r[1]: @@ -3739,7 +3769,7 @@ class orm(orm_template): if key: # uid == 1 for accessing objects having rules defined on store fields result = self._columns[val[0]].get(cr, self, ids, val, 1, context=context) - for id,value in result.items(): + for id, value in result.items(): if field_flag: for f in value.keys(): if f in field_dict[id]: @@ -3770,14 +3800,14 @@ class orm(orm_template): if r in field_dict.keys(): if f in field_dict[r]: result.pop(r) - for id,value in result.items(): + 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)) + '"'+f+'"='+self._columns[f]._symbol_set[0] + ' where id = %s', (self._columns[f]._symbol_set[1](value), id)) return True # @@ -3846,7 +3876,7 @@ class orm(orm_template): the appropriate JOIN for linking the current model to the one referenced in model_name. :return: True if additional clauses where applied. """ - added_clause, added_params, added_tables = self.pool.get('ir.rule').domain_get(cr, uid, model_name or self._name, mode, context=context) + added_clause, added_params, added_tables = self.pool.get('ir.rule').domain_get(cr, uid, model_name or self._name, mode, context=context) if added_clause: where_clause += added_clause where_clause_params += added_params @@ -3903,7 +3933,7 @@ class orm(orm_template): limit_str = limit and ' limit %d' % limit or '' offset_str = offset and ' offset %d' % offset or '' - + if where: where_str = " WHERE %s" % " AND ".join(where) else: @@ -4013,7 +4043,7 @@ class orm(orm_template): # we must recursively copy the translations for o2o and o2m if field_def['type'] in ('one2one', 'one2many'): target_obj = self.pool.get(field_def['relation']) - old_record, new_record = self.read(cr, uid, [old_id, new_id], [field_name], context=context) + old_record, new_record = self.read(cr, uid, [old_id, new_id], [field_name], context=context) # here we rely on the order of the ids to match the translations # as foreseen in copy_data() old_childs = sorted(old_record[field_name]) @@ -4031,9 +4061,9 @@ class orm(orm_template): if trans_name: trans_ids = trans_obj.search(cr, uid, [ ('name', '=', trans_name), - ('res_id','=', old_id) + ('res_id', '=', old_id) ]) - translation_records.extend(trans_obj.read(cr,uid,trans_ids,context=context)) + translation_records.extend(trans_obj.read(cr, uid, trans_ids, context=context)) for record in translation_records: del record['id'] @@ -4061,7 +4091,7 @@ class orm(orm_template): return new_id def exists(self, cr, uid, ids, context=None): - if type(ids) in (int,long): + if type(ids) in (int, long): ids = [ids] query = 'SELECT count(1) FROM "%s"' % (self._table) cr.execute(query + "WHERE ID IN %s", (tuple(ids),)) @@ -4110,10 +4140,10 @@ class orm(orm_template): """ result = dict.fromkeys(ids, '') model_data_obj = self.pool.get('ir.model.data') - data_ids = model_data_obj.search(cr,uid, - [('model','=',self._name),('res_id','in',ids)]) - data_results = model_data_obj.read(cr,uid,data_ids, - ['name','module','res_id']) + data_ids = model_data_obj.search(cr, uid, + [('model', '=', self._name), ('res_id', 'in', ids)]) + data_results = model_data_obj.read(cr, uid, data_ids, + ['name', 'module', 'res_id']) for record in data_results: result[record['res_id']] = '%(module)s.%(name)s' % record return result