1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
23 # Object relationnal mapping to postgresql module
24 # . Hierarchical structure
25 # . Constraints consistency, validations
26 # . Object meta Data depends on its status
27 # . Optimised processing by complex query (multiple actions at once)
28 # . Default fields value
29 # . Permissions optimisation
30 # . Persistant object: DB postgresql
32 # . Multi-level caching system
33 # . 2 different inheritancies
35 # - classicals (varchar, integer, boolean, ...)
36 # - relations (one2many, many2one, many2many)
52 from lxml import etree
57 from tools.config import config
58 from tools.translate import _
60 regex_order = re.compile('^([a-z0-9_]+( *desc| *asc)?( *, *|))+$', re.I)
62 def last_day_of_current_month():
63 today = datetime.date.today()
64 last_day = str(calendar.monthrange(today.year, today.month)[1])
65 return time.strftime('%Y-%m-' + last_day)
67 def intersect(la, lb):
68 return filter(lambda x: x in lb, la)
71 class except_orm(Exception):
72 def __init__(self, name, value):
75 self.args = (name, value)
77 class BrowseRecordError(Exception):
80 # Readonly python database object browser
81 class browse_null(object):
86 def __getitem__(self, name):
89 def __getattr__(self, name):
90 return None # XXX: return self ?
98 def __nonzero__(self):
101 def __unicode__(self):
106 # TODO: execute an object method on browse_record_list
108 class browse_record_list(list):
110 def __init__(self, lst, context=None):
113 super(browse_record_list, self).__init__(lst)
114 self.context = context
117 class browse_record(object):
118 logger = netsvc.Logger()
120 def __init__(self, cr, uid, id, table, cache, context=None, list_class = None, fields_process={}):
122 table : the object (inherited from orm)
123 context : a dictionary with an optional context
127 self._list_class = list_class or browse_record_list
132 self._table_name = self._table._name
133 self._context = context
134 self._fields_process = fields_process
136 cache.setdefault(table._name, {})
137 self._data = cache[table._name]
139 if not (id and isinstance(id, (int, long,))):
140 raise BrowseRecordError(_('Wrong ID for the browse record, got %r, expected an integer.') % (id,))
141 # if not table.exists(cr, uid, id, context):
142 # raise BrowseRecordError(_('Object %s does not exists') % (self,))
144 if id not in self._data:
145 self._data[id] = {'id': id}
149 def __getitem__(self, name):
152 if name not in self._data[self._id]:
153 # build the list of fields we will fetch
155 # fetch the definition of the field which was asked for
156 if name in self._table._columns:
157 col = self._table._columns[name]
158 elif name in self._table._inherit_fields:
159 col = self._table._inherit_fields[name][2]
160 elif hasattr(self._table, str(name)):
161 if isinstance(getattr(self._table, name), (types.MethodType, types.LambdaType, types.FunctionType)):
162 return lambda *args, **argv: getattr(self._table, name)(self._cr, self._uid, [self._id], *args, **argv)
164 return getattr(self._table, name)
166 self.logger.notifyChannel("browse_record", netsvc.LOG_WARNING,
167 "Field '%s' does not exist in object '%s': \n%s" % (
168 name, self, ''.join(traceback.format_exc())))
169 raise KeyError("Field '%s' does not exist in object '%s'" % (
172 # if the field is a classic one or a many2one, we'll fetch all classic and many2one fields
174 # gen the list of "local" (ie not inherited) fields which are classic or many2one
175 ffields = filter(lambda x: x[1]._classic_write, self._table._columns.items())
176 # gen the list of inherited fields
177 inherits = map(lambda x: (x[0], x[1][2]), self._table._inherit_fields.items())
178 # complete the field list with the inherited fields which are classic or many2one
179 ffields += filter(lambda x: x[1]._classic_write, inherits)
180 # otherwise we fetch only that field
182 ffields = [(name, col)]
183 ids = filter(lambda id: name not in self._data[id], self._data.keys())
185 fffields = map(lambda x: x[0], ffields)
186 datas = self._table.read(self._cr, self._uid, ids, fffields, context=self._context, load="_classic_write")
187 if self._fields_process:
188 lang = self._context.get('lang', 'en_US') or 'en_US'
189 lang_obj_ids = self.pool.get('res.lang').search(self._cr, self._uid,[('code','=',lang)])
191 raise Exception(_('Language with code "%s" is not defined in your system !\nDefine it through the Administration menu.') % (lang,))
192 lang_obj = self.pool.get('res.lang').browse(self._cr, self._uid,lang_obj_ids[0])
195 if f._type in self._fields_process:
197 d[n] = self._fields_process[f._type](d[n])
199 d[n].set_value(self._cr, self._uid, d[n], self, f, lang_obj)
203 # Where did those ids come from? Perhaps old entries in ir_model_dat?
204 self.logger.notifyChannel("browse_record", netsvc.LOG_WARNING,
205 "No datas found for ids %s in %s \n%s" % (
206 ids, self, ''.join(traceback.format_exc())))
207 raise KeyError('Field %s not found in %s'%(name,self))
208 # create browse records for 'remote' objects
210 if len(str(data['id']).split('-')) > 1:
211 data['id'] = int(str(data['id']).split('-')[0])
213 if f._type in ('many2one', 'one2one'):
215 obj = self._table.pool.get(f._obj)
217 if type(data[n]) in (type([]),type( (1,) )):
222 data[n] = browse_record(self._cr, self._uid, ids2, obj, self._cache, context=self._context, list_class=self._list_class, fields_process=self._fields_process)
224 data[n] = browse_null()
226 data[n] = browse_null()
227 elif f._type in ('one2many', 'many2many') and len(data[n]):
228 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)
229 self._data[data['id']].update(data)
230 if not name in self._data[self._id]:
231 #how did this happen?
232 self.logger.notifyChannel("browse_record", netsvc.LOG_ERROR,
233 "Ffields: %s, datas: %s"%(fffields, datas))
234 self.logger.notifyChannel("browse_record", netsvc.LOG_ERROR,
235 "Data: %s, Table: %s"%(self._data[self._id], self._table))
236 raise KeyError(_('Unknown attribute %s in %s ') % (name, self))
237 return self._data[self._id][name]
239 def __getattr__(self, name):
243 raise AttributeError(e)
245 def __contains__(self, name):
246 return (name in self._table._columns) or (name in self._table._inherit_fields) or hasattr(self._table, name)
248 def __hasattr__(self, name):
255 return "browse_record(%s, %d)" % (self._table_name, self._id)
257 def __eq__(self, other):
258 return (self._table_name, self._id) == (other._table_name, other._id)
260 def __ne__(self, other):
261 return (self._table_name, self._id) != (other._table_name, other._id)
263 # we need to define __unicode__ even though we've already defined __str__
264 # because we have overridden __getattr__
265 def __unicode__(self):
266 return unicode(str(self))
269 return hash((self._table_name, self._id))
277 (type returned by postgres when the column was created, type expression to create the column)
281 fields.boolean: 'bool',
282 fields.integer: 'int4',
283 fields.integer_big: 'int8',
287 fields.datetime: 'timestamp',
288 fields.binary: 'bytea',
289 fields.many2one: 'int4',
291 if type(f) in type_dict:
292 f_type = (type_dict[type(f)], type_dict[type(f)])
293 elif isinstance(f, fields.float):
295 f_type = ('numeric', 'NUMERIC(%d,%d)' % (f.digits[0], f.digits[1]))
297 f_type = ('float8', 'DOUBLE PRECISION')
298 elif isinstance(f, (fields.char, fields.reference)):
299 f_type = ('varchar', 'VARCHAR(%d)' % (f.size,))
300 elif isinstance(f, fields.selection):
301 if isinstance(f.selection, list) and isinstance(f.selection[0][0], (str, unicode)):
302 f_size = reduce(lambda x, y: max(x, len(y[0])), f.selection, f.size or 16)
303 elif isinstance(f.selection, list) and isinstance(f.selection[0][0], int):
306 f_size = getattr(f, 'size', None) or 16
309 f_type = ('int4', 'INTEGER')
311 f_type = ('varchar', 'VARCHAR(%d)' % f_size)
312 elif isinstance(f, fields.function) and eval('fields.'+(f._type)) in type_dict:
313 t = eval('fields.'+(f._type))
314 f_type = (type_dict[t], type_dict[t])
315 elif isinstance(f, fields.function) and f._type == 'float':
317 f_type = ('numeric', 'NUMERIC(%d,%d)' % (f.digits[0], f.digits[1]))
319 f_type = ('float8', 'DOUBLE PRECISION')
320 elif isinstance(f, fields.function) and f._type == 'selection':
321 f_type = ('text', 'text')
322 elif isinstance(f, fields.function) and f._type == 'char':
323 f_type = ('varchar', 'VARCHAR(%d)' % (f.size))
325 logger = netsvc.Logger()
326 logger.notifyChannel("init", netsvc.LOG_WARNING, '%s type not supported!' % (type(f)))
331 class orm_template(object):
337 _parent_name = 'parent_id'
338 _parent_store = False
339 _parent_order = False
348 CONCURRENCY_CHECK_FIELD = '__last_update'
350 def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None):
351 raise _('The read_group method is not implemented on this object !')
353 def _field_create(self, cr, context={}):
354 cr.execute("SELECT id FROM ir_model WHERE model=%s", (self._name,))
356 cr.execute('SELECT nextval(%s)', ('ir_model_id_seq',))
357 model_id = cr.fetchone()[0]
358 cr.execute("INSERT INTO ir_model (id,model, name, info,state) VALUES (%s, %s, %s, %s, %s)", (model_id, self._name, self._description, self.__doc__, 'base'))
360 model_id = cr.fetchone()[0]
361 if 'module' in context:
362 name_id = 'model_'+self._name.replace('.','_')
363 cr.execute('select * from ir_model_data where name=%s and res_id=%s', (name_id,model_id))
365 cr.execute("INSERT INTO ir_model_data (name,date_init,date_update,module,model,res_id) VALUES (%s, now(), now(), %s, %s, %s)", \
366 (name_id, context['module'], 'ir.model', model_id)
371 cr.execute("SELECT * FROM ir_model_fields WHERE model=%s", (self._name,))
373 for rec in cr.dictfetchall():
374 cols[rec['name']] = rec
376 for (k, f) in self._columns.items():
378 'model_id': model_id,
381 'field_description': f.string.replace("'", " "),
383 'relation': f._obj or 'NULL',
384 'view_load': (f.view_load and 1) or 0,
385 'select_level': tools.ustr(f.select or 0),
386 'readonly':(f.readonly and 1) or 0,
387 'required':(f.required and 1) or 0,
388 'selectable' : (f.selectable and 1) or 0,
390 # When its a custom field,it does not contain f.select
391 if context.get('field_state','base') == 'manual':
392 if context.get('field_name','') == k:
393 vals['select_level'] = context.get('select','0')
394 #setting value to let the problem NOT occur next time
396 vals['select_level'] = cols[k]['select_level']
399 cr.execute('select nextval(%s)', ('ir_model_fields_id_seq',))
400 id = cr.fetchone()[0]
402 cr.execute("""INSERT INTO ir_model_fields (
403 id, model_id, model, name, field_description, ttype,
404 relation,view_load,state,select_level
406 %s,%s,%s,%s,%s,%s,%s,%s,%s,%s
408 id, vals['model_id'], vals['model'], vals['name'], vals['field_description'], vals['ttype'],
409 vals['relation'], bool(vals['view_load']), 'base',
412 if 'module' in context:
413 name1 = 'field_' + self._table + '_' + k
414 cr.execute("select name from ir_model_data where name=%s", (name1,))
416 name1 = name1 + "_" + str(id)
417 cr.execute("INSERT INTO ir_model_data (name,date_init,date_update,module,model,res_id) VALUES (%s, now(), now(), %s, %s, %s)", \
418 (name1, context['module'], 'ir.model.fields', id)
421 for key, val in vals.items():
422 if cols[k][key] != vals[key]:
423 cr.execute('update ir_model_fields set field_description=%s where model=%s and name=%s', (vals['field_description'], vals['model'], vals['name']))
425 cr.execute("""UPDATE ir_model_fields SET
426 model_id=%s, field_description=%s, ttype=%s, relation=%s,
427 view_load=%s, select_level=%s, readonly=%s ,required=%s, selectable=%s
429 model=%s AND name=%s""", (
430 vals['model_id'], vals['field_description'], vals['ttype'],
431 vals['relation'], bool(vals['view_load']),
432 vals['select_level'], bool(vals['readonly']),bool(vals['required']),bool(vals['selectable']),vals['model'], vals['name']
437 def _auto_init(self, cr, context={}):
438 self._field_create(cr, context)
440 def __init__(self, cr):
441 if not self._name and not hasattr(self, '_inherit'):
442 name = type(self).__name__.split('.')[0]
443 msg = "The class %s has to have a _name attribute" % name
445 logger = netsvc.Logger()
446 logger.notifyChannel('orm', netsvc.LOG_ERROR, msg )
447 raise except_orm('ValueError', msg )
449 if not self._description:
450 self._description = self._name
452 self._table = self._name.replace('.', '_')
454 def browse(self, cr, uid, select, context=None, list_class=None, fields_process={}):
457 self._list_class = list_class or browse_record_list
459 # need to accepts ints and longs because ids coming from a method
460 # launched by button in the interface have a type long...
461 if isinstance(select, (int, long)):
462 return browse_record(cr, uid, select, self, cache, context=context, list_class=self._list_class, fields_process=fields_process)
463 elif isinstance(select, list):
464 return self._list_class([browse_record(cr, uid, id, self, cache, context=context, list_class=self._list_class, fields_process=fields_process) for id in select], context)
468 def __export_row(self, cr, uid, row, fields, context=None):
470 def check_type(field_type):
471 if field_type == 'float':
473 elif field_type == 'integer':
475 elif field_type == 'boolean':
479 def selection_field(in_field):
480 col_obj = self.pool.get(in_field.keys()[0])
481 if f[i] in col_obj._columns.keys():
482 return col_obj._columns[f[i]]
483 elif f[i] in col_obj._inherits.keys():
484 selection_field(col_obj._inherits)
490 data = map(lambda x: '', range(len(fields)))
492 for fpos in range(len(fields)):
501 model_data = self.pool.get('ir.model.data')
502 data_ids = model_data.search(cr, uid, [('model','=',r._table_name),('res_id','=',r['id'])])
504 d = model_data.read(cr, uid, data_ids, ['name','module'])[0]
506 r = '%s.%s'%(d['module'],d['name'])
513 # To display external name of selection field when its exported
514 if not context.get('import_comp',False):# Allow external name only if its not import compatible
516 if f[i] in self._columns.keys():
517 cols = self._columns[f[i]]
518 elif f[i] in self._inherit_fields.keys():
519 cols = selection_field(self._inherits)
520 if cols and cols._type == 'selection':
521 sel_list = cols.selection
522 if type(sel_list) == type([]):
523 r = [x[1] for x in sel_list if r==x[0]][0]
526 if f[i] in self._columns:
527 r = check_type(self._columns[f[i]]._type)
528 elif f[i] in self._inherit_fields:
529 r = check_type(self._inherit_fields[f[i]][2]._type)
532 if isinstance(r, (browse_record_list, list)):
534 fields2 = map(lambda x: (x[:i+1]==f[:i+1] and x[i+1:]) \
540 lines2 = self.__export_row(cr, uid, row2, fields2,
543 for fpos2 in range(len(fields)):
544 if lines2 and lines2[0][fpos2]:
545 data[fpos2] = lines2[0][fpos2]
549 if isinstance(rr.name, browse_record):
551 rr_name = self.pool.get(rr._table_name).name_get(cr, uid, [rr.id])
552 rr_name = rr_name and rr_name[0] and rr_name[0][1] or ''
553 dt += tools.ustr(rr_name or '') + ','
563 if isinstance(r, browse_record):
564 r = self.pool.get(r._table_name).name_get(cr, uid, [r.id])
565 r = r and r[0] and r[0][1] or ''
566 data[fpos] = tools.ustr(r or '')
567 return [data] + lines
569 def export_data(self, cr, uid, ids, fields_to_export, context=None):
572 imp_comp = context.get('import_comp',False)
573 cols = self._columns.copy()
574 for f in self._inherit_fields:
575 cols.update({f: self._inherit_fields[f][2]})
576 fields_to_export = map(lambda x: x.split('/'), fields_to_export)
577 fields_export = fields_to_export+[]
580 for field in fields_export:
581 if imp_comp and len(field)>1:
582 warning_fields.append('/'.join(map(lambda x:x in cols and cols[x].string or x,field)))
583 elif len (field) <=1:
584 if imp_comp and cols.get(field and field[0],False):
585 if ((isinstance(cols[field[0]], fields.function) and not cols[field[0]].store) \
586 or isinstance(cols[field[0]], fields.related)\
587 or isinstance(cols[field[0]], fields.one2many)):
588 warning_fields.append('/'.join(map(lambda x:x in cols and cols[x].string or x,field)))
590 if imp_comp and len(warning_fields):
591 warning = 'Following columns cannot be exported since you select to be import compatible.\n%s' %('\n'.join(warning_fields))
593 return {'warning' : warning}
594 for row in self.browse(cr, uid, ids, context):
595 datas += self.__export_row(cr, uid, row, fields_to_export, context)
596 return {'datas':datas}
598 def import_data(self, cr, uid, fields, datas, mode='init', current_module='', noupdate=False, context=None, filename=None):
601 fields = map(lambda x: x.split('/'), fields)
602 logger = netsvc.Logger()
603 ir_model_data_obj = self.pool.get('ir.model.data')
605 def _check_db_id(self, model_name, db_id):
606 obj_model = self.pool.get(model_name)
607 ids = obj_model.search(cr, uid, [('id','=',int(db_id))])
609 raise Exception(_("Database ID doesn't exist: %s : %s") %(model_name, db_id))
612 def process_liness(self, datas, prefix, current_module, model_name, fields_def, position=0):
613 line = datas[position]
622 ir_model_data_obj = self.pool.get('ir.model.data')
624 # Import normal fields
626 for i in range(len(fields)):
628 raise Exception(_('Please check that all your lines have %d columns.') % (len(fields),))
633 if prefix and not prefix[0] in field:
636 if (len(field)==len(prefix)+1) and field[len(prefix)].endswith(':db_id'):
640 field_name = field[0].split(':')[0]
641 model_rel = fields_def[field_name]['relation']
643 if fields_def[field[len(prefix)][:-6]]['type']=='many2many':
645 for db_id in line[i].split(config.get('csv_internal_sep')):
647 _check_db_id(self, model_rel, db_id)
650 warning += [tools.exception_to_unicode(e)]
651 logger.notifyChannel("import", netsvc.LOG_ERROR,
652 tools.exception_to_unicode(e))
654 res = [(6, 0, res_id)]
657 _check_db_id(self, model_rel, line[i])
660 warning += [tools.exception_to_unicode(e)]
661 logger.notifyChannel("import", netsvc.LOG_ERROR,
662 tools.exception_to_unicode(e))
663 row[field_name] = res or False
666 if (len(field)==len(prefix)+1) and field[len(prefix)].endswith(':id'):
669 if fields_def[field[len(prefix)][:-3]]['type']=='many2many':
671 for word in line[i].split(config.get('csv_internal_sep')):
673 module, xml_id = word.rsplit('.', 1)
675 module, xml_id = current_module, word
676 id = ir_model_data_obj._get_id(cr, uid, module,
678 res_id2 = ir_model_data_obj.read(cr, uid, [id],
679 ['res_id'])[0]['res_id']
681 res_id.append(res_id2)
683 res_id = [(6, 0, res_id)]
686 module, xml_id = line[i].rsplit('.', 1)
688 module, xml_id = current_module, line[i]
689 id = ir_model_data_obj._get_id(cr, uid, module, xml_id)
690 res_res_id = ir_model_data_obj.read(cr, uid, [id],
693 res_id = res_res_id[0]['res_id']
694 row[field[-1][:-3]] = res_id or False
696 if (len(field) == len(prefix)+1) and \
697 len(field[len(prefix)].split(':lang=')) == 2:
698 f, lang = field[len(prefix)].split(':lang=')
699 translate.setdefault(lang, {})[f]=line[i] or False
701 if (len(field) == len(prefix)+1) and \
702 (prefix == field[0:len(prefix)]):
703 if field[len(prefix)] == "id":
706 is_xml_id = data_id = line[i]
707 d = data_id.split('.')
708 module = len(d)>1 and d[0] or ''
709 name = len(d)>1 and d[1] or d[0]
710 data_ids = ir_model_data_obj.search(cr, uid, [('module','=',module),('model','=',model_name),('name','=',name)])
712 d = ir_model_data_obj.read(cr, uid, data_ids, ['res_id'])[0]
714 if is_db_id and not db_id:
715 data_ids = ir_model_data_obj.search(cr, uid, [('module','=',module),('model','=',model_name),('res_id','=',is_db_id)])
716 if not len(data_ids):
717 ir_model_data_obj.create(cr, uid, {'module':module, 'model':model_name, 'name':name, 'res_id':is_db_id})
719 if is_db_id and int(db_id) != int(is_db_id):
720 warning += [_("Id is not the same than existing one: %s")%(is_db_id)]
721 logger.notifyChannel("import", netsvc.LOG_ERROR,
722 _("Id is not the same than existing one: %s")%(is_db_id))
725 if field[len(prefix)] == "db_id":
728 _check_db_id(self, model_name, line[i])
729 data_res_id = is_db_id = int(line[i])
731 warning += [tools.exception_to_unicode(e)]
732 logger.notifyChannel("import", netsvc.LOG_ERROR,
733 tools.exception_to_unicode(e))
735 data_ids = ir_model_data_obj.search(cr, uid, [('model','=',model_name),('res_id','=',line[i])])
737 d = ir_model_data_obj.read(cr, uid, data_ids, ['name','module'])[0]
740 data_id = '%s.%s'%(d['module'],d['name'])
743 if is_xml_id and not data_id:
745 if is_xml_id and is_xml_id!=data_id:
746 warning += [_("Id is not the same than existing one: %s")%(line[i])]
747 logger.notifyChannel("import", netsvc.LOG_ERROR,
748 _("Id is not the same than existing one: %s")%(line[i]))
751 if fields_def[field[len(prefix)]]['type'] == 'integer':
752 res = line[i] and int(line[i])
753 elif fields_def[field[len(prefix)]]['type'] == 'boolean':
754 res = line[i].lower() not in ('0', 'false', 'off')
755 elif fields_def[field[len(prefix)]]['type'] == 'float':
756 res = line[i] and float(line[i])
757 elif fields_def[field[len(prefix)]]['type'] == 'selection':
759 if isinstance(fields_def[field[len(prefix)]]['selection'],
761 sel = fields_def[field[len(prefix)]]['selection']
763 sel = fields_def[field[len(prefix)]]['selection'](self,
766 if line[i] in [tools.ustr(key),tools.ustr(val)]: #Acepting key or value for selection field
769 if line[i] and not res:
770 logger.notifyChannel("import", netsvc.LOG_WARNING,
771 _("key '%s' not found in selection field '%s'") % \
772 (line[i], field[len(prefix)]))
774 warning += [_("Key/value '%s' not found in selection field '%s'")%(line[i],field[len(prefix)])]
776 elif fields_def[field[len(prefix)]]['type']=='many2one':
779 relation = fields_def[field[len(prefix)]]['relation']
780 res2 = self.pool.get(relation).name_search(cr, uid,
781 line[i], [], operator='=', context=context)
782 res = (res2 and res2[0][0]) or False
784 warning += [_("Relation not found: %s on '%s'")%(line[i],relation)]
785 logger.notifyChannel("import", netsvc.LOG_WARNING,
786 _("Relation not found: %s on '%s'")%(line[i],relation))
787 elif fields_def[field[len(prefix)]]['type']=='many2many':
790 relation = fields_def[field[len(prefix)]]['relation']
791 for word in line[i].split(config.get('csv_internal_sep')):
792 res2 = self.pool.get(relation).name_search(cr,
793 uid, word, [], operator='=', context=context)
794 res3 = (res2 and res2[0][0]) or False
796 warning += [_("Relation not found: %s on '%s'")%(line[i],relation)]
797 logger.notifyChannel("import",
799 _("Relation not found: %s on '%s'")%(line[i],relation))
805 res = line[i] or False
806 row[field[len(prefix)]] = res
807 elif (prefix==field[0:len(prefix)]):
808 if field[0] not in todo:
809 todo.append(field[len(prefix)])
811 # Import one2many, many2many fields
815 relation_obj = self.pool.get(fields_def[field]['relation'])
816 newfd = relation_obj.fields_get(
817 cr, uid, context=context)
818 res = process_liness(self, datas, prefix + [field], current_module, relation_obj._name, newfd, position)
819 (newrow, max2, w2, translate2, data_id2, data_res_id2) = res
820 nbrmax = max(nbrmax, max2)
821 warning = warning + w2
822 reduce(lambda x, y: x and y, newrow)
823 row[field] = (reduce(lambda x, y: x or y, newrow.values()) and \
824 [(0, 0, newrow)]) or []
826 while (position+i)<len(datas):
828 for j in range(len(fields)):
830 if (len(field2) <= (len(prefix)+1)) and datas[position+i][j]:
835 (newrow, max2, w2, translate2, data_id2, data_res_id2) = process_liness(
836 self, datas, prefix+[field], current_module, relation_obj._name, newfd, position+i)
838 if reduce(lambda x, y: x or y, newrow.values()):
839 row[field].append((0, 0, newrow))
841 nbrmax = max(nbrmax, i)
844 for i in range(max(nbrmax, 1)):
847 result = (row, nbrmax, warning, translate, data_id, data_res_id)
850 fields_def = self.fields_get(cr, uid, context=context)
853 initial_size = len(datas)
854 if config.get('import_partial', False) and filename:
855 data = pickle.load(file(config.get('import_partial')))
856 original_value = data.get(filename, 0)
862 (res, other, warning, translate, data_id, res_id) = \
863 process_liness(self, datas, [], current_module, self._name, fields_def)
866 return (-1, res, 'Line ' + str(counter) +' : ' + '!\n'.join(warning), '')
869 id = ir_model_data_obj._update(cr, uid, self._name,
870 current_module, res, xml_id=data_id, mode=mode,
871 noupdate=noupdate, res_id=res_id, context=context)
876 if isinstance(e,psycopg2.IntegrityError):
877 msg= _('Insertion Failed! ')
878 for key in self.pool._sql_error.keys():
880 msg = self.pool._sql_error[key]
882 return (-1, res, 'Line ' + str(counter) +' : ' + msg, '' )
883 if isinstance(e, osv.orm.except_orm ):
884 msg = _('Insertion Failed! ' + e[1])
885 return (-1, res, 'Line ' + str(counter) +' : ' + msg, '' )
886 for lang in translate:
887 context2 = context.copy()
888 context2['lang'] = lang
889 self.write(cr, uid, [id], translate[lang], context2)
890 if config.get('import_partial', False) and filename and (not (counter%100)) :
891 data = pickle.load(file(config.get('import_partial')))
892 data[filename] = initial_size - len(datas) + original_value
893 pickle.dump(data, file(config.get('import_partial'),'wb'))
896 #except Exception, e:
897 # logger.notifyChannel("import", netsvc.LOG_ERROR, e)
900 # return (-1, res, e[0], warning)
902 # return (-1, res, e[0], '')
905 # TODO: Send a request with the result and multi-thread !
907 return (done, 0, 0, 0)
909 def read(self, cr, user, ids, fields=None, context=None, load='_classic_read'):
910 raise _('The read method is not implemented on this object !')
912 def get_invalid_fields(self,cr,uid):
913 return list(self._invalids)
915 def _validate(self, cr, uid, ids, context=None):
916 context = context or {}
917 lng = context.get('lang', False) or 'en_US'
918 trans = self.pool.get('ir.translation')
920 for constraint in self._constraints:
921 fun, msg, fields = constraint
922 if not fun(self, cr, uid, ids):
923 translated_msg = trans._get_source(cr, uid, self._name, 'constraint', lng, source=msg) or msg
925 _("Error occurred while validating the field(s) %s: %s") % (','.join(fields), translated_msg)
927 self._invalids.update(fields)
930 raise except_orm('ValidateError', '\n'.join(error_msgs))
932 self._invalids.clear()
934 def default_get(self, cr, uid, fields_list, context=None):
935 """ Set default values for the object's fields.
937 Returns a dict of {field_name:default_value}
940 `fields_list`: the fields for which the object doesn't have
941 any value yet, and default values need to be
942 provided. If fields outside this list are
943 returned, the user-provided values will be
948 def perm_read(self, cr, user, ids, context=None, details=True):
949 raise _('The perm_read method is not implemented on this object !')
951 def unlink(self, cr, uid, ids, context=None):
952 raise _('The unlink method is not implemented on this object !')
954 def write(self, cr, user, ids, vals, context=None):
955 raise _('The write method is not implemented on this object !')
957 def create(self, cr, user, vals, context=None):
958 raise _('The create method is not implemented on this object !')
960 # returns the definition of each field in the object
961 # the optional fields parameter can limit the result to some fields
962 def fields_get_keys(self, cr, user, context=None, read_access=True):
965 res = self._columns.keys()
966 for parent in self._inherits:
967 res.extend(self.pool.get(parent).fields_get_keys(cr, user, fields, context))
970 def fields_get(self, cr, user, fields=None, context=None, read_access=True):
974 translation_obj = self.pool.get('ir.translation')
975 model_access_obj = self.pool.get('ir.model.access')
976 for parent in self._inherits:
977 res.update(self.pool.get(parent).fields_get(cr, user, fields, context))
979 if self._columns.keys():
980 for f in self._columns.keys():
981 if fields and f not in fields:
983 res[f] = {'type': self._columns[f]._type}
984 for arg in ('string', 'readonly', 'states', 'size', 'required',
985 'change_default', 'translate', 'help', 'select', 'selectable'):
986 if getattr(self._columns[f], arg):
987 res[f][arg] = getattr(self._columns[f], arg)
989 res[f]['readonly'] = True
990 res[f]['states'] = {}
991 for arg in ('digits', 'invisible','filters'):
992 if getattr(self._columns[f], arg, None):
993 res[f][arg] = getattr(self._columns[f], arg)
996 res_trans = translation_obj._get_source(cr, user, self._name + ',' + f, 'field', context.get('lang', False) or 'en_US')
998 res[f]['string'] = res_trans
999 help_trans = translation_obj._get_source(cr, user, self._name + ',' + f, 'help', context.get('lang', False) or 'en_US')
1001 res[f]['help'] = help_trans
1003 if hasattr(self._columns[f], 'selection'):
1004 if isinstance(self._columns[f].selection, (tuple, list)):
1005 sel = self._columns[f].selection
1006 # translate each selection option
1008 for (key, val) in sel:
1011 val2 = translation_obj._get_source(cr, user, self._name + ',' + f, 'selection', context.get('lang', False) or 'en_US', val)
1012 sel2.append((key, val2 or val))
1014 res[f]['selection'] = sel
1016 # call the 'dynamic selection' function
1017 res[f]['selection'] = self._columns[f].selection(self, cr,
1019 if res[f]['type'] in ('one2many', 'many2many', 'many2one', 'one2one'):
1020 res[f]['relation'] = self._columns[f]._obj
1021 res[f]['domain'] = self._columns[f]._domain
1022 res[f]['context'] = self._columns[f]._context
1024 #TODO : read the fields from the database
1028 # filter out fields which aren't in the fields list
1029 for r in res.keys():
1035 # Overload this method if you need a window title which depends on the context
1037 def view_header_get(self, cr, user, view_id=None, view_type='form', context=None):
1040 def __view_look_dom(self, cr, user, node, view_id, context=None):
1047 if node.tag == 'field':
1048 if node.get('name'):
1051 if node.get('name') in self._columns:
1052 column = self._columns[node.get('name')]
1054 column = self._inherit_fields[node.get('name')][2]
1059 relation = column._obj
1063 if f.tag in ('form', 'tree', 'graph'):
1065 ctx = context.copy()
1066 ctx['base_model_name'] = self._name
1067 xarch, xfields = self.pool.get(relation).__view_look_dom_arch(cr, user, f, view_id, ctx)
1068 views[str(f.tag)] = {
1072 attrs = {'views': views}
1073 if node.get('widget') and node.get('widget') == 'selection':
1074 # We can not use the 'string' domain has it is defined according to the record !
1076 if column._domain and not isinstance(column._domain, (str, unicode)):
1077 dom = column._domain
1078 dom += eval(node.get('domain','[]'), {'uid':user, 'time':time})
1079 attrs['selection'] = self.pool.get(relation).name_search(cr, user, '', dom, context=context)
1080 if (node.get('required') and not int(node.get('required'))) or not column.required:
1081 attrs['selection'].append((False,''))
1082 fields[node.get('name')] = attrs
1084 elif node.tag in ('form', 'tree'):
1085 result = self.view_header_get(cr, user, False, node.tag, context)
1087 node.set('string', result)
1089 elif node.tag == 'calendar':
1090 for additional_field in ('date_start', 'date_delay', 'date_stop', 'color'):
1091 if node.get(additional_field):
1092 fields[node.get(additional_field)] = {}
1094 if 'groups' in node.attrib:
1095 if node.get('groups'):
1096 groups = node.get('groups').split(',')
1098 access_pool = self.pool.get('ir.model.access')
1099 for group in groups:
1100 readonly = readonly or access_pool.check_groups(cr, user, group)
1102 node.set('invisible', '1')
1103 del(node.attrib['groups'])
1106 if ('lang' in context) and not result:
1107 if node.get('string'):
1108 trans = self.pool.get('ir.translation')._get_source(cr, user, self._name, 'view', context['lang'], node.get('string').encode('utf8'))
1109 if not trans and ('base_model_name' in context):
1110 trans = self.pool.get('ir.translation')._get_source(cr, user, context['base_model_name'], 'view', context['lang'], node.get('string').encode('utf8'))
1112 node.set('string', trans)
1114 trans = self.pool.get('ir.translation')._get_source(cr, user, self._name, 'view', context['lang'], node.get('sum').encode('utf8'))
1116 node.set('sum', trans)
1120 fields.update(self.__view_look_dom(cr, user, f, view_id, context))
1124 def __view_look_dom_arch(self, cr, user, node, view_id, context=None):
1125 fields_def = self.__view_look_dom(cr, user, node, view_id, context=context)
1127 rolesobj = self.pool.get('res.roles')
1128 usersobj = self.pool.get('res.users')
1130 buttons = (n for n in node.getiterator('button') if n.get('type') != 'object')
1131 for button in buttons:
1133 if user != 1: # admin user has all roles
1134 user_roles = usersobj.read(cr, user, [user], ['roles_id'])[0]['roles_id']
1135 # TODO handle the case of more than one workflow for a model
1136 cr.execute("""SELECT DISTINCT t.role_id
1138 INNER JOIN wkf_activity a ON a.wkf_id = wkf.id
1139 INNER JOIN wkf_transition t ON (t.act_to = a.id)
1142 """, (self._name, button.get('name'),))
1143 roles = cr.fetchall()
1145 # draft -> valid = signal_next (role X)
1146 # draft -> cancel = signal_cancel (no role)
1148 # valid -> running = signal_next (role Y)
1149 # valid -> cancel = signal_cancel (role Z)
1151 # running -> done = signal_next (role Z)
1152 # running -> cancel = signal_cancel (role Z)
1154 # As we don't know the object state, in this scenario,
1155 # the button "signal_cancel" will be always shown as there is no restriction to cancel in draft
1156 # the button "signal_next" will be show if the user has any of the roles (X Y or Z)
1157 # The verification will be made later in workflow process...
1159 can_click = any((not role) or rolesobj.check(cr, user, user_roles, role) for (role,) in roles)
1161 button.set('readonly', str(int(not can_click)))
1163 arch = etree.tostring(node, encoding="utf-8").replace('\t', '')
1164 fields = self.fields_get(cr, user, fields_def.keys(), context)
1165 for field in fields_def:
1167 # sometime, the view may containt the (invisible) field 'id' needed for a domain (when 2 objects have cross references)
1168 fields['id'] = {'readonly': True, 'type': 'integer', 'string': 'ID'}
1169 elif field in fields:
1170 fields[field].update(fields_def[field])
1172 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))
1173 res = cr.fetchall()[:]
1175 res.insert(0, ("Can't find field '%s' in the following view parts composing the view of object model '%s':" % (field, model), None))
1176 msg = "\n * ".join([r[0] for r in res])
1177 msg += "\n\nEither you wrongly customised this view, or some modules bringing those views are not compatible with your current data model"
1178 netsvc.Logger().notifyChannel('orm', netsvc.LOG_ERROR, msg)
1179 raise except_orm('View error', msg)
1183 def __get_default_calendar_view(self):
1184 """Generate a default calendar view (For internal use only).
1187 arch = ('<?xml version="1.0" encoding="utf-8"?>\n'
1188 '<calendar string="%s"') % (self._description)
1190 if (self._date_name not in self._columns):
1192 for dt in ['date','date_start','x_date','x_date_start']:
1193 if dt in self._columns:
1194 self._date_name = dt
1199 raise except_orm(_('Invalid Object Architecture!'),_("Insufficient fields for Calendar View!"))
1202 arch +=' date_start="%s"' % (self._date_name)
1204 for color in ["user_id","partner_id","x_user_id","x_partner_id"]:
1205 if color in self._columns:
1206 arch += ' color="' + color + '"'
1209 dt_stop_flag = False
1211 for dt_stop in ["date_stop","date_end","x_date_stop","x_date_end"]:
1212 if dt_stop in self._columns:
1213 arch += ' date_stop="' + dt_stop + '"'
1217 if not dt_stop_flag:
1218 for dt_delay in ["date_delay","planned_hours","x_date_delay","x_planned_hours"]:
1219 if dt_delay in self._columns:
1220 arch += ' date_delay="' + dt_delay + '"'
1224 ' <field name="%s"/>\n'
1225 '</calendar>') % (self._rec_name)
1229 def __get_default_search_view(self, cr, uid, context={}):
1232 if isinstance(s, unicode):
1233 return s.encode('utf8')
1236 view = self.fields_view_get(cr, uid, False, 'form', context)
1238 root = etree.fromstring(encode(view['arch']))
1239 res = etree.XML("<search string='%s'></search>" % root.get("string", ""))
1240 node = etree.Element("group")
1243 fields = root.xpath("//field[@select=1]")
1244 for field in fields:
1247 return etree.tostring(res, encoding="utf-8").replace('\t', '')
1250 # if view_id, view_type is not required
1252 def fields_view_get(self, cr, user, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
1257 if isinstance(s, unicode):
1258 return s.encode('utf8')
1261 def _inherit_apply(src, inherit):
1262 def _find(node, node2):
1263 if node2.tag == 'xpath':
1264 res = node.xpath(node2.get('expr'))
1270 for n in node.getiterator(node2.tag):
1272 for attr in node2.attrib:
1273 if attr == 'position':
1276 if n.get(attr) == node2.get(attr):
1283 # End: _find(node, node2)
1285 doc_dest = etree.fromstring(encode(inherit))
1286 toparse = [ doc_dest ]
1289 node2 = toparse.pop(0)
1290 if node2.tag == 'data':
1291 toparse += [ c for c in doc_dest ]
1293 node = _find(src, node2)
1294 if node is not None:
1296 if node2.get('position'):
1297 pos = node2.get('position')
1298 if pos == 'replace':
1299 parent = node.getparent()
1301 src = copy.deepcopy(node2[0])
1304 node.addprevious(child)
1305 node.getparent().remove(node)
1306 elif pos == 'attributes':
1307 for child in node2.getiterator('attribute'):
1308 attribute = (child.get('name'), child.text and child.text.encode('utf8') or None)
1310 node.set(attribute[0], attribute[1])
1312 del(node.attrib[attribute[0]])
1314 sib = node.getnext()
1318 elif pos == 'after':
1322 sib.addprevious(child)
1323 elif pos == 'before':
1324 node.addprevious(child)
1326 raise AttributeError(_('Unknown position in inherited view %s !') % pos)
1329 ' %s="%s"' % (attr, node2.get(attr))
1330 for attr in node2.attrib
1331 if attr != 'position'
1333 tag = "<%s%s>" % (node2.tag, attrs)
1334 raise AttributeError(_("Couldn't find tag '%s' in parent view !") % tag)
1336 # End: _inherit_apply(src, inherit)
1338 result = {'type': view_type, 'model': self._name}
1344 view_ref = context.get(view_type + '_view_ref', False)
1347 module, view_ref = view_ref.split('.', 1)
1348 cr.execute("SELECT res_id FROM ir_model_data WHERE model='ir.ui.view' AND module=%s AND name=%s", (module, view_ref))
1349 view_ref_res = cr.fetchone()
1351 view_id = view_ref_res[0]
1354 where = (model and (" and model='%s'" % (self._name,))) or ''
1355 cr.execute('SELECT arch,name,field_parent,id,type,inherit_id FROM ir_ui_view WHERE id=%s'+where, (view_id,))
1357 cr.execute('''SELECT
1358 arch,name,field_parent,id,type,inherit_id
1365 ORDER BY priority''', (self._name, view_type))
1366 sql_res = cr.fetchone()
1372 view_id = ok or sql_res[3]
1375 # if a view was found
1377 result['type'] = sql_res[4]
1378 result['view_id'] = sql_res[3]
1379 result['arch'] = sql_res[0]
1381 def _inherit_apply_rec(result, inherit_id):
1382 # get all views which inherit from (ie modify) this view
1383 cr.execute('select arch,id from ir_ui_view where inherit_id=%s and model=%s order by priority', (inherit_id, self._name))
1384 sql_inherit = cr.fetchall()
1385 for (inherit, id) in sql_inherit:
1386 result = _inherit_apply(result, inherit)
1387 result = _inherit_apply_rec(result, id)
1390 inherit_result = etree.fromstring(encode(result['arch']))
1391 result['arch'] = _inherit_apply_rec(inherit_result, sql_res[3])
1393 result['name'] = sql_res[1]
1394 result['field_parent'] = sql_res[2] or False
1397 # otherwise, build some kind of default view
1398 if view_type == 'form':
1399 res = self.fields_get(cr, user, context=context)
1400 xml = '<?xml version="1.0" encoding="utf-8"?> ' \
1401 '<form string="%s">' % (self._description,)
1403 if res[x]['type'] not in ('one2many', 'many2many'):
1404 xml += '<field name="%s"/>' % (x,)
1405 if res[x]['type'] == 'text':
1409 elif view_type == 'tree':
1410 _rec_name = self._rec_name
1411 if _rec_name not in self._columns:
1412 _rec_name = self._columns.keys()[0]
1413 xml = '<?xml version="1.0" encoding="utf-8"?>' \
1414 '<tree string="%s"><field name="%s"/></tree>' \
1415 % (self._description, self._rec_name)
1417 elif view_type == 'calendar':
1418 xml = self.__get_default_calendar_view()
1420 elif view_type == 'search':
1421 xml = self.__get_default_search_view(cr, user, context)
1424 xml = '<?xml version="1.0"?>' # what happens here, graph case?
1425 raise except_orm(_('Invalid Architecture!'),_("There is no view of type '%s' defined for the structure!") % view_type)
1426 result['arch'] = etree.fromstring(encode(xml))
1427 result['name'] = 'default'
1428 result['field_parent'] = False
1429 result['view_id'] = 0
1431 xarch, xfields = self.__view_look_dom_arch(cr, user, result['arch'], view_id, context=context)
1432 result['arch'] = xarch
1433 result['fields'] = xfields
1436 if context and context.get('active_id',False):
1437 data_menu = self.pool.get('ir.ui.menu').browse(cr, user, context['active_id'], context).action
1439 act_id = int(data_menu.split(',')[1])
1441 data_action = self.pool.get('ir.actions.act_window').browse(cr, user, [act_id], context)[0]
1442 result['submenu'] = getattr(data_action,'menus', False)
1446 for key in ('report_sxw_content', 'report_rml_content',
1447 'report_sxw', 'report_rml',
1448 'report_sxw_content_data', 'report_rml_content_data'):
1452 ir_values_obj = self.pool.get('ir.values')
1453 resprint = ir_values_obj.get(cr, user, 'action',
1454 'client_print_multi', [(self._name, False)], False,
1456 resaction = ir_values_obj.get(cr, user, 'action',
1457 'client_action_multi', [(self._name, False)], False,
1460 resrelate = ir_values_obj.get(cr, user, 'action',
1461 'client_action_relate', [(self._name, False)], False,
1463 resprint = map(clean, resprint)
1464 print "resprintresprint",resprint
1465 resaction = map(clean, resaction)
1466 resaction = filter(lambda x: not x.get('multi', False), resaction)
1467 resprint = filter(lambda x: not x.get('multi', False), resprint)
1468 resrelate = map(lambda x: x[2], resrelate)
1470 for x in resprint+resaction+resrelate:
1471 x['string'] = x['name']
1473 result['toolbar'] = {
1475 'action': resaction,
1480 _view_look_dom_arch = __view_look_dom_arch
1482 def search_count(self, cr, user, args, context=None):
1485 res = self.search(cr, user, args, context=context, count=True)
1486 if isinstance(res, list):
1490 def search(self, cr, user, args, offset=0, limit=None, order=None,
1491 context=None, count=False):
1492 raise _('The search method is not implemented on this object !')
1494 def name_get(self, cr, user, ids, context=None):
1495 raise _('The name_get method is not implemented on this object !')
1497 def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100):
1498 raise _('The name_search method is not implemented on this object !')
1500 def copy(self, cr, uid, id, default=None, context=None):
1501 raise _('The copy method is not implemented on this object !')
1503 def exists(self, cr, uid, id, context=None):
1504 raise _('The exists method is not implemented on this object !')
1506 def read_string(self, cr, uid, id, langs, fields=None, context=None):
1509 self.pool.get('ir.model.access').check(cr, uid, 'ir.translation', 'read', context=context)
1511 fields = self._columns.keys() + self._inherit_fields.keys()
1512 #FIXME: collect all calls to _get_source into one SQL call.
1514 res[lang] = {'code': lang}
1516 if f in self._columns:
1517 res_trans = self.pool.get('ir.translation')._get_source(cr, uid, self._name+','+f, 'field', lang)
1519 res[lang][f] = res_trans
1521 res[lang][f] = self._columns[f].string
1522 for table in self._inherits:
1523 cols = intersect(self._inherit_fields.keys(), fields)
1524 res2 = self.pool.get(table).read_string(cr, uid, id, langs, cols, context)
1527 res[lang]['code'] = lang
1528 for f in res2[lang]:
1529 res[lang][f] = res2[lang][f]
1532 def write_string(self, cr, uid, id, langs, vals, context=None):
1533 self.pool.get('ir.model.access').check(cr, uid, 'ir.translation', 'write', context=context)
1534 #FIXME: try to only call the translation in one SQL
1537 if field in self._columns:
1538 self.pool.get('ir.translation')._set_ids(cr, uid, self._name+','+field, 'field', lang, [0], vals[field])
1539 for table in self._inherits:
1540 cols = intersect(self._inherit_fields.keys(), vals)
1542 self.pool.get(table).write_string(cr, uid, id, langs, vals, context)
1545 def _check_removed_columns(self, cr, log=False):
1546 raise NotImplementedError()
1548 class orm_memory(orm_template):
1549 _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']
1550 _inherit_fields = {}
1555 def __init__(self, cr):
1556 super(orm_memory, self).__init__(cr)
1560 cr.execute('delete from wkf_instance where res_type=%s', (self._name,))
1562 def vaccum(self, cr, uid):
1564 if self.check_id % self._check_time:
1567 max = time.time() - self._max_hours * 60 * 60
1568 for id in self.datas:
1569 if self.datas[id]['internal.date_access'] < max:
1571 self.unlink(cr, uid, tounlink)
1572 if len(self.datas)>self._max_count:
1573 sorted = map(lambda x: (x[1]['internal.date_access'], x[0]), self.datas.items())
1575 ids = map(lambda x: x[1], sorted[:len(self.datas)-self._max_count])
1576 self.unlink(cr, uid, ids)
1579 def read(self, cr, user, ids, fields_to_read=None, context=None, load='_classic_read'):
1582 if not fields_to_read:
1583 fields_to_read = self._columns.keys()
1587 if isinstance(ids, (int, long)):
1591 for f in fields_to_read:
1592 if id in self.datas:
1593 r[f] = self.datas[id].get(f, False)
1594 if r[f] and isinstance(self._columns[f], fields.binary) and context.get('bin_size', False):
1597 if id in self.datas:
1598 self.datas[id]['internal.date_access'] = time.time()
1599 fields_post = filter(lambda x: x in self._columns and not getattr(self._columns[x], load), fields_to_read)
1600 for f in fields_post:
1601 res2 = self._columns[f].get_memory(cr, self, ids, f, user, context=context, values=result)
1602 for record in result:
1603 record[f] = res2[record['id']]
1604 if isinstance(ids_orig, (int, long)):
1608 def write(self, cr, user, ids, vals, context=None):
1614 if self._columns[field]._classic_write:
1615 vals2[field] = vals[field]
1617 upd_todo.append(field)
1619 self.datas[id_new].update(vals2)
1620 self.datas[id_new]['internal.date_access'] = time.time()
1621 for field in upd_todo:
1622 self._columns[field].set_memory(cr, self, id_new, field, vals[field], user, context)
1623 self._validate(cr, user, [id_new], context)
1624 wf_service = netsvc.LocalService("workflow")
1625 wf_service.trg_write(user, self._name, id_new, cr)
1628 def create(self, cr, user, vals, context=None):
1629 self.vaccum(cr, user)
1631 id_new = self.next_id
1633 for f in self._columns.keys():
1637 vals.update(self.default_get(cr, user, default, context))
1641 if self._columns[field]._classic_write:
1642 vals2[field] = vals[field]
1644 upd_todo.append(field)
1645 self.datas[id_new] = vals2
1646 self.datas[id_new]['internal.date_access'] = time.time()
1648 for field in upd_todo:
1649 self._columns[field].set_memory(cr, self, id_new, field, vals[field], user, context)
1650 self._validate(cr, user, [id_new], context)
1651 wf_service = netsvc.LocalService("workflow")
1652 wf_service.trg_create(user, self._name, id_new, cr)
1655 def default_get(self, cr, uid, fields_list, context=None):
1659 # get the default values for the inherited fields
1660 for f in fields_list:
1661 if f in self._defaults:
1662 if callable(self._defaults[f]):
1663 value[f] = self._defaults[f](self, cr, uid, context)
1665 value[f] = self._defaults[f]
1667 fld_def = ((f in self._columns) and self._columns[f]) \
1668 or ((f in self._inherit_fields) and self._inherit_fields[f][2]) \
1671 # get the default values set by the user and override the default
1672 # values defined in the object
1673 ir_values_obj = self.pool.get('ir.values')
1674 res = ir_values_obj.get(cr, uid, 'default', False, [self._name])
1675 for id, field, field_value in res:
1676 if field in fields_list:
1677 fld_def = (field in self._columns) and self._columns[field] or self._inherit_fields[field][2]
1678 if fld_def._type in ('many2one', 'one2one'):
1679 obj = self.pool.get(fld_def._obj)
1680 if not obj.search(cr, uid, [('id', '=', field_value)]):
1682 if fld_def._type in ('many2many'):
1683 obj = self.pool.get(fld_def._obj)
1685 for i in range(len(field_value)):
1686 if not obj.search(cr, uid, [('id', '=',
1689 field_value2.append(field_value[i])
1690 field_value = field_value2
1691 if fld_def._type in ('one2many'):
1692 obj = self.pool.get(fld_def._obj)
1694 for i in range(len(field_value)):
1695 field_value2.append({})
1696 for field2 in field_value[i]:
1697 if obj._columns[field2]._type in ('many2one', 'one2one'):
1698 obj2 = self.pool.get(obj._columns[field2]._obj)
1699 if not obj2.search(cr, uid,
1700 [('id', '=', field_value[i][field2])]):
1702 # TODO add test for many2many and one2many
1703 field_value2[i][field2] = field_value[i][field2]
1704 field_value = field_value2
1705 value[field] = field_value
1707 # get the default values from the context
1708 for key in context or {}:
1709 if key.startswith('default_') and (key[8:] in fields_list):
1710 value[key[8:]] = context[key]
1713 def _where_calc(self, cr, user, args, active_test=True, context=None):
1718 # if the object has a field named 'active', filter out all inactive
1719 # records unless they were explicitely asked for
1720 if 'active' in self._columns and (active_test and context.get('active_test', True)):
1722 active_in_args = False
1724 if a[0] == 'active':
1725 active_in_args = True
1726 if not active_in_args:
1727 args.insert(0, ('active', '=', 1))
1729 args = [('active', '=', 1)]
1732 e = expression.expression(args)
1733 e.parse(cr, user, self, context)
1734 res=e.__dict__['_expression__exp']
1738 def search(self, cr, user, args, offset=0, limit=None, order=None,
1739 context=None, count=False):
1742 result = self._where_calc(cr, user, args, context=context)
1744 return self.datas.keys()
1748 #Find the value of dict
1751 for id, data in self.datas.items():
1754 if limit and (counter >int(limit)):
1759 val =eval('data[arg[0]]'+'==' +' arg[2]')
1760 elif arg[1] in ['<','>','in','not in','<=','>=','<>']:
1761 val =eval('data[arg[0]]'+arg[1] +' arg[2]')
1762 elif arg[1] in ['ilike']:
1763 if str(data[arg[0]]).find(str(arg[2]))!=-1:
1778 def unlink(self, cr, uid, ids, context=None):
1780 if id in self.datas:
1783 cr.execute('delete from wkf_instance where res_type=%s and res_id = ANY (%s)', (self._name,ids))
1786 def perm_read(self, cr, user, ids, context=None, details=True):
1790 'create_uid': (user, 'Root'),
1791 'create_date': time.strftime('%Y-%m-%d %H:%M:%S'),
1793 'write_date': False,
1798 def _check_removed_columns(self, cr, log=False):
1799 # nothing to check in memory...
1802 def exists(self, cr, uid, id, context=None):
1803 return id in self.datas
1805 class orm(orm_template):
1806 _sql_constraints = []
1808 _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']
1810 def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None):
1811 context = context or {}
1812 self.pool.get('ir.model.access').check(cr, uid, self._name, 'read', context=context)
1814 fields = self._columns.keys()
1816 (qu1, qu2, tables) = self._where_calc(cr, uid, domain, context=context)
1817 dom = self.pool.get('ir.rule').domain_get(cr, uid, self._name, context=context)
1824 qu1 = ' where '+string.join(qu1, ' and ')
1827 limit_str = limit and ' limit %d' % limit or ''
1828 offset_str = offset and ' offset %d' % offset or ''
1830 fget = self.fields_get(cr, uid, fields)
1831 float_int_fields = filter(lambda x: fget[x]['type'] in ('float','integer'), fields)
1834 fields_pre = [f for f in float_int_fields if
1835 f == self.CONCURRENCY_CHECK_FIELD
1836 or (f in self._columns and getattr(self._columns[f], '_classic_write'))]
1837 avg_fields = context.get('avg',[])
1838 for f in fields_pre:
1839 if f not in ['id','sequence']:
1841 flist += ',avg('+f+') as '+f
1843 flist += ',sum('+f+') as '+f
1844 cr.execute('select min(id) as id,'+flist+' from ' + self._table +qu1+' group by '+ groupby + limit_str + offset_str,qu2)
1846 for r in cr.dictfetchall():
1847 alldata[r['id']] = r
1850 data = self.read(cr, uid, alldata.keys(), [groupby], context=context)
1852 if fget.has_key(groupby):
1853 if fget[groupby]['type'] == 'many2one':
1854 d[groupby] = d[groupby] and d[groupby][1] or ''
1855 if d[groupby] and fget[groupby]['type'] in ('date','datetime'):
1856 today = datetime.date.today()
1857 if d[groupby][:10] == str(today):
1858 d[groupby] = 'Today'
1860 d[groupby] = datetime.datetime.strptime(d[groupby][:10],'%Y-%m-%d').strftime('%B %Y')
1861 d['__domain'] = [(groupby,'=',alldata[d['id']][groupby] or False)] + domain
1862 del alldata[d['id']][groupby]
1863 d.update(alldata[d['id']])
1867 def _parent_store_compute(self, cr):
1868 logger = netsvc.Logger()
1869 logger.notifyChannel('orm', netsvc.LOG_INFO, 'Computing parent left and right for table %s...' % (self._table, ))
1870 def browse_rec(root, pos=0):
1872 where = self._parent_name+'='+str(root)
1874 where = self._parent_name+' IS NULL'
1875 if self._parent_order:
1876 where += ' order by '+self._parent_order
1877 cr.execute('SELECT id FROM '+self._table+' WHERE '+where)
1879 childs = cr.fetchall()
1881 pos2 = browse_rec(id[0], pos2)
1882 cr.execute('update '+self._table+' set parent_left=%s, parent_right=%s where id=%s', (pos,pos2,root))
1884 query = 'SELECT id FROM '+self._table+' WHERE '+self._parent_name+' IS NULL'
1885 if self._parent_order:
1886 query += ' order by '+self._parent_order
1889 for (root,) in cr.fetchall():
1890 pos = browse_rec(root, pos)
1893 def _update_store(self, cr, f, k):
1894 logger = netsvc.Logger()
1895 logger.notifyChannel('orm', netsvc.LOG_INFO, "storing computed values of fields.function '%s'" % (k,))
1896 ss = self._columns[k]._symbol_set
1897 update_query = 'UPDATE "%s" SET "%s"=%s WHERE id=%%s' % (self._table, k, ss[0])
1898 cr.execute('select id from '+self._table)
1899 ids_lst = map(lambda x: x[0], cr.fetchall())
1902 ids_lst = ids_lst[40:]
1903 res = f.get(cr, self, iids, k, 1, {})
1904 for key,val in res.items():
1907 # if val is a many2one, just write the ID
1908 if type(val)==tuple:
1910 if (val<>False) or (type(val)<>bool):
1911 cr.execute(update_query, (ss[1](val), key))
1913 def _check_removed_columns(self, cr, log=False):
1914 logger = netsvc.Logger()
1915 # iterate on the database columns to drop the NOT NULL constraints
1916 # of fields which were required but have been removed (or will be added by another module)
1917 columns = [c for c in self._columns if not (isinstance(self._columns[c], fields.function) and not self._columns[c].store)]
1918 columns += ('id', 'write_uid', 'write_date', 'create_uid', 'create_date') # openerp access columns
1919 cr.execute("SELECT a.attname, a.attnotnull"
1920 " FROM pg_class c, pg_attribute a"
1921 " WHERE c.relname=%%s"
1922 " AND c.oid=a.attrelid"
1923 " AND a.attisdropped=%%s"
1924 " AND pg_catalog.format_type(a.atttypid, a.atttypmod) NOT IN ('cid', 'tid', 'oid', 'xid')"
1925 " AND a.attname NOT IN (%s)" % ",".join(['%s']*len(columns)),
1926 [self._table, False] + columns)
1927 for column in cr.dictfetchall():
1929 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))
1930 if column['attnotnull']:
1931 cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" DROP NOT NULL' % (self._table, column['attname']))
1933 def _auto_init(self, cr, context={}):
1934 store_compute = False
1935 logger = netsvc.Logger()
1938 self._field_create(cr, context=context)
1939 if getattr(self, '_auto', True):
1940 cr.execute("SELECT relname FROM pg_class WHERE relkind in ('r','v') AND relname='%s'" % self._table)
1942 cr.execute("CREATE TABLE \"%s\" (id SERIAL NOT NULL, PRIMARY KEY(id)) WITHOUT OIDS" % self._table)
1943 cr.execute("COMMENT ON TABLE \"%s\" IS '%s'" % (self._table, self._description.replace("'","''")))
1946 if self._parent_store:
1947 cr.execute("""SELECT c.relname
1948 FROM pg_class c, pg_attribute a
1949 WHERE c.relname=%s AND a.attname=%s AND c.oid=a.attrelid
1950 """, (self._table, 'parent_left'))
1952 if 'parent_left' not in self._columns:
1953 logger.notifyChannel('orm', netsvc.LOG_ERROR, 'create a column parent_left on object %s: fields.integer(\'Left Parent\', select=1)' % (self._table, ))
1954 if 'parent_right' not in self._columns:
1955 logger.notifyChannel('orm', netsvc.LOG_ERROR, 'create a column parent_right on object %s: fields.integer(\'Right Parent\', select=1)' % (self._table, ))
1956 if self._columns[self._parent_name].ondelete<>'cascade':
1957 logger.notifyChannel('orm', netsvc.LOG_ERROR, "the columns %s on object must be set as ondelete='cascasde'" % (self._name, self._parent_name))
1958 cr.execute('ALTER TABLE "%s" ADD COLUMN "parent_left" INTEGER' % (self._table,))
1959 cr.execute('ALTER TABLE "%s" ADD COLUMN "parent_right" INTEGER' % (self._table,))
1961 store_compute = True
1963 if self._log_access:
1965 'create_uid': 'INTEGER REFERENCES res_users ON DELETE SET NULL',
1966 'create_date': 'TIMESTAMP',
1967 'write_uid': 'INTEGER REFERENCES res_users ON DELETE SET NULL',
1968 'write_date': 'TIMESTAMP'
1973 FROM pg_class c, pg_attribute a
1974 WHERE c.relname=%s AND a.attname=%s AND c.oid=a.attrelid
1975 """, (self._table, k))
1977 cr.execute('ALTER TABLE "%s" ADD COLUMN "%s" %s' % (self._table, k, logs[k]))
1980 self._check_removed_columns(cr, log=False)
1982 # iterate on the "object columns"
1983 todo_update_store = []
1984 for k in self._columns:
1985 if k in ('id', 'write_uid', 'write_date', 'create_uid', 'create_date'):
1987 #raise _('Can not define a column %s. Reserved keyword !') % (k,)
1988 f = self._columns[k]
1990 if isinstance(f, fields.one2many):
1991 cr.execute("SELECT relname FROM pg_class WHERE relkind='r' AND relname=%s", (f._obj,))
1993 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))
1994 res = cr.fetchone()[0]
1996 cr.execute('ALTER TABLE "%s" ADD FOREIGN KEY (%s) REFERENCES "%s" ON DELETE SET NULL' % (self._obj, f._fields_id, f._table))
1997 elif isinstance(f, fields.many2many):
1998 cr.execute("SELECT relname FROM pg_class WHERE relkind in ('r','v') AND relname=%s", (f._rel,))
1999 if not cr.dictfetchall():
2000 if not self.pool.get(f._obj):
2001 raise except_orm('Programming Error', ('There is no reference available for %s') % (f._obj,))
2002 ref = self.pool.get(f._obj)._table
2003 # ref = f._obj.replace('.', '_')
2004 cr.execute('CREATE TABLE "%s" ("%s" INTEGER NOT NULL REFERENCES "%s" ON DELETE CASCADE, "%s" INTEGER NOT NULL REFERENCES "%s" ON DELETE CASCADE) WITH OIDS' % (f._rel, f._id1, self._table, f._id2, ref))
2005 cr.execute('CREATE INDEX "%s_%s_index" ON "%s" ("%s")' % (f._rel, f._id1, f._rel, f._id1))
2006 cr.execute('CREATE INDEX "%s_%s_index" ON "%s" ("%s")' % (f._rel, f._id2, f._rel, f._id2))
2007 cr.execute("COMMENT ON TABLE \"%s\" IS 'RELATION BETWEEN %s AND %s'" % (f._rel, self._table, ref))
2010 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 " \
2011 "FROM pg_class c,pg_attribute a,pg_type t " \
2012 "WHERE c.relname=%s " \
2013 "AND a.attname=%s " \
2014 "AND c.oid=a.attrelid " \
2015 "AND a.atttypid=t.oid", (self._table, k))
2016 res = cr.dictfetchall()
2017 if not res and hasattr(f,'oldname'):
2018 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 " \
2019 "FROM pg_class c,pg_attribute a,pg_type t " \
2020 "WHERE c.relname=%s " \
2021 "AND a.attname=%s " \
2022 "AND c.oid=a.attrelid " \
2023 "AND a.atttypid=t.oid", (self._table, f.oldname))
2024 res_old = cr.dictfetchall()
2025 logger.notifyChannel('orm', netsvc.LOG_DEBUG, 'trying to rename %s(%s) to %s'% (self._table, f.oldname, k))
2026 if res_old and len(res_old)==1:
2027 cr.execute('ALTER TABLE "%s" RENAME "%s" TO "%s"' % ( self._table,f.oldname, k))
2029 res[0]['attname'] = k
2032 if not isinstance(f, fields.function) or f.store:
2034 # add the missing field
2035 cr.execute('ALTER TABLE "%s" ADD COLUMN "%s" %s' % (self._table, k, get_pg_type(f)[1]))
2036 cr.execute("COMMENT ON COLUMN %s.%s IS '%s'" % (self._table, k, f.string.replace("'","''")))
2039 if not create and k in self._defaults:
2040 if callable(self._defaults[k]):
2041 default = self._defaults[k](self, cr, 1, context)
2043 default = self._defaults[k]
2045 ss = self._columns[k]._symbol_set
2046 query = 'UPDATE "%s" SET "%s"=%s' % (self._table, k, ss[0])
2047 cr.execute(query, (ss[1](default),))
2049 logger.notifyChannel('orm', netsvc.LOG_DEBUG, 'setting default value of new column %s of table %s'% (k, self._table))
2051 logger.notifyChannel('orm', netsvc.LOG_DEBUG, 'creating new column %s of table %s'% (k, self._table))
2053 if isinstance(f, fields.function):
2055 if f.store is not True:
2056 order = f.store[f.store.keys()[0]][2]
2057 todo_update_store.append((order, f,k))
2059 # and add constraints if needed
2060 if isinstance(f, fields.many2one):
2061 if not self.pool.get(f._obj):
2062 raise except_orm('Programming Error', ('There is no reference available for %s') % (f._obj,))
2063 ref = self.pool.get(f._obj)._table
2064 # ref = f._obj.replace('.', '_')
2065 # ir_actions is inherited so foreign key doesn't work on it
2066 if ref != 'ir_actions':
2067 cr.execute('ALTER TABLE "%s" ADD FOREIGN KEY ("%s") REFERENCES "%s" ON DELETE %s' % (self._table, k, ref, f.ondelete))
2069 cr.execute('CREATE INDEX "%s_%s_index" ON "%s" ("%s")' % (self._table, k, self._table, k))
2073 cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" SET NOT NULL' % (self._table, k))
2074 except Exception, e:
2075 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))
2079 f_pg_type = f_pg_def['typname']
2080 f_pg_size = f_pg_def['size']
2081 f_pg_notnull = f_pg_def['attnotnull']
2082 if isinstance(f, fields.function) and not f.store and\
2083 not getattr(f, 'nodrop', False):
2084 logger.notifyChannel('orm', netsvc.LOG_INFO, 'column %s (%s) in table %s removed: converted to a function !\n' % (k, f.string, self._table))
2085 cr.execute('ALTER TABLE "%s" DROP COLUMN "%s"'% (self._table, k))
2089 f_obj_type = get_pg_type(f) and get_pg_type(f)[0]
2094 ('text', 'char', 'VARCHAR(%d)' % (f.size or 0,), '::VARCHAR(%d)'%(f.size or 0,)),
2095 ('varchar', 'text', 'TEXT', ''),
2096 ('int4', 'float', get_pg_type(f)[1], '::'+get_pg_type(f)[1]),
2097 ('date', 'datetime', 'TIMESTAMP', '::TIMESTAMP'),
2098 ('numeric', 'float', get_pg_type(f)[1], '::'+get_pg_type(f)[1]),
2099 ('float8', 'float', get_pg_type(f)[1], '::'+get_pg_type(f)[1]),
2101 # !!! Avoid reduction of varchar field !!!
2102 if f_pg_type == 'varchar' and f._type == 'char' and f_pg_size < f.size:
2103 # if f_pg_type == 'varchar' and f._type == 'char' and f_pg_size != f.size:
2104 logger.notifyChannel('orm', netsvc.LOG_INFO, "column '%s' in table '%s' changed size" % (k, self._table))
2105 cr.execute('ALTER TABLE "%s" RENAME COLUMN "%s" TO temp_change_size' % (self._table, k))
2106 cr.execute('ALTER TABLE "%s" ADD COLUMN "%s" VARCHAR(%d)' % (self._table, k, f.size))
2107 cr.execute('UPDATE "%s" SET "%s"=temp_change_size::VARCHAR(%d)' % (self._table, k, f.size))
2108 cr.execute('ALTER TABLE "%s" DROP COLUMN temp_change_size CASCADE' % (self._table,))
2111 if (f_pg_type==c[0]) and (f._type==c[1]):
2112 # Adding upcoming 6 lines to check whether only the size of the fields got changed or not.E.g. :(16,3) to (16,4)
2113 field_size_change = False
2114 if f_pg_type in ['int4','numeric','float8']:
2116 field_size = (65535 * f.digits[0]) + f.digits[0] + f.digits[1]
2117 if field_size != f_pg_size:
2118 field_size_change = True
2120 if f_pg_type != f_obj_type or field_size_change:
2121 if f_pg_type != f_obj_type:
2122 logger.notifyChannel('orm', netsvc.LOG_INFO, "column '%s' in table '%s' changed type to %s." % (k, self._table, c[1]))
2123 if field_size_change:
2124 logger.notifyChannel('orm', netsvc.LOG_INFO, "column '%s' in table '%s' changed in the size." % (k, self._table))
2126 cr.execute('ALTER TABLE "%s" RENAME COLUMN "%s" TO temp_change_size' % (self._table, k))
2127 cr.execute('ALTER TABLE "%s" ADD COLUMN "%s" %s' % (self._table, k, c[2]))
2128 cr.execute(('UPDATE "%s" SET "%s"=temp_change_size'+c[3]) % (self._table, k))
2129 cr.execute('ALTER TABLE "%s" DROP COLUMN temp_change_size CASCADE' % (self._table,))
2133 if f_pg_type != f_obj_type:
2135 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))
2137 # if the field is required and hasn't got a NOT NULL constraint
2138 if f.required and f_pg_notnull == 0:
2139 # set the field to the default value if any
2140 if k in self._defaults:
2141 if callable(self._defaults[k]):
2142 default = self._defaults[k](self, cr, 1, context)
2144 default = self._defaults[k]
2146 if (default is not None):
2147 ss = self._columns[k]._symbol_set
2148 query = 'UPDATE "%s" SET "%s"=%s WHERE "%s" is NULL' % (self._table, k, ss[0], k)
2149 cr.execute(query, (ss[1](default),))
2150 # add the NOT NULL constraint
2153 cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" SET NOT NULL' % (self._table, k))
2155 except Exception, e:
2156 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))
2158 elif not f.required and f_pg_notnull == 1:
2159 cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" DROP NOT NULL' % (self._table, k))
2161 indexname = '%s_%s_index' % (self._table, k)
2162 cr.execute("SELECT indexname FROM pg_indexes WHERE indexname = %s and tablename = %s", (indexname, self._table))
2163 res = cr.dictfetchall()
2164 if not res and f.select:
2165 cr.execute('CREATE INDEX "%s_%s_index" ON "%s" ("%s")' % (self._table, k, self._table, k))
2167 if res and not f.select:
2168 cr.execute('DROP INDEX "%s_%s_index"' % (self._table, k))
2170 if isinstance(f, fields.many2one):
2171 ref = self.pool.get(f._obj)._table
2172 if ref != 'ir_actions':
2173 cr.execute('SELECT confdeltype, conname FROM pg_constraint as con, pg_class as cl1, pg_class as cl2, '
2174 'pg_attribute as att1, pg_attribute as att2 '
2175 'WHERE con.conrelid = cl1.oid '
2176 'AND cl1.relname = %s '
2177 'AND con.confrelid = cl2.oid '
2178 'AND cl2.relname = %s '
2179 'AND array_lower(con.conkey, 1) = 1 '
2180 'AND con.conkey[1] = att1.attnum '
2181 'AND att1.attrelid = cl1.oid '
2182 'AND att1.attname = %s '
2183 'AND array_lower(con.confkey, 1) = 1 '
2184 'AND con.confkey[1] = att2.attnum '
2185 'AND att2.attrelid = cl2.oid '
2186 'AND att2.attname = %s '
2187 "AND con.contype = 'f'", (self._table, ref, k, 'id'))
2188 res = cr.dictfetchall()
2197 if res[0]['confdeltype'] != confdeltype.get(f.ondelete.upper(), 'a'):
2198 cr.execute('ALTER TABLE "' + self._table + '" DROP CONSTRAINT "' + res[0]['conname'] + '"')
2199 cr.execute('ALTER TABLE "' + self._table + '" ADD FOREIGN KEY ("' + k + '") REFERENCES "' + ref + '" ON DELETE ' + f.ondelete)
2202 logger.notifyChannel('orm', netsvc.LOG_ERROR, "Programming error, column %s->%s has multiple instances !"%(self._table,k))
2203 for order,f,k in todo_update_store:
2204 todo_end.append((order, self._update_store, (f, k)))
2207 cr.execute("SELECT relname FROM pg_class WHERE relkind in ('r','v') AND relname=%s", (self._table,))
2208 create = not bool(cr.fetchone())
2210 for (key, con, _) in self._sql_constraints:
2211 conname = '%s_%s' % (self._table, key)
2212 cr.execute("SELECT conname FROM pg_constraint where conname=%s", (conname,))
2213 if not cr.dictfetchall():
2215 cr.execute('alter table "%s" add constraint "%s_%s" %s' % (self._table, self._table, key, con,))
2218 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,))
2221 if hasattr(self, "_sql"):
2222 for line in self._sql.split(';'):
2223 line2 = line.replace('\n', '').strip()
2228 self._parent_store_compute(cr)
2231 def __init__(self, cr):
2232 super(orm, self).__init__(cr)
2234 if not hasattr(self, '_log_access'):
2235 # if not access is not specify, it is the same value as _auto
2236 self._log_access = getattr(self, "_auto", True)
2238 self._columns = self._columns.copy()
2239 for store_field in self._columns:
2240 f = self._columns[store_field]
2241 if not isinstance(f, fields.function):
2245 if self._columns[store_field].store is True:
2246 sm = {self._name:(lambda self,cr, uid, ids, c={}: ids, None, 10, None)}
2248 sm = self._columns[store_field].store
2249 for object, aa in sm.items():
2251 (fnct,fields2,order,length)=aa
2253 (fnct,fields2,order)=aa
2256 raise except_orm('Error',
2257 ('Invalid function definition %s in object %s !\nYou must use the definition: store={object:(fnct, fields, priority, time length)}.' % (store_field, self._name)))
2258 self.pool._store_function.setdefault(object, [])
2260 for x,y,z,e,f,l in self.pool._store_function[object]:
2261 if (x==self._name) and (y==store_field) and (e==fields2):
2265 self.pool._store_function[object].append( (self._name, store_field, fnct, fields2, order, length))
2266 self.pool._store_function[object].sort(lambda x,y: cmp(x[4],y[4]))
2268 for (key, _, msg) in self._sql_constraints:
2269 self.pool._sql_error[self._table+'_'+key] = msg
2271 # Load manual fields
2273 cr.execute("SELECT id FROM ir_model_fields WHERE name=%s AND model=%s", ('state', 'ir.model.fields'))
2275 cr.execute('SELECT * FROM ir_model_fields WHERE model=%s AND state=%s', (self._name, 'manual'))
2276 for field in cr.dictfetchall():
2277 if field['name'] in self._columns:
2280 'string': field['field_description'],
2281 'required': bool(field['required']),
2282 'readonly': bool(field['readonly']),
2283 'domain': field['domain'] or None,
2284 'size': field['size'],
2285 'ondelete': field['on_delete'],
2286 'translate': (field['translate']),
2287 #'select': int(field['select_level'])
2290 if field['ttype'] == 'selection':
2291 self._columns[field['name']] = getattr(fields, field['ttype'])(eval(field['selection']), **attrs)
2292 elif field['ttype'] == 'reference':
2293 self._columns[field['name']] = getattr(fields, field['ttype'])(selection=eval(field['selection']), **attrs)
2294 elif field['ttype'] == 'many2one':
2295 self._columns[field['name']] = getattr(fields, field['ttype'])(field['relation'], **attrs)
2296 elif field['ttype'] == 'one2many':
2297 self._columns[field['name']] = getattr(fields, field['ttype'])(field['relation'], field['relation_field'], **attrs)
2298 elif field['ttype'] == 'many2many':
2299 _rel1 = field['relation'].replace('.', '_')
2300 _rel2 = field['model'].replace('.', '_')
2301 _rel_name = 'x_%s_%s_%s_rel' %(_rel1, _rel2, field['name'])
2302 self._columns[field['name']] = getattr(fields, field['ttype'])(field['relation'], _rel_name, 'id1', 'id2', **attrs)
2304 self._columns[field['name']] = getattr(fields, field['ttype'])(**attrs)
2306 self._inherits_reload()
2307 if not self._sequence:
2308 self._sequence = self._table+'_id_seq'
2309 for k in self._defaults:
2310 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,)
2311 for f in self._columns:
2312 self._columns[f].restart()
2314 def default_get(self, cr, uid, fields_list, context=None):
2318 # get the default values for the inherited fields
2319 for t in self._inherits.keys():
2320 value.update(self.pool.get(t).default_get(cr, uid, fields_list,
2323 # get the default values defined in the object
2324 for f in fields_list:
2325 if f in self._defaults:
2326 if callable(self._defaults[f]):
2327 value[f] = self._defaults[f](self, cr, uid, context)
2329 value[f] = self._defaults[f]
2331 fld_def = ((f in self._columns) and self._columns[f]) \
2332 or ((f in self._inherit_fields) and self._inherit_fields[f][2]) \
2334 if isinstance(fld_def, fields.property):
2335 property_obj = self.pool.get('ir.property')
2336 definition_id = fld_def._field_get(cr, uid, self._name, f)
2337 nid = property_obj.search(cr, uid, [('fields_id', '=',
2338 definition_id), ('res_id', '=', False)])
2340 prop_value = property_obj.browse(cr, uid, nid[0],
2341 context=context).value
2342 value[f] = (prop_value and int(prop_value.split(',')[1])) \
2345 # get the default values set by the user and override the default
2346 # values defined in the object
2347 ir_values_obj = self.pool.get('ir.values')
2348 res = ir_values_obj.get(cr, uid, 'default', False, [self._name])
2349 for id, field, field_value in res:
2350 if field in fields_list:
2351 fld_def = (field in self._columns) and self._columns[field] or self._inherit_fields[field][2]
2352 if fld_def._type in ('many2one', 'one2one'):
2353 obj = self.pool.get(fld_def._obj)
2354 if not obj.search(cr, uid, [('id', '=', field_value)]):
2356 if fld_def._type in ('many2many'):
2357 obj = self.pool.get(fld_def._obj)
2359 for i in range(len(field_value)):
2360 if not obj.search(cr, uid, [('id', '=',
2363 field_value2.append(field_value[i])
2364 field_value = field_value2
2365 if fld_def._type in ('one2many'):
2366 obj = self.pool.get(fld_def._obj)
2368 for i in range(len(field_value)):
2369 field_value2.append({})
2370 for field2 in field_value[i]:
2371 if obj._columns[field2]._type in ('many2one', 'one2one'):
2372 obj2 = self.pool.get(obj._columns[field2]._obj)
2373 if not obj2.search(cr, uid,
2374 [('id', '=', field_value[i][field2])]):
2376 # TODO add test for many2many and one2many
2377 field_value2[i][field2] = field_value[i][field2]
2378 field_value = field_value2
2379 value[field] = field_value
2380 for key in context or {}:
2381 if key.startswith('default_'):
2382 value[key[8:]] = context[key]
2386 # Update objects that uses this one to update their _inherits fields
2388 def _inherits_reload_src(self):
2389 for obj in self.pool.obj_pool.values():
2390 if self._name in obj._inherits:
2391 obj._inherits_reload()
2393 def _inherits_reload(self):
2395 for table in self._inherits:
2396 res.update(self.pool.get(table)._inherit_fields)
2397 for col in self.pool.get(table)._columns.keys():
2398 res[col] = (table, self._inherits[table], self.pool.get(table)._columns[col])
2399 for col in self.pool.get(table)._inherit_fields.keys():
2400 res[col] = (table, self._inherits[table], self.pool.get(table)._inherit_fields[col][2])
2401 self._inherit_fields = res
2402 self._inherits_reload_src()
2404 def fields_get(self, cr, user, fields=None, context=None):
2405 ira = self.pool.get('ir.model.access')
2406 read_access = ira.check(cr, user, self._name, 'write', raise_exception=False, context=context) or \
2407 ira.check(cr, user, self._name, 'create', raise_exception=False, context=context)
2408 return super(orm, self).fields_get(cr, user, fields, context, read_access)
2410 def read(self, cr, user, ids, fields=None, context=None, load='_classic_read'):
2413 self.pool.get('ir.model.access').check(cr, user, self._name, 'read', context=context)
2415 fields = self._columns.keys() + self._inherit_fields.keys()
2416 if isinstance(ids, (int, long)):
2420 select = map(lambda x: isinstance(x,dict) and x['id'] or x, select)
2421 result = self._read_flat(cr, user, select, fields, context, load)
2423 for key, v in r.items():
2426 if key in self._columns.keys():
2427 type = self._columns[key]._type
2428 elif key in self._inherit_fields.keys():
2429 type = self._inherit_fields[key][2]._type
2432 if type == 'reference' and v:
2433 model,ref_id = v.split(',')
2434 table = self.pool.get(model)._table
2435 cr.execute('select id from "%s" where id=%s' % (table,ref_id))
2436 id_exist = cr.fetchone()
2438 cr.execute('update "'+self._table+'" set "'+key+'"=NULL where "%s"=%s' %(key,''.join("'"+str(v)+"'")))
2440 if isinstance(ids, (int, long, dict)):
2441 return result and result[0] or False
2444 def _read_flat(self, cr, user, ids, fields_to_read, context=None, load='_classic_read'):
2447 #ids = map(lambda x:int(x), ids)
2450 if fields_to_read == None:
2451 fields_to_read = self._columns.keys()
2453 # construct a clause for the rules :
2454 d1, d2, tables = self.pool.get('ir.rule').domain_get(cr, user, self._name, context=context)
2456 # all inherited fields + all non inherited fields for which the attribute whose name is in load is True
2457 fields_pre = [f for f in fields_to_read if
2458 f == self.CONCURRENCY_CHECK_FIELD
2459 or (f in self._columns and getattr(self._columns[f], '_classic_write'))
2460 ] + self._inherits.values()
2464 def convert_field(f):
2465 if f in ('create_date', 'write_date'):
2466 return "date_trunc('second', %s) as %s" % (f, f)
2467 if f == self.CONCURRENCY_CHECK_FIELD:
2468 if self._log_access:
2469 return "COALESCE(%s.write_date, %s.create_date, now())::timestamp AS %s" % (self._table, self._table, f,)
2470 return "now()::timestamp AS %s" % (f,)
2471 if isinstance(self._columns[f], fields.binary) and context.get('bin_size', False):
2472 return 'length("%s") as "%s"' % (f, f)
2473 return '"%s"' % (f,)
2474 fields_pre2 = map(convert_field, fields_pre)
2475 order_by = self._parent_order or self._order
2476 for i in range(0, len(ids), cr.IN_MAX):
2477 sub_ids = ids[i:i+cr.IN_MAX]
2479 cr.execute('SELECT %s FROM %s WHERE %s.id = ANY (%%s) AND %s ORDER BY %s' % \
2480 (','.join(fields_pre2 + [self._table + '.id']), ','.join(tables), self._table, ' and '.join(d1),
2481 order_by),[sub_ids,]+d2)
2482 if not cr.rowcount == len({}.fromkeys(sub_ids)):
2483 raise except_orm(_('AccessError'),
2484 _('You try to bypass an access rule while reading (Document type: %s).') % self._description)
2486 cr.execute('SELECT %s FROM \"%s\" WHERE id = ANY (%%s) ORDER BY %s' %
2487 (','.join(fields_pre2 + ['id']), self._table,
2488 order_by), (sub_ids,))
2489 res.extend(cr.dictfetchall())
2491 res = map(lambda x: {'id': x}, ids)
2493 for f in fields_pre:
2494 if f == self.CONCURRENCY_CHECK_FIELD:
2496 if self._columns[f].translate:
2497 ids = map(lambda x: x['id'], res)
2498 #TODO: optimize out of this loop
2499 res_trans = self.pool.get('ir.translation')._get_ids(cr, user, self._name+','+f, 'model', context.get('lang', False) or 'en_US', ids)
2501 r[f] = res_trans.get(r['id'], False) or r[f]
2503 for table in self._inherits:
2504 col = self._inherits[table]
2505 cols = intersect(self._inherit_fields.keys(), fields_to_read)
2508 res2 = self.pool.get(table).read(cr, user, [x[col] for x in res], cols, context, load)
2516 record.update(res3[record[col]])
2517 if col not in fields_to_read:
2520 # all fields which need to be post-processed by a simple function (symbol_get)
2521 fields_post = filter(lambda x: x in self._columns and self._columns[x]._symbol_get, fields_to_read)
2523 # maybe it would be faster to iterate on the fields then on res, so that we wouldn't need
2524 # to get the _symbol_get in each occurence
2526 for f in fields_post:
2527 r[f] = self._columns[f]._symbol_get(r[f])
2528 ids = map(lambda x: x['id'], res)
2530 # all non inherited fields for which the attribute whose name is in load is False
2531 fields_post = filter(lambda x: x in self._columns and not getattr(self._columns[x], load), fields_to_read)
2533 # Compute POST fields
2535 for f in fields_post:
2536 todo.setdefault(self._columns[f]._multi, [])
2537 todo[self._columns[f]._multi].append(f)
2538 for key,val in todo.items():
2540 res2 = self._columns[val[0]].get(cr, self, ids, val, user, context=context, values=res)
2543 if isinstance(res2[record['id']], str):res2[record['id']] = eval(res2[record['id']]) #TOCHECK : why got string instend of dict in python2.6
2544 record[pos] = res2[record['id']][pos]
2547 res2 = self._columns[f].get(cr, self, ids, f, user, context=context, values=res)
2550 record[f] = res2[record['id']]
2554 #for f in fields_post:
2555 # # get the value of that field for all records/ids
2556 # res2 = self._columns[f].get(cr, self, ids, f, user, context=context, values=res)
2557 # for record in res:
2558 # record[f] = res2[record['id']]
2562 for field in vals.copy():
2564 if field in self._columns:
2565 fobj = self._columns[field]
2572 for group in groups:
2573 module = group.split(".")[0]
2574 grp = group.split(".")[1]
2575 cr.execute("select count(*) from res_groups_users_rel where gid in (select res_id from ir_model_data where name='%s' and module='%s' and model='%s') and uid=%s" % \
2576 (grp, module, 'res.groups', user))
2577 readonly = cr.fetchall()
2578 if readonly[0][0] >= 1:
2581 elif readonly[0][0] == 0:
2587 if type(vals[field]) == type([]):
2589 elif type(vals[field]) == type(0.0):
2591 elif type(vals[field]) == type(''):
2592 vals[field] = '=No Permission='
2597 def perm_read(self, cr, user, ids, context=None, details=True):
2603 if self._log_access:
2604 fields = ', u.create_uid, u.create_date, u.write_uid, u.write_date'
2605 if isinstance(ids, (int, long)):
2608 ids_str = string.join(map(lambda x: str(x), ids), ',')
2609 cr.execute('select u.id'+fields+' from "'+self._table+'" u where u.id in ('+ids_str+')')
2610 res = cr.dictfetchall()
2613 r[key] = r[key] or False
2614 if key in ('write_uid', 'create_uid', 'uid') and details:
2616 r[key] = self.pool.get('res.users').name_get(cr, user, [r[key]])[0]
2617 if isinstance(ids, (int, long)):
2621 def _check_concurrency(self, cr, ids, context):
2624 if context.get(self.CONCURRENCY_CHECK_FIELD) and self._log_access:
2626 return "%s,%s" % (self._name, oid)
2627 santa = "(id = %s AND %s < COALESCE(write_date, create_date, now())::timestamp)"
2628 for i in range(0, len(ids), cr.IN_MAX):
2629 sub_ids = tools.flatten(((oid, context[self.CONCURRENCY_CHECK_FIELD][key(oid)])
2630 for oid in ids[i:i+cr.IN_MAX]
2631 if key(oid) in context[self.CONCURRENCY_CHECK_FIELD]))
2633 cr.execute("SELECT count(1) FROM %s WHERE %s" % (self._table, " OR ".join([santa]*(len(sub_ids)/2))), sub_ids)
2636 raise except_orm('ConcurrencyException', _('Records were modified in the meanwhile'))
2638 def unlink(self, cr, uid, ids, context=None):
2641 if isinstance(ids, (int, long)):
2644 result_store = self._store_get_values(cr, uid, ids, None, context)
2646 self._check_concurrency(cr, ids, context)
2648 self.pool.get('ir.model.access').check(cr, uid, self._name, 'unlink', context=context)
2650 properties = self.pool.get('ir.property')
2651 domain = [('res_id', '=', False),
2652 ('value', 'in', ['%s,%s' % (self._name, i) for i in ids]),
2654 if properties.search(cr, uid, domain, context=context):
2655 raise except_orm(_('Error'), _('Unable to delete this document because it is used as a default property'))
2657 wf_service = netsvc.LocalService("workflow")
2659 wf_service.trg_delete(uid, self._name, oid, cr)
2661 #cr.execute('select * from '+self._table+' where id in ('+str_d+')', ids)
2662 #res = cr.dictfetchall()
2663 #for key in self._inherits:
2664 # ids2 = [x[self._inherits[key]] for x in res]
2665 # self.pool.get(key).unlink(cr, uid, ids2)
2667 d1, d2,tables = self.pool.get('ir.rule').domain_get(cr, uid, self._name, context=context)
2669 d1 = ' AND '+' and '.join(d1)
2671 for i in range(0, len(ids), cr.IN_MAX):
2672 sub_ids = ids[i:i+cr.IN_MAX]
2673 str_d = string.join(('%s',)*len(sub_ids), ',')
2675 cr.execute('SELECT '+self._table+'.id FROM '+','.join(tables)+' ' \
2676 'WHERE '+self._table+'.id IN ('+str_d+')'+d1, sub_ids+d2)
2677 if not cr.rowcount == len(sub_ids):
2678 raise except_orm(_('AccessError'),
2679 _('You try to bypass an access rule (Document type: %s).') % \
2682 cr.execute('delete from '+self._table+' ' \
2683 'where id in ('+str_d+')', sub_ids)
2685 for order, object, store_ids, fields in result_store:
2686 if object<>self._name:
2687 obj = self.pool.get(object)
2688 cr.execute('select id from '+obj._table+' where id in ('+','.join(map(str, store_ids))+')')
2689 rids = map(lambda x: x[0], cr.fetchall())
2691 obj._store_set_values(cr, uid, rids, fields, context)
2697 def write(self, cr, user, ids, vals, context=None):
2699 for field in vals.copy():
2701 if field in self._columns:
2702 fobj = self._columns[field]
2704 fobj = self._inherit_fields[field][2]
2711 for group in groups:
2712 module = group.split(".")[0]
2713 grp = group.split(".")[1]
2714 cr.execute("select count(*) from res_groups_users_rel where gid in (select res_id from ir_model_data where name='%s' and module='%s' and model='%s') and uid=%s" % \
2715 (grp, module, 'res.groups', user))
2716 readonly = cr.fetchall()
2717 if readonly[0][0] >= 1:
2720 elif readonly[0][0] == 0:
2732 if isinstance(ids, (int, long)):
2735 self._check_concurrency(cr, ids, context)
2737 self.pool.get('ir.model.access').check(cr, user, self._name, 'write', context=context)
2745 totranslate = context.get('lang', False) and (context['lang'] != 'en_US')
2747 if field in self._columns:
2748 if self._columns[field]._classic_write and not (hasattr(self._columns[field], '_fnct_inv')):
2749 if (not totranslate) or not self._columns[field].translate:
2750 upd0.append('"'+field+'"='+self._columns[field]._symbol_set[0])
2751 upd1.append(self._columns[field]._symbol_set[1](vals[field]))
2752 direct.append(field)
2754 upd_todo.append(field)
2756 updend.append(field)
2757 if field in self._columns \
2758 and hasattr(self._columns[field], 'selection') \
2760 if self._columns[field]._type == 'reference':
2761 val = vals[field].split(',')[0]
2764 if isinstance(self._columns[field].selection, (tuple, list)):
2765 if val not in dict(self._columns[field].selection):
2766 raise except_orm(_('ValidateError'),
2767 _('The value "%s" for the field "%s" is not in the selection') \
2768 % (vals[field], field))
2770 if val not in dict(self._columns[field].selection(
2771 self, cr, user, context=context)):
2772 raise except_orm(_('ValidateError'),
2773 _('The value "%s" for the field "%s" is not in the selection') \
2774 % (vals[field], field))
2776 if self._log_access:
2777 upd0.append('write_uid=%s')
2778 upd0.append('write_date=now()')
2783 d1, d2,tables = self.pool.get('ir.rule').domain_get(cr, user, self._name, context=context)
2785 d1 = ' and '+' and '.join(d1)
2787 for i in range(0, len(ids), cr.IN_MAX):
2788 sub_ids = ids[i:i+cr.IN_MAX]
2789 ids_str = string.join(map(str, sub_ids), ',')
2791 cr.execute('SELECT '+self._table+'.id FROM '+','.join(tables)+' ' \
2792 'WHERE '+self._table+'.id IN ('+ids_str+')'+d1, d2)
2793 if not cr.rowcount == len({}.fromkeys(sub_ids)):
2794 raise except_orm(_('AccessError'),
2795 _('You try to bypass an access rule while writing (Document type: %s).') % \
2798 cr.execute('SELECT id FROM "'+self._table+'" WHERE id IN ('+ids_str+')')
2799 if not cr.rowcount == len({}.fromkeys(sub_ids)):
2800 raise except_orm(_('AccessError'),
2801 _('You try to write on an record that doesn\'t exist ' \
2802 '(Document type: %s).') % self._description)
2803 cr.execute('update '+self._table+' set '+string.join(upd0, ',')+' ' \
2804 'where id in ('+ids_str+')', upd1)
2809 if self._columns[f].translate:
2810 src_trans = self.pool.get(self._name).read(cr,user,ids,[f])[0][f]
2813 # Inserting value to DB
2814 self.write(cr, user, ids, {f:vals[f]})
2815 self.pool.get('ir.translation')._set_ids(cr, user, self._name+','+f, 'model', context['lang'], ids, vals[f], src_trans)
2818 # call the 'set' method of fields which are not classic_write
2819 upd_todo.sort(lambda x, y: self._columns[x].priority-self._columns[y].priority)
2821 # default element in context must be removed when call a one2many or many2many
2822 rel_context = context.copy()
2823 for c in context.items():
2824 if c[0].startswith('default_'):
2825 del rel_context[c[0]]
2828 for field in upd_todo:
2830 result += self._columns[field].set(cr, self, id, field, vals[field], user, context=rel_context) or []
2832 for table in self._inherits:
2833 col = self._inherits[table]
2835 for i in range(0, len(ids), cr.IN_MAX):
2836 sub_ids = ids[i:i+cr.IN_MAX]
2837 ids_str = string.join(map(str, sub_ids), ',')
2838 cr.execute('select distinct "'+col+'" from "'+self._table+'" ' \
2839 'where id in ('+ids_str+')', upd1)
2840 nids.extend([x[0] for x in cr.fetchall()])
2844 if self._inherit_fields[val][0] == table:
2846 self.pool.get(table).write(cr, user, nids, v, context)
2848 self._validate(cr, user, ids, context)
2849 # TODO: use _order to set dest at the right position and not first node of parent
2850 if self._parent_store and (self._parent_name in vals):
2852 self.pool._init_parent[self._name]=True
2855 # Find Position of the element
2856 if vals[self._parent_name]:
2857 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],))
2859 cr.execute('select parent_left,parent_right,id from '+self._table+' where '+self._parent_name+' is null order by '+(self._parent_order or self._order))
2860 result_p = cr.fetchall()
2862 for (pleft,pright,pid) in result_p:
2867 # It's the first node of the parent: position = parent_left+1
2869 if not vals[self._parent_name]:
2872 cr.execute('select parent_left from '+self._table+' where id=%s', (vals[self._parent_name],))
2873 position = cr.fetchone()[0]+1
2875 # We have the new position !
2876 cr.execute('select parent_left,parent_right from '+self._table+' where id=%s', (id,))
2877 pleft,pright = cr.fetchone()
2878 distance = pright - pleft + 1
2880 if position>pleft and position<=pright:
2881 raise except_orm(_('UserError'), _('Recursivity Detected.'))
2884 cr.execute('update '+self._table+' set parent_left=parent_left+%s where parent_left>=%s', (distance, position))
2885 cr.execute('update '+self._table+' set parent_right=parent_right+%s where parent_right>=%s', (distance, position))
2886 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))
2888 cr.execute('update '+self._table+' set parent_left=parent_left+%s where parent_left>=%s', (distance, position))
2889 cr.execute('update '+self._table+' set parent_right=parent_right+%s where parent_right>=%s', (distance, position))
2890 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))
2892 result += self._store_get_values(cr, user, ids, vals.keys(), context)
2893 for order, object, ids, fields in result:
2894 self.pool.get(object)._store_set_values(cr, user, ids, fields, context)
2896 wf_service = netsvc.LocalService("workflow")
2898 wf_service.trg_write(user, self._name, id, cr)
2902 # TODO: Should set perm to user.xxx
2904 def create(self, cr, user, vals, context=None):
2905 """ create(cr, user, vals, context) -> int
2906 cr = database cursor
2908 vals = dictionary of the form {'field_name':field_value, ...}
2912 self.pool.get('ir.model.access').check(cr, user, self._name, 'create')
2917 for (t, c) in self._inherits.items():
2919 avoid_table.append(t)
2920 for f in self._columns.keys(): # + self._inherit_fields.keys():
2924 for f in self._inherit_fields.keys():
2925 if (not f in vals) and (self._inherit_fields[f][0] not in avoid_table):
2929 default_values = self.default_get(cr, user, default, context)
2930 for dv in default_values:
2931 if dv in self._columns and self._columns[dv]._type == 'many2many':
2932 if default_values[dv] and isinstance(default_values[dv][0], (int, long)):
2933 default_values[dv] = [(6, 0, default_values[dv])]
2935 vals.update(default_values)
2938 for v in self._inherits:
2939 if self._inherits[v] not in vals:
2942 tocreate[v] = {self._inherits[v]:vals[self._inherits[v]]}
2943 (upd0, upd1, upd2) = ('', '', [])
2945 for v in vals.keys():
2946 if v in self._inherit_fields:
2947 (table, col, col_detail) = self._inherit_fields[v]
2948 tocreate[table][v] = vals[v]
2951 if (v not in self._inherit_fields) and (v not in self._columns):
2954 # Try-except added to filter the creation of those records whose filds are readonly.
2955 # Example : any dashboard which has all the fields readonly.(due to Views(database views))
2957 cr.execute("SELECT nextval('"+self._sequence+"')")
2959 raise except_orm(_('UserError'),
2960 _('You cannot perform this operation. New Record Creation is not allowed for this object as this object is for reporting purpose.'))
2962 id_new = cr.fetchone()[0]
2963 for table in tocreate:
2964 if self._inherits[table] in vals:
2965 del vals[self._inherits[table]]
2966 id = self.pool.get(table).create(cr, user, tocreate[table])
2967 upd0 += ','+self._inherits[table]
2971 #Start : Set bool fields to be False if they are not touched(to make search more powerful)
2972 bool_fields = [x for x in self._columns.keys() if self._columns[x]._type=='boolean']
2974 for bool_field in bool_fields:
2975 if bool_field not in vals:
2976 vals[bool_field] = False
2978 for field in vals.copy():
2980 if field in self._columns:
2981 fobj = self._columns[field]
2983 fobj = self._inherit_fields[field][2]
2989 for group in groups:
2990 module = group.split(".")[0]
2991 grp = group.split(".")[1]
2992 cr.execute("select count(*) from res_groups_users_rel where gid in (select res_id from ir_model_data where name='%s' and module='%s' and model='%s') and uid=%s" % \
2993 (grp, module, 'res.groups', user))
2994 readonly = cr.fetchall()
2995 if readonly[0][0] >= 1:
2998 elif readonly[0][0] == 0:
3006 if self._columns[field]._classic_write:
3007 upd0 = upd0 + ',"' + field + '"'
3008 upd1 = upd1 + ',' + self._columns[field]._symbol_set[0]
3009 upd2.append(self._columns[field]._symbol_set[1](vals[field]))
3011 upd_todo.append(field)
3012 if field in self._columns \
3013 and hasattr(self._columns[field], 'selection') \
3015 if self._columns[field]._type == 'reference':
3016 val = vals[field].split(',')[0]
3019 if isinstance(self._columns[field].selection, (tuple, list)):
3020 if val not in dict(self._columns[field].selection):
3021 raise except_orm(_('ValidateError'),
3022 _('The value "%s" for the field "%s" is not in the selection') \
3023 % (vals[field], field))
3025 if val not in dict(self._columns[field].selection(
3026 self, cr, user, context=context)):
3027 raise except_orm(_('ValidateError'),
3028 _('The value "%s" for the field "%s" is not in the selection') \
3029 % (vals[field], field))
3030 if self._log_access:
3031 upd0 += ',create_uid,create_date'
3034 cr.execute('insert into "'+self._table+'" (id'+upd0+") values ("+str(id_new)+upd1+')', tuple(upd2))
3035 upd_todo.sort(lambda x, y: self._columns[x].priority-self._columns[y].priority)
3037 if self._parent_store:
3039 self.pool._init_parent[self._name]=True
3041 parent = vals.get(self._parent_name, False)
3043 cr.execute('select parent_right from '+self._table+' where '+self._parent_name+'=%s order by '+(self._parent_order or self._order), (parent,))
3045 result_p = cr.fetchall()
3046 for (pleft,) in result_p:
3051 cr.execute('select parent_left from '+self._table+' where id=%s', (parent,))
3052 pleft_old = cr.fetchone()[0]
3055 cr.execute('select max(parent_right) from '+self._table)
3056 pleft = cr.fetchone()[0] or 0
3057 cr.execute('update '+self._table+' set parent_left=parent_left+2 where parent_left>%s', (pleft,))
3058 cr.execute('update '+self._table+' set parent_right=parent_right+2 where parent_right>%s', (pleft,))
3059 cr.execute('update '+self._table+' set parent_left=%s,parent_right=%s where id=%s', (pleft+1,pleft+2,id_new))
3061 # default element in context must be remove when call a one2many or many2many
3062 rel_context = context.copy()
3063 for c in context.items():
3064 if c[0].startswith('default_'):
3065 del rel_context[c[0]]
3068 for field in upd_todo:
3069 result += self._columns[field].set(cr, self, id_new, field, vals[field], user, rel_context) or []
3070 self._validate(cr, user, [id_new], context)
3072 if not context.get('no_store_function', False):
3073 result += self._store_get_values(cr, user, [id_new], vals.keys(), context)
3076 for order, object, ids, fields2 in result:
3077 if not (object, ids, fields2) in done:
3078 self.pool.get(object)._store_set_values(cr, user, ids, fields2, context)
3079 done.append((object, ids, fields2))
3081 wf_service = netsvc.LocalService("workflow")
3082 wf_service.trg_create(user, self._name, id_new, cr)
3085 def _store_get_values(self, cr, uid, ids, fields, context):
3087 fncts = self.pool._store_function.get(self._name, [])
3088 for fnct in range(len(fncts)):
3093 for f in (fields or []):
3094 if f in fncts[fnct][3]:
3100 result.setdefault(fncts[fnct][0], {})
3102 # uid == 1 for accessing objects having rules defined on store fields
3103 ids2 = fncts[fnct][2](self,cr, 1, ids, context)
3104 for id in filter(None, ids2):
3105 result[fncts[fnct][0]].setdefault(id, [])
3106 result[fncts[fnct][0]][id].append(fnct)
3108 for object in result:
3110 for id,fnct in result[object].items():
3111 k2.setdefault(tuple(fnct), [])
3112 k2[tuple(fnct)].append(id)
3113 for fnct,id in k2.items():
3114 dict.setdefault(fncts[fnct[0]][4],[])
3115 dict[fncts[fnct[0]][4]].append((fncts[fnct[0]][4],object,id,map(lambda x: fncts[x][1], fnct)))
3123 def _store_set_values(self, cr, uid, ids, fields, context):
3126 if self._log_access:
3127 cr.execute('select id,write_date from '+self._table+' where id in ('+','.join(map(str, ids))+')')
3131 field_dict.setdefault(r[0], [])
3132 res_date = time.strptime((r[1])[:19], '%Y-%m-%d %H:%M:%S')
3133 write_date = datetime.datetime.fromtimestamp(time.mktime(res_date))
3134 for i in self.pool._store_function.get(self._name, []):
3136 up_write_date = write_date + datetime.timedelta(hours=i[5])
3137 if datetime.datetime.now() < up_write_date:
3139 field_dict[r[0]].append(i[1])
3145 if self._columns[f]._multi not in keys:
3146 keys.append(self._columns[f]._multi)
3147 todo.setdefault(self._columns[f]._multi, [])
3148 todo[self._columns[f]._multi].append(f)
3152 # uid == 1 for accessing objects having rules defined on store fields
3153 result = self._columns[val[0]].get(cr, self, ids, val, 1, context=context)
3154 for id,value in result.items():
3156 for f in value.keys():
3157 if f in field_dict[id]:
3164 if self._columns[v]._type in ('many2one', 'one2one'):
3166 value[v] = value[v][0]
3169 upd0.append('"'+v+'"='+self._columns[v]._symbol_set[0])
3170 upd1.append(self._columns[v]._symbol_set[1](value[v]))
3172 cr.execute('update "' + self._table + '" set ' + \
3173 string.join(upd0, ',') + ' where id = %s', upd1)
3177 # uid == 1 for accessing objects having rules defined on store fields
3178 result = self._columns[f].get(cr, self, ids, f, 1, context=context)
3179 for r in result.keys():
3181 if r in field_dict.keys():
3182 if f in field_dict[r]:
3184 for id,value in result.items():
3185 if self._columns[f]._type in ('many2one', 'one2one'):
3190 cr.execute('update "' + self._table + '" set ' + \
3191 '"'+f+'"='+self._columns[f]._symbol_set[0] + ' where id = %s', (self._columns[f]._symbol_set[1](value),id))
3197 def perm_write(self, cr, user, ids, fields, context=None):
3198 raise _('This method does not exist anymore')
3200 # TODO: ameliorer avec NULL
3201 def _where_calc(self, cr, user, args, active_test=True, context=None):
3205 # if the object has a field named 'active', filter out all inactive
3206 # records unless they were explicitely asked for
3207 if 'active' in self._columns and (active_test and context.get('active_test', True)):
3209 active_in_args = False
3211 if a[0] == 'active':
3212 active_in_args = True
3213 if not active_in_args:
3214 args.insert(0, ('active', '=', 1))
3216 args = [('active', '=', 1)]
3220 e = expression.expression(args)
3221 e.parse(cr, user, self, context)
3222 tables = e.get_tables()
3223 qu1, qu2 = e.to_sql()
3224 qu1 = qu1 and [qu1] or []
3226 qu1, qu2, tables = [], [], ['"%s"' % self._table]
3228 return (qu1, qu2, tables)
3230 def _check_qorder(self, word):
3231 if not regex_order.match(word):
3232 raise except_orm(_('AccessError'), _('Bad query.'))
3235 def search(self, cr, user, args, offset=0, limit=None, order=None,
3236 context=None, count=False):
3239 # compute the where, order by, limit and offset clauses
3240 (qu1, qu2, tables) = self._where_calc(cr, user, args, context=context)
3241 dom = self.pool.get('ir.rule').domain_get(cr, user, self._name, context=context)
3249 qu1 = ' where '+string.join(qu1, ' and ')
3254 order_by = self._order
3256 self._check_qorder(order)
3257 o = order.split(' ')[0]
3258 if (o in self._columns) and getattr(self._columns[o], '_classic_write'):
3261 limit_str = limit and ' limit %d' % limit or ''
3262 offset_str = offset and ' offset %d' % offset or ''
3266 cr.execute('select count(%s.id) from ' % self._table +
3267 ','.join(tables) +qu1 + limit_str + offset_str, qu2)
3270 cr.execute('select %s.id from ' % self._table + ','.join(tables) +qu1+' order by '+order_by+limit_str+offset_str, qu2)
3272 return [x[0] for x in res]
3274 # returns the different values ever entered for one field
3275 # this is used, for example, in the client when the user hits enter on
3277 def distinct_field_get(self, cr, uid, field, value, args=None, offset=0, limit=None):
3280 if field in self._inherit_fields:
3281 return self.pool.get(self._inherit_fields[field][0]).distinct_field_get(cr, uid, field, value, args, offset, limit)
3283 return self._columns[field].search(cr, self, args, field, value, offset, limit, uid)
3285 def name_get(self, cr, user, ids, context=None):
3290 if isinstance(ids, (int, long)):
3292 return [(r['id'], tools.ustr(r[self._rec_name])) for r in self.read(cr, user, ids,
3293 [self._rec_name], context, load='_classic_write')]
3295 def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100):
3302 args += [(self._rec_name, operator, name)]
3303 ids = self.search(cr, user, args, limit=limit, context=context)
3304 res = self.name_get(cr, user, ids, context)
3307 def copy_data(self, cr, uid, id, default=None, context=None):
3312 if 'state' not in default:
3313 if 'state' in self._defaults:
3314 if callable(self._defaults['state']):
3315 default['state'] = self._defaults['state'](self, cr, uid, context)
3317 default['state'] = self._defaults['state']
3319 data = self.read(cr, uid, [id], context=context)[0]
3320 fields = self.fields_get(cr, uid, context=context)
3323 ftype = fields[f]['type']
3325 if self._log_access and f in ('create_date', 'create_uid', 'write_date', 'write_uid'):
3329 data[f] = default[f]
3330 elif ftype == 'function':
3332 elif ftype == 'many2one':
3334 data[f] = data[f] and data[f][0]
3337 elif ftype in ('one2many', 'one2one'):
3339 rel = self.pool.get(fields[f]['relation'])
3340 if data[f] != False:
3341 for rel_id in data[f]:
3342 # the lines are first duplicated using the wrong (old)
3343 # parent but then are reassigned to the correct one thanks
3345 d,t = rel.copy_data(cr, uid, rel_id, context=context)
3346 res.append((0, 0, d))
3349 elif ftype == 'many2many':
3350 data[f] = [(6, 0, data[f])]
3352 trans_obj = self.pool.get('ir.translation')
3353 #TODO: optimize translations
3357 if f in self._columns and self._columns[f].translate:
3358 trans_name=self._name+","+f
3359 elif f in self._inherit_fields and self._inherit_fields[f][2].translate:
3360 trans_name=self._inherit_fields[f][0]+","+f
3365 trans_ids = trans_obj.search(cr, uid, [
3366 ('name', '=', trans_name),
3367 ('res_id','=',data['id'])
3370 trans_data.extend(trans_obj.read(cr,uid,trans_ids,context=context))
3374 for v in self._inherits:
3375 del data[self._inherits[v]]
3376 return data, trans_data
3378 def copy(self, cr, uid, id, default=None, context=None):
3379 trans_obj = self.pool.get('ir.translation')
3380 data, trans_data = self.copy_data(cr, uid, id, default, context)
3381 new_id = self.create(cr, uid, data, context)
3382 for record in trans_data:
3384 record['res_id'] = new_id
3385 trans_obj.create(cr, uid, record, context)
3388 def exists(self, cr, uid, id, context=None):
3389 cr.execute('SELECT count(1) FROM "%s" where id=%%s' % (self._table,), (id,))
3390 return bool(cr.fetchone()[0])
3392 def check_recursion(self, cr, uid, ids, parent=None):
3394 parent = self._parent_name
3396 while len(ids_parent):
3398 for i in range(0, len(ids), cr.IN_MAX):
3399 sub_ids_parent = ids_parent[i:i+cr.IN_MAX]
3400 cr.execute('SELECT distinct "'+parent+'"'+
3401 ' FROM "'+self._table+'" ' \
3402 'WHERE id = ANY(%s)',(sub_ids_parent,))
3403 ids_parent2.extend(filter(None, map(lambda x: x[0], cr.fetchall())))
3404 ids_parent = ids_parent2
3405 for i in ids_parent:
3410 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: