1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ##############################################################################
24 # Object relationnal mapping to postgresql module
25 # . Hierarchical structure
26 # . Constraints consistency, validations
27 # . Object meta Data depends on its status
28 # . Optimised processing by complex query (multiple actions at once)
29 # . Default fields value
30 # . Permissions optimisation
31 # . Persistant object: DB postgresql
33 # . Multi-level caching system
34 # . 2 different inheritancies
36 # - classicals (varchar, integer, boolean, ...)
37 # - relations (one2many, many2one, many2many)
57 from xml import dom, xpath
59 sys.stderr.write("ERROR: Import xpath module\n")
60 sys.stderr.write("ERROR: Try to install the old python-xml package\n")
61 sys.stderr.write('On Ubuntu Jaunty, try this: sudo cp /usr/lib/python2.6/dist-packages/oldxml/_xmlplus/utils/boolean.so /usr/lib/python2.5/site-packages/oldxml/_xmlplus/utils\n')
64 from tools.config import config
66 regex_order = re.compile('^([a-z0-9_]+( *desc| *asc)?( *, *|))+$', re.I)
68 def last_day_of_current_month():
71 today = datetime.date.today()
72 last_day = str(calendar.monthrange(today.year, today.month)[1])
73 return time.strftime('%Y-%m-' + last_day)
75 def intersect(la, lb):
76 return filter(lambda x: x in lb, la)
79 class except_orm(Exception):
80 def __init__(self, name, value):
83 self.args = (name, value)
85 class BrowseRecordError(Exception):
88 # Readonly python database object browser
89 class browse_null(object):
94 def __getitem__(self, name):
97 def __getattr__(self, name):
98 return False # XXX: return self ?
106 def __nonzero__(self):
109 def __unicode__(self):
114 # TODO: execute an object method on browse_record_list
116 class browse_record_list(list):
118 def __init__(self, lst, context=None):
121 super(browse_record_list, self).__init__(lst)
122 self.context = context
125 class browse_record(object):
126 def __init__(self, cr, uid, id, table, cache, context=None, list_class = None, fields_process={}):
128 table : the object (inherited from orm)
129 context : a dictionary with an optional context
133 self._list_class = list_class or browse_record_list
138 self._table_name = self._table._name
139 self._context = context
140 self._fields_process = fields_process
142 cache.setdefault(table._name, {})
143 self._data = cache[table._name]
145 if not (id and isinstance(id, (int, long,))):
146 raise BrowseRecordError(_('Wrong ID for the browse record, got %r, expected an integer.') % (id,))
147 # if not table.exists(cr, uid, id, context):
148 # raise BrowseRecordError(_('Object %s does not exists') % (self,))
150 if id not in self._data:
151 self._data[id] = {'id': id}
155 def __getitem__(self, name):
158 if name not in self._data[self._id]:
159 # build the list of fields we will fetch
161 # fetch the definition of the field which was asked for
162 if name in self._table._columns:
163 col = self._table._columns[name]
164 elif name in self._table._inherit_fields:
165 col = self._table._inherit_fields[name][2]
166 elif hasattr(self._table, name):
167 if isinstance(getattr(self._table, name), (types.MethodType, types.LambdaType, types.FunctionType)):
168 return lambda *args, **argv: getattr(self._table, name)(self._cr, self._uid, [self._id], *args, **argv)
170 return getattr(self._table, name)
172 logger = netsvc.Logger()
173 logger.notifyChannel('orm', netsvc.LOG_ERROR, "Programming error: field '%s' does not exist in object '%s' !" % (name, self._table._name))
176 # if the field is a classic one or a many2one, we'll fetch all classic and many2one fields
178 # gen the list of "local" (ie not inherited) fields which are classic or many2one
179 ffields = filter(lambda x: x[1]._classic_write, self._table._columns.items())
180 # gen the list of inherited fields
181 inherits = map(lambda x: (x[0], x[1][2]), self._table._inherit_fields.items())
182 # complete the field list with the inherited fields which are classic or many2one
183 ffields += filter(lambda x: x[1]._classic_write, inherits)
184 # otherwise we fetch only that field
186 ffields = [(name, col)]
187 ids = filter(lambda id: name not in self._data[id], self._data.keys())
189 fffields = map(lambda x: x[0], ffields)
190 datas = self._table.read(self._cr, self._uid, ids, fffields, context=self._context, load="_classic_write")
191 if self._fields_process:
192 lang = self._context.get('lang', 'en_US') or 'en_US'
193 lang_obj_ids = self.pool.get('res.lang').search(self._cr, self._uid,[('code','=',lang)])
195 raise Exception(_('Language with code "%s" is not defined in your system !\nDefine it through the Administration menu.') % (lang,))
196 lang_obj = self.pool.get('res.lang').browse(self._cr, self._uid,lang_obj_ids[0])
198 if f._type in self._fields_process:
200 d[n] = self._fields_process[f._type](d[n])
201 if (d[n] is not None) and (d[n] is not False):
202 d[n].set_value(self._cr, self._uid, d[n], self, f, lang_obj)
205 # create browse records for 'remote' objects
208 if f._type in ('many2one', 'one2one'):
210 obj = self._table.pool.get(f._obj)
212 if type(data[n]) in (type([]),type( (1,) )):
217 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)
219 data[n] = browse_null()
221 data[n] = browse_null()
222 elif f._type in ('one2many', 'many2many') and len(data[n]):
223 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)
224 self._data[data['id']].update(data)
225 return self._data[self._id][name]
227 def __getattr__(self, name):
228 # raise an AttributeError exception.
231 def __contains__(self, name):
232 return (name in self._table._columns) or (name in self._table._inherit_fields) or hasattr(self._table, name)
234 def __hasattr__(self, name):
241 return "browse_record(%s, %d)" % (self._table_name, self._id)
243 def __eq__(self, other):
244 return (self._table_name, self._id) == (other._table_name, other._id)
246 def __ne__(self, other):
247 return (self._table_name, self._id) != (other._table_name, other._id)
249 # we need to define __unicode__ even though we've already defined __str__
250 # because we have overridden __getattr__
251 def __unicode__(self):
252 return unicode(str(self))
255 return hash((self._table_name, self._id))
263 (type returned by postgres when the column was created, type expression to create the column)
267 fields.boolean: 'bool',
268 fields.integer: 'int4',
269 fields.integer_big: 'int8',
273 fields.datetime: 'timestamp',
274 fields.binary: 'bytea',
275 fields.many2one: 'int4',
277 if type(f) in type_dict:
278 f_type = (type_dict[type(f)], type_dict[type(f)])
279 elif isinstance(f, fields.float):
281 f_type = ('numeric', 'NUMERIC(%d,%d)' % (f.digits[0], f.digits[1]))
283 f_type = ('float8', 'DOUBLE PRECISION')
284 elif isinstance(f, (fields.char, fields.reference)):
285 f_type = ('varchar', 'VARCHAR(%d)' % (f.size,))
286 elif isinstance(f, fields.selection):
287 if isinstance(f.selection, list) and isinstance(f.selection[0][0], (str, unicode)):
288 f_size = reduce(lambda x, y: max(x, len(y[0])), f.selection, f.size or 16)
289 elif isinstance(f.selection, list) and isinstance(f.selection[0][0], int):
292 f_size = (hasattr(f, 'size') and f.size) or 16
295 f_type = ('int4', 'INTEGER')
297 f_type = ('varchar', 'VARCHAR(%d)' % f_size)
298 elif isinstance(f, fields.function) and eval('fields.'+(f._type)) in type_dict:
299 t = eval('fields.'+(f._type))
300 f_type = (type_dict[t], type_dict[t])
301 elif isinstance(f, fields.function) and f._type == 'float':
303 f_type = ('numeric', 'NUMERIC(%d,%d)' % (f.digits[0], f.digits[1]))
305 f_type = ('float8', 'DOUBLE PRECISION')
306 elif isinstance(f, fields.function) and f._type == 'selection':
307 f_type = ('text', 'text')
308 elif isinstance(f, fields.function) and f._type == 'char':
309 f_type = ('varchar', 'VARCHAR(%d)' % (f.size))
311 logger = netsvc.Logger()
312 logger.notifyChannel("init", netsvc.LOG_WARNING, '%s type not supported!' % (type(f)))
317 class orm_template(object):
323 _parent_name = 'parent_id'
324 _parent_store = False
325 _parent_order = False
334 CONCURRENCY_CHECK_FIELD = '__last_update'
336 def _field_create(self, cr, context={}):
337 cr.execute("SELECT id FROM ir_model WHERE model=%s", (self._name,))
339 cr.execute('SELECT nextval(%s)', ('ir_model_id_seq',))
340 model_id = cr.fetchone()[0]
341 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'))
343 model_id = cr.fetchone()[0]
344 if 'module' in context:
345 name_id = 'model_'+self._name.replace('.','_')
346 cr.execute('select * from ir_model_data where name=%s and res_id=%s', (name_id,model_id))
348 cr.execute("INSERT INTO ir_model_data (name,date_init,date_update,module,model,res_id) VALUES (%s, now(), now(), %s, %s, %s)", \
349 (name_id, context['module'], 'ir.model', model_id)
354 cr.execute("SELECT * FROM ir_model_fields WHERE model=%s", (self._name,))
356 for rec in cr.dictfetchall():
357 cols[rec['name']] = rec
359 for (k, f) in self._columns.items():
361 'model_id': model_id,
364 'field_description': f.string.replace("'", " "),
366 'relation': f._obj or 'NULL',
367 'view_load': (f.view_load and 1) or 0,
368 'select_level': str(f.select or 0),
369 'readonly':(f.readonly and 1) or 0,
370 'required':(f.required and 1) or 0,
373 cr.execute('select nextval(%s)', ('ir_model_fields_id_seq',))
374 id = cr.fetchone()[0]
376 cr.execute("""INSERT INTO ir_model_fields (
377 id, model_id, model, name, field_description, ttype,
378 relation,view_load,state,select_level
380 %s,%s,%s,%s,%s,%s,%s,%s,%s,%s
382 id, vals['model_id'], vals['model'], vals['name'], vals['field_description'], vals['ttype'],
383 vals['relation'], bool(vals['view_load']), 'base',
386 if 'module' in context:
387 name1 = 'field_' + self._table + '_' + k
388 cr.execute("select name from ir_model_data where name=%s", (name1,))
390 name1 = name1 + "_" + str(id)
391 cr.execute("INSERT INTO ir_model_data (name,date_init,date_update,module,model,res_id) VALUES (%s, now(), now(), %s, %s, %s)", \
392 (name1, context['module'], 'ir.model.fields', id)
395 for key, val in vals.items():
396 if cols[k][key] != vals[key]:
397 cr.execute('update ir_model_fields set field_description=%s where model=%s and name=%s', (vals['field_description'], vals['model'], vals['name']))
399 cr.execute("""UPDATE ir_model_fields SET
400 model_id=%s, field_description=%s, ttype=%s, relation=%s,
401 view_load=%s, select_level=%s, readonly=%s ,required=%s
403 model=%s AND name=%s""", (
404 vals['model_id'], vals['field_description'], vals['ttype'],
405 vals['relation'], bool(vals['view_load']),
406 vals['select_level'], bool(vals['readonly']),bool(vals['required']), vals['model'], vals['name']
411 def _auto_init(self, cr, context={}):
412 self._field_create(cr, context)
414 def __init__(self, cr):
415 if not self._name and not hasattr(self, '_inherit'):
416 name = type(self).__name__.split('.')[0]
417 msg = "The class %s has to have a _name attribute" % name
419 logger = netsvc.Logger()
420 logger.notifyChannel('orm', netsvc.LOG_ERROR, msg )
421 raise except_orm('ValueError', msg )
423 if not self._description:
424 self._description = self._name
426 self._table = self._name.replace('.', '_')
428 def browse(self, cr, uid, select, context=None, list_class=None, fields_process={}):
431 self._list_class = list_class or browse_record_list
433 # need to accepts ints and longs because ids coming from a method
434 # launched by button in the interface have a type long...
435 if isinstance(select, (int, long)):
436 return browse_record(cr, uid, select, self, cache, context=context, list_class=self._list_class, fields_process=fields_process)
437 elif isinstance(select, list):
438 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)
442 def __export_row(self, cr, uid, row, fields, context=None):
444 def check_type(field_type):
445 if field_type == 'float':
447 elif field_type == 'integer':
449 elif field_type == 'boolean':
454 data = map(lambda x: '', range(len(fields)))
456 for fpos in range(len(fields)):
465 model_data = self.pool.get('ir.model.data')
466 data_ids = model_data.search(cr, uid, [('model','=',r._table_name),('res_id','=',r['id'])])
468 d = model_data.read(cr, uid, data_ids, ['name','module'])[0]
470 r = '%s.%s'%(d['module'],d['name'])
478 if f[i] in self._columns:
479 r = check_type(self._columns[f[i]]._type)
480 elif f[i] in self._inherit_fields:
481 r = check_type(self._inherit_fields[f[i]][2]._type)
484 if isinstance(r, (browse_record_list, list)):
486 fields2 = map(lambda x: (x[:i+1]==f[:i+1] and x[i+1:]) \
492 lines2 = self.__export_row(cr, uid, row2, fields2,
495 for fpos2 in range(len(fields)):
496 if lines2 and lines2[0][fpos2]:
497 data[fpos2] = lines2[0][fpos2]
501 if isinstance(rr.name, browse_record):
503 dt += rr.name or '' + ','
513 if isinstance(r, browse_record):
515 data[fpos] = tools.ustr(r or '')
516 return [data] + lines
518 def export_data(self, cr, uid, ids, fields_to_export, context=None):
521 imp_comp = context.get('import_comp',False)
522 cols = self._columns.copy()
523 for f in self._inherit_fields:
524 cols.update({f: self._inherit_fields[f][2]})
525 fields_to_export = map(lambda x: x.split('/'), fields_to_export)
526 fields_export = fields_to_export+[]
529 for field in fields_export:
530 if imp_comp and len(field)>1:
531 warning_fields.append('/'.join(map(lambda x:x in cols and cols[x].string or x,field)))
532 elif len (field) <=1:
533 if imp_comp and cols.get(field and field[0],False):
534 if ((isinstance(cols[field[0]], fields.function) and not cols[field[0]].store) \
535 or isinstance(cols[field[0]], fields.related)\
536 or isinstance(cols[field[0]], fields.one2many)):
537 warning_fields.append('/'.join(map(lambda x:x in cols and cols[x].string or x,field)))
539 if imp_comp and len(warning_fields):
540 warning = 'Following columns cannot be exported since you select to be import compatible.\n%s' %('\n'.join(warning_fields))
542 return {'warning' : warning}
543 for row in self.browse(cr, uid, ids, context):
544 datas += self.__export_row(cr, uid, row, fields_to_export, context)
545 return {'datas':datas}
547 def import_data(self, cr, uid, fields, datas, mode='init', current_module='', noupdate=False, context=None, filename=None):
550 fields = map(lambda x: x.split('/'), fields)
551 logger = netsvc.Logger()
552 ir_model_data_obj = self.pool.get('ir.model.data')
554 def _check_db_id(self, model_name, db_id):
555 obj_model = self.pool.get(model_name)
556 ids = obj_model.search(cr, uid, [('id','=',int(db_id))])
558 raise Exception(_("Database ID doesn't exist: %s : %s") %(model_name, db_id))
561 def process_liness(self, datas, prefix, current_module, model_name, fields_def, position=0):
562 line = datas[position]
571 ir_model_data_obj = self.pool.get('ir.model.data')
573 # Import normal fields
575 for i in range(len(fields)):
577 raise Exception(_('Please check that all your lines have %d columns.') % (len(fields),))
581 if (len(field)==len(prefix)+1) and field[len(prefix)].endswith(':db_id'):
585 field_name = field[0].split(':')[0]
586 model_rel = fields_def[field_name]['relation']
588 if fields_def[field[len(prefix)][:-6]]['type']=='many2many':
590 for db_id in line[i].split(config.get('csv_internal_sep')):
592 _check_db_id(self, model_rel, db_id)
595 warning += [tools.exception_to_unicode(e)]
596 logger.notifyChannel("import", netsvc.LOG_ERROR,
597 tools.exception_to_unicode(e))
599 res = [(6, 0, res_id)]
602 _check_db_id(self, model_rel, line[i])
605 warning += [tools.exception_to_unicode(e)]
606 logger.notifyChannel("import", netsvc.LOG_ERROR,
607 tools.exception_to_unicode(e))
608 row[field_name] = res or False
611 if (len(field)==len(prefix)+1) and field[len(prefix)].endswith(':id'):
614 if fields_def[field[len(prefix)][:-3]]['type']=='many2many':
616 for word in line[i].split(config.get('csv_internal_sep')):
618 module, xml_id = word.rsplit('.', 1)
620 module, xml_id = current_module, word
621 id = ir_model_data_obj._get_id(cr, uid, module,
623 res_id2 = ir_model_data_obj.read(cr, uid, [id],
624 ['res_id'])[0]['res_id']
626 res_id.append(res_id2)
628 res_id = [(6, 0, res_id)]
631 module, xml_id = line[i].rsplit('.', 1)
633 module, xml_id = current_module, line[i]
634 id = ir_model_data_obj._get_id(cr, uid, module, xml_id)
635 res_id = ir_model_data_obj.read(cr, uid, [id],
636 ['res_id'])[0]['res_id']
637 row[field[0][:-3]] = res_id or False
639 if (len(field) == len(prefix)+1) and \
640 len(field[len(prefix)].split(':lang=')) == 2:
641 f, lang = field[len(prefix)].split(':lang=')
642 translate.setdefault(lang, {})[f]=line[i] or False
644 if (len(field) == len(prefix)+1) and \
645 (prefix == field[0:len(prefix)]):
646 if field[len(prefix)] == "id":
649 is_xml_id = data_id = line[i]
650 d = data_id.split('.')
651 module = len(d)>1 and d[0] or ''
652 name = len(d)>1 and d[1] or d[0]
653 data_ids = ir_model_data_obj.search(cr, uid, [('module','=',module),('model','=',model_name),('name','=',name)])
655 d = ir_model_data_obj.read(cr, uid, data_ids, ['res_id'])[0]
657 if is_db_id and not db_id:
658 data_ids = ir_model_data_obj.search(cr, uid, [('module','=',module),('model','=',model_name),('res_id','=',is_db_id)])
659 if not len(data_ids):
660 ir_model_data_obj.create(cr, uid, {'module':module, 'model':model_name, 'name':name, 'res_id':is_db_id})
662 if is_db_id and int(db_id) != int(is_db_id):
663 warning += [_("Id is not the same than existing one: %s")%(is_db_id)]
664 logger.notifyChannel("import", netsvc.LOG_ERROR,
665 _("Id is not the same than existing one: %s")%(is_db_id))
668 if field[len(prefix)] == "db_id":
671 _check_db_id(self, model_name, line[i])
672 data_res_id = is_db_id = int(line[i])
674 warning += [tools.exception_to_unicode(e)]
675 logger.notifyChannel("import", netsvc.LOG_ERROR,
676 tools.exception_to_unicode(e))
678 data_ids = ir_model_data_obj.search(cr, uid, [('model','=',model_name),('res_id','=',line[i])])
680 d = ir_model_data_obj.read(cr, uid, data_ids, ['name','module'])[0]
683 data_id = '%s.%s'%(d['module'],d['name'])
686 if is_xml_id and not data_id:
688 if is_xml_id and is_xml_id!=data_id:
689 warning += [_("Id is not the same than existing one: %s")%(line[i])]
690 logger.notifyChannel("import", netsvc.LOG_ERROR,
691 _("Id is not the same than existing one: %s")%(line[i]))
694 if fields_def[field[len(prefix)]]['type'] == 'integer':
695 res = line[i] and int(line[i])
696 elif fields_def[field[len(prefix)]]['type'] == 'boolean':
697 res = line[i].lower() not in ('0', 'false', 'off')
698 elif fields_def[field[len(prefix)]]['type'] == 'float':
699 res = line[i] and float(line[i])
700 elif fields_def[field[len(prefix)]]['type'] == 'selection':
702 if isinstance(fields_def[field[len(prefix)]]['selection'],
704 sel = fields_def[field[len(prefix)]]['selection']
706 sel = fields_def[field[len(prefix)]]['selection'](self,
709 if line[i] in [tools.ustr(key),tools.ustr(val)]: #Acepting key or value for selection field
712 if line[i] and not res:
713 logger.notifyChannel("import", netsvc.LOG_WARNING,
714 _("key '%s' not found in selection field '%s'") % \
715 (line[i], field[len(prefix)]))
717 warning += [_("Key/value '%s' not found in selection field '%s'")%(line[i],field[len(prefix)])]
719 elif fields_def[field[len(prefix)]]['type']=='many2one':
722 relation = fields_def[field[len(prefix)]]['relation']
723 res2 = self.pool.get(relation).name_search(cr, uid,
724 line[i], [], operator='=', context=context)
725 res = (res2 and res2[0][0]) or False
727 warning += [_("Relation not found: %s on '%s'")%(line[i],relation)]
728 logger.notifyChannel("import", netsvc.LOG_WARNING,
729 _("Relation not found: %s on '%s'")%(line[i],relation))
730 elif fields_def[field[len(prefix)]]['type']=='many2many':
733 relation = fields_def[field[len(prefix)]]['relation']
734 for word in line[i].split(config.get('csv_internal_sep')):
735 res2 = self.pool.get(relation).name_search(cr,
736 uid, word, [], operator='=', context=context)
737 res3 = (res2 and res2[0][0]) or False
739 warning += [_("Relation not found: %s on '%s'")%(line[i],relation)]
740 logger.notifyChannel("import",
742 _("Relation not found: %s on '%s'")%(line[i],relation))
748 res = line[i] or False
749 row[field[len(prefix)]] = res
750 elif (prefix==field[0:len(prefix)]):
751 if field[0] not in todo:
752 todo.append(field[len(prefix)])
754 # Import one2many, many2many fields
758 relation_obj = self.pool.get(fields_def[field]['relation'])
759 newfd = relation_obj.fields_get(
760 cr, uid, context=context)
761 res = process_liness(self, datas, prefix + [field], current_module, relation_obj._name, newfd, position)
762 (newrow, max2, w2, translate2, data_id2, data_res_id2) = res
763 nbrmax = max(nbrmax, max2)
764 warning = warning + w2
765 reduce(lambda x, y: x and y, newrow)
766 row[field] = (reduce(lambda x, y: x or y, newrow.values()) and \
767 [(0, 0, newrow)]) or []
769 while (position+i)<len(datas):
771 for j in range(len(fields)):
773 if (len(field2) <= (len(prefix)+1)) and datas[position+i][j]:
778 (newrow, max2, w2, translate2, data_id2, data_res_id2) = process_liness(
779 self, datas, prefix+[field], current_module, relation_obj._name, newfd, position+i)
781 if reduce(lambda x, y: x or y, newrow.values()):
782 row[field].append((0, 0, newrow))
784 nbrmax = max(nbrmax, i)
787 for i in range(max(nbrmax, 1)):
790 result = (row, nbrmax, warning, translate, data_id, data_res_id)
793 fields_def = self.fields_get(cr, uid, context=context)
796 initial_size = len(datas)
797 if config.get('import_partial', False) and filename:
798 data = pickle.load(file(config.get('import_partial')))
799 original_value = data.get(filename, 0)
805 (res, other, warning, translate, data_id, res_id) = \
806 process_liness(self, datas, [], current_module, self._name, fields_def)
809 return (-1, res, 'Line ' + str(counter) +' : ' + '!\n'.join(warning), '')
812 id = ir_model_data_obj._update(cr, uid, self._name,
813 current_module, res, xml_id=data_id, mode=mode,
814 noupdate=noupdate, res_id=res_id)
817 if isinstance(e,psycopg2.IntegrityError):
818 msg= _('Insertion Failed!')
819 for key in self.pool._sql_error.keys():
821 msg = self.pool._sql_error[key]
823 return (-1, res,'Line ' + str(counter) +' : ' + msg,'' )
825 for lang in translate:
826 context2 = context.copy()
827 context2['lang'] = lang
828 self.write(cr, uid, [id], translate[lang], context2)
829 if config.get('import_partial', False) and filename and (not (counter%100)) :
830 data = pickle.load(file(config.get('import_partial')))
831 data[filename] = initial_size - len(datas) + original_value
832 pickle.dump(data, file(config.get('import_partial'),'wb'))
835 #except Exception, e:
836 # logger.notifyChannel("import", netsvc.LOG_ERROR, e)
839 # return (-1, res, e[0], warning)
841 # return (-1, res, e[0], '')
844 # TODO: Send a request with the result and multi-thread !
846 return (done, 0, 0, 0)
848 def read(self, cr, user, ids, fields=None, context=None, load='_classic_read'):
849 raise _('The read method is not implemented on this object !')
851 def get_invalid_fields(self,cr,uid):
852 return list(self._invalids)
854 def _validate(self, cr, uid, ids, context=None):
855 context = context or {}
856 lng = context.get('lang', False) or 'en_US'
857 trans = self.pool.get('ir.translation')
859 for constraint in self._constraints:
860 fun, msg, fields = constraint
861 if not fun(self, cr, uid, ids):
862 translated_msg = trans._get_source(cr, uid, self._name, 'constraint', lng, source=msg) or msg
864 _("Error occurred while validating the field(s) %s: %s") % (','.join(fields), translated_msg)
866 self._invalids.update(fields)
869 raise except_orm('ValidateError', '\n'.join(error_msgs))
871 self._invalids.clear()
873 def default_get(self, cr, uid, fields_list, context=None):
876 def perm_read(self, cr, user, ids, context=None, details=True):
877 raise _('The perm_read method is not implemented on this object !')
879 def unlink(self, cr, uid, ids, context=None):
880 raise _('The unlink method is not implemented on this object !')
882 def write(self, cr, user, ids, vals, context=None):
883 raise _('The write method is not implemented on this object !')
885 def create(self, cr, user, vals, context=None):
886 raise _('The create method is not implemented on this object !')
888 # returns the definition of each field in the object
889 # the optional fields parameter can limit the result to some fields
890 def fields_get_keys(self, cr, user, context=None, read_access=True):
893 res = self._columns.keys()
894 for parent in self._inherits:
895 res.extend(self.pool.get(parent).fields_get_keys(cr, user, fields, context))
898 def fields_get(self, cr, user, fields=None, context=None, read_access=True):
902 translation_obj = self.pool.get('ir.translation')
903 model_access_obj = self.pool.get('ir.model.access')
904 for parent in self._inherits:
905 res.update(self.pool.get(parent).fields_get(cr, user, fields, context))
907 if self._columns.keys():
908 for f in self._columns.keys():
909 if fields and f not in fields:
911 res[f] = {'type': self._columns[f]._type}
912 for arg in ('string', 'readonly', 'states', 'size', 'required',
913 'change_default', 'translate', 'help', 'select'):
914 if getattr(self._columns[f], arg):
915 res[f][arg] = getattr(self._columns[f], arg)
917 res[f]['readonly'] = True
918 res[f]['states'] = {}
919 for arg in ('digits', 'invisible','filters'):
920 if hasattr(self._columns[f], arg) \
921 and getattr(self._columns[f], arg):
922 res[f][arg] = getattr(self._columns[f], arg)
924 res_trans = translation_obj._get_source(cr, user, self._name + ',' + f, 'field', context.get('lang', False) or 'en_US')
926 res[f]['string'] = res_trans
927 help_trans = translation_obj._get_source(cr, user, self._name + ',' + f, 'help', context.get('lang', False) or 'en_US')
929 res[f]['help'] = help_trans
931 if hasattr(self._columns[f], 'selection'):
932 if isinstance(self._columns[f].selection, (tuple, list)):
933 sel = self._columns[f].selection
934 # translate each selection option
936 for (key, val) in sel:
939 val2 = translation_obj._get_source(cr, user, self._name + ',' + f, 'selection', context.get('lang', False) or 'en_US', val)
940 sel2.append((key, val2 or val))
942 res[f]['selection'] = sel
944 # call the 'dynamic selection' function
945 res[f]['selection'] = self._columns[f].selection(self, cr,
947 if res[f]['type'] in ('one2many', 'many2many', 'many2one', 'one2one'):
948 res[f]['relation'] = self._columns[f]._obj
949 res[f]['domain'] = self._columns[f]._domain
950 res[f]['context'] = self._columns[f]._context
952 #TODO : read the fields from the database
956 # filter out fields which aren't in the fields list
963 # Overload this method if you need a window title which depends on the context
965 def view_header_get(self, cr, user, view_id=None, view_type='form', context=None):
968 def __view_look_dom(self, cr, user, node, view_id, context=None):
975 if node.nodeType == node.ELEMENT_NODE and node.localName == 'field':
976 if node.hasAttribute('name'):
979 if node.getAttribute('name') in self._columns:
980 column = self._columns[node.getAttribute('name')]
982 column = self._inherit_fields[node.getAttribute('name')][2]
987 relation = column._obj
990 for f in node.childNodes:
991 if f.nodeType == f.ELEMENT_NODE and f.localName in ('form', 'tree', 'graph'):
994 ctx['base_model_name'] = self._name
995 xarch, xfields = self.pool.get(relation).__view_look_dom_arch(cr, user, f, view_id, ctx)
996 views[str(f.localName)] = {
1000 attrs = {'views': views}
1001 if node.hasAttribute('widget') and node.getAttribute('widget')=='selection':
1002 # We can not use the 'string' domain has it is defined according to the record !
1004 if column._domain and not isinstance(column._domain, (str, unicode)):
1005 dom = column._domain
1007 attrs['selection'] = self.pool.get(relation).name_search(cr, user, '', dom, context=context)
1008 if (node.hasAttribute('required') and not int(node.getAttribute('required'))) or not column.required:
1009 attrs['selection'].append((False,''))
1010 fields[node.getAttribute('name')] = attrs
1012 elif node.nodeType==node.ELEMENT_NODE and node.localName in ('form', 'tree'):
1013 result = self.view_header_get(cr, user, False, node.localName, context)
1015 node.setAttribute('string', result)
1017 elif node.nodeType==node.ELEMENT_NODE and node.localName == 'calendar':
1018 for additional_field in ('date_start', 'date_delay', 'date_stop', 'color'):
1019 if node.hasAttribute(additional_field) and node.getAttribute(additional_field):
1020 fields[node.getAttribute(additional_field)] = {}
1022 if node.nodeType == node.ELEMENT_NODE and node.hasAttribute('groups'):
1023 if node.getAttribute('groups'):
1024 groups = node.getAttribute('groups').split(',')
1026 access_pool = self.pool.get('ir.model.access')
1027 for group in groups:
1028 readonly = readonly or access_pool.check_groups(cr, user, group)
1030 node.setAttribute('invisible', '1')
1031 node.removeAttribute('groups')
1033 if node.nodeType == node.ELEMENT_NODE:
1035 if ('lang' in context) and not result:
1036 if node.hasAttribute('string') and node.getAttribute('string'):
1037 trans = self.pool.get('ir.translation')._get_source(cr, user, self._name, 'view', context['lang'], node.getAttribute('string').encode('utf8'))
1038 if not trans and ('base_model_name' in context):
1039 trans = self.pool.get('ir.translation')._get_source(cr, user, context['base_model_name'], 'view', context['lang'], node.getAttribute('string').encode('utf8'))
1041 node.setAttribute('string', trans)
1042 if node.hasAttribute('sum') and node.getAttribute('sum'):
1043 trans = self.pool.get('ir.translation')._get_source(cr, user, self._name, 'view', context['lang'], node.getAttribute('sum').encode('utf8'))
1045 node.setAttribute('sum', trans)
1048 for f in node.childNodes:
1049 fields.update(self.__view_look_dom(cr, user, f, view_id, context))
1053 def __view_look_dom_arch(self, cr, user, node, view_id, context=None):
1054 fields_def = self.__view_look_dom(cr, user, node, view_id, context=context)
1056 rolesobj = self.pool.get('res.roles')
1057 usersobj = self.pool.get('res.users')
1059 buttons = (n for n in node.getElementsByTagName('button') if n.getAttribute('type') != 'object')
1060 for button in buttons:
1062 if user != 1: # admin user has all roles
1063 user_roles = usersobj.read(cr, user, [user], ['roles_id'])[0]['roles_id']
1064 cr.execute("select role_id from wkf_transition where signal=%s", (button.getAttribute('name'),))
1065 roles = cr.fetchall()
1068 ok = ok and rolesobj.check(cr, user, user_roles, role[0])
1071 button.setAttribute('readonly', '1')
1073 button.setAttribute('readonly', '0')
1075 arch = node.toxml(encoding="utf-8").replace('\t', '')
1076 fields = self.fields_get(cr, user, fields_def.keys(), context)
1077 for field in fields_def:
1079 # sometime, the view may containt the (invisible) field 'id' needed for a domain (when 2 objects have cross references)
1080 fields['id'] = {'readonly': True, 'type': 'integer', 'string': 'ID'}
1081 elif field in fields:
1082 fields[field].update(fields_def[field])
1084 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))
1085 res = cr.fetchall()[:]
1087 res.insert(0, ("Can't find field '%s' in the following view parts composing the view of object model '%s':" % (field, model), None))
1088 msg = "\n * ".join([r[0] for r in res])
1089 msg += "\n\nEither you wrongly customised this view, or some modules bringing those views are not compatible with your current data model"
1090 netsvc.Logger().notifyChannel('orm', netsvc.LOG_ERROR, msg)
1091 raise except_orm('View error', msg)
1095 def __get_default_calendar_view(self):
1096 """Generate a default calendar view (For internal use only).
1099 arch = ('<?xml version="1.0" encoding="utf-8"?>\n'
1100 '<calendar string="%s" date_start="%s"') % (self._description, self._date_name)
1102 if 'user_id' in self._columns:
1103 arch += ' color="user_id"'
1105 elif 'partner_id' in self._columns:
1106 arch += ' color="partner_id"'
1108 if 'date_stop' in self._columns:
1109 arch += ' date_stop="date_stop"'
1111 elif 'date_end' in self._columns:
1112 arch += ' date_stop="date_end"'
1114 elif 'date_delay' in self._columns:
1115 arch += ' date_delay="date_delay"'
1117 elif 'planned_hours' in self._columns:
1118 arch += ' date_delay="planned_hours"'
1121 ' <field name="%s"/>\n'
1122 '</calendar>') % (self._rec_name)
1127 # if view_id, view_type is not required
1129 def fields_view_get(self, cr, user, view_id=None, view_type='form', context=None, toolbar=False):
1134 if isinstance(s, unicode):
1135 return s.encode('utf8')
1138 def _inherit_apply(src, inherit):
1139 def _find(node, node2):
1140 if node2.nodeType == node2.ELEMENT_NODE and node2.localName == 'xpath':
1141 res = xpath.Evaluate(node2.getAttribute('expr'), node)
1142 return res and res[0]
1144 if node.nodeType == node.ELEMENT_NODE and node.localName == node2.localName:
1146 for attr in node2.attributes.keys():
1147 if attr == 'position':
1149 if node.hasAttribute(attr):
1150 if node.getAttribute(attr)==node2.getAttribute(attr):
1155 for child in node.childNodes:
1156 res = _find(child, node2)
1162 doc_src = dom.minidom.parseString(encode(src))
1163 doc_dest = dom.minidom.parseString(encode(inherit))
1164 toparse = doc_dest.childNodes
1166 node2 = toparse.pop(0)
1167 if not node2.nodeType == node2.ELEMENT_NODE:
1169 if node2.localName == 'data':
1170 toparse += node2.childNodes
1172 node = _find(doc_src, node2)
1175 if node2.hasAttribute('position'):
1176 pos = node2.getAttribute('position')
1177 if pos == 'replace':
1178 parent = node.parentNode
1179 for child in node2.childNodes:
1180 if child.nodeType == child.ELEMENT_NODE:
1181 parent.insertBefore(child, node)
1182 parent.removeChild(node)
1184 sib = node.nextSibling
1185 for child in node2.childNodes:
1186 if child.nodeType == child.ELEMENT_NODE:
1188 node.appendChild(child)
1189 elif pos == 'after':
1190 node.parentNode.insertBefore(child, sib)
1192 node.parentNode.insertBefore(child, node)
1194 raise AttributeError(_('Unknown position in inherited view %s !') % pos)
1197 ' %s="%s"' % (attr, node2.getAttribute(attr))
1198 for attr in node2.attributes.keys()
1199 if attr != 'position'
1201 tag = "<%s%s>" % (node2.localName, attrs)
1202 raise AttributeError(_("Couldn't find tag '%s' in parent view !") % tag)
1203 return doc_src.toxml(encoding="utf-8").replace('\t', '')
1205 result = {'type': view_type, 'model': self._name}
1212 where = (model and (" and model='%s'" % (self._name,))) or ''
1213 cr.execute('SELECT arch,name,field_parent,id,type,inherit_id FROM ir_ui_view WHERE id=%s'+where, (view_id,))
1215 cr.execute('''SELECT
1216 arch,name,field_parent,id,type,inherit_id
1223 ORDER BY priority''', (self._name, view_type))
1224 sql_res = cr.fetchone()
1228 view_id = ok or sql_res[3]
1231 # if a view was found
1233 result['type'] = sql_res[4]
1234 result['view_id'] = sql_res[3]
1235 result['arch'] = sql_res[0]
1237 def _inherit_apply_rec(result, inherit_id):
1238 # get all views which inherit from (ie modify) this view
1239 cr.execute('select arch,id from ir_ui_view where inherit_id=%s and model=%s order by priority', (inherit_id, self._name))
1240 sql_inherit = cr.fetchall()
1241 for (inherit, id) in sql_inherit:
1242 result = _inherit_apply(result, inherit)
1243 result = _inherit_apply_rec(result, id)
1246 result['arch'] = _inherit_apply_rec(result['arch'], sql_res[3])
1248 result['name'] = sql_res[1]
1249 result['field_parent'] = sql_res[2] or False
1251 # otherwise, build some kind of default view
1252 if view_type == 'form':
1253 res = self.fields_get(cr, user, context=context)
1254 xml = '''<?xml version="1.0" encoding="utf-8"?>''' \
1255 '''<form string="%s">''' % (self._description,)
1257 if res[x]['type'] not in ('one2many', 'many2many'):
1258 xml += '<field name="%s"/>' % (x,)
1259 if res[x]['type'] == 'text':
1262 elif view_type == 'tree':
1263 _rec_name = self._rec_name
1264 if _rec_name not in self._columns:
1265 _rec_name = self._columns.keys()[0]
1266 xml = '''<?xml version="1.0" encoding="utf-8"?>''' \
1267 '''<tree string="%s"><field name="%s"/></tree>''' \
1268 % (self._description, self._rec_name)
1269 elif view_type == 'calendar':
1270 xml = self.__get_default_calendar_view()
1273 result['arch'] = xml
1274 result['name'] = 'default'
1275 result['field_parent'] = False
1276 result['view_id'] = 0
1278 doc = dom.minidom.parseString(encode(result['arch']))
1279 xarch, xfields = self.__view_look_dom_arch(cr, user, doc, view_id, context=context)
1280 result['arch'] = xarch
1281 result['fields'] = xfields
1285 for key in ('report_sxw_content', 'report_rml_content',
1286 'report_sxw', 'report_rml',
1287 'report_sxw_content_data', 'report_rml_content_data'):
1291 ir_values_obj = self.pool.get('ir.values')
1292 resprint = ir_values_obj.get(cr, user, 'action',
1293 'client_print_multi', [(self._name, False)], False,
1295 resaction = ir_values_obj.get(cr, user, 'action',
1296 'client_action_multi', [(self._name, False)], False,
1299 resrelate = ir_values_obj.get(cr, user, 'action',
1300 'client_action_relate', [(self._name, False)], False,
1302 resprint = map(clean, resprint)
1303 resaction = map(clean, resaction)
1304 resaction = filter(lambda x: not x.get('multi', False), resaction)
1305 resprint = filter(lambda x: not x.get('multi', False), resprint)
1306 resrelate = map(lambda x: x[2], resrelate)
1308 for x in resprint+resaction+resrelate:
1309 x['string'] = x['name']
1311 result['toolbar'] = {
1313 'action': resaction,
1318 _view_look_dom_arch = __view_look_dom_arch
1320 def search_count(self, cr, user, args, context=None):
1323 res = self.search(cr, user, args, context=context, count=True)
1324 if isinstance(res, list):
1328 def search(self, cr, user, args, offset=0, limit=None, order=None,
1329 context=None, count=False):
1330 raise _('The search method is not implemented on this object !')
1332 def name_get(self, cr, user, ids, context=None):
1333 raise _('The name_get method is not implemented on this object !')
1335 def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=None):
1336 raise _('The name_search method is not implemented on this object !')
1338 def copy(self, cr, uid, id, default=None, context=None):
1339 raise _('The copy method is not implemented on this object !')
1341 def exists(self, cr, uid, id, context=None):
1342 raise _('The exists method is not implemented on this object !')
1344 def read_string(self, cr, uid, id, langs, fields=None, context=None):
1347 self.pool.get('ir.model.access').check(cr, uid, 'ir.translation', 'read', context=context)
1349 fields = self._columns.keys() + self._inherit_fields.keys()
1351 res[lang] = {'code': lang}
1353 if f in self._columns:
1354 res_trans = self.pool.get('ir.translation')._get_source(cr, uid, self._name+','+f, 'field', lang)
1356 res[lang][f] = res_trans
1358 res[lang][f] = self._columns[f].string
1359 for table in self._inherits:
1360 cols = intersect(self._inherit_fields.keys(), fields)
1361 res2 = self.pool.get(table).read_string(cr, uid, id, langs, cols, context)
1364 res[lang]['code'] = lang
1365 for f in res2[lang]:
1366 res[lang][f] = res2[lang][f]
1369 def write_string(self, cr, uid, id, langs, vals, context=None):
1370 self.pool.get('ir.model.access').check(cr, uid, 'ir.translation', 'write', context=context)
1373 if field in self._columns:
1374 self.pool.get('ir.translation')._set_ids(cr, uid, self._name+','+field, 'field', lang, [0], vals[field])
1375 for table in self._inherits:
1376 cols = intersect(self._inherit_fields.keys(), vals)
1378 self.pool.get(table).write_string(cr, uid, id, langs, vals, context)
1381 def _check_removed_columns(self, cr, log=False):
1382 raise NotImplementedError()
1384 class orm_memory(orm_template):
1385 _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']
1386 _inherit_fields = {}
1391 def __init__(self, cr):
1392 super(orm_memory, self).__init__(cr)
1396 cr.execute('delete from wkf_instance where res_type=%s', (self._name,))
1398 def vaccum(self, cr, uid):
1400 if self.check_id % self._check_time:
1403 max = time.time() - self._max_hours * 60 * 60
1404 for id in self.datas:
1405 if self.datas[id]['internal.date_access'] < max:
1407 self.unlink(cr, uid, tounlink)
1408 if len(self.datas)>self._max_count:
1409 sorted = map(lambda x: (x[1]['internal.date_access'], x[0]), self.datas.items())
1411 ids = map(lambda x: x[1], sorted[:len(self.datas)-self._max_count])
1412 self.unlink(cr, uid, ids)
1415 def read(self, cr, user, ids, fields_to_read=None, context=None, load='_classic_read'):
1418 if not fields_to_read:
1419 fields_to_read = self._columns.keys()
1422 if isinstance(ids, (int, long)):
1426 for f in fields_to_read:
1427 if id in self.datas:
1428 r[f] = self.datas[id].get(f, False)
1429 if r[f] and isinstance(self._columns[f], fields.binary) and context.get('bin_size', False):
1432 if id in self.datas:
1433 self.datas[id]['internal.date_access'] = time.time()
1434 fields_post = filter(lambda x: x in self._columns and not getattr(self._columns[x], load), fields_to_read)
1435 for f in fields_post:
1436 res2 = self._columns[f].get_memory(cr, self, ids, f, user, context=context, values=result)
1437 for record in result:
1438 record[f] = res2[record['id']]
1439 if isinstance(ids, (int, long)):
1443 def write(self, cr, user, ids, vals, context=None):
1447 if self._columns[field]._classic_write:
1448 vals2[field] = vals[field]
1450 upd_todo.append(field)
1452 self.datas[id_new].update(vals2)
1453 self.datas[id_new]['internal.date_access'] = time.time()
1454 for field in upd_todo:
1455 self._columns[field].set_memory(cr, self, id_new, field, vals[field], user, context)
1456 self._validate(cr, user, [id_new], context)
1457 wf_service = netsvc.LocalService("workflow")
1458 wf_service.trg_write(user, self._name, id_new, cr)
1461 def create(self, cr, user, vals, context=None):
1462 self.vaccum(cr, user)
1464 id_new = self.next_id
1466 for f in self._columns.keys():
1470 vals.update(self.default_get(cr, user, default, context))
1474 if self._columns[field]._classic_write:
1475 vals2[field] = vals[field]
1477 upd_todo.append(field)
1478 self.datas[id_new] = vals2
1479 self.datas[id_new]['internal.date_access'] = time.time()
1481 for field in upd_todo:
1482 self._columns[field].set_memory(cr, self, id_new, field, vals[field], user, context)
1483 self._validate(cr, user, [id_new], context)
1484 wf_service = netsvc.LocalService("workflow")
1485 wf_service.trg_create(user, self._name, id_new, cr)
1488 def default_get(self, cr, uid, fields_list, context=None):
1492 # get the default values for the inherited fields
1493 for f in fields_list:
1494 if f in self._defaults:
1495 value[f] = self._defaults[f](self, cr, uid, context)
1496 fld_def = ((f in self._columns) and self._columns[f]) \
1497 or ((f in self._inherit_fields) and self._inherit_fields[f][2]) \
1500 # get the default values set by the user and override the default
1501 # values defined in the object
1502 ir_values_obj = self.pool.get('ir.values')
1503 res = ir_values_obj.get(cr, uid, 'default', False, [self._name])
1504 for id, field, field_value in res:
1505 if field in fields_list:
1506 fld_def = (field in self._columns) and self._columns[field] or self._inherit_fields[field][2]
1507 if fld_def._type in ('many2one', 'one2one'):
1508 obj = self.pool.get(fld_def._obj)
1509 if not obj.search(cr, uid, [('id', '=', field_value)]):
1511 if fld_def._type in ('many2many'):
1512 obj = self.pool.get(fld_def._obj)
1514 for i in range(len(field_value)):
1515 if not obj.search(cr, uid, [('id', '=',
1518 field_value2.append(field_value[i])
1519 field_value = field_value2
1520 if fld_def._type in ('one2many'):
1521 obj = self.pool.get(fld_def._obj)
1523 for i in range(len(field_value)):
1524 field_value2.append({})
1525 for field2 in field_value[i]:
1526 if obj._columns[field2]._type in ('many2one', 'one2one'):
1527 obj2 = self.pool.get(obj._columns[field2]._obj)
1528 if not obj2.search(cr, uid,
1529 [('id', '=', field_value[i][field2])]):
1531 # TODO add test for many2many and one2many
1532 field_value2[i][field2] = field_value[i][field2]
1533 field_value = field_value2
1534 value[field] = field_value
1536 # get the default values from the context
1537 for key in context or {}:
1538 if key.startswith('default_'):
1539 value[key[8:]] = context[key]
1542 def search(self, cr, user, args, offset=0, limit=None, order=None,
1543 context=None, count=False):
1544 return self.datas.keys()
1546 def unlink(self, cr, uid, ids, context=None):
1548 if id in self.datas:
1551 cr.execute('delete from wkf_instance where res_type=%s and res_id in ('+','.join(map(str, ids))+')', (self._name, ))
1554 def perm_read(self, cr, user, ids, context=None, details=True):
1558 'create_uid': (user, 'Root'),
1559 'create_date': time.strftime('%Y-%m-%d %H:%M:%S'),
1561 'write_date': False,
1566 def _check_removed_columns(self, cr, log=False):
1567 # nothing to check in memory...
1570 def exists(self, cr, uid, id, context=None):
1571 return id in self.datas
1573 class orm(orm_template):
1574 _sql_constraints = []
1576 _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']
1578 def _parent_store_compute(self, cr):
1579 logger = netsvc.Logger()
1580 logger.notifyChannel('orm', netsvc.LOG_INFO, 'Computing parent left and right for table %s...' % (self._table, ))
1581 def browse_rec(root, pos=0):
1583 where = self._parent_name+'='+str(root)
1585 where = self._parent_name+' IS NULL'
1586 if self._parent_order:
1587 where += ' order by '+self._parent_order
1588 cr.execute('SELECT id FROM '+self._table+' WHERE '+where)
1590 childs = cr.fetchall()
1592 pos2 = browse_rec(id[0], pos2)
1593 cr.execute('update '+self._table+' set parent_left=%s, parent_right=%s where id=%s', (pos,pos2,root))
1595 query = 'SELECT id FROM '+self._table+' WHERE '+self._parent_name+' IS NULL'
1596 if self._parent_order:
1597 query += ' order by '+self._parent_order
1600 for (root,) in cr.fetchall():
1601 pos = browse_rec(root, pos)
1604 def _update_store(self, cr, f, k):
1605 logger = netsvc.Logger()
1606 logger.notifyChannel('orm', netsvc.LOG_INFO, "storing computed values of fields.function '%s'" % (k,))
1607 ss = self._columns[k]._symbol_set
1608 update_query = 'UPDATE "%s" SET "%s"=%s WHERE id=%%s' % (self._table, k, ss[0])
1609 cr.execute('select id from '+self._table)
1610 ids_lst = map(lambda x: x[0], cr.fetchall())
1613 ids_lst = ids_lst[40:]
1614 res = f.get(cr, self, iids, k, 1, {})
1615 for key,val in res.items():
1618 # if val is a many2one, just write the ID
1619 if type(val)==tuple:
1621 if (val<>False) or (type(val)<>bool):
1622 cr.execute(update_query, (ss[1](val), key))
1624 def _check_removed_columns(self, cr, log=False):
1625 logger = netsvc.Logger()
1626 # iterate on the database columns to drop the NOT NULL constraints
1627 # of fields which were required but have been removed (or will be added by another module)
1628 columns = [c for c in self._columns if not (isinstance(self._columns[c], fields.function) and not self._columns[c].store)]
1629 columns += ('id', 'write_uid', 'write_date', 'create_uid', 'create_date') # openerp access columns
1630 cr.execute("SELECT a.attname, a.attnotnull"
1631 " FROM pg_class c, pg_attribute a"
1632 " WHERE c.relname=%%s"
1633 " AND c.oid=a.attrelid"
1634 " AND a.attisdropped=%%s"
1635 " AND pg_catalog.format_type(a.atttypid, a.atttypmod) NOT IN ('cid', 'tid', 'oid', 'xid')"
1636 " AND a.attname NOT IN (%s)" % ",".join(['%s']*len(columns)),
1637 [self._table, False] + columns)
1638 for column in cr.dictfetchall():
1640 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))
1641 if column['attnotnull']:
1642 cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" DROP NOT NULL' % (self._table, column['attname']))
1644 def _auto_init(self, cr, context={}):
1645 store_compute = False
1646 logger = netsvc.Logger()
1649 self._field_create(cr, context=context)
1650 if not hasattr(self, "_auto") or self._auto:
1651 cr.execute("SELECT relname FROM pg_class WHERE relkind in ('r','v') AND relname='%s'" % self._table)
1653 cr.execute("CREATE TABLE \"%s\" (id SERIAL NOT NULL, PRIMARY KEY(id)) WITH OIDS" % self._table)
1656 if self._parent_store:
1657 cr.execute("""SELECT c.relname
1658 FROM pg_class c, pg_attribute a
1659 WHERE c.relname=%s AND a.attname=%s AND c.oid=a.attrelid
1660 """, (self._table, 'parent_left'))
1662 if 'parent_left' not in self._columns:
1663 logger.notifyChannel('orm', netsvc.LOG_ERROR, 'create a column parent_left on object %s: fields.integer(\'Left Parent\', select=1)' % (self._table, ))
1664 if 'parent_right' not in self._columns:
1665 logger.notifyChannel('orm', netsvc.LOG_ERROR, 'create a column parent_right on object %s: fields.integer(\'Right Parent\', select=1)' % (self._table, ))
1666 if self._columns[self._parent_name].ondelete<>'cascade':
1667 logger.notifyChannel('orm', netsvc.LOG_ERROR, "the columns %s on object must be set as ondelete='cascasde'" % (self._name, self._parent_name))
1668 cr.execute('ALTER TABLE "%s" ADD COLUMN "parent_left" INTEGER' % (self._table,))
1669 cr.execute('ALTER TABLE "%s" ADD COLUMN "parent_right" INTEGER' % (self._table,))
1671 store_compute = True
1673 if self._log_access:
1675 'create_uid': 'INTEGER REFERENCES res_users ON DELETE SET NULL',
1676 'create_date': 'TIMESTAMP',
1677 'write_uid': 'INTEGER REFERENCES res_users ON DELETE SET NULL',
1678 'write_date': 'TIMESTAMP'
1683 FROM pg_class c, pg_attribute a
1684 WHERE c.relname=%s AND a.attname=%s AND c.oid=a.attrelid
1685 """, (self._table, k))
1687 cr.execute('ALTER TABLE "%s" ADD COLUMN "%s" %s' % (self._table, k, logs[k]))
1690 self._check_removed_columns(cr, log=False)
1692 # iterate on the "object columns"
1693 todo_update_store = []
1694 for k in self._columns:
1695 if k in ('id', 'write_uid', 'write_date', 'create_uid', 'create_date'):
1697 #raise _('Can not define a column %s. Reserved keyword !') % (k,)
1698 f = self._columns[k]
1700 if isinstance(f, fields.one2many):
1701 cr.execute("SELECT relname FROM pg_class WHERE relkind='r' AND relname=%s", (f._obj,))
1703 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))
1704 res = cr.fetchone()[0]
1706 cr.execute('ALTER TABLE "%s" ADD FOREIGN KEY (%s) REFERENCES "%s" ON DELETE SET NULL' % (self._obj, f._fields_id, f._table))
1707 elif isinstance(f, fields.many2many):
1708 cr.execute("SELECT relname FROM pg_class WHERE relkind in ('r','v') AND relname=%s", (f._rel,))
1709 if not cr.dictfetchall():
1710 #FIXME: Remove this try/except
1712 ref = self.pool.get(f._obj)._table
1713 except AttributeError:
1714 ref = f._obj.replace('.', '_')
1715 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))
1716 cr.execute('CREATE INDEX "%s_%s_index" ON "%s" ("%s")' % (f._rel, f._id1, f._rel, f._id1))
1717 cr.execute('CREATE INDEX "%s_%s_index" ON "%s" ("%s")' % (f._rel, f._id2, f._rel, f._id2))
1720 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 " \
1721 "FROM pg_class c,pg_attribute a,pg_type t " \
1722 "WHERE c.relname=%s " \
1723 "AND a.attname=%s " \
1724 "AND c.oid=a.attrelid " \
1725 "AND a.atttypid=t.oid", (self._table, k))
1726 res = cr.dictfetchall()
1728 if not isinstance(f, fields.function) or f.store:
1730 # add the missing field
1731 cr.execute('ALTER TABLE "%s" ADD COLUMN "%s" %s' % (self._table, k, get_pg_type(f)[1]))
1734 if not create and k in self._defaults:
1735 default = self._defaults[k](self, cr, 1, {})
1736 ss = self._columns[k]._symbol_set
1737 query = 'UPDATE "%s" SET "%s"=%s' % (self._table, k, ss[0])
1738 cr.execute(query, (ss[1](default),))
1740 logger.notifyChannel('orm', netsvc.LOG_DEBUG, 'setting default value of new column %s of table %s'% (k, self._table))
1742 logger.notifyChannel('orm', netsvc.LOG_DEBUG, 'creating new column %s of table %s'% (k, self._table))
1744 if isinstance(f, fields.function):
1746 if f.store is not True:
1747 order = f.store[f.store.keys()[0]][2]
1748 todo_update_store.append((order, f,k))
1750 # and add constraints if needed
1751 if isinstance(f, fields.many2one):
1752 #FIXME: Remove this try/except
1754 ref = self.pool.get(f._obj)._table
1755 except AttributeError:
1756 ref = f._obj.replace('.', '_')
1757 # ir_actions is inherited so foreign key doesn't work on it
1758 if ref != 'ir_actions':
1759 cr.execute('ALTER TABLE "%s" ADD FOREIGN KEY ("%s") REFERENCES "%s" ON DELETE %s' % (self._table, k, ref, f.ondelete))
1761 cr.execute('CREATE INDEX "%s_%s_index" ON "%s" ("%s")' % (self._table, k, self._table, k))
1765 cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" SET NOT NULL' % (self._table, k))
1766 except Exception, e:
1767 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))
1771 f_pg_type = f_pg_def['typname']
1772 f_pg_size = f_pg_def['size']
1773 f_pg_notnull = f_pg_def['attnotnull']
1774 if isinstance(f, fields.function) and not f.store:
1775 logger.notifyChannel('orm', netsvc.LOG_INFO, 'column %s (%s) in table %s removed: converted to a function !\n' % (k, f.string, self._table))
1776 cr.execute('ALTER TABLE %s DROP COLUMN %s'% (self._table, k))
1780 f_obj_type = get_pg_type(f) and get_pg_type(f)[0]
1785 ('text', 'char', 'VARCHAR(%d)' % (f.size or 0,), '::VARCHAR(%d)'%(f.size or 0,)),
1786 ('varchar', 'text', 'TEXT', ''),
1787 ('int4', 'float', get_pg_type(f)[1], '::'+get_pg_type(f)[1]),
1788 ('date', 'datetime', 'TIMESTAMP', '::TIMESTAMP'),
1790 # !!! Avoid reduction of varchar field !!!
1791 if f_pg_type == 'varchar' and f._type == 'char' and f_pg_size < f.size:
1792 # if f_pg_type == 'varchar' and f._type == 'char' and f_pg_size != f.size:
1793 logger.notifyChannel('orm', netsvc.LOG_INFO, "column '%s' in table '%s' changed size" % (k, self._table))
1794 cr.execute('ALTER TABLE "%s" RENAME COLUMN "%s" TO temp_change_size' % (self._table, k))
1795 cr.execute('ALTER TABLE "%s" ADD COLUMN "%s" VARCHAR(%d)' % (self._table, k, f.size))
1796 cr.execute('UPDATE "%s" SET "%s"=temp_change_size::VARCHAR(%d)' % (self._table, k, f.size))
1797 cr.execute('ALTER TABLE "%s" DROP COLUMN temp_change_size' % (self._table,))
1800 if (f_pg_type==c[0]) and (f._type==c[1]):
1801 logger.notifyChannel('orm', netsvc.LOG_INFO, "column '%s' in table '%s' changed type to %s." % (k, self._table, c[1]))
1803 cr.execute('ALTER TABLE "%s" RENAME COLUMN "%s" TO temp_change_size' % (self._table, k))
1804 cr.execute('ALTER TABLE "%s" ADD COLUMN "%s" %s' % (self._table, k, c[2]))
1805 cr.execute(('UPDATE "%s" SET "%s"=temp_change_size'+c[3]) % (self._table, k))
1806 cr.execute('ALTER TABLE "%s" DROP COLUMN temp_change_size CASCADE' % (self._table,))
1809 if f_pg_type != f_obj_type:
1811 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))
1813 # if the field is required and hasn't got a NOT NULL constraint
1814 if f.required and f_pg_notnull == 0:
1815 # set the field to the default value if any
1816 if k in self._defaults:
1817 default = self._defaults[k](self, cr, 1, {})
1818 if (default is not None):
1819 ss = self._columns[k]._symbol_set
1820 query = 'UPDATE "%s" SET "%s"=%s WHERE %s is NULL' % (self._table, k, ss[0], k)
1821 cr.execute(query, (ss[1](default),))
1822 # add the NOT NULL constraint
1825 cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" SET NOT NULL' % (self._table, k))
1827 except Exception, e:
1828 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))
1830 elif not f.required and f_pg_notnull == 1:
1831 cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" DROP NOT NULL' % (self._table, k))
1833 indexname = '%s_%s_index' % (self._table, k)
1834 cr.execute("SELECT indexname FROM pg_indexes WHERE indexname = %s and tablename = %s", (indexname, self._table))
1835 res = cr.dictfetchall()
1836 if not res and f.select:
1837 cr.execute('CREATE INDEX "%s_%s_index" ON "%s" ("%s")' % (self._table, k, self._table, k))
1839 if res and not f.select:
1840 cr.execute('DROP INDEX "%s_%s_index"' % (self._table, k))
1842 if isinstance(f, fields.many2one):
1843 ref = self.pool.get(f._obj)._table
1844 if ref != 'ir_actions':
1845 cr.execute('SELECT confdeltype, conname FROM pg_constraint as con, pg_class as cl1, pg_class as cl2, '
1846 'pg_attribute as att1, pg_attribute as att2 '
1847 'WHERE con.conrelid = cl1.oid '
1848 'AND cl1.relname = %s '
1849 'AND con.confrelid = cl2.oid '
1850 'AND cl2.relname = %s '
1851 'AND array_lower(con.conkey, 1) = 1 '
1852 'AND con.conkey[1] = att1.attnum '
1853 'AND att1.attrelid = cl1.oid '
1854 'AND att1.attname = %s '
1855 'AND array_lower(con.confkey, 1) = 1 '
1856 'AND con.confkey[1] = att2.attnum '
1857 'AND att2.attrelid = cl2.oid '
1858 'AND att2.attname = %s '
1859 "AND con.contype = 'f'", (self._table, ref, k, 'id'))
1860 res = cr.dictfetchall()
1869 if res[0]['confdeltype'] != confdeltype.get(f.ondelete.upper(), 'a'):
1870 cr.execute('ALTER TABLE "' + self._table + '" DROP CONSTRAINT "' + res[0]['conname'] + '"')
1871 cr.execute('ALTER TABLE "' + self._table + '" ADD FOREIGN KEY ("' + k + '") REFERENCES "' + ref + '" ON DELETE ' + f.ondelete)
1874 logger.notifyChannel('orm', netsvc.LOG_ERROR, "Programming error !")
1875 for order,f,k in todo_update_store:
1876 todo_end.append((order, self._update_store, (f, k)))
1879 cr.execute("SELECT relname FROM pg_class WHERE relkind in ('r','v') AND relname=%s", (self._table,))
1880 create = not bool(cr.fetchone())
1882 for (key, con, _) in self._sql_constraints:
1883 conname = '%s_%s' % (self._table, key)
1884 cr.execute("SELECT conname FROM pg_constraint where conname=%s", (conname,))
1885 if not cr.dictfetchall():
1887 cr.execute('alter table "%s" add constraint "%s_%s" %s' % (self._table, self._table, key, con,))
1890 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,))
1893 if hasattr(self, "_sql"):
1894 for line in self._sql.split(';'):
1895 line2 = line.replace('\n', '').strip()
1900 self._parent_store_compute(cr)
1903 def __init__(self, cr):
1904 super(orm, self).__init__(cr)
1906 if not hasattr(self, '_log_access'):
1907 # if not access is not specify, it is the same value as _auto
1908 self._log_access = not hasattr(self, "_auto") or self._auto
1910 self._columns = self._columns.copy()
1911 for store_field in self._columns:
1912 f = self._columns[store_field]
1913 if not isinstance(f, fields.function):
1917 if self._columns[store_field].store is True:
1918 sm = {self._name:(lambda self,cr, uid, ids, c={}: ids, None, 10)}
1920 sm = self._columns[store_field].store
1921 for object, aa in sm.items():
1923 (fnct,fields2,order)=aa
1925 raise except_orm('Error',
1926 ('Invalid function definition %s in object %s !\nYou must use the definition: store={object:(fnct, fields, priority)}.' % (store_field, self._name)))
1927 self.pool._store_function.setdefault(object, [])
1929 for x,y,z,e,f in self.pool._store_function[object]:
1930 if (x==self._name) and (y==store_field) and (e==fields2):
1933 self.pool._store_function[object].append( (self._name, store_field, fnct, fields2, order))
1934 self.pool._store_function[object].sort(lambda x,y: cmp(x[4],y[4]))
1936 for (key, _, msg) in self._sql_constraints:
1937 self.pool._sql_error[self._table+'_'+key] = msg
1939 # Load manual fields
1941 cr.execute("SELECT id FROM ir_model_fields WHERE name=%s AND model=%s", ('state', 'ir.model.fields'))
1943 cr.execute('SELECT * FROM ir_model_fields WHERE model=%s AND state=%s', (self._name, 'manual'))
1944 for field in cr.dictfetchall():
1945 if field['name'] in self._columns:
1948 'string': field['field_description'],
1949 'required': bool(field['required']),
1950 'readonly': bool(field['readonly']),
1951 'domain': field['domain'] or None,
1952 'size': field['size'],
1953 'ondelete': field['on_delete'],
1954 'translate': (field['translate']),
1955 #'select': int(field['select_level'])
1958 if field['ttype'] == 'selection':
1959 self._columns[field['name']] = getattr(fields, field['ttype'])(eval(field['selection']), **attrs)
1960 elif field['ttype'] == 'reference':
1961 self._columns[field['name']] = getattr(fields, field['ttype'])(selection=eval(field['selection']), **attrs)
1962 elif field['ttype'] == 'many2one':
1963 self._columns[field['name']] = getattr(fields, field['ttype'])(field['relation'], **attrs)
1964 elif field['ttype'] == 'one2many':
1965 self._columns[field['name']] = getattr(fields, field['ttype'])(field['relation'], field['relation_field'], **attrs)
1966 elif field['ttype'] == 'many2many':
1968 _rel1 = field['relation'].replace('.', '_')
1969 _rel2 = field['model'].replace('.', '_')
1970 _rel_name = 'x_%s_%s_%s_rel' %(_rel1, _rel2, field['name'])
1971 self._columns[field['name']] = getattr(fields, field['ttype'])(field['relation'], _rel_name, 'id1', 'id2', **attrs)
1973 self._columns[field['name']] = getattr(fields, field['ttype'])(**attrs)
1975 self._inherits_reload()
1976 if not self._sequence:
1977 self._sequence = self._table+'_id_seq'
1978 for k in self._defaults:
1979 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,)
1980 for f in self._columns:
1981 self._columns[f].restart()
1983 def default_get(self, cr, uid, fields_list, context=None):
1987 # get the default values for the inherited fields
1988 for t in self._inherits.keys():
1989 value.update(self.pool.get(t).default_get(cr, uid, fields_list,
1992 # get the default values defined in the object
1993 for f in fields_list:
1994 if f in self._defaults:
1995 value[f] = self._defaults[f](self, cr, uid, context)
1996 fld_def = ((f in self._columns) and self._columns[f]) \
1997 or ((f in self._inherit_fields) and self._inherit_fields[f][2]) \
1999 if isinstance(fld_def, fields.property):
2000 property_obj = self.pool.get('ir.property')
2001 definition_id = fld_def._field_get(cr, uid, self._name, f)
2002 nid = property_obj.search(cr, uid, [('fields_id', '=',
2003 definition_id), ('res_id', '=', False)])
2005 prop_value = property_obj.browse(cr, uid, nid[0],
2006 context=context).value
2007 value[f] = (prop_value and int(prop_value.split(',')[1])) \
2010 # get the default values set by the user and override the default
2011 # values defined in the object
2012 ir_values_obj = self.pool.get('ir.values')
2013 res = ir_values_obj.get(cr, uid, 'default', False, [self._name])
2014 for id, field, field_value in res:
2015 if field in fields_list:
2016 fld_def = (field in self._columns) and self._columns[field] or self._inherit_fields[field][2]
2017 if fld_def._type in ('many2one', 'one2one'):
2018 obj = self.pool.get(fld_def._obj)
2019 if not obj.search(cr, uid, [('id', '=', field_value)]):
2021 if fld_def._type in ('many2many'):
2022 obj = self.pool.get(fld_def._obj)
2024 for i in range(len(field_value)):
2025 if not obj.search(cr, uid, [('id', '=',
2028 field_value2.append(field_value[i])
2029 field_value = field_value2
2030 if fld_def._type in ('one2many'):
2031 obj = self.pool.get(fld_def._obj)
2033 for i in range(len(field_value)):
2034 field_value2.append({})
2035 for field2 in field_value[i]:
2036 if obj._columns[field2]._type in ('many2one', 'one2one'):
2037 obj2 = self.pool.get(obj._columns[field2]._obj)
2038 if not obj2.search(cr, uid,
2039 [('id', '=', field_value[i][field2])]):
2041 # TODO add test for many2many and one2many
2042 field_value2[i][field2] = field_value[i][field2]
2043 field_value = field_value2
2044 value[field] = field_value
2045 for key in context or {}:
2046 if key.startswith('default_'):
2047 value[key[8:]] = context[key]
2051 # Update objects that uses this one to update their _inherits fields
2053 def _inherits_reload_src(self):
2054 for obj in self.pool.obj_pool.values():
2055 if self._name in obj._inherits:
2056 obj._inherits_reload()
2058 def _inherits_reload(self):
2060 for table in self._inherits:
2061 res.update(self.pool.get(table)._inherit_fields)
2062 for col in self.pool.get(table)._columns.keys():
2063 res[col] = (table, self._inherits[table], self.pool.get(table)._columns[col])
2064 for col in self.pool.get(table)._inherit_fields.keys():
2065 res[col] = (table, self._inherits[table], self.pool.get(table)._inherit_fields[col][2])
2066 self._inherit_fields = res
2067 self._inherits_reload_src()
2069 def fields_get(self, cr, user, fields=None, context=None):
2070 ira = self.pool.get('ir.model.access')
2071 read_access = ira.check(cr, user, self._name, 'write', raise_exception=False, context=context) or \
2072 ira.check(cr, user, self._name, 'create', raise_exception=False, context=context)
2073 return super(orm, self).fields_get(cr, user, fields, context, read_access)
2075 def read(self, cr, user, ids, fields=None, context=None, load='_classic_read'):
2078 self.pool.get('ir.model.access').check(cr, user, self._name, 'read', context=context)
2080 fields = self._columns.keys() + self._inherit_fields.keys()
2082 if isinstance(ids, (int, long)):
2084 result = self._read_flat(cr, user, select, fields, context, load)
2086 for key, v in r.items():
2089 if isinstance(ids, (int, long)):
2090 return result and result[0] or False
2093 def _read_flat(self, cr, user, ids, fields_to_read, context=None, load='_classic_read'):
2099 if fields_to_read == None:
2100 fields_to_read = self._columns.keys()
2102 # construct a clause for the rules :
2103 d1, d2 = self.pool.get('ir.rule').domain_get(cr, user, self._name)
2105 # all inherited fields + all non inherited fields for which the attribute whose name is in load is True
2106 fields_pre = [f for f in fields_to_read if
2107 f == self.CONCURRENCY_CHECK_FIELD
2108 or (f in self._columns and getattr(self._columns[f], '_classic_write'))
2109 ] + self._inherits.values()
2113 def convert_field(f):
2114 if f in ('create_date', 'write_date'):
2115 return "date_trunc('second', %s) as %s" % (f, f)
2116 if f == self.CONCURRENCY_CHECK_FIELD:
2117 if self._log_access:
2118 return "COALESCE(write_date, create_date, now())::timestamp AS %s" % (f,)
2119 return "now()::timestamp AS %s" % (f,)
2120 if isinstance(self._columns[f], fields.binary) and context.get('bin_size', False):
2121 return 'length("%s") as "%s"' % (f, f)
2122 return '"%s"' % (f,)
2123 fields_pre2 = map(convert_field, fields_pre)
2124 for i in range(0, len(ids), cr.IN_MAX):
2125 sub_ids = ids[i:i+cr.IN_MAX]
2127 cr.execute('SELECT %s FROM \"%s\" WHERE id IN (%s) AND %s ORDER BY %s' % \
2128 (','.join(fields_pre2 + ['id']), self._table,
2129 ','.join(['%s' for x in sub_ids]), d1,
2130 self._order),sub_ids + d2)
2131 if not cr.rowcount == len({}.fromkeys(sub_ids)):
2132 raise except_orm(_('AccessError'),
2133 _('You try to bypass an access rule (Document type: %s).') % self._description)
2135 cr.execute('SELECT %s FROM \"%s\" WHERE id IN (%s) ORDER BY %s' % \
2136 (','.join(fields_pre2 + ['id']), self._table,
2137 ','.join(['%s' for x in sub_ids]),
2138 self._order), sub_ids)
2139 res.extend(cr.dictfetchall())
2141 res = map(lambda x: {'id': x}, ids)
2143 for f in fields_pre:
2144 if f == self.CONCURRENCY_CHECK_FIELD:
2146 if self._columns[f].translate:
2147 ids = map(lambda x: x['id'], res)
2148 res_trans = self.pool.get('ir.translation')._get_ids(cr, user, self._name+','+f, 'model', context.get('lang', False) or 'en_US', ids)
2150 r[f] = res_trans.get(r['id'], False) or r[f]
2152 for table in self._inherits:
2153 col = self._inherits[table]
2154 cols = intersect(self._inherit_fields.keys(), fields_to_read)
2157 res2 = self.pool.get(table).read(cr, user, [x[col] for x in res], cols, context, load)
2165 record.update(res3[record[col]])
2166 if col not in fields_to_read:
2169 # all fields which need to be post-processed by a simple function (symbol_get)
2170 fields_post = filter(lambda x: x in self._columns and self._columns[x]._symbol_get, fields_to_read)
2172 # maybe it would be faster to iterate on the fields then on res, so that we wouldn't need
2173 # to get the _symbol_get in each occurence
2175 for f in fields_post:
2176 r[f] = self._columns[f]._symbol_get(r[f])
2177 ids = map(lambda x: x['id'], res)
2179 # all non inherited fields for which the attribute whose name is in load is False
2180 fields_post = filter(lambda x: x in self._columns and not getattr(self._columns[x], load), fields_to_read)
2182 # Compute POST fields
2184 for f in fields_post:
2185 todo.setdefault(self._columns[f]._multi, [])
2186 todo[self._columns[f]._multi].append(f)
2187 for key,val in todo.items():
2189 res2 = self._columns[val[0]].get(cr, self, ids, val, user, context=context, values=res)
2192 record[pos] = res2[record['id']][pos]
2195 res2 = self._columns[f].get(cr, self, ids, f, user, context=context, values=res)
2197 record[f] = res2[record['id']]
2199 #for f in fields_post:
2200 # # get the value of that field for all records/ids
2201 # res2 = self._columns[f].get(cr, self, ids, f, user, context=context, values=res)
2202 # for record in res:
2203 # record[f] = res2[record['id']]
2207 for field in vals.copy():
2209 if field in self._columns:
2210 fobj = self._columns[field]
2217 for group in groups:
2218 module = group.split(".")[0]
2219 grp = group.split(".")[1]
2220 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" % \
2221 (grp, module, 'res.groups', user))
2222 readonly = cr.fetchall()
2223 if readonly[0][0] >= 1:
2226 elif readonly[0][0] == 0:
2232 if type(vals[field]) == type([]):
2234 elif type(vals[field]) == type(0.0):
2236 elif type(vals[field]) == type(''):
2237 vals[field] = '=No Permission='
2242 def perm_read(self, cr, user, ids, context=None, details=True):
2248 if self._log_access:
2249 fields = ', u.create_uid, u.create_date, u.write_uid, u.write_date'
2250 if isinstance(ids, (int, long)):
2253 ids_str = string.join(map(lambda x: str(x), ids), ',')
2254 cr.execute('select u.id'+fields+' from "'+self._table+'" u where u.id in ('+ids_str+')')
2255 res = cr.dictfetchall()
2258 r[key] = r[key] or False
2259 if key in ('write_uid', 'create_uid', 'uid') and details:
2261 r[key] = self.pool.get('res.users').name_get(cr, user, [r[key]])[0]
2262 if isinstance(ids, (int, long)):
2266 def _check_concurrency(self, cr, ids, context):
2269 if context.get(self.CONCURRENCY_CHECK_FIELD) and self._log_access:
2271 return "%s,%s" % (self._name, oid)
2272 santa = "(id = %s AND %s < COALESCE(write_date, create_date, now())::timestamp)"
2273 for i in range(0, len(ids), cr.IN_MAX):
2274 sub_ids = tools.flatten(((oid, context[self.CONCURRENCY_CHECK_FIELD][key(oid)])
2275 for oid in ids[i:i+cr.IN_MAX]
2276 if key(oid) in context[self.CONCURRENCY_CHECK_FIELD]))
2278 cr.execute("SELECT count(1) FROM %s WHERE %s" % (self._table, " OR ".join([santa]*(len(sub_ids)/2))), sub_ids)
2281 raise except_orm('ConcurrencyException', _('Records were modified in the meanwhile'))
2283 def unlink(self, cr, uid, ids, context=None):
2286 if isinstance(ids, (int, long)):
2289 result_store = self._store_get_values(cr, uid, ids, None, context)
2291 self._check_concurrency(cr, ids, context)
2293 self.pool.get('ir.model.access').check(cr, uid, self._name, 'unlink', context=context)
2295 properties = self.pool.get('ir.property')
2296 domain = [('res_id', '=', False),
2297 ('value', 'in', ['%s,%s' % (self._name, i) for i in ids]),
2299 if properties.search(cr, uid, domain, context=context):
2300 raise except_orm(_('Error'), _('Unable to delete this document because it is used as a default property'))
2302 wf_service = netsvc.LocalService("workflow")
2304 wf_service.trg_delete(uid, self._name, oid, cr)
2306 #cr.execute('select * from '+self._table+' where id in ('+str_d+')', ids)
2307 #res = cr.dictfetchall()
2308 #for key in self._inherits:
2309 # ids2 = [x[self._inherits[key]] for x in res]
2310 # self.pool.get(key).unlink(cr, uid, ids2)
2312 d1, d2 = self.pool.get('ir.rule').domain_get(cr, uid, self._name)
2316 for i in range(0, len(ids), cr.IN_MAX):
2317 sub_ids = ids[i:i+cr.IN_MAX]
2318 str_d = string.join(('%s',)*len(sub_ids), ',')
2320 cr.execute('SELECT id FROM "'+self._table+'" ' \
2321 'WHERE id IN ('+str_d+')'+d1, sub_ids+d2)
2322 if not cr.rowcount == len(sub_ids):
2323 raise except_orm(_('AccessError'),
2324 _('You try to bypass an access rule (Document type: %s).') % \
2328 cr.execute('delete from "'+self._table+'" ' \
2329 'where id in ('+str_d+')'+d1, sub_ids+d2)
2331 cr.execute('delete from "'+self._table+'" ' \
2332 'where id in ('+str_d+')', sub_ids)
2334 for order, object, store_ids, fields in result_store:
2335 if object<>self._name:
2336 obj = self.pool.get(object)
2337 cr.execute('select id from '+obj._table+' where id in ('+','.join(map(str, store_ids))+')')
2338 rids = map(lambda x: x[0], cr.fetchall())
2340 obj._store_set_values(cr, uid, rids, fields, context)
2346 def write(self, cr, user, ids, vals, context=None):
2348 for field in vals.copy():
2350 if field in self._columns:
2351 fobj = self._columns[field]
2353 fobj = self._inherit_fields[field][2]
2360 for group in groups:
2361 module = group.split(".")[0]
2362 grp = group.split(".")[1]
2363 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" % \
2364 (grp, module, 'res.groups', user))
2365 readonly = cr.fetchall()
2366 if readonly[0][0] >= 1:
2369 elif readonly[0][0] == 0:
2382 if isinstance(ids, (int, long)):
2385 self._check_concurrency(cr, ids, context)
2387 self.pool.get('ir.model.access').check(cr, user, self._name, 'write', context=context)
2395 totranslate = context.get('lang', False) and (context['lang'] != 'en_US')
2397 if field in self._columns:
2398 if self._columns[field]._classic_write and not (hasattr(self._columns[field], '_fnct_inv')):
2399 if (not totranslate) or not self._columns[field].translate:
2400 upd0.append('"'+field+'"='+self._columns[field]._symbol_set[0])
2401 upd1.append(self._columns[field]._symbol_set[1](vals[field]))
2402 direct.append(field)
2404 upd_todo.append(field)
2406 updend.append(field)
2407 if field in self._columns \
2408 and hasattr(self._columns[field], 'selection') \
2410 if self._columns[field]._type == 'reference':
2411 val = vals[field].split(',')[0]
2414 if isinstance(self._columns[field].selection, (tuple, list)):
2415 if val not in dict(self._columns[field].selection):
2416 raise except_orm(_('ValidateError'),
2417 _('The value "%s" for the field "%s" is not in the selection') \
2418 % (vals[field], field))
2420 if val not in dict(self._columns[field].selection(
2421 self, cr, user, context=context)):
2422 raise except_orm(_('ValidateError'),
2423 _('The value "%s" for the field "%s" is not in the selection') \
2424 % (vals[field], field))
2426 if self._log_access:
2427 upd0.append('write_uid=%s')
2428 upd0.append('write_date=now()')
2433 d1, d2 = self.pool.get('ir.rule').domain_get(cr, user, self._name)
2437 for i in range(0, len(ids), cr.IN_MAX):
2438 sub_ids = ids[i:i+cr.IN_MAX]
2439 ids_str = string.join(map(str, sub_ids), ',')
2441 cr.execute('SELECT id FROM "'+self._table+'" ' \
2442 'WHERE id IN ('+ids_str+')'+d1, d2)
2443 if not cr.rowcount == len({}.fromkeys(sub_ids)):
2444 raise except_orm(_('AccessError'),
2445 _('You try to bypass an access rule (Document type: %s).') % \
2448 cr.execute('SELECT id FROM "'+self._table+'" WHERE id IN ('+ids_str+')')
2449 if not cr.rowcount == len({}.fromkeys(sub_ids)):
2450 raise except_orm(_('AccessError'),
2451 _('You try to write on an record that doesn\'t exist ' \
2452 '(Document type: %s).') % self._description)
2454 cr.execute('update "'+self._table+'" set '+string.join(upd0, ',')+' ' \
2455 'where id in ('+ids_str+')'+d1, upd1+ d2)
2457 cr.execute('update "'+self._table+'" set '+string.join(upd0, ',')+' ' \
2458 'where id in ('+ids_str+')', upd1)
2462 if self._columns[f].translate:
2463 src_trans = self.pool.get(self._name).read(cr,user,ids,[f])
2464 self.pool.get('ir.translation')._set_ids(cr, user, self._name+','+f, 'model', context['lang'], ids, vals[f], src_trans[0][f])
2467 # call the 'set' method of fields which are not classic_write
2468 upd_todo.sort(lambda x, y: self._columns[x].priority-self._columns[y].priority)
2470 # default element in context must be removed when call a one2many or many2many
2471 rel_context = context.copy()
2472 for c in context.items():
2473 if c[0].startswith('default_'):
2474 del rel_context[c[0]]
2477 for field in upd_todo:
2479 result += self._columns[field].set(cr, self, id, field, vals[field], user, context=rel_context) or []
2481 for table in self._inherits:
2482 col = self._inherits[table]
2484 for i in range(0, len(ids), cr.IN_MAX):
2485 sub_ids = ids[i:i+cr.IN_MAX]
2486 ids_str = string.join(map(str, sub_ids), ',')
2487 cr.execute('select distinct "'+col+'" from "'+self._table+'" ' \
2488 'where id in ('+ids_str+')', upd1)
2489 nids.extend([x[0] for x in cr.fetchall()])
2493 if self._inherit_fields[val][0] == table:
2495 self.pool.get(table).write(cr, user, nids, v, context)
2497 self._validate(cr, user, ids, context)
2498 # TODO: use _order to set dest at the right position and not first node of parent
2499 if self._parent_store and (self._parent_name in vals):
2501 self.pool._init_parent[self._name]=True
2504 # Find Position of the element
2505 if vals[self._parent_name]:
2506 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],))
2508 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))
2509 result_p = cr.fetchall()
2511 for (pleft,pright,pid) in result_p:
2516 # It's the first node of the parent: position = parent_left+1
2518 if not vals[self._parent_name]:
2521 cr.execute('select parent_left from '+self._table+' where id=%s', (vals[self._parent_name],))
2522 position = cr.fetchone()[0]+1
2524 # We have the new position !
2525 cr.execute('select parent_left,parent_right from '+self._table+' where id=%s', (id,))
2526 pleft,pright = cr.fetchone()
2527 distance = pright - pleft + 1
2529 if position>pleft and position<=pright:
2530 raise except_orm(_('UserError'), _('Recursivity Detected.'))
2533 cr.execute('update '+self._table+' set parent_left=parent_left+%s where parent_left>=%s', (distance, position))
2534 cr.execute('update '+self._table+' set parent_right=parent_right+%s where parent_right>=%s', (distance, position))
2535 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))
2537 cr.execute('update '+self._table+' set parent_left=parent_left+%s where parent_left>=%s', (distance, position))
2538 cr.execute('update '+self._table+' set parent_right=parent_right+%s where parent_right>=%s', (distance, position))
2539 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))
2541 result += self._store_get_values(cr, user, ids, vals.keys(), context)
2542 for order, object, ids, fields in result:
2543 self.pool.get(object)._store_set_values(cr, user, ids, fields, context)
2545 wf_service = netsvc.LocalService("workflow")
2547 wf_service.trg_write(user, self._name, id, cr)
2551 # TODO: Should set perm to user.xxx
2553 def create(self, cr, user, vals, context=None):
2554 """ create(cr, user, vals, context) -> int
2555 cr = database cursor
2557 vals = dictionary of the form {'field_name':field_value, ...}
2561 self.pool.get('ir.model.access').check(cr, user, self._name, 'create', context=context)
2566 for (t, c) in self._inherits.items():
2568 avoid_table.append(t)
2569 for f in self._columns.keys():
2570 if (not f in vals) and (not isinstance(self._columns[f], fields.property)):
2573 for f in self._inherit_fields.keys():
2574 if (not f in vals) and (self._inherit_fields[f][0] not in avoid_table) and (not isinstance(self._inherit_fields[f][2], fields.property)):
2578 default_values = self.default_get(cr, user, default, context)
2579 for dv in default_values:
2580 if dv in self._columns and self._columns[dv]._type == 'many2many':
2581 if default_values[dv] and isinstance(default_values[dv][0], (int, long)):
2582 default_values[dv] = [(6, 0, default_values[dv])]
2583 vals.update(default_values)
2586 for v in self._inherits:
2587 if self._inherits[v] not in vals:
2590 (upd0, upd1, upd2) = ('', '', [])
2593 for v in vals.keys():
2594 if v in self._inherit_fields:
2595 (table, col, col_detail) = self._inherit_fields[v]
2596 tocreate[table][v] = vals[v]
2599 # Try-except added to filter the creation of those records whose filds are readonly.
2600 # Example : any dashboard which has all the fields readonly.(due to Views(database views))
2602 cr.execute("SELECT nextval('"+self._sequence+"')")
2604 raise except_orm(_('UserError'),
2605 _('You cannot perform this operation.'))
2607 id_new = cr.fetchone()[0]
2608 for table in tocreate:
2609 id = self.pool.get(table).create(cr, user, tocreate[table])
2610 upd0 += ','+self._inherits[table]
2614 #Start : Set bool fields to be False if they are not touched(to make search more powerful)
2615 bool_fields = [x for x in self._columns.keys() if self._columns[x]._type=='boolean']
2617 for bool_field in bool_fields:
2618 if bool_field not in vals:
2619 vals[bool_field] = False
2623 if field in self._columns:
2624 if self._columns[field]._classic_write:
2625 upd0 = upd0 + ',"' + field + '"'
2626 upd1 = upd1 + ',' + self._columns[field]._symbol_set[0]
2627 upd2.append(self._columns[field]._symbol_set[1](vals[field]))
2629 upd_todo.append(field)
2630 if field in self._columns \
2631 and hasattr(self._columns[field], 'selection') \
2633 if self._columns[field]._type == 'reference':
2634 val = vals[field].split(',')[0]
2637 if isinstance(self._columns[field].selection, (tuple, list)):
2638 if val not in dict(self._columns[field].selection):
2639 raise except_orm(_('ValidateError'),
2640 _('The value "%s" for the field "%s" is not in the selection') \
2641 % (vals[field], field))
2643 if val not in dict(self._columns[field].selection(
2644 self, cr, user, context=context)):
2645 raise except_orm(_('ValidateError'),
2646 _('The value "%s" for the field "%s" is not in the selection') \
2647 % (vals[field], field))
2648 if self._log_access:
2649 upd0 += ',create_uid,create_date'
2652 cr.execute('insert into "'+self._table+'" (id'+upd0+") values ("+str(id_new)+upd1+')', tuple(upd2))
2653 upd_todo.sort(lambda x, y: self._columns[x].priority-self._columns[y].priority)
2655 if self._parent_store:
2657 self.pool._init_parent[self._name]=True
2659 parent = vals.get(self._parent_name, False)
2661 cr.execute('select parent_right from '+self._table+' where '+self._parent_name+'=%s order by '+(self._parent_order or self._order), (parent,))
2663 result_p = cr.fetchall()
2664 for (pleft,) in result_p:
2669 cr.execute('select parent_left from '+self._table+' where id=%s', (parent,))
2670 pleft_old = cr.fetchone()[0]
2673 cr.execute('select max(parent_right) from '+self._table)
2674 pleft = cr.fetchone()[0] or 0
2675 cr.execute('update '+self._table+' set parent_left=parent_left+2 where parent_left>%s', (pleft,))
2676 cr.execute('update '+self._table+' set parent_right=parent_right+2 where parent_right>%s', (pleft,))
2677 cr.execute('update '+self._table+' set parent_left=%s,parent_right=%s where id=%s', (pleft+1,pleft+2,id_new))
2679 # default element in context must be removed when call a one2many or many2many
2680 rel_context = context.copy()
2681 for c in context.items():
2682 if c[0].startswith('default_'):
2683 del rel_context[c[0]]
2686 for field in upd_todo:
2687 result += self._columns[field].set(cr, self, id_new, field, vals[field], user, rel_context) or []
2688 self._validate(cr, user, [id_new], context)
2690 if not context.get('no_store_function', False):
2691 result += self._store_get_values(cr, user, [id_new], vals.keys(), context)
2694 for order, object, ids, fields2 in result:
2695 if not (object, ids, fields2) in done:
2696 self.pool.get(object)._store_set_values(cr, user, ids, fields2, context)
2697 done.append((object, ids, fields2))
2699 wf_service = netsvc.LocalService("workflow")
2700 wf_service.trg_create(user, self._name, id_new, cr)
2703 def _store_get_values(self, cr, uid, ids, fields, context):
2705 fncts = self.pool._store_function.get(self._name, [])
2706 for fnct in range(len(fncts)):
2709 for f in (fields or []):
2710 if f in fncts[fnct][3]:
2716 result.setdefault(fncts[fnct][0], {})
2717 ids2 = fncts[fnct][2](self,cr, uid, ids, context)
2718 for id in filter(None, ids2):
2719 result[fncts[fnct][0]].setdefault(id, [])
2720 result[fncts[fnct][0]][id].append(fnct)
2722 for object in result:
2724 for id,fnct in result[object].items():
2725 k2.setdefault(tuple(fnct), [])
2726 k2[tuple(fnct)].append(id)
2727 for fnct,id in k2.items():
2728 dict.setdefault(fncts[fnct[0]][4],[])
2729 dict[fncts[fnct[0]][4]].append((fncts[fnct[0]][4],object,id,map(lambda x: fncts[x][1], fnct)))
2737 def _store_set_values(self, cr, uid, ids, fields, context):
2741 if self._columns[f]._multi not in keys:
2742 keys.append(self._columns[f]._multi)
2743 todo.setdefault(self._columns[f]._multi, [])
2744 todo[self._columns[f]._multi].append(f)
2748 result = self._columns[val[0]].get(cr, self, ids, val, uid, context=context)
2749 for id,value in result.items():
2755 if self._columns[v]._type in ('many2one', 'one2one'):
2757 value[v] = value[v][0]
2760 upd0.append('"'+v+'"='+self._columns[v]._symbol_set[0])
2761 upd1.append(self._columns[v]._symbol_set[1](value[v]))
2763 cr.execute('update "' + self._table + '" set ' + \
2764 string.join(upd0, ',') + ' where id = %s', upd1)
2768 result = self._columns[f].get(cr, self, ids, f, uid, context=context)
2769 for id,value in result.items():
2770 if self._columns[f]._type in ('many2one', 'one2one'):
2775 cr.execute('update "' + self._table + '" set ' + \
2776 '"'+f+'"='+self._columns[f]._symbol_set[0] + ' where id = %s', (self._columns[f]._symbol_set[1](value),id))
2782 def perm_write(self, cr, user, ids, fields, context=None):
2783 raise _('This method does not exist anymore')
2785 # TODO: ameliorer avec NULL
2786 def _where_calc(self, cr, user, args, active_test=True, context=None):
2790 # if the object has a field named 'active', filter out all inactive
2791 # records unless they were explicitely asked for
2792 if 'active' in self._columns and (active_test and context.get('active_test', True)):
2794 active_in_args = False
2796 if a[0] == 'active':
2797 active_in_args = True
2798 if not active_in_args:
2799 args.insert(0, ('active', '=', 1))
2801 args = [('active', '=', 1)]
2805 e = expression.expression(args)
2806 e.parse(cr, user, self, context)
2807 tables = e.get_tables()
2808 qu1, qu2 = e.to_sql()
2809 qu1 = qu1 and [qu1] or []
2811 qu1, qu2, tables = [], [], ['"%s"' % self._table]
2813 return (qu1, qu2, tables)
2815 def _check_qorder(self, word):
2816 if not regex_order.match(word):
2817 raise except_orm(_('AccessError'), _('Bad query.'))
2820 def search(self, cr, user, args, offset=0, limit=None, order=None,
2821 context=None, count=False):
2824 # compute the where, order by, limit and offset clauses
2825 (qu1, qu2, tables) = self._where_calc(cr, user, args, context=context)
2828 qu1 = ' where '+string.join(qu1, ' and ')
2833 self._check_qorder(order)
2834 order_by = order or self._order
2836 limit_str = limit and ' limit %d' % limit or ''
2837 offset_str = offset and ' offset %d' % offset or ''
2840 # construct a clause for the rules :
2841 d1, d2 = self.pool.get('ir.rule').domain_get(cr, user, self._name)
2843 qu1 = qu1 and qu1+' and '+d1 or ' where '+d1
2847 cr.execute('select count(%s.id) from ' % self._table +
2848 ','.join(tables) +qu1 + limit_str + offset_str, qu2)
2851 # execute the "main" query to fetch the ids we were searching for
2852 cr.execute('select %s.id from ' % self._table + ','.join(tables) +qu1+' order by '+order_by+limit_str+offset_str, qu2)
2854 return [x[0] for x in res]
2856 # returns the different values ever entered for one field
2857 # this is used, for example, in the client when the user hits enter on
2859 def distinct_field_get(self, cr, uid, field, value, args=None, offset=0, limit=None):
2862 if field in self._inherit_fields:
2863 return self.pool.get(self._inherit_fields[field][0]).distinct_field_get(cr, uid, field, value, args, offset, limit)
2865 return self._columns[field].search(cr, self, args, field, value, offset, limit, uid)
2867 def name_get(self, cr, user, ids, context=None):
2872 if isinstance(ids, (int, long)):
2874 return [(r['id'], tools.ustr(r[self._rec_name])) for r in self.read(cr, user, ids,
2875 [self._rec_name], context, load='_classic_write')]
2877 def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=None):
2884 args += [(self._rec_name, operator, name)]
2885 ids = self.search(cr, user, args, limit=limit, context=context)
2886 res = self.name_get(cr, user, ids, context)
2889 def copy_data(self, cr, uid, id, default=None, context=None):
2894 if 'state' not in default:
2895 if 'state' in self._defaults:
2896 default['state'] = self._defaults['state'](self, cr, uid, context)
2897 data = self.read(cr, uid, [id], context=context)[0]
2898 fields = self.fields_get(cr, uid, context=context)
2901 ftype = fields[f]['type']
2903 if self._log_access and f in ('create_date', 'create_uid', 'write_date', 'write_uid'):
2907 data[f] = default[f]
2908 elif ftype == 'function':
2910 elif ftype == 'many2one':
2912 data[f] = data[f] and data[f][0]
2915 elif ftype in ('one2many', 'one2one'):
2917 rel = self.pool.get(fields[f]['relation'])
2918 for rel_id in data[f]:
2919 # the lines are first duplicated using the wrong (old)
2920 # parent but then are reassigned to the correct one thanks
2922 d,t = rel.copy_data(cr, uid, rel_id, context=context)
2923 res.append((0, 0, d))
2926 elif ftype == 'many2many':
2927 data[f] = [(6, 0, data[f])]
2929 trans_obj = self.pool.get('ir.translation')
2933 if f in self._columns and self._columns[f].translate:
2934 trans_name=self._name+","+f
2935 elif f in self._inherit_fields and self._inherit_fields[f][2].translate:
2936 trans_name=self._inherit_fields[f][0]+","+f
2941 trans_ids = trans_obj.search(cr, uid, [
2942 ('name', '=', trans_name),
2943 ('res_id','=',data['id'])
2946 trans_data.extend(trans_obj.read(cr,uid,trans_ids,context=context))
2950 for v in self._inherits:
2951 del data[self._inherits[v]]
2952 return data, trans_data
2954 def copy(self, cr, uid, id, default=None, context=None):
2955 trans_obj = self.pool.get('ir.translation')
2956 data, trans_data = self.copy_data(cr, uid, id, default, context)
2957 new_id = self.create(cr, uid, data, context)
2958 for record in trans_data:
2960 record['res_id'] = new_id
2961 trans_obj.create(cr, uid, record, context)
2964 def exists(self, cr, uid, id, context=None):
2965 cr.execute('SELECT count(1) FROM "%s" where id=%%s' % (self._table,), (id,))
2966 return bool(cr.fetchone()[0])
2968 def check_recursion(self, cr, uid, ids, parent=None):
2970 parent = self._parent_name
2972 while len(ids_parent):
2974 for i in range(0, len(ids), cr.IN_MAX):
2975 sub_ids_parent = ids_parent[i:i+cr.IN_MAX]
2976 cr.execute('SELECT distinct "'+parent+'"'+
2977 ' FROM "'+self._table+'" ' \
2978 'WHERE id in ('+','.join(map(str, sub_ids_parent))+')')
2979 ids_parent2.extend(filter(None, map(lambda x: x[0], cr.fetchall())))
2980 ids_parent = ids_parent2
2981 for i in ids_parent:
2987 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: