1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
23 # Object relationnal mapping to postgresql module
24 # . Hierarchical structure
25 # . Constraints consistency, validations
26 # . Object meta Data depends on its status
27 # . Optimised processing by complex query (multiple actions at once)
28 # . Default fields value
29 # . Permissions optimisation
30 # . Persistant object: DB postgresql
32 # . Multi-level caching system
33 # . 2 different inheritancies
35 # - classicals (varchar, integer, boolean, ...)
36 # - relations (one2many, many2one, many2many)
57 from tools.translate import _
63 from lxml import etree
65 sys.stderr.write("ERROR: Import lxml module\n")
66 sys.stderr.write("ERROR: Try to install the python-lxml package\n")
68 from tools.config import config
70 regex_order = re.compile('^(([a-z0-9_]+|"[a-z0-9_]+")( *desc| *asc)?( *, *|))+$', re.I)
73 POSTGRES_CONFDELTYPES = {
81 def last_day_of_current_month():
82 today = datetime.date.today()
83 last_day = str(calendar.monthrange(today.year, today.month)[1])
84 return time.strftime('%Y-%m-' + last_day)
86 def intersect(la, lb):
87 return filter(lambda x: x in lb, la)
90 class except_orm(Exception):
91 def __init__(self, name, value):
94 self.args = (name, value)
96 class BrowseRecordError(Exception):
99 # Readonly python database object browser
100 class browse_null(object):
105 def __getitem__(self, name):
108 def __getattr__(self, name):
109 return None # XXX: return self ?
117 def __nonzero__(self):
120 def __unicode__(self):
125 # TODO: execute an object method on browse_record_list
127 class browse_record_list(list):
129 def __init__(self, lst, context=None):
132 super(browse_record_list, self).__init__(lst)
133 self.context = context
136 class browse_record(object):
137 logger = netsvc.Logger()
139 def __init__(self, cr, uid, id, table, cache, context=None, list_class = None, fields_process={}):
141 table : the object (inherited from orm)
142 context : dictionary with an optional context
146 self._list_class = list_class or browse_record_list
151 self._table_name = self._table._name
152 self.__logger = logging.getLogger(
153 'osv.browse_record.' + self._table_name)
154 self._context = context
155 self._fields_process = fields_process
157 cache.setdefault(table._name, {})
158 self._data = cache[table._name]
160 if not (id and isinstance(id, (int, long,))):
161 raise BrowseRecordError(_('Wrong ID for the browse record, got %r, expected an integer.') % (id,))
162 # if not table.exists(cr, uid, id, context):
163 # raise BrowseRecordError(_('Object %s does not exists') % (self,))
165 if id not in self._data:
166 self._data[id] = {'id': id}
170 def __getitem__(self, name):
174 if name not in self._data[self._id]:
175 # build the list of fields we will fetch
177 # fetch the definition of the field which was asked for
178 if name in self._table._columns:
179 col = self._table._columns[name]
180 elif name in self._table._inherit_fields:
181 col = self._table._inherit_fields[name][2]
182 elif hasattr(self._table, str(name)):
183 attr = getattr(self._table, name)
185 if isinstance(attr, (types.MethodType, types.LambdaType, types.FunctionType)):
186 return lambda *args, **argv: attr(self._cr, self._uid, [self._id], *args, **argv)
190 self.logger.notifyChannel("browse_record", netsvc.LOG_WARNING,
191 "Field '%s' does not exist in object '%s': \n%s" % (
192 name, self, ''.join(traceback.format_exc())))
193 raise KeyError("Field '%s' does not exist in object '%s'" % (
196 # if the field is a classic one or a many2one, we'll fetch all classic and many2one fields
198 # gen the list of "local" (ie not inherited) fields which are classic or many2one
199 ffields = filter(lambda x: x[1]._classic_write, self._table._columns.items())
200 # gen the list of inherited fields
201 inherits = map(lambda x: (x[0], x[1][2]), self._table._inherit_fields.items())
202 # complete the field list with the inherited fields which are classic or many2one
203 ffields += filter(lambda x: x[1]._classic_write, inherits)
204 # otherwise we fetch only that field
206 ffields = [(name, col)]
207 ids = filter(lambda id: name not in self._data[id], self._data.keys())
209 fffields = map(lambda x: x[0], ffields)
210 datas = self._table.read(self._cr, self._uid, ids, fffields, context=self._context, load="_classic_write")
211 if self._fields_process:
212 lang = self._context.get('lang', 'en_US') or 'en_US'
213 lang_obj_ids = self.pool.get('res.lang').search(self._cr, self._uid,[('code','=',lang)])
215 raise Exception(_('Language with code "%s" is not defined in your system !\nDefine it through the Administration menu.') % (lang,))
216 lang_obj = self.pool.get('res.lang').browse(self._cr, self._uid,lang_obj_ids[0])
219 if f._type in self._fields_process:
221 d[n] = self._fields_process[f._type](d[n])
223 d[n].set_value(self._cr, self._uid, d[n], self, f, lang_obj)
227 # Where did those ids come from? Perhaps old entries in ir_model_dat?
228 self.__logger.warn("No datas found for ids %s in %s",
230 raise KeyError('Field %s not found in %s'%(name,self))
231 # create browse records for 'remote' objects
233 if len(str(data['id']).split('-')) > 1:
234 data['id'] = int(str(data['id']).split('-')[0])
237 if f._type in ('many2one', 'one2one'):
239 obj = self._table.pool.get(f._obj)
241 if type(data[n]) in (type([]),type( (1,) )):
246 # FIXME: this happen when a _inherits object
247 # overwrite a field of it parent. Need
248 # testing to be sure we got the right
249 # object and not the parent one.
250 if not isinstance(ids2, browse_record):
251 new_data[n] = browse_record(self._cr,
252 self._uid, ids2, obj, self._cache,
253 context=self._context,
254 list_class=self._list_class,
255 fields_process=self._fields_process)
259 new_data[n] = browse_null()
261 new_data[n] = browse_null()
262 elif f._type in ('one2many', 'many2many') and len(data[n]):
263 new_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)
264 elif f._type in ('reference'):
266 if isinstance(data[n], browse_record):
267 new_data[n] = data[n]
269 ref_obj, ref_id = data[n].split(',')
270 ref_id = long(ref_id)
271 obj = self._table.pool.get(ref_obj)
273 new_data[n] = browse_record(self._cr, self._uid, ref_id, obj, self._cache, context=self._context, list_class=self._list_class, fields_process=self._fields_process)
275 new_data[n] = browse_null()
277 new_data[n] = data[n]
278 self._data[data['id']].update(new_data)
280 if not name in self._data[self._id]:
281 #how did this happen?
282 self.logger.notifyChannel("browse_record", netsvc.LOG_ERROR,
283 "Ffields: %s, datas: %s"%(fffields, datas))
284 self.logger.notifyChannel("browse_record", netsvc.LOG_ERROR,
285 "Data: %s, Table: %s"%(self._data[self._id], self._table))
286 raise KeyError(_('Unknown attribute %s in %s ') % (name, self))
287 return self._data[self._id][name]
289 def __getattr__(self, name):
293 raise AttributeError(e)
295 def __contains__(self, name):
296 return (name in self._table._columns) or (name in self._table._inherit_fields) or hasattr(self._table, name)
298 def __hasattr__(self, name):
305 return "browse_record(%s, %d)" % (self._table_name, self._id)
307 def __eq__(self, other):
308 if not isinstance(other, browse_record):
310 return (self._table_name, self._id) == (other._table_name, other._id)
312 def __ne__(self, other):
313 if not isinstance(other, browse_record):
315 return (self._table_name, self._id) != (other._table_name, other._id)
317 # we need to define __unicode__ even though we've already defined __str__
318 # because we have overridden __getattr__
319 def __unicode__(self):
320 return unicode(str(self))
323 return hash((self._table_name, self._id))
331 (type returned by postgres when the column was created, type expression to create the column)
335 fields.boolean: 'bool',
336 fields.integer: 'int4',
337 fields.integer_big: 'int8',
341 fields.datetime: 'timestamp',
342 fields.binary: 'bytea',
343 fields.many2one: 'int4',
345 if type(f) in type_dict:
346 f_type = (type_dict[type(f)], type_dict[type(f)])
347 elif isinstance(f, fields.float):
349 f_type = ('numeric', 'NUMERIC')
351 f_type = ('float8', 'DOUBLE PRECISION')
352 elif isinstance(f, (fields.char, fields.reference)):
353 f_type = ('varchar', 'VARCHAR(%d)' % (f.size,))
354 elif isinstance(f, fields.selection):
355 if isinstance(f.selection, list) and isinstance(f.selection[0][0], (str, unicode)):
356 f_size = reduce(lambda x, y: max(x, len(y[0])), f.selection, f.size or 16)
357 elif isinstance(f.selection, list) and isinstance(f.selection[0][0], int):
360 f_size = getattr(f, 'size', None) or 16
363 f_type = ('int4', 'INTEGER')
365 f_type = ('varchar', 'VARCHAR(%d)' % f_size)
366 elif isinstance(f, fields.function) and eval('fields.'+(f._type)) in type_dict:
367 t = eval('fields.'+(f._type))
368 f_type = (type_dict[t], type_dict[t])
369 elif isinstance(f, fields.function) and f._type == 'float':
371 f_type = ('numeric', 'NUMERIC')
373 f_type = ('float8', 'DOUBLE PRECISION')
374 elif isinstance(f, fields.function) and f._type == 'selection':
375 f_type = ('text', 'text')
376 elif isinstance(f, fields.function) and f._type == 'char':
377 f_type = ('varchar', 'VARCHAR(%d)' % (f.size))
379 logger = netsvc.Logger()
380 logger.notifyChannel("init", netsvc.LOG_WARNING, '%s type not supported!' % (type(f)))
385 class orm_template(object):
391 _parent_name = 'parent_id'
392 _parent_store = False
393 _parent_order = False
402 CONCURRENCY_CHECK_FIELD = '__last_update'
404 def view_init(self, cr , uid , fields_list, context=None):
405 """Override this method to do specific things when a view on the object is opened."""
408 def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None):
409 raise _('The read_group method is not implemented on this object !')
411 def _field_create(self, cr, context={}):
412 cr.execute("SELECT id FROM ir_model WHERE model=%s", (self._name,))
414 cr.execute('SELECT nextval(%s)', ('ir_model_id_seq',))
415 model_id = cr.fetchone()[0]
416 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'))
418 model_id = cr.fetchone()[0]
419 if 'module' in context:
420 name_id = 'model_'+self._name.replace('.','_')
421 cr.execute('select * from ir_model_data where name=%s and res_id=%s and module=%s', (name_id,model_id,context['module']))
423 cr.execute("INSERT INTO ir_model_data (name,date_init,date_update,module,model,res_id) VALUES (%s, now(), now(), %s, %s, %s)", \
424 (name_id, context['module'], 'ir.model', model_id)
429 cr.execute("SELECT * FROM ir_model_fields WHERE model=%s", (self._name,))
431 for rec in cr.dictfetchall():
432 cols[rec['name']] = rec
434 for (k, f) in self._columns.items():
436 'model_id': model_id,
439 'field_description': f.string.replace("'", " "),
441 'relation': f._obj or '',
442 'view_load': (f.view_load and 1) or 0,
443 'select_level': tools.ustr(f.select or 0),
444 'readonly':(f.readonly and 1) or 0,
445 'required':(f.required and 1) or 0,
446 'selectable' : (f.selectable and 1) or 0,
447 'relation_field': (f._type=='one2many' and isinstance(f,fields.one2many)) and f._fields_id or '',
449 # When its a custom field,it does not contain f.select
450 if context.get('field_state','base') == 'manual':
451 if context.get('field_name','') == k:
452 vals['select_level'] = context.get('select','0')
453 #setting value to let the problem NOT occur next time
455 vals['select_level'] = cols[k]['select_level']
458 cr.execute('select nextval(%s)', ('ir_model_fields_id_seq',))
459 id = cr.fetchone()[0]
461 cr.execute("""INSERT INTO ir_model_fields (
462 id, model_id, model, name, field_description, ttype,
463 relation,view_load,state,select_level,relation_field
465 %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s
467 id, vals['model_id'], vals['model'], vals['name'], vals['field_description'], vals['ttype'],
468 vals['relation'], bool(vals['view_load']), 'base',
469 vals['select_level'],vals['relation_field']
471 if 'module' in context:
472 name1 = 'field_' + self._table + '_' + k
473 cr.execute("select name from ir_model_data where name=%s", (name1,))
475 name1 = name1 + "_" + str(id)
476 cr.execute("INSERT INTO ir_model_data (name,date_init,date_update,module,model,res_id) VALUES (%s, now(), now(), %s, %s, %s)", \
477 (name1, context['module'], 'ir.model.fields', id)
480 for key, val in vals.items():
481 if cols[k][key] != vals[key]:
482 cr.execute('update ir_model_fields set field_description=%s where model=%s and name=%s', (vals['field_description'], vals['model'], vals['name']))
484 cr.execute("""UPDATE ir_model_fields SET
485 model_id=%s, field_description=%s, ttype=%s, relation=%s,
486 view_load=%s, select_level=%s, readonly=%s ,required=%s, selectable=%s, relation_field=%s
488 model=%s AND name=%s""", (
489 vals['model_id'], vals['field_description'], vals['ttype'],
490 vals['relation'], bool(vals['view_load']),
491 vals['select_level'], bool(vals['readonly']),bool(vals['required']),bool(vals['selectable']),vals['relation_field'],vals['model'], vals['name']
496 def _auto_init(self, cr, context={}):
497 self._field_create(cr, context)
499 def __init__(self, cr):
500 if not self._name and not hasattr(self, '_inherit'):
501 name = type(self).__name__.split('.')[0]
502 msg = "The class %s has to have a _name attribute" % name
504 logger = netsvc.Logger()
505 logger.notifyChannel('orm', netsvc.LOG_ERROR, msg )
506 raise except_orm('ValueError', msg )
508 if not self._description:
509 self._description = self._name
511 self._table = self._name.replace('.', '_')
513 def browse(self, cr, uid, select, context=None, list_class=None, fields_process={}):
515 Fetch records as objects allowing to use dot notation to browse fields and relations
517 :param cr: database cursor
518 :param user: current user id
519 :param select: id or list of ids
520 :param context: context arguments, like lang, time zone
521 :rtype: object or list of objects requested
526 self._list_class = list_class or browse_record_list
528 # need to accepts ints and longs because ids coming from a method
529 # launched by button in the interface have a type long...
530 if isinstance(select, (int, long)):
531 return browse_record(cr, uid, select, self, cache, context=context, list_class=self._list_class, fields_process=fields_process)
532 elif isinstance(select, list):
533 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)
537 def __export_row(self, cr, uid, row, fields, context=None):
541 def check_type(field_type):
542 if field_type == 'float':
544 elif field_type == 'integer':
546 elif field_type == 'boolean':
550 def selection_field(in_field):
551 col_obj = self.pool.get(in_field.keys()[0])
552 if f[i] in col_obj._columns.keys():
553 return col_obj._columns[f[i]]
554 elif f[i] in col_obj._inherits.keys():
555 selection_field(col_obj._inherits)
561 data = map(lambda x: '', range(len(fields)))
563 for fpos in range(len(fields)):
572 model_data = self.pool.get('ir.model.data')
573 data_ids = model_data.search(cr, uid, [('model','=',r._table_name),('res_id','=',r['id'])])
575 d = model_data.read(cr, uid, data_ids, ['name','module'])[0]
577 r = '%s.%s'%(d['module'],d['name'])
584 # To display external name of selection field when its exported
585 if not context.get('import_comp',False):# Allow external name only if its not import compatible
587 if f[i] in self._columns.keys():
588 cols = self._columns[f[i]]
589 elif f[i] in self._inherit_fields.keys():
590 cols = selection_field(self._inherits)
591 if cols and cols._type == 'selection':
592 sel_list = cols.selection
593 if type(sel_list) == type([]):
594 r = [x[1] for x in sel_list if r==x[0]][0]
597 if f[i] in self._columns:
598 r = check_type(self._columns[f[i]]._type)
599 elif f[i] in self._inherit_fields:
600 r = check_type(self._inherit_fields[f[i]][2]._type)
603 if isinstance(r, (browse_record_list, list)):
605 fields2 = map(lambda x: (x[:i+1]==f[:i+1] and x[i+1:]) \
611 lines2 = self.__export_row(cr, uid, row2, fields2,
614 for fpos2 in range(len(fields)):
615 if lines2 and lines2[0][fpos2]:
616 data[fpos2] = lines2[0][fpos2]
620 if isinstance(rr.name, browse_record):
622 rr_name = self.pool.get(rr._table_name).name_get(cr, uid, [rr.id], context=context)
623 rr_name = rr_name and rr_name[0] and rr_name[0][1] or ''
624 dt += tools.ustr(rr_name or '') + ','
634 if isinstance(r, browse_record):
635 r = self.pool.get(r._table_name).name_get(cr, uid, [r.id], context=context)
636 r = r and r[0] and r[0][1] or ''
637 data[fpos] = tools.ustr(r or '')
638 return [data] + lines
640 def export_data(self, cr, uid, ids, fields_to_export, context=None):
642 Export fields for selected objects
644 :param cr: database cursor
645 :param uid: current user id
646 :param ids: list of ids
647 :param fields_to_export: list of fields
648 :param context: context arguments, like lang, time zone, may contain import_comp(default: False) to make exported data compatible with import_data()
649 :rtype: dictionary with a *datas* matrix
651 This method is used when exporting data via client menu
656 imp_comp = context.get('import_comp',False)
657 cols = self._columns.copy()
658 for f in self._inherit_fields:
659 cols.update({f: self._inherit_fields[f][2]})
660 fields_to_export = map(lambda x: x.split('/'), fields_to_export)
661 fields_export = fields_to_export+[]
664 for field in fields_export:
665 if imp_comp and len(field)>1:
666 warning_fields.append('/'.join(map(lambda x:x in cols and cols[x].string or x,field)))
667 elif len (field) <=1:
668 if imp_comp and cols.get(field and field[0],False):
669 if ((isinstance(cols[field[0]], fields.function) and not cols[field[0]].store) \
670 or isinstance(cols[field[0]], fields.related)\
671 or isinstance(cols[field[0]], fields.one2many)):
672 warning_fields.append('/'.join(map(lambda x:x in cols and cols[x].string or x,field)))
674 if imp_comp and len(warning_fields):
675 warning = 'Following columns cannot be exported since you select to be import compatible.\n%s' %('\n'.join(warning_fields))
677 return {'warning' : warning}
678 for row in self.browse(cr, uid, ids, context):
679 datas += self.__export_row(cr, uid, row, fields_to_export, context)
680 return {'datas':datas}
682 def import_data(self, cr, uid, fields, datas, mode='init', current_module='', noupdate=False, context=None, filename=None):
684 Import given data in given module
686 :param cr: database cursor
687 :param uid: current user id
688 :param ids: list of ids
689 :param fields: list of fields
690 :param data: data to import
691 :param mode: 'init' or 'update' for record creation
692 :param current_module: module name
693 :param noupdate: flag for record creation
694 :param context: context arguments, like lang, time zone,
695 :param filename: optional file to store partial import state for recovery
698 This method is used when importing data via client menu
703 fields = map(lambda x: x.split('/'), fields)
704 logger = netsvc.Logger()
705 ir_model_data_obj = self.pool.get('ir.model.data')
707 def _check_db_id(self, model_name, db_id):
708 obj_model = self.pool.get(model_name)
709 ids = obj_model.search(cr, uid, [('id','=',int(db_id))])
711 raise Exception(_("Database ID doesn't exist: %s : %s") %(model_name, db_id))
714 def process_liness(self, datas, prefix, current_module, model_name, fields_def, position=0):
715 line = datas[position]
724 ir_model_data_obj = self.pool.get('ir.model.data')
726 # Import normal fields
728 for i in range(len(fields)):
730 raise Exception(_('Please check that all your lines have %d columns.') % (len(fields),))
735 if prefix and not prefix[0] in field:
738 if (len(field)==len(prefix)+1) and field[len(prefix)].endswith(':db_id'):
742 field_name = field[0].split(':')[0]
743 model_rel = fields_def[field_name]['relation']
745 if fields_def[field[len(prefix)][:-6]]['type']=='many2many':
747 for db_id in line[i].split(config.get('csv_internal_sep')):
749 _check_db_id(self, model_rel, db_id)
752 warning += [tools.exception_to_unicode(e)]
753 logger.notifyChannel("import", netsvc.LOG_ERROR,
754 tools.exception_to_unicode(e))
756 res = [(6, 0, res_id)]
759 _check_db_id(self, model_rel, line[i])
762 warning += [tools.exception_to_unicode(e)]
763 logger.notifyChannel("import", netsvc.LOG_ERROR,
764 tools.exception_to_unicode(e))
765 row[field_name] = res or False
768 if (len(field)==len(prefix)+1) and field[len(prefix)].endswith(':id'):
771 if fields_def[field[len(prefix)][:-3]]['type']=='many2many':
773 for word in line[i].split(config.get('csv_internal_sep')):
775 module, xml_id = word.rsplit('.', 1)
777 module, xml_id = current_module, word
778 id = ir_model_data_obj._get_id(cr, uid, module,
780 res_id2 = ir_model_data_obj.read(cr, uid, [id],
781 ['res_id'])[0]['res_id']
783 res_id.append(res_id2)
785 res_id = [(6, 0, res_id)]
788 module, xml_id = line[i].rsplit('.', 1)
790 module, xml_id = current_module, line[i]
791 record_id = ir_model_data_obj._get_id(cr, uid, module, xml_id)
792 ir_model_data = ir_model_data_obj.read(cr, uid, [record_id], ['res_id'])
794 res_id = ir_model_data[0]['res_id']
796 raise ValueError('No references to %s.%s' % (module, xml_id))
797 row[field[-1][:-3]] = res_id or False
799 if (len(field) == len(prefix)+1) and \
800 len(field[len(prefix)].split(':lang=')) == 2:
801 f, lang = field[len(prefix)].split(':lang=')
802 translate.setdefault(lang, {})[f]=line[i] or False
804 if (len(field) == len(prefix)+1) and \
805 (prefix == field[0:len(prefix)]):
806 if field[len(prefix)] == "id":
809 is_xml_id = data_id = line[i]
810 d = data_id.split('.')
811 module = len(d)>1 and d[0] or ''
812 name = len(d)>1 and d[1] or d[0]
813 data_ids = ir_model_data_obj.search(cr, uid, [('module','=',module),('model','=',model_name),('name','=',name)])
815 d = ir_model_data_obj.read(cr, uid, data_ids, ['res_id'])[0]
817 if is_db_id and not db_id:
818 data_ids = ir_model_data_obj.search(cr, uid, [('module','=',module),('model','=',model_name),('res_id','=',is_db_id)])
819 if not len(data_ids):
820 ir_model_data_obj.create(cr, uid, {'module':module, 'model':model_name, 'name':name, 'res_id':is_db_id})
822 if is_db_id and int(db_id) != int(is_db_id):
823 warning += [_("Id is not the same than existing one: %s")%(is_db_id)]
824 logger.notifyChannel("import", netsvc.LOG_ERROR,
825 _("Id is not the same than existing one: %s")%(is_db_id))
828 if field[len(prefix)] == "db_id":
831 _check_db_id(self, model_name, line[i])
832 data_res_id = is_db_id = int(line[i])
834 warning += [tools.exception_to_unicode(e)]
835 logger.notifyChannel("import", netsvc.LOG_ERROR,
836 tools.exception_to_unicode(e))
838 data_ids = ir_model_data_obj.search(cr, uid, [('model','=',model_name),('res_id','=',line[i])])
840 d = ir_model_data_obj.read(cr, uid, data_ids, ['name','module'])[0]
843 data_id = '%s.%s'%(d['module'],d['name'])
846 if is_xml_id and not data_id:
848 if is_xml_id and is_xml_id!=data_id:
849 warning += [_("Id is not the same than existing one: %s")%(line[i])]
850 logger.notifyChannel("import", netsvc.LOG_ERROR,
851 _("Id is not the same than existing one: %s")%(line[i]))
854 if fields_def[field[len(prefix)]]['type'] == 'integer':
855 res = line[i] and int(line[i])
856 elif fields_def[field[len(prefix)]]['type'] == 'boolean':
857 res = line[i].lower() not in ('0', 'false', 'off')
858 elif fields_def[field[len(prefix)]]['type'] == 'float':
859 res = line[i] and float(line[i])
860 elif fields_def[field[len(prefix)]]['type'] == 'selection':
862 if isinstance(fields_def[field[len(prefix)]]['selection'],
864 sel = fields_def[field[len(prefix)]]['selection']
866 sel = fields_def[field[len(prefix)]]['selection'](self,
869 if line[i] in [tools.ustr(key),tools.ustr(val)]: #Acepting key or value for selection field
872 if line[i] and not res:
873 logger.notifyChannel("import", netsvc.LOG_WARNING,
874 _("key '%s' not found in selection field '%s'") % \
875 (line[i], field[len(prefix)]))
877 warning += [_("Key/value '%s' not found in selection field '%s'")%(line[i],field[len(prefix)])]
879 elif fields_def[field[len(prefix)]]['type']=='many2one':
882 relation = fields_def[field[len(prefix)]]['relation']
883 res2 = self.pool.get(relation).name_search(cr, uid,
884 line[i], [], operator='=', context=context)
885 res = (res2 and res2[0][0]) or False
887 warning += [_("Relation not found: %s on '%s'")%(line[i],relation)]
888 logger.notifyChannel("import", netsvc.LOG_WARNING,
889 _("Relation not found: %s on '%s'")%(line[i],relation))
890 elif fields_def[field[len(prefix)]]['type']=='many2many':
893 relation = fields_def[field[len(prefix)]]['relation']
894 for word in line[i].split(config.get('csv_internal_sep')):
895 res2 = self.pool.get(relation).name_search(cr,
896 uid, word, [], operator='=', context=context)
897 res3 = (res2 and res2[0][0]) or False
899 warning += [_("Relation not found: %s on '%s'")%(line[i],relation)]
900 logger.notifyChannel("import",
902 _("Relation not found: %s on '%s'")%(line[i],relation))
908 res = line[i] or False
909 row[field[len(prefix)]] = res
910 elif (prefix==field[0:len(prefix)]):
911 if field[0] not in todo:
912 todo.append(field[len(prefix)])
914 # Import one2many, many2many fields
918 relation_obj = self.pool.get(fields_def[field]['relation'])
919 newfd = relation_obj.fields_get(
920 cr, uid, context=context)
921 res = process_liness(self, datas, prefix + [field], current_module, relation_obj._name, newfd, position)
922 (newrow, max2, w2, translate2, data_id2, data_res_id2) = res
923 nbrmax = max(nbrmax, max2)
924 warning = warning + w2
925 reduce(lambda x, y: x and y, newrow)
926 row[field] = (reduce(lambda x, y: x or y, newrow.values()) and \
927 [(0, 0, newrow)]) or []
929 while (position+i)<len(datas):
931 for j in range(len(fields)):
933 if (len(field2) <= (len(prefix)+1)) and datas[position+i][j]:
938 (newrow, max2, w2, translate2, data_id2, data_res_id2) = process_liness(
939 self, datas, prefix+[field], current_module, relation_obj._name, newfd, position+i)
941 if reduce(lambda x, y: x or y, newrow.values()):
942 row[field].append((0, 0, newrow))
944 nbrmax = max(nbrmax, i)
947 for i in range(max(nbrmax, 1)):
950 result = (row, nbrmax, warning, translate, data_id, data_res_id)
953 fields_def = self.fields_get(cr, uid, context=context)
956 initial_size = len(datas)
957 if config.get('import_partial', False) and filename:
958 data = pickle.load(file(config.get('import_partial')))
959 original_value = data.get(filename, 0)
965 (res, other, warning, translate, data_id, res_id) = \
966 process_liness(self, datas, [], current_module, self._name, fields_def)
969 return (-1, res, 'Line ' + str(counter) +' : ' + '!\n'.join(warning), '')
972 id = ir_model_data_obj._update(cr, uid, self._name,
973 current_module, res, xml_id=data_id, mode=mode,
974 noupdate=noupdate, res_id=res_id, context=context)
979 if isinstance(e,psycopg2.IntegrityError):
980 msg= _('Insertion Failed! ')
981 for key in self.pool._sql_error.keys():
983 msg = self.pool._sql_error[key]
985 return (-1, res, 'Line ' + str(counter) +' : ' + msg, '' )
986 if isinstance(e, osv.orm.except_orm ):
987 msg = _('Insertion Failed! ' + e[1])
988 return (-1, res, 'Line ' + str(counter) +' : ' + msg, '' )
989 #Raising Uncaught exception
990 return (-1, res, 'Line ' + str(counter) +' : ' + str(e), '' )
992 for lang in translate:
993 context2 = context.copy()
994 context2['lang'] = lang
995 self.write(cr, uid, [id], translate[lang], context2)
996 if config.get('import_partial', False) and filename and (not (counter%100)) :
997 data = pickle.load(file(config.get('import_partial')))
998 data[filename] = initial_size - len(datas) + original_value
999 pickle.dump(data, file(config.get('import_partial'),'wb'))
1002 #except Exception, e:
1003 # logger.notifyChannel("import", netsvc.LOG_ERROR, e)
1006 # return (-1, res, e[0], warning)
1008 # return (-1, res, e[0], '')
1011 # TODO: Send a request with the result and multi-thread !
1013 return (done, 0, 0, 0)
1015 def read(self, cr, user, ids, fields=None, context=None, load='_classic_read'):
1016 raise _('The read method is not implemented on this object !')
1018 def get_invalid_fields(self,cr,uid):
1019 return list(self._invalids)
1021 def _validate(self, cr, uid, ids, context=None):
1022 context = context or {}
1023 lng = context.get('lang', False) or 'en_US'
1024 trans = self.pool.get('ir.translation')
1026 for constraint in self._constraints:
1027 fun, msg, fields = constraint
1028 if not fun(self, cr, uid, ids):
1029 translated_msg = trans._get_source(cr, uid, self._name, 'constraint', lng, source=msg) or msg
1031 _("Error occurred while validating the field(s) %s: %s") % (','.join(fields), translated_msg)
1033 self._invalids.update(fields)
1036 raise except_orm('ValidateError', '\n'.join(error_msgs))
1038 self._invalids.clear()
1040 def default_get(self, cr, uid, fields_list, context=None):
1042 Set default values for the object's fields.
1044 :param fields_list: fields for which the object doesn't have any value yet, and default values need to be provided.
1045 If fields outside this list are returned, the user-provided values will be overwritten.
1046 :rtype: a dict of {field_name:default_value}
1051 def perm_read(self, cr, user, ids, context=None, details=True):
1052 raise _('The perm_read method is not implemented on this object !')
1054 def unlink(self, cr, uid, ids, context=None):
1055 raise _('The unlink method is not implemented on this object !')
1057 def write(self, cr, user, ids, vals, context=None):
1058 raise _('The write method is not implemented on this object !')
1060 def create(self, cr, user, vals, context=None):
1061 raise _('The create method is not implemented on this object !')
1063 # returns the definition of each field in the object
1064 # the optional fields parameter can limit the result to some fields
1065 def fields_get_keys(self, cr, user, context=None, read_access=True):
1068 res = self._columns.keys()
1069 for parent in self._inherits:
1070 res.extend(self.pool.get(parent).fields_get_keys(cr, user, fields, context))
1073 def fields_get(self, cr, user, fields=None, context=None, read_access=True):
1077 translation_obj = self.pool.get('ir.translation')
1078 model_access_obj = self.pool.get('ir.model.access')
1079 for parent in self._inherits:
1080 res.update(self.pool.get(parent).fields_get(cr, user, fields, context))
1082 if self._columns.keys():
1083 for f in self._columns.keys():
1084 if fields and f not in fields:
1086 res[f] = {'type': self._columns[f]._type}
1087 for arg in ('string', 'readonly', 'states', 'size', 'required', 'group_operator',
1088 'change_default', 'translate', 'help', 'select', 'selectable'):
1089 if getattr(self._columns[f], arg):
1090 res[f][arg] = getattr(self._columns[f], arg)
1092 res[f]['readonly'] = True
1093 res[f]['states'] = {}
1094 for arg in ('digits', 'invisible','filters'):
1095 if getattr(self._columns[f], arg, None):
1096 res[f][arg] = getattr(self._columns[f], arg)
1098 res_trans = translation_obj._get_source(cr, user, self._name + ',' + f, 'field', context.get('lang', False) or 'en_US', self._columns[f].string)
1100 res[f]['string'] = res_trans
1101 help_trans = translation_obj._get_source(cr, user, self._name + ',' + f, 'help', context.get('lang', False) or 'en_US')
1103 res[f]['help'] = help_trans
1105 if hasattr(self._columns[f], 'selection'):
1106 if isinstance(self._columns[f].selection, (tuple, list)):
1107 sel = self._columns[f].selection
1108 # translate each selection option
1110 for (key, val) in sel:
1113 val2 = translation_obj._get_source(cr, user, self._name + ',' + f, 'selection', context.get('lang', False) or 'en_US', val)
1114 sel2.append((key, val2 or val))
1116 res[f]['selection'] = sel
1118 # call the 'dynamic selection' function
1119 res[f]['selection'] = self._columns[f].selection(self, cr,
1121 if res[f]['type'] in ('one2many', 'many2many', 'many2one', 'one2one'):
1122 res[f]['relation'] = self._columns[f]._obj
1123 res[f]['domain'] = self._columns[f]._domain
1124 res[f]['context'] = self._columns[f]._context
1126 #TODO : read the fields from the database
1130 # filter out fields which aren't in the fields list
1131 for r in res.keys():
1137 # Overload this method if you need a window title which depends on the context
1139 def view_header_get(self, cr, user, view_id=None, view_type='form', context=None):
1142 def __view_look_dom(self, cr, user, node, view_id, context=None):
1150 if isinstance(s, unicode):
1151 return s.encode('utf8')
1154 # return True if node can be displayed to current user
1155 def check_group(node):
1156 if node.get('groups'):
1157 groups = node.get('groups').split(',')
1158 access_pool = self.pool.get('ir.model.access')
1159 can_see = any(access_pool.check_groups(cr, user, group) for group in groups)
1161 node.set('invisible', '1')
1162 if 'attrs' in node.attrib:
1163 del(node.attrib['attrs']) #avoid making field visible later
1164 del(node.attrib['groups'])
1169 if node.tag in ('field', 'node', 'arrow'):
1170 if node.get('object'):
1175 if f.tag in ('field'):
1176 xml += etree.tostring(f, encoding="utf-8")
1178 new_xml = etree.fromstring(encode(xml))
1179 ctx = context.copy()
1180 ctx['base_model_name'] = self._name
1181 xarch, xfields = self.pool.get(node.get('object',False)).__view_look_dom_arch(cr, user, new_xml, view_id, ctx)
1182 views[str(f.tag)] = {
1186 attrs = {'views': views}
1188 fields = views.get('field',False) and views['field'].get('fields',False)
1189 if node.get('name'):
1192 if node.get('name') in self._columns:
1193 column = self._columns[node.get('name')]
1195 column = self._inherit_fields[node.get('name')][2]
1200 relation = self.pool.get(column._obj)
1205 if f.tag in ('form', 'tree', 'graph'):
1207 ctx = context.copy()
1208 ctx['base_model_name'] = self._name
1209 xarch, xfields = relation.__view_look_dom_arch(cr, user, f, view_id, ctx)
1210 views[str(f.tag)] = {
1214 attrs = {'views': views}
1215 if node.get('widget') and node.get('widget') == 'selection':
1216 if not check_group(node):
1217 # the field is just invisible. default value must be in the selection
1218 name = node.get('name')
1219 default = self.default_get(cr, user, [name], context=context).get(name)
1221 attrs['selection'] = relation.name_get(cr, 1, default, context=context)
1223 attrs['selection'] = []
1224 # We can not use the 'string' domain has it is defined according to the record !
1227 if column._domain and not isinstance(column._domain, (str, unicode)):
1228 dom = column._domain
1229 dom += eval(node.get('domain','[]'), {'uid':user, 'time':time})
1230 context.update(eval(node.get('context','{}')))
1231 attrs['selection'] = relation._name_search(cr, user, '', dom, context=context, limit=None, name_get_uid=1)
1232 if (node.get('required') and not int(node.get('required'))) or not column.required:
1233 attrs['selection'].append((False,''))
1234 fields[node.get('name')] = attrs
1236 elif node.tag in ('form', 'tree'):
1237 result = self.view_header_get(cr, user, False, node.tag, context)
1239 node.set('string', result)
1241 elif node.tag == 'calendar':
1242 for additional_field in ('date_start', 'date_delay', 'date_stop', 'color'):
1243 if node.get(additional_field):
1244 fields[node.get(additional_field)] = {}
1246 if 'groups' in node.attrib:
1250 if ('lang' in context) and not result:
1251 if node.get('string'):
1252 trans = self.pool.get('ir.translation')._get_source(cr, user, self._name, 'view', context['lang'], node.get('string').encode('utf8'))
1253 if not trans and ('base_model_name' in context):
1254 trans = self.pool.get('ir.translation')._get_source(cr, user, context['base_model_name'], 'view', context['lang'], node.get('string').encode('utf8'))
1256 node.set('string', trans)
1258 trans = self.pool.get('ir.translation')._get_source(cr, user, self._name, 'view', context['lang'], node.get('sum').encode('utf8'))
1260 node.set('sum', trans)
1264 fields.update(self.__view_look_dom(cr, user, f, view_id, context))
1268 def __view_look_dom_arch(self, cr, user, node, view_id, context=None):
1269 fields_def = self.__view_look_dom(cr, user, node, view_id, context=context)
1271 rolesobj = self.pool.get('res.roles')
1272 usersobj = self.pool.get('res.users')
1274 buttons = (n for n in node.getiterator('button') if n.get('type') != 'object')
1275 for button in buttons:
1277 if user != 1: # admin user has all roles
1278 user_roles = usersobj.read(cr, user, [user], ['roles_id'])[0]['roles_id']
1279 # TODO handle the case of more than one workflow for a model
1280 cr.execute("""SELECT DISTINCT t.role_id
1282 INNER JOIN wkf_activity a ON a.wkf_id = wkf.id
1283 INNER JOIN wkf_transition t ON (t.act_to = a.id)
1286 """, (self._name, button.get('name'),))
1287 roles = cr.fetchall()
1289 # draft -> valid = signal_next (role X)
1290 # draft -> cancel = signal_cancel (no role)
1292 # valid -> running = signal_next (role Y)
1293 # valid -> cancel = signal_cancel (role Z)
1295 # running -> done = signal_next (role Z)
1296 # running -> cancel = signal_cancel (role Z)
1298 # As we don't know the object state, in this scenario,
1299 # the button "signal_cancel" will be always shown as there is no restriction to cancel in draft
1300 # the button "signal_next" will be show if the user has any of the roles (X Y or Z)
1301 # The verification will be made later in workflow process...
1303 can_click = any((not role) or rolesobj.check(cr, user, user_roles, role) for (role,) in roles)
1305 button.set('readonly', str(int(not can_click)))
1307 arch = etree.tostring(node, encoding="utf-8").replace('\t', '')
1310 if node.tag=='diagram':
1311 if node.getchildren()[0].tag=='node':
1312 node_fields=self.pool.get(node.getchildren()[0].get('object')).fields_get(cr, user, fields_def.keys(), context)
1313 if node.getchildren()[1].tag=='arrow':
1314 arrow_fields = self.pool.get(node.getchildren()[1].get('object')).fields_get(cr, user, fields_def.keys(), context)
1315 for key,value in node_fields.items():
1317 for key,value in arrow_fields.items():
1320 fields = self.fields_get(cr, user, fields_def.keys(), context)
1321 for field in fields_def:
1323 # sometime, the view may containt the (invisible) field 'id' needed for a domain (when 2 objects have cross references)
1324 fields['id'] = {'readonly': True, 'type': 'integer', 'string': 'ID'}
1325 elif field in fields:
1326 fields[field].update(fields_def[field])
1328 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))
1329 res = cr.fetchall()[:]
1331 res.insert(0, ("Can't find field '%s' in the following view parts composing the view of object model '%s':" % (field, model), None))
1332 msg = "\n * ".join([r[0] for r in res])
1333 msg += "\n\nEither you wrongly customised this view, or some modules bringing those views are not compatible with your current data model"
1334 netsvc.Logger().notifyChannel('orm', netsvc.LOG_ERROR, msg)
1335 raise except_orm('View error', msg)
1338 def __get_default_calendar_view(self):
1339 """Generate a default calendar view (For internal use only).
1342 arch = ('<?xml version="1.0" encoding="utf-8"?>\n'
1343 '<calendar string="%s"') % (self._description)
1345 if (self._date_name not in self._columns):
1347 for dt in ['date','date_start','x_date','x_date_start']:
1348 if dt in self._columns:
1349 self._date_name = dt
1354 raise except_orm(_('Invalid Object Architecture!'),_("Insufficient fields for Calendar View!"))
1357 arch +=' date_start="%s"' % (self._date_name)
1359 for color in ["user_id","partner_id","x_user_id","x_partner_id"]:
1360 if color in self._columns:
1361 arch += ' color="' + color + '"'
1364 dt_stop_flag = False
1366 for dt_stop in ["date_stop","date_end","x_date_stop","x_date_end"]:
1367 if dt_stop in self._columns:
1368 arch += ' date_stop="' + dt_stop + '"'
1372 if not dt_stop_flag:
1373 for dt_delay in ["date_delay","planned_hours","x_date_delay","x_planned_hours"]:
1374 if dt_delay in self._columns:
1375 arch += ' date_delay="' + dt_delay + '"'
1379 ' <field name="%s"/>\n'
1380 '</calendar>') % (self._rec_name)
1384 def __get_default_search_view(self, cr, uid, context={}):
1387 if isinstance(s, unicode):
1388 return s.encode('utf8')
1391 view = self.fields_view_get(cr, uid, False, 'form', context)
1393 root = etree.fromstring(encode(view['arch']))
1394 res = etree.XML("<search string='%s'></search>" % root.get("string", ""))
1395 node = etree.Element("group")
1398 fields = root.xpath("//field[@select=1]")
1399 for field in fields:
1402 return etree.tostring(res, encoding="utf-8").replace('\t', '')
1405 # if view_id, view_type is not required
1407 def fields_view_get(self, cr, user, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
1409 Get the detailed composition of the requested view like fields, model, view architecture
1411 :param cr: database cursor
1412 :param user: current user id
1413 :param view_id: id of the view or None
1414 :param view_type: type of the view to return if view_id is None ('form', tree', ...)
1415 :param context: context arguments, like lang, time zone
1416 :param toolbar: true to include contextual actions
1417 :param submenu: example (portal_project module)
1418 :return: dictionary describing the composition of the requested view (including inherited views and extensions)
1419 :raise AttributeError:
1420 * if the inherited view has unknown position to work with other than 'before', 'after', 'inside', 'replace'
1421 * if some tag other than 'position' is found in parent view
1422 :raise Invalid ArchitectureError: if there is view type other than form, tree, calendar, search etc defined on the structure
1429 if isinstance(s, unicode):
1430 return s.encode('utf8')
1433 def _inherit_apply(src, inherit):
1434 def _find(node, node2):
1435 if node2.tag == 'xpath':
1436 res = node.xpath(node2.get('expr'))
1442 for n in node.getiterator(node2.tag):
1444 for attr in node2.attrib:
1445 if attr == 'position':
1448 if n.get(attr) == node2.get(attr):
1455 # End: _find(node, node2)
1457 doc_dest = etree.fromstring(encode(inherit))
1458 toparse = [ doc_dest ]
1461 node2 = toparse.pop(0)
1462 if node2.tag == 'data':
1463 toparse += [ c for c in doc_dest ]
1465 node = _find(src, node2)
1466 if node is not None:
1468 if node2.get('position'):
1469 pos = node2.get('position')
1470 if pos == 'replace':
1471 parent = node.getparent()
1473 src = copy.deepcopy(node2[0])
1476 node.addprevious(child)
1477 node.getparent().remove(node)
1478 elif pos == 'attributes':
1479 for child in node2.getiterator('attribute'):
1480 attribute = (child.get('name'), child.text and child.text.encode('utf8') or None)
1482 node.set(attribute[0], attribute[1])
1484 del(node.attrib[attribute[0]])
1486 sib = node.getnext()
1490 elif pos == 'after':
1494 sib.addprevious(child)
1495 elif pos == 'before':
1496 node.addprevious(child)
1498 raise AttributeError(_('Unknown position in inherited view %s !') % pos)
1501 ' %s="%s"' % (attr, node2.get(attr))
1502 for attr in node2.attrib
1503 if attr != 'position'
1505 tag = "<%s%s>" % (node2.tag, attrs)
1506 raise AttributeError(_("Couldn't find tag '%s' in parent view !") % tag)
1508 # End: _inherit_apply(src, inherit)
1510 result = {'type': view_type, 'model': self._name}
1516 view_ref = context.get(view_type + '_view_ref', False)
1519 module, view_ref = view_ref.split('.', 1)
1520 cr.execute("SELECT res_id FROM ir_model_data WHERE model='ir.ui.view' AND module=%s AND name=%s", (module, view_ref))
1521 view_ref_res = cr.fetchone()
1523 view_id = view_ref_res[0]
1526 where = (model and (" and model='%s'" % (self._name,))) or ''
1527 cr.execute('SELECT arch,name,field_parent,id,type,inherit_id FROM ir_ui_view WHERE id=%s'+where, (view_id,))
1529 cr.execute('''SELECT
1530 arch,name,field_parent,id,type,inherit_id
1537 ORDER BY priority''', (self._name, view_type))
1538 sql_res = cr.fetchone()
1544 view_id = ok or sql_res[3]
1547 # if a view was found
1549 result['type'] = sql_res[4]
1550 result['view_id'] = sql_res[3]
1551 result['arch'] = sql_res[0]
1553 def _inherit_apply_rec(result, inherit_id):
1554 # get all views which inherit from (ie modify) this view
1555 cr.execute('select arch,id from ir_ui_view where inherit_id=%s and model=%s order by priority', (inherit_id, self._name))
1556 sql_inherit = cr.fetchall()
1557 for (inherit, id) in sql_inherit:
1558 result = _inherit_apply(result, inherit)
1559 result = _inherit_apply_rec(result, id)
1562 inherit_result = etree.fromstring(encode(result['arch']))
1563 result['arch'] = _inherit_apply_rec(inherit_result, sql_res[3])
1565 result['name'] = sql_res[1]
1566 result['field_parent'] = sql_res[2] or False
1569 # otherwise, build some kind of default view
1570 if view_type == 'form':
1571 res = self.fields_get(cr, user, context=context)
1572 xml = '<?xml version="1.0" encoding="utf-8"?> ' \
1573 '<form string="%s">' % (self._description,)
1575 if res[x]['type'] not in ('one2many', 'many2many'):
1576 xml += '<field name="%s"/>' % (x,)
1577 if res[x]['type'] == 'text':
1581 elif view_type == 'tree':
1582 _rec_name = self._rec_name
1583 if _rec_name not in self._columns:
1584 _rec_name = self._columns.keys()[0]
1585 xml = '<?xml version="1.0" encoding="utf-8"?>' \
1586 '<tree string="%s"><field name="%s"/></tree>' \
1587 % (self._description, self._rec_name)
1589 elif view_type == 'calendar':
1590 xml = self.__get_default_calendar_view()
1592 elif view_type == 'search':
1593 xml = self.__get_default_search_view(cr, user, context)
1596 xml = '<?xml version="1.0"?>' # what happens here, graph case?
1597 raise except_orm(_('Invalid Architecture!'),_("There is no view of type '%s' defined for the structure!") % view_type)
1598 result['arch'] = etree.fromstring(encode(xml))
1599 result['name'] = 'default'
1600 result['field_parent'] = False
1601 result['view_id'] = 0
1603 xarch, xfields = self.__view_look_dom_arch(cr, user, result['arch'], view_id, context=context)
1604 result['arch'] = xarch
1605 result['fields'] = xfields
1608 if context and context.get('active_id',False):
1609 data_menu = self.pool.get('ir.ui.menu').browse(cr, user, context['active_id'], context).action
1611 act_id = data_menu.id
1613 data_action = self.pool.get('ir.actions.act_window').browse(cr, user, [act_id], context)[0]
1614 result['submenu'] = getattr(data_action,'menus', False)
1618 for key in ('report_sxw_content', 'report_rml_content',
1619 'report_sxw', 'report_rml',
1620 'report_sxw_content_data', 'report_rml_content_data'):
1624 ir_values_obj = self.pool.get('ir.values')
1625 resprint = ir_values_obj.get(cr, user, 'action',
1626 'client_print_multi', [(self._name, False)], False,
1628 resaction = ir_values_obj.get(cr, user, 'action',
1629 'client_action_multi', [(self._name, False)], False,
1632 resrelate = ir_values_obj.get(cr, user, 'action',
1633 'client_action_relate', [(self._name, False)], False,
1635 resprint = map(clean, resprint)
1636 resaction = map(clean, resaction)
1637 resaction = filter(lambda x: not x.get('multi', False), resaction)
1638 resprint = filter(lambda x: not x.get('multi', False), resprint)
1639 resrelate = map(lambda x: x[2], resrelate)
1641 for x in resprint+resaction+resrelate:
1642 x['string'] = x['name']
1644 result['toolbar'] = {
1646 'action': resaction,
1649 if result['type']=='form' and result['arch'].count("default_focus")>1:
1650 msg = "Form View contain more than one default_focus attribute"
1651 netsvc.Logger().notifyChannel('orm', netsvc.LOG_ERROR, msg)
1652 raise except_orm('View Error !',msg)
1655 _view_look_dom_arch = __view_look_dom_arch
1657 def search_count(self, cr, user, args, context=None):
1660 res = self.search(cr, user, args, context=context, count=True)
1661 if isinstance(res, list):
1665 def search(self, cr, user, args, offset=0, limit=None, order=None,
1666 context=None, count=False):
1667 raise _('The search method is not implemented on this object !')
1669 def name_get(self, cr, user, ids, context=None):
1670 raise _('The name_get method is not implemented on this object !')
1672 def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100):
1673 raise _('The name_search method is not implemented on this object !')
1675 def copy(self, cr, uid, id, default=None, context=None):
1676 raise _('The copy method is not implemented on this object !')
1678 def exists(self, cr, uid, id, context=None):
1679 raise _('The exists method is not implemented on this object !')
1681 def read_string(self, cr, uid, id, langs, fields=None, context=None):
1684 self.pool.get('ir.model.access').check(cr, uid, 'ir.translation', 'read', context=context)
1686 fields = self._columns.keys() + self._inherit_fields.keys()
1687 #FIXME: collect all calls to _get_source into one SQL call.
1689 res[lang] = {'code': lang}
1691 if f in self._columns:
1692 res_trans = self.pool.get('ir.translation')._get_source(cr, uid, self._name+','+f, 'field', lang)
1694 res[lang][f] = res_trans
1696 res[lang][f] = self._columns[f].string
1697 for table in self._inherits:
1698 cols = intersect(self._inherit_fields.keys(), fields)
1699 res2 = self.pool.get(table).read_string(cr, uid, id, langs, cols, context)
1702 res[lang]['code'] = lang
1703 for f in res2[lang]:
1704 res[lang][f] = res2[lang][f]
1707 def write_string(self, cr, uid, id, langs, vals, context=None):
1708 self.pool.get('ir.model.access').check(cr, uid, 'ir.translation', 'write', context=context)
1709 #FIXME: try to only call the translation in one SQL
1712 if field in self._columns:
1713 src = self._columns[field].string
1714 self.pool.get('ir.translation')._set_ids(cr, uid, self._name+','+field, 'field', lang, [0], vals[field], src)
1715 for table in self._inherits:
1716 cols = intersect(self._inherit_fields.keys(), vals)
1718 self.pool.get(table).write_string(cr, uid, id, langs, vals, context)
1721 def _check_removed_columns(self, cr, log=False):
1722 raise NotImplementedError()
1724 class orm_memory(orm_template):
1725 _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']
1726 _inherit_fields = {}
1731 def __init__(self, cr):
1732 super(orm_memory, self).__init__(cr)
1736 cr.execute('delete from wkf_instance where res_type=%s', (self._name,))
1738 def vaccum(self, cr, uid):
1740 if self.check_id % self._check_time:
1743 max = time.time() - self._max_hours * 60 * 60
1744 for id in self.datas:
1745 if self.datas[id]['internal.date_access'] < max:
1747 self.unlink(cr, uid, tounlink)
1748 if len(self.datas)>self._max_count:
1749 sorted = map(lambda x: (x[1]['internal.date_access'], x[0]), self.datas.items())
1751 ids = map(lambda x: x[1], sorted[:len(self.datas)-self._max_count])
1752 self.unlink(cr, uid, ids)
1755 def read(self, cr, user, ids, fields_to_read=None, context=None, load='_classic_read'):
1758 self.pool.get('ir.model.access').check(cr, user, self._name, 'read', context=context)
1759 if not fields_to_read:
1760 fields_to_read = self._columns.keys()
1764 if isinstance(ids, (int, long)):
1768 for f in fields_to_read:
1769 if id in self.datas:
1770 r[f] = self.datas[id].get(f, False)
1771 if r[f] and isinstance(self._columns[f], fields.binary) and context.get('bin_size', False):
1774 if id in self.datas:
1775 self.datas[id]['internal.date_access'] = time.time()
1776 fields_post = filter(lambda x: x in self._columns and not getattr(self._columns[x], load), fields_to_read)
1777 for f in fields_post:
1778 res2 = self._columns[f].get_memory(cr, self, ids, f, user, context=context, values=result)
1779 for record in result:
1780 record[f] = res2[record['id']]
1781 if isinstance(ids_orig, (int, long)):
1785 def write(self, cr, user, ids, vals, context=None):
1788 self.pool.get('ir.model.access').check(cr, user, self._name, 'write', context=context)
1792 if self._columns[field]._classic_write:
1793 vals2[field] = vals[field]
1795 upd_todo.append(field)
1797 self.datas[id_new].update(vals2)
1798 self.datas[id_new]['internal.date_access'] = time.time()
1799 for field in upd_todo:
1800 self._columns[field].set_memory(cr, self, id_new, field, vals[field], user, context)
1801 self._validate(cr, user, [id_new], context)
1802 wf_service = netsvc.LocalService("workflow")
1803 wf_service.trg_write(user, self._name, id_new, cr)
1806 def create(self, cr, user, vals, context=None):
1807 self.pool.get('ir.model.access').check(cr, user, self._name, 'create', context=context)
1808 self.vaccum(cr, user)
1810 id_new = self.next_id
1812 for f in self._columns.keys():
1816 vals.update(self.default_get(cr, user, default, context))
1820 if self._columns[field]._classic_write:
1821 vals2[field] = vals[field]
1823 upd_todo.append(field)
1824 self.datas[id_new] = vals2
1825 self.datas[id_new]['internal.date_access'] = time.time()
1827 for field in upd_todo:
1828 self._columns[field].set_memory(cr, self, id_new, field, vals[field], user, context)
1829 self._validate(cr, user, [id_new], context)
1830 wf_service = netsvc.LocalService("workflow")
1831 wf_service.trg_create(user, self._name, id_new, cr)
1834 def default_get(self, cr, uid, fields_list, context=None):
1835 self.view_init(cr, uid, fields_list, context)
1839 # get the default values for the inherited fields
1840 for f in fields_list:
1841 if f in self._defaults:
1842 if callable(self._defaults[f]):
1843 value[f] = self._defaults[f](self, cr, uid, context)
1845 value[f] = self._defaults[f]
1847 fld_def = ((f in self._columns) and self._columns[f]) \
1848 or ((f in self._inherit_fields) and self._inherit_fields[f][2]) \
1851 # get the default values set by the user and override the default
1852 # values defined in the object
1853 ir_values_obj = self.pool.get('ir.values')
1854 res = ir_values_obj.get(cr, uid, 'default', False, [self._name])
1855 for id, field, field_value in res:
1856 if field in fields_list:
1857 fld_def = (field in self._columns) and self._columns[field] or self._inherit_fields[field][2]
1858 if fld_def._type in ('many2one', 'one2one'):
1859 obj = self.pool.get(fld_def._obj)
1860 if not obj.search(cr, uid, [('id', '=', field_value)]):
1862 if fld_def._type in ('many2many'):
1863 obj = self.pool.get(fld_def._obj)
1865 for i in range(len(field_value)):
1866 if not obj.search(cr, uid, [('id', '=',
1869 field_value2.append(field_value[i])
1870 field_value = field_value2
1871 if fld_def._type in ('one2many'):
1872 obj = self.pool.get(fld_def._obj)
1874 for i in range(len(field_value)):
1875 field_value2.append({})
1876 for field2 in field_value[i]:
1877 if obj._columns[field2]._type in ('many2one', 'one2one'):
1878 obj2 = self.pool.get(obj._columns[field2]._obj)
1879 if not obj2.search(cr, uid,
1880 [('id', '=', field_value[i][field2])]):
1882 # TODO add test for many2many and one2many
1883 field_value2[i][field2] = field_value[i][field2]
1884 field_value = field_value2
1885 value[field] = field_value
1887 # get the default values from the context
1888 for key in context or {}:
1889 if key.startswith('default_') and (key[8:] in fields_list):
1890 value[key[8:]] = context[key]
1893 def _where_calc(self, cr, user, args, active_test=True, context=None):
1898 # if the object has a field named 'active', filter out all inactive
1899 # records unless they were explicitely asked for
1900 if 'active' in self._columns and (active_test and context.get('active_test', True)):
1902 active_in_args = False
1904 if a[0] == 'active':
1905 active_in_args = True
1906 if not active_in_args:
1907 args.insert(0, ('active', '=', 1))
1909 args = [('active', '=', 1)]
1912 e = expression.expression(args)
1913 e.parse(cr, user, self, context)
1914 res=e.__dict__['_expression__exp']
1918 def search(self, cr, user, args, offset=0, limit=None, order=None,
1919 context=None, count=False):
1922 result = self._where_calc(cr, user, args, context=context)
1924 return self.datas.keys()
1928 #Find the value of dict
1931 for id, data in self.datas.items():
1934 if limit and (counter >int(limit)):
1939 val =eval('data[arg[0]]'+'==' +' arg[2]')
1940 elif arg[1] in ['<','>','in','not in','<=','>=','<>']:
1941 val =eval('data[arg[0]]'+arg[1] +' arg[2]')
1942 elif arg[1] in ['ilike']:
1943 if str(data[arg[0]]).find(str(arg[2]))!=-1:
1958 def unlink(self, cr, uid, ids, context=None):
1959 self.pool.get('ir.model.access').check(cr, uid, self._name, 'unlink', context=context)
1961 if id in self.datas:
1964 cr.execute('delete from wkf_instance where res_type=%s and res_id = ANY (%s)', (self._name,ids))
1967 def perm_read(self, cr, user, ids, context=None, details=True):
1971 'create_uid': (user, 'Root'),
1972 'create_date': time.strftime('%Y-%m-%d %H:%M:%S'),
1974 'write_date': False,
1979 def _check_removed_columns(self, cr, log=False):
1980 # nothing to check in memory...
1983 def exists(self, cr, uid, id, context=None):
1984 return id in self.datas
1986 class orm(orm_template):
1987 _sql_constraints = []
1989 _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']
1991 def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None):
1993 Get the list of records in list view grouped by the given ``groupby`` fields
1995 :param cr: database cursor
1996 :param uid: current user id
1997 :param domain: list specifying search criteria [['field_name', 'operator', 'value'], ...]
1998 :param fields: list of fields present in the list view specified on the object
1999 :param groupby: list of fields on which to groupby the records
2000 :type fields_list: list (example ['field_name_1', ...])
2001 :param offset: optional number of records to skip
2002 :param limit: optional max number of records to return
2003 :param context: context arguments, like lang, time zone
2004 :return: list of dictionaries(one dictionary for each record) containing:
2006 * the values of fields grouped by the fields in ``groupby`` argument
2007 * __domain: list of tuples specifying the search criteria
2008 * __context: dictionary with argument like ``groupby``
2009 :rtype: [{'field_name_1': value, ...]
2010 :raise AccessError: * if user has no read rights on the requested object
2011 * if user tries to bypass access rules for read on the requested object
2014 context = context or {}
2015 self.pool.get('ir.model.access').check(cr, uid, self._name, 'read', context=context)
2017 fields = self._columns.keys()
2019 (where_clause, where_params, tables) = self._where_calc(cr, uid, domain, context=context)
2020 dom = self.pool.get('ir.rule').domain_get(cr, uid, self._name, 'read', context=context)
2021 where_clause = where_clause + dom[0]
2022 where_params = where_params + dom[1]
2027 # Take care of adding join(s) if groupby is an '_inherits'ed field
2028 groupby_list = groupby
2030 if groupby and isinstance(groupby, list):
2031 groupby = groupby[0]
2032 tables, where_clause = self._inherits_join_calc(groupby,tables,where_clause)
2034 if len(where_clause):
2035 where_clause = ' where '+string.join(where_clause, ' and ')
2038 limit_str = limit and ' limit %d' % limit or ''
2039 offset_str = offset and ' offset %d' % offset or ''
2041 fget = self.fields_get(cr, uid, fields)
2042 float_int_fields = filter(lambda x: fget[x]['type'] in ('float','integer'), fields)
2048 if fget.get(groupby,False) and fget[groupby]['type'] in ('date','datetime'):
2049 flist = "to_char(%s,'yyyy-mm') as %s "%(groupby,groupby)
2050 groupby = "to_char(%s,'yyyy-mm')"%(groupby)
2055 fields_pre = [f for f in float_int_fields if
2056 f == self.CONCURRENCY_CHECK_FIELD
2057 or (f in self._columns and getattr(self._columns[f], '_classic_write'))]
2058 for f in fields_pre:
2059 if f not in ['id','sequence']:
2060 operator = fget[f].get('group_operator','sum')
2063 flist += operator+'('+f+') as '+f
2066 gb = ' group by '+groupby
2069 cr.execute('select min(%s.id) as id,' % self._table + flist + ' from ' + ','.join(tables) + where_clause + gb + limit_str + offset_str, where_params)
2072 for r in cr.dictfetchall():
2073 for fld,val in r.items():
2074 if val == None:r[fld] = False
2075 alldata[r['id']] = r
2077 data = self.read(cr, uid, alldata.keys(), groupby and [groupby] or ['id'], context=context)
2078 today = datetime.date.today()
2081 d['__domain'] = [(groupby,'=',alldata[d['id']][groupby] or False)] + domain
2082 if not isinstance(groupby_list,(str, unicode)):
2083 if groupby or not context.get('group_by_no_leaf', False):
2084 d['__context'] = {'group_by':groupby_list[1:]}
2085 if groupby and fget.has_key(groupby):
2086 if d[groupby] and fget[groupby]['type'] in ('date','datetime'):
2087 dt = datetime.datetime.strptime(alldata[d['id']][groupby][:7],'%Y-%m')
2088 days = calendar.monthrange(dt.year, dt.month)[1]
2090 d[groupby] = datetime.datetime.strptime(d[groupby][:10],'%Y-%m-%d').strftime('%B %Y')
2091 if not context.get('group_by_no_leaf', False):
2092 d['__domain'] = [(groupby,'>=',alldata[d['id']][groupby] and datetime.datetime.strptime(alldata[d['id']][groupby][:7] + '-01','%Y-%m-%d').strftime('%Y-%m-%d') or False),\
2093 (groupby,'<=',alldata[d['id']][groupby] and datetime.datetime.strptime(alldata[d['id']][groupby][:7] + '-' + str(days),'%Y-%m-%d').strftime('%Y-%m-%d') or False)] + domain
2094 elif fget[groupby]['type'] == 'many2one':
2095 d[groupby] = d[groupby] and ((type(d[groupby])==type(1)) and d[groupby] or d[groupby][1]) or ''
2097 del alldata[d['id']][groupby]
2098 d.update(alldata[d['id']])
2102 def _inherits_join_calc(self, field, tables, where_clause):
2104 Adds missing table select and join clause(s) for reaching
2105 the field coming from an '_inherits' parent table.
2107 :param tables: list of table._table names enclosed in double quotes as returned
2111 current_table = self
2112 while field in current_table._inherit_fields and not field in current_table._columns:
2113 parent_table = self.pool.get(current_table._inherit_fields[field][0])
2114 parent_table_name = parent_table._table
2115 if '"%s"'%parent_table_name not in tables:
2116 tables.append('"%s"'%parent_table_name)
2117 where_clause.append('(%s.%s = %s.id)' % (current_table._table, current_table._inherits[parent_table._name], parent_table_name))
2118 current_table = parent_table
2119 return (tables, where_clause)
2121 def _parent_store_compute(self, cr):
2122 logger = netsvc.Logger()
2123 logger.notifyChannel('orm', netsvc.LOG_INFO, 'Computing parent left and right for table %s...' % (self._table, ))
2124 def browse_rec(root, pos=0):
2126 where = self._parent_name+'='+str(root)
2128 where = self._parent_name+' IS NULL'
2129 if self._parent_order:
2130 where += ' order by '+self._parent_order
2131 cr.execute('SELECT id FROM '+self._table+' WHERE '+where)
2133 childs = cr.fetchall()
2135 pos2 = browse_rec(id[0], pos2)
2136 cr.execute('update '+self._table+' set parent_left=%s, parent_right=%s where id=%s', (pos,pos2,root))
2138 query = 'SELECT id FROM '+self._table+' WHERE '+self._parent_name+' IS NULL'
2139 if self._parent_order:
2140 query += ' order by '+self._parent_order
2143 for (root,) in cr.fetchall():
2144 pos = browse_rec(root, pos)
2147 def _update_store(self, cr, f, k):
2148 logger = netsvc.Logger()
2149 logger.notifyChannel('orm', netsvc.LOG_INFO, "storing computed values of fields.function '%s'" % (k,))
2150 ss = self._columns[k]._symbol_set
2151 update_query = 'UPDATE "%s" SET "%s"=%s WHERE id=%%s' % (self._table, k, ss[0])
2152 cr.execute('select id from '+self._table)
2153 ids_lst = map(lambda x: x[0], cr.fetchall())
2156 ids_lst = ids_lst[40:]
2157 res = f.get(cr, self, iids, k, 1, {})
2158 for key,val in res.items():
2161 # if val is a many2one, just write the ID
2162 if type(val)==tuple:
2164 if (val<>False) or (type(val)<>bool):
2165 cr.execute(update_query, (ss[1](val), key))
2167 def _check_removed_columns(self, cr, log=False):
2168 logger = netsvc.Logger()
2169 # iterate on the database columns to drop the NOT NULL constraints
2170 # of fields which were required but have been removed (or will be added by another module)
2171 columns = [c for c in self._columns if not (isinstance(self._columns[c], fields.function) and not self._columns[c].store)]
2172 columns += ('id', 'write_uid', 'write_date', 'create_uid', 'create_date') # openerp access columns
2173 cr.execute("SELECT a.attname, a.attnotnull"
2174 " FROM pg_class c, pg_attribute a"
2175 " WHERE c.relname=%%s"
2176 " AND c.oid=a.attrelid"
2177 " AND a.attisdropped=%%s"
2178 " AND pg_catalog.format_type(a.atttypid, a.atttypmod) NOT IN ('cid', 'tid', 'oid', 'xid')"
2179 " AND a.attname NOT IN (%s)" % ",".join(['%s']*len(columns)),
2180 [self._table, False] + columns)
2181 for column in cr.dictfetchall():
2183 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))
2184 if column['attnotnull']:
2185 cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" DROP NOT NULL' % (self._table, column['attname']))
2187 def _auto_init(self, cr, context={}):
2188 store_compute = False
2189 logger = netsvc.Logger()
2192 self._field_create(cr, context=context)
2193 if getattr(self, '_auto', True):
2194 cr.execute("SELECT relname FROM pg_class WHERE relkind in ('r','v') AND relname='%s'" % self._table)
2196 cr.execute("CREATE TABLE \"%s\" (id SERIAL NOT NULL, PRIMARY KEY(id)) WITHOUT OIDS" % self._table)
2197 cr.execute("COMMENT ON TABLE \"%s\" IS '%s'" % (self._table, self._description.replace("'","''")))
2200 if self._parent_store:
2201 cr.execute("""SELECT c.relname
2202 FROM pg_class c, pg_attribute a
2203 WHERE c.relname=%s AND a.attname=%s AND c.oid=a.attrelid
2204 """, (self._table, 'parent_left'))
2206 if 'parent_left' not in self._columns:
2207 logger.notifyChannel('orm', netsvc.LOG_ERROR, 'create a column parent_left on object %s: fields.integer(\'Left Parent\', select=1)' % (self._table, ))
2208 if 'parent_right' not in self._columns:
2209 logger.notifyChannel('orm', netsvc.LOG_ERROR, 'create a column parent_right on object %s: fields.integer(\'Right Parent\', select=1)' % (self._table, ))
2210 if self._columns[self._parent_name].ondelete<>'cascade':
2211 logger.notifyChannel('orm', netsvc.LOG_ERROR, "The column %s on object %s must be set as ondelete='cascade'" % (self._parent_name, self._name))
2212 cr.execute('ALTER TABLE "%s" ADD COLUMN "parent_left" INTEGER' % (self._table,))
2213 cr.execute('ALTER TABLE "%s" ADD COLUMN "parent_right" INTEGER' % (self._table,))
2215 store_compute = True
2217 if self._log_access:
2219 'create_uid': 'INTEGER REFERENCES res_users ON DELETE SET NULL',
2220 'create_date': 'TIMESTAMP',
2221 'write_uid': 'INTEGER REFERENCES res_users ON DELETE SET NULL',
2222 'write_date': 'TIMESTAMP'
2227 FROM pg_class c, pg_attribute a
2228 WHERE c.relname=%s AND a.attname=%s AND c.oid=a.attrelid
2229 """, (self._table, k))
2231 cr.execute('ALTER TABLE "%s" ADD COLUMN "%s" %s' % (self._table, k, logs[k]))
2234 self._check_removed_columns(cr, log=False)
2236 # iterate on the "object columns"
2237 todo_update_store = []
2238 update_custom_fields = context.get('update_custom_fields', False)
2239 for k in self._columns:
2240 if k in ('id', 'write_uid', 'write_date', 'create_uid', 'create_date'):
2242 #raise _('Can not define a column %s. Reserved keyword !') % (k,)
2243 #Not Updating Custom fields
2244 if k.startswith('x_') and not update_custom_fields:
2246 f = self._columns[k]
2248 if isinstance(f, fields.one2many):
2249 cr.execute("SELECT relname FROM pg_class WHERE relkind='r' AND relname=%s", (f._obj,))
2251 if self.pool.get(f._obj):
2252 if f._fields_id not in self.pool.get(f._obj)._columns.keys():
2253 if not self.pool.get(f._obj)._inherits or (f._fields_id not in self.pool.get(f._obj)._inherit_fields.keys()):
2254 raise except_orm('Programming Error', ("There is no reference field '%s' found for '%s'") % (f._fields_id,f._obj,))
2257 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))
2258 res = cr.fetchone()[0]
2260 cr.execute('ALTER TABLE "%s" ADD FOREIGN KEY (%s) REFERENCES "%s" ON DELETE SET NULL' % (self._obj, f._fields_id, f._table))
2261 elif isinstance(f, fields.many2many):
2262 cr.execute("SELECT relname FROM pg_class WHERE relkind in ('r','v') AND relname=%s", (f._rel,))
2263 if not cr.dictfetchall():
2264 if not self.pool.get(f._obj):
2265 raise except_orm('Programming Error', ('There is no reference available for %s') % (f._obj,))
2266 ref = self.pool.get(f._obj)._table
2267 # ref = f._obj.replace('.', '_')
2268 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))
2269 cr.execute('CREATE INDEX "%s_%s_index" ON "%s" ("%s")' % (f._rel, f._id1, f._rel, f._id1))
2270 cr.execute('CREATE INDEX "%s_%s_index" ON "%s" ("%s")' % (f._rel, f._id2, f._rel, f._id2))
2271 cr.execute("COMMENT ON TABLE \"%s\" IS 'RELATION BETWEEN %s AND %s'" % (f._rel, self._table, ref))
2274 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 " \
2275 "FROM pg_class c,pg_attribute a,pg_type t " \
2276 "WHERE c.relname=%s " \
2277 "AND a.attname=%s " \
2278 "AND c.oid=a.attrelid " \
2279 "AND a.atttypid=t.oid", (self._table, k))
2280 res = cr.dictfetchall()
2281 if not res and hasattr(f,'oldname'):
2282 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 " \
2283 "FROM pg_class c,pg_attribute a,pg_type t " \
2284 "WHERE c.relname=%s " \
2285 "AND a.attname=%s " \
2286 "AND c.oid=a.attrelid " \
2287 "AND a.atttypid=t.oid", (self._table, f.oldname))
2288 res_old = cr.dictfetchall()
2289 logger.notifyChannel('orm', netsvc.LOG_DEBUG, 'trying to rename %s(%s) to %s'% (self._table, f.oldname, k))
2290 if res_old and len(res_old)==1:
2291 cr.execute('ALTER TABLE "%s" RENAME "%s" TO "%s"' % ( self._table,f.oldname, k))
2293 res[0]['attname'] = k
2296 if not isinstance(f, fields.function) or f.store:
2298 # add the missing field
2299 cr.execute('ALTER TABLE "%s" ADD COLUMN "%s" %s' % (self._table, k, get_pg_type(f)[1]))
2300 cr.execute("COMMENT ON COLUMN %s.%s IS '%s'" % (self._table, k, f.string.replace("'","''")))
2303 if not create and k in self._defaults:
2304 if callable(self._defaults[k]):
2305 default = self._defaults[k](self, cr, 1, context)
2307 default = self._defaults[k]
2309 ss = self._columns[k]._symbol_set
2310 query = 'UPDATE "%s" SET "%s"=%s' % (self._table, k, ss[0])
2311 cr.execute(query, (ss[1](default),))
2313 logger.notifyChannel('orm', netsvc.LOG_DEBUG, 'setting default value of new column %s of table %s'% (k, self._table))
2315 logger.notifyChannel('orm', netsvc.LOG_DEBUG, 'creating new column %s of table %s'% (k, self._table))
2317 if isinstance(f, fields.function):
2319 if f.store is not True:
2320 order = f.store[f.store.keys()[0]][2]
2321 todo_update_store.append((order, f,k))
2323 # and add constraints if needed
2324 if isinstance(f, fields.many2one):
2325 if not self.pool.get(f._obj):
2326 raise except_orm('Programming Error', ('There is no reference available for %s') % (f._obj,))
2327 ref = self.pool.get(f._obj)._table
2328 # ref = f._obj.replace('.', '_')
2329 # ir_actions is inherited so foreign key doesn't work on it
2330 if ref != 'ir_actions':
2331 cr.execute('ALTER TABLE "%s" ADD FOREIGN KEY ("%s") REFERENCES "%s" ON DELETE %s' % (self._table, k, ref, f.ondelete))
2333 cr.execute('CREATE INDEX "%s_%s_index" ON "%s" ("%s")' % (self._table, k, self._table, k))
2337 cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" SET NOT NULL' % (self._table, k))
2338 except Exception, e:
2339 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))
2343 f_pg_type = f_pg_def['typname']
2344 f_pg_size = f_pg_def['size']
2345 f_pg_notnull = f_pg_def['attnotnull']
2346 if isinstance(f, fields.function) and not f.store and\
2347 not getattr(f, 'nodrop', False):
2348 logger.notifyChannel('orm', netsvc.LOG_INFO, 'column %s (%s) in table %s removed: converted to a function !\n' % (k, f.string, self._table))
2349 cr.execute('ALTER TABLE "%s" DROP COLUMN "%s" CASCADE'% (self._table, k))
2353 f_obj_type = get_pg_type(f) and get_pg_type(f)[0]
2358 ('text', 'char', 'VARCHAR(%d)' % (f.size or 0,), '::VARCHAR(%d)'%(f.size or 0,)),
2359 ('varchar', 'text', 'TEXT', ''),
2360 ('int4', 'float', get_pg_type(f)[1], '::'+get_pg_type(f)[1]),
2361 ('date', 'datetime', 'TIMESTAMP', '::TIMESTAMP'),
2362 ('numeric', 'float', get_pg_type(f)[1], '::'+get_pg_type(f)[1]),
2363 ('float8', 'float', get_pg_type(f)[1], '::'+get_pg_type(f)[1]),
2365 # !!! Avoid reduction of varchar field !!!
2366 if f_pg_type == 'varchar' and f._type == 'char' and f_pg_size < f.size:
2367 # if f_pg_type == 'varchar' and f._type == 'char' and f_pg_size != f.size:
2368 logger.notifyChannel('orm', netsvc.LOG_INFO, "column '%s' in table '%s' changed size" % (k, self._table))
2369 cr.execute('ALTER TABLE "%s" RENAME COLUMN "%s" TO temp_change_size' % (self._table, k))
2370 cr.execute('ALTER TABLE "%s" ADD COLUMN "%s" VARCHAR(%d)' % (self._table, k, f.size))
2371 cr.execute('UPDATE "%s" SET "%s"=temp_change_size::VARCHAR(%d)' % (self._table, k, f.size))
2372 cr.execute('ALTER TABLE "%s" DROP COLUMN temp_change_size CASCADE' % (self._table,))
2375 if (f_pg_type==c[0]) and (f._type==c[1]):
2376 if f_pg_type != f_obj_type:
2377 if f_pg_type != f_obj_type:
2378 logger.notifyChannel('orm', netsvc.LOG_INFO, "column '%s' in table '%s' changed type to %s." % (k, self._table, c[1]))
2380 cr.execute('ALTER TABLE "%s" RENAME COLUMN "%s" TO temp_change_size' % (self._table, k))
2381 cr.execute('ALTER TABLE "%s" ADD COLUMN "%s" %s' % (self._table, k, c[2]))
2382 cr.execute(('UPDATE "%s" SET "%s"=temp_change_size'+c[3]) % (self._table, k))
2383 cr.execute('ALTER TABLE "%s" DROP COLUMN temp_change_size CASCADE' % (self._table,))
2387 if f_pg_type != f_obj_type:
2389 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))
2391 # if the field is required and hasn't got a NOT NULL constraint
2392 if f.required and f_pg_notnull == 0:
2393 # set the field to the default value if any
2394 if k in self._defaults:
2395 if callable(self._defaults[k]):
2396 default = self._defaults[k](self, cr, 1, context)
2398 default = self._defaults[k]
2400 if (default is not None):
2401 ss = self._columns[k]._symbol_set
2402 query = 'UPDATE "%s" SET "%s"=%s WHERE "%s" is NULL' % (self._table, k, ss[0], k)
2403 cr.execute(query, (ss[1](default),))
2404 # add the NOT NULL constraint
2407 cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" SET NOT NULL' % (self._table, k))
2409 except Exception, e:
2410 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))
2412 elif not f.required and f_pg_notnull == 1:
2413 cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" DROP NOT NULL' % (self._table, k))
2415 indexname = '%s_%s_index' % (self._table, k)
2416 cr.execute("SELECT indexname FROM pg_indexes WHERE indexname = %s and tablename = %s", (indexname, self._table))
2417 res = cr.dictfetchall()
2418 if not res and f.select:
2419 cr.execute('CREATE INDEX "%s_%s_index" ON "%s" ("%s")' % (self._table, k, self._table, k))
2421 if res and not f.select:
2422 cr.execute('DROP INDEX "%s_%s_index"' % (self._table, k))
2424 if isinstance(f, fields.many2one):
2425 ref = self.pool.get(f._obj)._table
2426 if ref != 'ir_actions':
2427 cr.execute('SELECT confdeltype, conname FROM pg_constraint as con, pg_class as cl1, pg_class as cl2, '
2428 'pg_attribute as att1, pg_attribute as att2 '
2429 'WHERE con.conrelid = cl1.oid '
2430 'AND cl1.relname = %s '
2431 'AND con.confrelid = cl2.oid '
2432 'AND cl2.relname = %s '
2433 'AND array_lower(con.conkey, 1) = 1 '
2434 'AND con.conkey[1] = att1.attnum '
2435 'AND att1.attrelid = cl1.oid '
2436 'AND att1.attname = %s '
2437 'AND array_lower(con.confkey, 1) = 1 '
2438 'AND con.confkey[1] = att2.attnum '
2439 'AND att2.attrelid = cl2.oid '
2440 'AND att2.attname = %s '
2441 "AND con.contype = 'f'", (self._table, ref, k, 'id'))
2442 res = cr.dictfetchall()
2444 if res[0]['confdeltype'] != POSTGRES_CONFDELTYPES.get(f.ondelete.upper(), 'a'):
2445 cr.execute('ALTER TABLE "' + self._table + '" DROP CONSTRAINT "' + res[0]['conname'] + '"')
2446 cr.execute('ALTER TABLE "' + self._table + '" ADD FOREIGN KEY ("' + k + '") REFERENCES "' + ref + '" ON DELETE ' + f.ondelete)
2449 logger.notifyChannel('orm', netsvc.LOG_ERROR, "Programming error, column %s->%s has multiple instances !"%(self._table,k))
2450 for order,f,k in todo_update_store:
2451 todo_end.append((order, self._update_store, (f, k)))
2454 cr.execute("SELECT relname FROM pg_class WHERE relkind in ('r','v') AND relname=%s", (self._table,))
2455 create = not bool(cr.fetchone())
2457 cr.commit() # start a new transaction
2459 for (key, con, _) in self._sql_constraints:
2460 conname = '%s_%s' % (self._table, key)
2461 cr.execute("SELECT conname FROM pg_constraint where conname=%s", (conname,))
2462 if not cr.dictfetchall():
2464 cr.execute('alter table "%s" add constraint "%s_%s" %s' % (self._table, self._table, key, con,))
2467 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,))
2471 if hasattr(self, "_sql"):
2472 for line in self._sql.split(';'):
2473 line2 = line.replace('\n', '').strip()
2478 self._parent_store_compute(cr)
2482 def __init__(self, cr):
2483 super(orm, self).__init__(cr)
2485 if not hasattr(self, '_log_access'):
2486 # if not access is not specify, it is the same value as _auto
2487 self._log_access = getattr(self, "_auto", True)
2489 self._columns = self._columns.copy()
2490 for store_field in self._columns:
2491 f = self._columns[store_field]
2492 if hasattr(f, 'digits_change'):
2494 if not isinstance(f, fields.function):
2498 if self._columns[store_field].store is True:
2499 sm = {self._name:(lambda self,cr, uid, ids, c={}: ids, None, 10, None)}
2501 sm = self._columns[store_field].store
2502 for object, aa in sm.items():
2504 (fnct,fields2,order,length)=aa
2506 (fnct,fields2,order)=aa
2509 raise except_orm('Error',
2510 ('Invalid function definition %s in object %s !\nYou must use the definition: store={object:(fnct, fields, priority, time length)}.' % (store_field, self._name)))
2511 self.pool._store_function.setdefault(object, [])
2513 for x,y,z,e,f,l in self.pool._store_function[object]:
2514 if (x==self._name) and (y==store_field) and (e==fields2):
2518 self.pool._store_function[object].append( (self._name, store_field, fnct, fields2, order, length))
2519 self.pool._store_function[object].sort(lambda x,y: cmp(x[4],y[4]))
2521 for (key, _, msg) in self._sql_constraints:
2522 self.pool._sql_error[self._table+'_'+key] = msg
2524 # Load manual fields
2526 cr.execute("SELECT id FROM ir_model_fields WHERE name=%s AND model=%s", ('state', 'ir.model.fields'))
2528 cr.execute('SELECT * FROM ir_model_fields WHERE model=%s AND state=%s', (self._name, 'manual'))
2529 for field in cr.dictfetchall():
2530 if field['name'] in self._columns:
2533 'string': field['field_description'],
2534 'required': bool(field['required']),
2535 'readonly': bool(field['readonly']),
2536 'domain': field['domain'] or None,
2537 'size': field['size'],
2538 'ondelete': field['on_delete'],
2539 'translate': (field['translate']),
2540 #'select': int(field['select_level'])
2543 if field['ttype'] == 'selection':
2544 self._columns[field['name']] = getattr(fields, field['ttype'])(eval(field['selection']), **attrs)
2545 elif field['ttype'] == 'reference':
2546 self._columns[field['name']] = getattr(fields, field['ttype'])(selection=eval(field['selection']), **attrs)
2547 elif field['ttype'] == 'many2one':
2548 self._columns[field['name']] = getattr(fields, field['ttype'])(field['relation'], **attrs)
2549 elif field['ttype'] == 'one2many':
2550 self._columns[field['name']] = getattr(fields, field['ttype'])(field['relation'], field['relation_field'], **attrs)
2551 elif field['ttype'] == 'many2many':
2552 _rel1 = field['relation'].replace('.', '_')
2553 _rel2 = field['model'].replace('.', '_')
2554 _rel_name = 'x_%s_%s_%s_rel' %(_rel1, _rel2, field['name'])
2555 self._columns[field['name']] = getattr(fields, field['ttype'])(field['relation'], _rel_name, 'id1', 'id2', **attrs)
2557 self._columns[field['name']] = getattr(fields, field['ttype'])(**attrs)
2559 self._inherits_reload()
2560 if not self._sequence:
2561 self._sequence = self._table+'_id_seq'
2562 for k in self._defaults:
2563 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,)
2564 for f in self._columns:
2565 self._columns[f].restart()
2567 def default_get(self, cr, uid, fields_list, context=None):
2569 To Get default field values of given fields list of the model
2571 :param cr: database cursor
2572 :param uid: current user id
2573 :param fields_list: list of fields to get the default value
2574 :type fields_list: list (example ['field1', 'field2',])
2575 :param context: context arguments, like lang, time zone
2576 :return: dictionary of the default values for fields (set on the object class, by the user preferences, or via the context)
2582 # get the default values for the inherited fields
2583 for t in self._inherits.keys():
2584 value.update(self.pool.get(t).default_get(cr, uid, fields_list,
2587 # get the default values defined in the object
2588 for f in fields_list:
2589 if f in self._defaults:
2590 if callable(self._defaults[f]):
2591 value[f] = self._defaults[f](self, cr, uid, context)
2593 value[f] = self._defaults[f]
2595 fld_def = ((f in self._columns) and self._columns[f]) \
2596 or ((f in self._inherit_fields) and self._inherit_fields[f][2]) \
2598 if isinstance(fld_def, fields.property):
2599 property_obj = self.pool.get('ir.property')
2600 prop_value = property_obj.get(cr, uid, f, self._name, context=context)
2602 if isinstance(prop_value, (browse_record, browse_null)):
2603 value[f] = prop_value.id
2605 value[f] = prop_value
2609 # get the default values set by the user and override the default
2610 # values defined in the object
2611 ir_values_obj = self.pool.get('ir.values')
2612 res = ir_values_obj.get(cr, uid, 'default', False, [self._name])
2613 for id, field, field_value in res:
2614 if field in fields_list:
2615 fld_def = (field in self._columns) and self._columns[field] or self._inherit_fields[field][2]
2616 if fld_def._type in ('many2one', 'one2one'):
2617 obj = self.pool.get(fld_def._obj)
2618 if not obj.search(cr, uid, [('id', '=', field_value or False)]):
2620 if fld_def._type in ('many2many'):
2621 obj = self.pool.get(fld_def._obj)
2623 for i in range(len(field_value)):
2624 if not obj.search(cr, uid, [('id', '=',
2627 field_value2.append(field_value[i])
2628 field_value = field_value2
2629 if fld_def._type in ('one2many'):
2630 obj = self.pool.get(fld_def._obj)
2632 for i in range(len(field_value)):
2633 field_value2.append({})
2634 for field2 in field_value[i]:
2635 if field2 in obj._columns.keys() and obj._columns[field2]._type in ('many2one', 'one2one'):
2636 obj2 = self.pool.get(obj._columns[field2]._obj)
2637 if not obj2.search(cr, uid,
2638 [('id', '=', field_value[i][field2])]):
2640 elif field2 in obj._inherit_fields.keys() and obj._inherit_fields[field2][2]._type in ('many2one', 'one2one'):
2641 obj2 = self.pool.get(obj._inherit_fields[field2][2]._obj)
2642 if not obj2.search(cr, uid,
2643 [('id', '=', field_value[i][field2])]):
2645 # TODO add test for many2many and one2many
2646 field_value2[i][field2] = field_value[i][field2]
2647 field_value = field_value2
2648 value[field] = field_value
2649 for key in context or {}:
2650 if key.startswith('default_') and (key[8:] in fields_list):
2651 value[key[8:]] = context[key]
2655 # Update objects that uses this one to update their _inherits fields
2658 def _inherits_reload_src(self):
2659 for obj in self.pool.obj_pool.values():
2660 if self._name in obj._inherits:
2661 obj._inherits_reload()
2663 def _inherits_reload(self):
2665 for table in self._inherits:
2666 res.update(self.pool.get(table)._inherit_fields)
2667 for col in self.pool.get(table)._columns.keys():
2668 res[col] = (table, self._inherits[table], self.pool.get(table)._columns[col])
2669 for col in self.pool.get(table)._inherit_fields.keys():
2670 res[col] = (table, self._inherits[table], self.pool.get(table)._inherit_fields[col][2])
2671 self._inherit_fields = res
2672 self._inherits_reload_src()
2674 def fields_get(self, cr, user, fields=None, context=None):
2676 Get the description of list of fields
2678 :param cr: database cursor
2679 :param user: current user id
2680 :param fields: list of fields
2681 :param context: context arguments, like lang, time zone
2682 :return: dictionary of field dictionaries, each one describing a field of the business object
2683 :raise AccessError: * if user has no create/write rights on the requested object
2686 ira = self.pool.get('ir.model.access')
2687 read_access = ira.check(cr, user, self._name, 'write', raise_exception=False, context=context) or \
2688 ira.check(cr, user, self._name, 'create', raise_exception=False, context=context)
2689 return super(orm, self).fields_get(cr, user, fields, context, read_access)
2691 def read(self, cr, user, ids, fields=None, context=None, load='_classic_read'):
2693 Read records with given ids with the given fields
2695 :param cr: database cursor
2696 :param user: current user id
2697 :param ids: id or list of the ids of the records to read
2698 :param fields: optional list of field names to return (default: all fields would be returned)
2699 :type fields: list (example ['field_name_1', ...])
2700 :param context(optional, highly recommended): context arguments, like lang, time zone
2701 :return: list of dictionaries((dictionary per record asked)) with requested field values
2702 :rtype: [{‘name_of_the_field’: value, ...}, ...]
2703 :raise AccessError: * if user has no read rights on the requested object
2704 * if user tries to bypass access rules for read on the requested object
2709 self.pool.get('ir.model.access').check(cr, user, self._name, 'read', context=context)
2711 fields = self._columns.keys() + self._inherit_fields.keys()
2712 if isinstance(ids, (int, long)):
2716 select = map(lambda x: isinstance(x,dict) and x['id'] or x, select)
2717 result = self._read_flat(cr, user, select, fields, context, load)
2720 for key, v in r.items():
2723 if key in self._columns:
2724 column = self._columns[key]
2725 elif key in self._inherit_fields:
2726 column = self._inherit_fields[key][2]
2729 if v and column._type == 'reference':
2730 model_name, ref_id = v.split(',', 1)
2731 model = self.pool.get(model_name)
2735 cr.execute('SELECT count(1) FROM "%s" WHERE id=%%s' % (model._table,), (ref_id,))
2736 reset = not cr.fetchone()[0]
2738 if column._classic_write:
2739 query = 'UPDATE "%s" SET "%s"=NULL WHERE id=%%s' % (self._table, key)
2740 cr.execute(query, (r['id'],))
2743 if isinstance(ids, (int, long, dict)):
2744 return result and result[0] or False
2747 def _read_flat(self, cr, user, ids, fields_to_read, context=None, load='_classic_read'):
2750 #ids = map(lambda x:int(x), ids)
2753 if fields_to_read == None:
2754 fields_to_read = self._columns.keys()
2756 # construct a clause for the rules :
2757 d1, d2, tables = self.pool.get('ir.rule').domain_get(cr, user, self._name, 'read', context=context)
2758 # all inherited fields + all non inherited fields for which the attribute whose name is in load is True
2759 fields_pre = [f for f in fields_to_read if
2760 f == self.CONCURRENCY_CHECK_FIELD
2761 or (f in self._columns and getattr(self._columns[f], '_classic_write'))
2762 ] + self._inherits.values()
2766 def convert_field(f):
2767 if f in ('create_date', 'write_date'):
2768 return "date_trunc('second', %s) as %s" % (f, f)
2769 if f == self.CONCURRENCY_CHECK_FIELD:
2770 if self._log_access:
2771 return "COALESCE(%s.write_date, %s.create_date, now())::timestamp AS %s" % (self._table, self._table, f,)
2772 return "now()::timestamp AS %s" % (f,)
2773 if isinstance(self._columns[f], fields.binary) and context.get('bin_size', False):
2774 return 'length("%s") as "%s"' % (f, f)
2775 return '"%s"' % (f,)
2776 fields_pre2 = map(convert_field, fields_pre)
2777 order_by = self._parent_order or self._order
2778 for sub_ids in cr.split_for_in_conditions(ids):
2780 cr.execute('SELECT %s FROM %s WHERE %s.id IN %%s AND %s ORDER BY %s' % \
2781 (','.join(fields_pre2 + [self._table + '.id']), ','.join(tables), self._table, ' and '.join(d1),
2782 order_by),[sub_ids,]+d2)
2783 if cr.rowcount != len(sub_ids):
2784 raise except_orm(_('AccessError'),
2785 _('You try to bypass an access rule while reading (Document type: %s).') % self._description)
2787 cr.execute('SELECT %s FROM \"%s\" WHERE id IN %%s ORDER BY %s' %
2788 (','.join(fields_pre2 + ['id']), self._table,
2789 order_by), (sub_ids,))
2790 res.extend(cr.dictfetchall())
2792 res = map(lambda x: {'id': x}, ids)
2794 for f in fields_pre:
2795 if f == self.CONCURRENCY_CHECK_FIELD:
2797 if self._columns[f].translate:
2798 ids = map(lambda x: x['id'], res)
2799 #TODO: optimize out of this loop
2800 res_trans = self.pool.get('ir.translation')._get_ids(cr, user, self._name+','+f, 'model', context.get('lang', False) or 'en_US', ids)
2802 r[f] = res_trans.get(r['id'], False) or r[f]
2804 for table in self._inherits:
2805 col = self._inherits[table]
2806 cols = intersect(self._inherit_fields.keys(), set(fields_to_read) - set(self._columns.keys()))
2809 res2 = self.pool.get(table).read(cr, user, [x[col] for x in res], cols, context, load)
2817 if not record[col]:# if the record is deleted from _inherits table?
2819 record.update(res3[record[col]])
2820 if col not in fields_to_read:
2823 # all fields which need to be post-processed by a simple function (symbol_get)
2824 fields_post = filter(lambda x: x in self._columns and self._columns[x]._symbol_get, fields_to_read)
2827 for f in fields_post:
2828 r[f] = self._columns[f]._symbol_get(r[f])
2829 ids = map(lambda x: x['id'], res)
2831 # all non inherited fields for which the attribute whose name is in load is False
2832 fields_post = filter(lambda x: x in self._columns and not getattr(self._columns[x], load), fields_to_read)
2834 # Compute POST fields
2836 for f in fields_post:
2837 todo.setdefault(self._columns[f]._multi, [])
2838 todo[self._columns[f]._multi].append(f)
2839 for key,val in todo.items():
2841 res2 = self._columns[val[0]].get(cr, self, ids, val, user, context=context, values=res)
2844 if isinstance(res2[record['id']], str):res2[record['id']] = eval(res2[record['id']]) #TOCHECK : why got string instend of dict in python2.6
2845 record[pos] = res2[record['id']][pos]
2848 res2 = self._columns[f].get(cr, self, ids, f, user, context=context, values=res)
2851 record[f] = res2[record['id']]
2855 #for f in fields_post:
2856 # # get the value of that field for all records/ids
2857 # res2 = self._columns[f].get(cr, self, ids, f, user, context=context, values=res)
2858 # for record in res:
2859 # record[f] = res2[record['id']]
2863 for field in vals.copy():
2865 if field in self._columns:
2866 fobj = self._columns[field]
2873 for group in groups:
2874 module = group.split(".")[0]
2875 grp = group.split(".")[1]
2876 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" % \
2877 (grp, module, 'res.groups', user))
2878 readonly = cr.fetchall()
2879 if readonly[0][0] >= 1:
2882 elif readonly[0][0] == 0:
2888 if type(vals[field]) == type([]):
2890 elif type(vals[field]) == type(0.0):
2892 elif type(vals[field]) == type(''):
2893 vals[field] = '=No Permission='
2898 def perm_read(self, cr, user, ids, context=None, details=True):
2900 Read the permission for record of the given ids
2902 :param cr: database cursor
2903 :param user: current user id
2904 :param ids: id or list of ids
2905 :param context: context arguments, like lang, time zone
2906 :param details: if True, \*_uid fields are replaced with the name of the user
2907 :return: list of ownership dictionaries for each requested record
2908 :rtype: list of dictionaries with the following keys:
2911 * create_uid: user who created the record
2912 * create_date: date when the record was created
2913 * write_uid: last user who changed the record
2914 * write_date: date of the last change to the record
2922 if self._log_access:
2923 fields = ', u.create_uid, u.create_date, u.write_uid, u.write_date'
2924 if isinstance(ids, (int, long)):
2927 ids_str = string.join(map(lambda x: str(x), ids), ',')
2928 cr.execute('select u.id'+fields+' from "'+self._table+'" u where u.id in ('+ids_str+')')
2929 res = cr.dictfetchall()
2932 r[key] = r[key] or False
2933 if key in ('write_uid', 'create_uid', 'uid') and details:
2935 r[key] = self.pool.get('res.users').name_get(cr, user, [r[key]])[0]
2936 if isinstance(ids, (int, long)):
2940 def _check_concurrency(self, cr, ids, context):
2943 if context.get(self.CONCURRENCY_CHECK_FIELD) and self._log_access:
2945 return "%s,%s" % (self._name, oid)
2946 santa = "(id = %s AND %s < COALESCE(write_date, create_date, now())::timestamp)"
2947 for i in range(0, len(ids), cr.IN_MAX):
2948 sub_ids = tools.flatten(((oid, context[self.CONCURRENCY_CHECK_FIELD][key(oid)])
2949 for oid in ids[i:i+cr.IN_MAX]
2950 if key(oid) in context[self.CONCURRENCY_CHECK_FIELD]))
2952 cr.execute("SELECT count(1) FROM %s WHERE %s" % (self._table, " OR ".join([santa]*(len(sub_ids)/2))), sub_ids)
2955 raise except_orm('ConcurrencyException', _('Records were modified in the meanwhile'))
2957 def check_access_rule(self, cr, uid, ids, operation, context=None):
2958 """Verifies that the operation given by ``operation`` is allowed for the user
2959 according to ir.rules.
2961 :param operation: one of ``write``, ``unlink``
2962 :raise except_orm: * if current ir.rules do not permit this operation.
2963 :return: None if the operation is allowed
2965 where_clause, where_params, tables = self.pool.get('ir.rule').domain_get(cr, uid, self._name, operation, context=context)
2967 where_clause = ' and ' + ' and '.join(where_clause)
2968 for sub_ids in cr.split_for_in_conditions(ids):
2969 cr.execute('SELECT ' + self._table + '.id FROM ' + ','.join(tables) +
2970 ' WHERE ' + self._table + '.id IN %s' + where_clause,
2971 [sub_ids] + where_params)
2972 if cr.rowcount != len(sub_ids):
2973 raise except_orm(_('AccessError'),
2974 _('Operation prohibited by access rules (Operation: %s, Document type: %s).')
2975 % (operation, self._name))
2977 def unlink(self, cr, uid, ids, context=None):
2979 Delete records with given ids
2981 :param cr: database cursor
2982 :param uid: current user id
2983 :param ids: id or list of ids
2984 :param context(optional, highly recommended): context arguments, like lang, time zone
2986 :raise AccessError: * if user has no unlink rights on the requested object
2987 * if user tries to bypass access rules for unlink on the requested object
2988 :raise UserError: if the record is default property for other records
2993 if isinstance(ids, (int, long)):
2996 result_store = self._store_get_values(cr, uid, ids, None, context)
2998 self._check_concurrency(cr, ids, context)
3000 self.pool.get('ir.model.access').check(cr, uid, self._name, 'unlink', context=context)
3002 properties = self.pool.get('ir.property')
3003 domain = [('res_id', '=', False),
3004 ('value_reference', 'in', ['%s,%s' % (self._name, i) for i in ids]),
3006 if properties.search(cr, uid, domain, context=context):
3007 raise except_orm(_('Error'), _('Unable to delete this document because it is used as a default property'))
3009 wf_service = netsvc.LocalService("workflow")
3011 wf_service.trg_delete(uid, self._name, oid, cr)
3013 #cr.execute('select * from '+self._table+' where id in ('+str_d+')', ids)
3014 #res = cr.dictfetchall()
3015 #for key in self._inherits:
3016 # ids2 = [x[self._inherits[key]] for x in res]
3017 # self.pool.get(key).unlink(cr, uid, ids2)
3019 self.check_access_rule(cr, uid, ids, 'unlink', context=context)
3020 for sub_ids in cr.split_for_in_conditions(ids):
3021 cr.execute('delete from ' + self._table + ' ' \
3022 'where id in %s', (sub_ids,))
3023 for order, object, store_ids, fields in result_store:
3024 if object != self._name:
3025 obj = self.pool.get(object)
3026 cr.execute('select id from '+obj._table+' where id in ('+','.join(map(str, store_ids))+')')
3027 rids = map(lambda x: x[0], cr.fetchall())
3029 obj._store_set_values(cr, uid, rids, fields, context)
3035 def write(self, cr, user, ids, vals, context=None):
3037 Update records with given ids with the given field values
3039 :param cr: database cursor
3040 :param user: current user id
3041 :type user: integer (example 1)
3042 :param ids: id or list of ids
3043 :param vals: dictionary of field values to update
3044 :type vals: dictionary (example {'field_name': 'value', ...})
3045 :param context(optional, highly recommended): context arguments, like lang, time zone
3047 :raise AccessError: * if user has no write rights on the requested object
3048 * if user tries to bypass access rules for write on the requested object
3049 :raise ValidateError: if user tries to enter invalid value for a field that is not in selection
3050 :raise UserError: if recurssion is found
3052 vals format for relational field type.
3054 + many2many field : [(6, 0, list of ids)] (example: [(6, 0, [8, 5, 6, 4])])
3055 + one2many field : [(0, 0, dictionary of values)] (example: [(0, 0, {'field_name':field_value, ...})])
3056 + many2one field : ID of related record
3057 + reference field : model name, id (example: 'product.product, 5')
3061 for field in vals.copy():
3063 if field in self._columns:
3064 fobj = self._columns[field]
3066 fobj = self._inherit_fields[field][2]
3073 for group in groups:
3074 module = group.split(".")[0]
3075 grp = group.split(".")[1]
3076 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" % \
3077 (grp, module, 'res.groups', user))
3078 readonly = cr.fetchall()
3079 if readonly[0][0] >= 1:
3082 elif readonly[0][0] == 0:
3094 if isinstance(ids, (int, long)):
3097 self._check_concurrency(cr, ids, context)
3099 self.pool.get('ir.model.access').check(cr, user, self._name, 'write', context=context)
3107 totranslate = context.get('lang', False) and (context['lang'] != 'en_US')
3109 if field in self._columns:
3110 if self._columns[field]._classic_write and not (hasattr(self._columns[field], '_fnct_inv')):
3111 if (not totranslate) or not self._columns[field].translate:
3112 upd0.append('"'+field+'"='+self._columns[field]._symbol_set[0])
3113 upd1.append(self._columns[field]._symbol_set[1](vals[field]))
3114 direct.append(field)
3116 upd_todo.append(field)
3118 updend.append(field)
3119 if field in self._columns \
3120 and hasattr(self._columns[field], 'selection') \
3122 if self._columns[field]._type == 'reference':
3123 val = vals[field].split(',')[0]
3126 if isinstance(self._columns[field].selection, (tuple, list)):
3127 if val not in dict(self._columns[field].selection):
3128 raise except_orm(_('ValidateError'),
3129 _('The value "%s" for the field "%s" is not in the selection') \
3130 % (vals[field], field))
3132 if val not in dict(self._columns[field].selection(
3133 self, cr, user, context=context)):
3134 raise except_orm(_('ValidateError'),
3135 _('The value "%s" for the field "%s" is not in the selection') \
3136 % (vals[field], field))
3138 if self._log_access:
3139 upd0.append('write_uid=%s')
3140 upd0.append('write_date=now()')
3144 self.check_access_rule(cr, user, ids, 'write', context=context)
3145 for sub_ids in cr.split_for_in_conditions(ids):
3146 cr.execute('update ' + self._table + ' set ' + ','.join(upd0) + ' ' \
3147 'where id in %s', upd1 + [sub_ids])
3152 if self._columns[f].translate:
3153 src_trans = self.pool.get(self._name).read(cr,user,ids,[f])[0][f]
3156 # Inserting value to DB
3157 self.write(cr, user, ids, {f:vals[f]})
3158 self.pool.get('ir.translation')._set_ids(cr, user, self._name+','+f, 'model', context['lang'], ids, vals[f], src_trans)
3161 # call the 'set' method of fields which are not classic_write
3162 upd_todo.sort(lambda x, y: self._columns[x].priority-self._columns[y].priority)
3164 # default element in context must be removed when call a one2many or many2many
3165 rel_context = context.copy()
3166 for c in context.items():
3167 if c[0].startswith('default_'):
3168 del rel_context[c[0]]
3171 for field in upd_todo:
3173 result += self._columns[field].set(cr, self, id, field, vals[field], user, context=rel_context) or []
3175 for table in self._inherits:
3176 col = self._inherits[table]
3178 for sub_ids in cr.split_for_in_conditions(ids):
3179 cr.execute('select distinct "'+col+'" from "'+self._table+'" ' \
3180 'where id in %s', (sub_ids,))
3181 nids.extend([x[0] for x in cr.fetchall()])
3185 if self._inherit_fields[val][0] == table:
3187 self.pool.get(table).write(cr, user, nids, v, context)
3189 self._validate(cr, user, ids, context)
3190 # TODO: use _order to set dest at the right position and not first node of parent
3191 if self._parent_store and (self._parent_name in vals):
3193 self.pool._init_parent[self._name]=True
3196 # Find Position of the element
3197 if vals[self._parent_name]:
3198 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],))
3200 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))
3201 result_p = cr.fetchall()
3203 for (pleft,pright,pid) in result_p:
3208 # It's the first node of the parent: position = parent_left+1
3210 if not vals[self._parent_name]:
3213 cr.execute('select parent_left from '+self._table+' where id=%s', (vals[self._parent_name],))
3214 position = cr.fetchone()[0]+1
3216 # We have the new position !
3217 cr.execute('select parent_left,parent_right from '+self._table+' where id=%s', (id,))
3218 pleft,pright = cr.fetchone()
3219 distance = pright - pleft + 1
3221 if position>pleft and position<=pright:
3222 raise except_orm(_('UserError'), _('Recursivity Detected.'))
3225 cr.execute('update '+self._table+' set parent_left=parent_left+%s where parent_left>=%s', (distance, position))
3226 cr.execute('update '+self._table+' set parent_right=parent_right+%s where parent_right>=%s', (distance, position))
3227 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))
3229 cr.execute('update '+self._table+' set parent_left=parent_left+%s where parent_left>=%s', (distance, position))
3230 cr.execute('update '+self._table+' set parent_right=parent_right+%s where parent_right>=%s', (distance, position))
3231 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))
3233 result += self._store_get_values(cr, user, ids, vals.keys(), context)
3234 for order, object, ids, fields in result:
3235 self.pool.get(object)._store_set_values(cr, user, ids, fields, context)
3237 wf_service = netsvc.LocalService("workflow")
3239 wf_service.trg_write(user, self._name, id, cr)
3243 # TODO: Should set perm to user.xxx
3245 def create(self, cr, user, vals, context=None):
3247 Create new record with specified value
3249 :param cr: database cursor
3250 :param user: current user id
3251 :type user: integer (example 1)
3252 :param vals: dictionary for new record {'field_name': field_value, ...}
3253 :type vals: dictionary (example {'field_name': field_value, ...})
3254 :param context(optional, highly recommended): context arguments, like lang, time zone
3255 :type context: dictionary (example {'lang': 'en_us', ...})
3256 :return: id of new record created
3257 :raise AccessError: * if user has no create rights on the requested object
3258 * if user tries to bypass access rules for create on the requested object
3259 :raise ValidateError: if user tries to enter invalid value for a field that is not in selection
3261 vals format for relational field type.
3263 + many2many field : [(6, 0, list of ids)] (example: [(6, 0, [8, 5, 6, 4])])
3264 + one2many field : [(0, 0, dictionary of values)] (example: [(0, 0, {'field_name':field_value, ...})])
3265 + many2one field : ID of related record
3266 + reference field : model name, id (example: 'product.product, 5')
3271 self.pool.get('ir.model.access').check(cr, user, self._name, 'create', context=context)
3276 for (t, c) in self._inherits.items():
3278 avoid_table.append(t)
3279 for f in self._columns.keys(): # + self._inherit_fields.keys():
3283 for f in self._inherit_fields.keys():
3284 if (not f in vals) and (self._inherit_fields[f][0] not in avoid_table):
3288 default_values = self.default_get(cr, user, default, context)
3289 for dv in default_values:
3290 if dv in self._columns and self._columns[dv]._type == 'many2many':
3291 if default_values[dv] and isinstance(default_values[dv][0], (int, long)):
3292 default_values[dv] = [(6, 0, default_values[dv])]
3294 vals.update(default_values)
3297 for v in self._inherits:
3298 if self._inherits[v] not in vals:
3301 tocreate[v] = {'id' : vals[self._inherits[v]]}
3302 (upd0, upd1, upd2) = ('', '', [])
3304 for v in vals.keys():
3305 if v in self._inherit_fields:
3306 (table, col, col_detail) = self._inherit_fields[v]
3307 tocreate[table][v] = vals[v]
3310 if (v not in self._inherit_fields) and (v not in self._columns):
3313 # Try-except added to filter the creation of those records whose filds are readonly.
3314 # Example : any dashboard which has all the fields readonly.(due to Views(database views))
3316 cr.execute("SELECT nextval('"+self._sequence+"')")
3318 raise except_orm(_('UserError'),
3319 _('You cannot perform this operation. New Record Creation is not allowed for this object as this object is for reporting purpose.'))
3321 id_new = cr.fetchone()[0]
3322 for table in tocreate:
3323 if self._inherits[table] in vals:
3324 del vals[self._inherits[table]]
3326 record_id = tocreate[table].pop('id', None)
3328 if record_id is None or not record_id:
3329 record_id = self.pool.get(table).create(cr, user, tocreate[table], context=context)
3331 self.pool.get(table).write(cr, user, [record_id], tocreate[table], context=context)
3333 upd0 += ','+self._inherits[table]
3335 upd2.append(record_id)
3337 #Start : Set bool fields to be False if they are not touched(to make search more powerful)
3338 bool_fields = [x for x in self._columns.keys() if self._columns[x]._type=='boolean']
3340 for bool_field in bool_fields:
3341 if bool_field not in vals:
3342 vals[bool_field] = False
3344 for field in vals.copy():
3346 if field in self._columns:
3347 fobj = self._columns[field]
3349 fobj = self._inherit_fields[field][2]
3355 for group in groups:
3356 module = group.split(".")[0]
3357 grp = group.split(".")[1]
3358 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" % \
3359 (grp, module, 'res.groups', user))
3360 readonly = cr.fetchall()
3361 if readonly[0][0] >= 1:
3364 elif readonly[0][0] == 0:
3372 if self._columns[field]._classic_write:
3373 upd0 = upd0 + ',"' + field + '"'
3374 upd1 = upd1 + ',' + self._columns[field]._symbol_set[0]
3375 upd2.append(self._columns[field]._symbol_set[1](vals[field]))
3377 if not isinstance(self._columns[field], fields.related):
3378 upd_todo.append(field)
3379 if field in self._columns \
3380 and hasattr(self._columns[field], 'selection') \
3382 if self._columns[field]._type == 'reference':
3383 val = vals[field].split(',')[0]
3386 if isinstance(self._columns[field].selection, (tuple, list)):
3387 if val not in dict(self._columns[field].selection):
3388 raise except_orm(_('ValidateError'),
3389 _('The value "%s" for the field "%s" is not in the selection') \
3390 % (vals[field], field))
3392 if val not in dict(self._columns[field].selection(
3393 self, cr, user, context=context)):
3394 raise except_orm(_('ValidateError'),
3395 _('The value "%s" for the field "%s" is not in the selection') \
3396 % (vals[field], field))
3397 if self._log_access:
3398 upd0 += ',create_uid,create_date'
3401 cr.execute('insert into "'+self._table+'" (id'+upd0+") values ("+str(id_new)+upd1+')', tuple(upd2))
3402 self.check_access_rule(cr, user, [id_new], 'create', context=context)
3403 upd_todo.sort(lambda x, y: self._columns[x].priority-self._columns[y].priority)
3405 if self._parent_store:
3407 self.pool._init_parent[self._name]=True
3409 parent = vals.get(self._parent_name, False)
3411 cr.execute('select parent_right from '+self._table+' where '+self._parent_name+'=%s order by '+(self._parent_order or self._order), (parent,))
3413 result_p = cr.fetchall()
3414 for (pleft,) in result_p:
3419 cr.execute('select parent_left from '+self._table+' where id=%s', (parent,))
3420 pleft_old = cr.fetchone()[0]
3423 cr.execute('select max(parent_right) from '+self._table)
3424 pleft = cr.fetchone()[0] or 0
3425 cr.execute('update '+self._table+' set parent_left=parent_left+2 where parent_left>%s', (pleft,))
3426 cr.execute('update '+self._table+' set parent_right=parent_right+2 where parent_right>%s', (pleft,))
3427 cr.execute('update '+self._table+' set parent_left=%s,parent_right=%s where id=%s', (pleft+1,pleft+2,id_new))
3429 # default element in context must be remove when call a one2many or many2many
3430 rel_context = context.copy()
3431 for c in context.items():
3432 if c[0].startswith('default_'):
3433 del rel_context[c[0]]
3436 for field in upd_todo:
3437 result += self._columns[field].set(cr, self, id_new, field, vals[field], user, rel_context) or []
3438 self._validate(cr, user, [id_new], context)
3440 if not context.get('no_store_function', False):
3441 result += self._store_get_values(cr, user, [id_new], vals.keys(), context)
3444 for order, object, ids, fields2 in result:
3445 if not (object, ids, fields2) in done:
3446 self.pool.get(object)._store_set_values(cr, user, ids, fields2, context)
3447 done.append((object, ids, fields2))
3449 wf_service = netsvc.LocalService("workflow")
3450 wf_service.trg_create(user, self._name, id_new, cr)
3453 def _store_get_values(self, cr, uid, ids, fields, context):
3455 fncts = self.pool._store_function.get(self._name, [])
3456 for fnct in range(len(fncts)):
3461 for f in (fields or []):
3462 if f in fncts[fnct][3]:
3468 result.setdefault(fncts[fnct][0], {})
3470 # uid == 1 for accessing objects having rules defined on store fields
3471 ids2 = fncts[fnct][2](self,cr, 1, ids, context)
3472 for id in filter(None, ids2):
3473 result[fncts[fnct][0]].setdefault(id, [])
3474 result[fncts[fnct][0]][id].append(fnct)
3476 for object in result:
3478 for id,fnct in result[object].items():
3479 k2.setdefault(tuple(fnct), [])
3480 k2[tuple(fnct)].append(id)
3481 for fnct,id in k2.items():
3482 dict.setdefault(fncts[fnct[0]][4],[])
3483 dict[fncts[fnct[0]][4]].append((fncts[fnct[0]][4],object,id,map(lambda x: fncts[x][1], fnct)))
3491 def _store_set_values(self, cr, uid, ids, fields, context):
3494 if self._log_access:
3495 cr.execute('select id,write_date from '+self._table+' where id in ('+','.join(map(str, ids))+')')
3499 field_dict.setdefault(r[0], [])
3500 res_date = time.strptime((r[1])[:19], '%Y-%m-%d %H:%M:%S')
3501 write_date = datetime.datetime.fromtimestamp(time.mktime(res_date))
3502 for i in self.pool._store_function.get(self._name, []):
3504 up_write_date = write_date + datetime.timedelta(hours=i[5])
3505 if datetime.datetime.now() < up_write_date:
3507 field_dict[r[0]].append(i[1])
3513 if self._columns[f]._multi not in keys:
3514 keys.append(self._columns[f]._multi)
3515 todo.setdefault(self._columns[f]._multi, [])
3516 todo[self._columns[f]._multi].append(f)
3520 # uid == 1 for accessing objects having rules defined on store fields
3521 result = self._columns[val[0]].get(cr, self, ids, val, 1, context=context)
3522 for id,value in result.items():
3524 for f in value.keys():
3525 if f in field_dict[id]:
3532 if self._columns[v]._type in ('many2one', 'one2one'):
3534 value[v] = value[v][0]
3537 upd0.append('"'+v+'"='+self._columns[v]._symbol_set[0])
3538 upd1.append(self._columns[v]._symbol_set[1](value[v]))
3541 cr.execute('update "' + self._table + '" set ' + \
3542 string.join(upd0, ',') + ' where id = %s', upd1)
3546 # uid == 1 for accessing objects having rules defined on store fields
3547 result = self._columns[f].get(cr, self, ids, f, 1, context=context)
3548 for r in result.keys():
3550 if r in field_dict.keys():
3551 if f in field_dict[r]:
3553 for id,value in result.items():
3554 if self._columns[f]._type in ('many2one', 'one2one'):
3559 cr.execute('update "' + self._table + '" set ' + \
3560 '"'+f+'"='+self._columns[f]._symbol_set[0] + ' where id = %s', (self._columns[f]._symbol_set[1](value),id))
3566 def perm_write(self, cr, user, ids, fields, context=None):
3567 raise _('This method does not exist anymore')
3569 # TODO: ameliorer avec NULL
3570 def _where_calc(self, cr, user, args, active_test=True, context=None):
3574 # if the object has a field named 'active', filter out all inactive
3575 # records unless they were explicitely asked for
3576 if 'active' in self._columns and (active_test and context.get('active_test', True)):
3578 active_in_args = False
3580 if a[0] == 'active':
3581 active_in_args = True
3582 if not active_in_args:
3583 args.insert(0, ('active', '=', 1))
3585 args = [('active', '=', 1)]
3589 e = expression.expression(args)
3590 e.parse(cr, user, self, context)
3591 tables = e.get_tables()
3592 qu1, qu2 = e.to_sql()
3593 qu1 = qu1 and [qu1] or []
3595 qu1, qu2, tables = [], [], ['"%s"' % self._table]
3597 return (qu1, qu2, tables)
3599 def _check_qorder(self, word):
3600 if not regex_order.match(word):
3601 raise except_orm(_('AccessError'), _('Bad query.'))
3604 def search(self, cr, user, args, offset=0, limit=None, order=None,
3605 context=None, count=False):
3607 Search for record/s with or without domain
3609 :param cr: database cursor
3610 :param user: current user id
3611 :param args: list of tuples specifying search criteria [('field_name', 'operator', 'value'), ...]
3612 :param offset: optional number from search starts
3613 :param limit: optional max number of records to return
3614 :param order: optional columns to sort by (default: self._order=id )
3615 :param context(optional, highly recommended): context arguments, like lang, time zone
3616 :param count: if True, returns only the number of records matching the criteria, not their ids
3617 :return: id or list of ids of records matching the criteria
3618 :rtype: integer or list of integers
3619 :raise AccessError: * if user tries to bypass access rules for read on the requested object.
3622 * =, !=, >, >=, <, <=, like, ilike, in, not in, child_of, parent_left, parent_right
3624 * '&' (default), '|', '!'
3629 self.pool.get('ir.model.access').check(cr, user, self._name, 'read', context=context)
3630 # compute the where, order by, limit and offset clauses
3631 (qu1, qu2, tables) = self._where_calc(cr, user, args, context=context)
3632 dom = self.pool.get('ir.rule').domain_get(cr, user, self._name, 'read', context=context)
3640 qu1 = ' where '+string.join(qu1, ' and ')
3645 order_by = self._order
3647 self._check_qorder(order)
3648 o = order.split(' ')[0]
3649 if (o in self._columns) and getattr(self._columns[o], '_classic_write'):
3652 limit_str = limit and ' limit %d' % limit or ''
3653 offset_str = offset and ' offset %d' % offset or ''
3657 cr.execute('select count(%s.id) from ' % self._table +
3658 ','.join(tables) +qu1 + limit_str + offset_str, qu2)
3661 cr.execute('select %s.id from ' % self._table + ','.join(tables) +qu1+' order by '+order_by+limit_str+offset_str, qu2)
3663 return [x[0] for x in res]
3665 # returns the different values ever entered for one field
3666 # this is used, for example, in the client when the user hits enter on
3668 def distinct_field_get(self, cr, uid, field, value, args=None, offset=0, limit=None):
3671 if field in self._inherit_fields:
3672 return self.pool.get(self._inherit_fields[field][0]).distinct_field_get(cr, uid, field, value, args, offset, limit)
3674 return self._columns[field].search(cr, self, args, field, value, offset, limit, uid)
3676 def name_get(self, cr, user, ids, context=None):
3679 :param cr: database cursor
3680 :param user: current user id
3681 :type user: integer (example 1)
3682 :param ids: list of ids
3683 :param context: context arguments, like lang, time zone
3684 :return: tuples with the text representation of requested objects for to-many relationships
3691 if isinstance(ids, (int, long)):
3693 return [(r['id'], tools.ustr(r[self._rec_name])) for r in self.read(cr, user, ids,
3694 [self._rec_name], context, load='_classic_write')]
3696 # private implementation of name_search, allows passing a dedicated user for the name_get part to
3697 # solve some access rights issues
3698 def _name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100, name_get_uid=None):
3705 args += [(self._rec_name, operator, name)]
3706 ids = self.search(cr, user, args, limit=limit, context=context)
3707 res = self.name_get(cr, name_get_uid or user, ids, context)
3710 def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100):
3713 :param cr: database cursor
3714 :param user: current user id
3715 :param name: object name to search
3716 :param args: list of tuples specifying search criteria [('field_name', 'operator', 'value'), ...]
3717 :param operator: operator for search criterion
3718 :param context: context arguments, like lang, time zone
3719 :param limit: optional max number of records to return
3720 :return: list of object names matching the search criteria, used to provide completion for to-many relationships
3722 This method is equivalent of search() on name + name_get()
3725 return self._name_search(cr, user, name, args, operator, context, limit)
3727 def copy_data(self, cr, uid, id, default=None, context=None):
3729 Copy given record's data with all its fields values
3731 :param cr: database cursor
3732 :param user: current user id
3733 :param ids: id of the record to copy
3734 :param default: dictionary of field values to update before saving the duplicate object
3735 :param context: context arguments, like lang, time zone
3736 :return: dictionary containing all the field values
3743 if 'state' not in default:
3744 if 'state' in self._defaults:
3745 if callable(self._defaults['state']):
3746 default['state'] = self._defaults['state'](self, cr, uid, context)
3748 default['state'] = self._defaults['state']
3750 data = self.read(cr, uid, [id], context=context)[0]
3751 fields = self.fields_get(cr, uid, context=context)
3754 ftype = fields[f]['type']
3756 if self._log_access and f in ('create_date', 'create_uid', 'write_date', 'write_uid'):
3760 data[f] = default[f]
3761 elif ftype == 'function':
3763 elif ftype == 'many2one':
3765 data[f] = data[f] and data[f][0]
3768 elif ftype in ('one2many', 'one2one'):
3770 rel = self.pool.get(fields[f]['relation'])
3771 if data[f] != False:
3772 for rel_id in data[f]:
3773 # the lines are first duplicated using the wrong (old)
3774 # parent but then are reassigned to the correct one thanks
3776 d,t = rel.copy_data(cr, uid, rel_id, context=context)
3777 res.append((0, 0, d))
3780 elif ftype == 'many2many':
3781 data[f] = [(6, 0, data[f])]
3783 trans_obj = self.pool.get('ir.translation')
3784 #TODO: optimize translations
3788 if f in self._columns and self._columns[f].translate:
3789 trans_name=self._name+","+f
3790 elif f in self._inherit_fields and self._inherit_fields[f][2].translate:
3791 trans_name=self._inherit_fields[f][0]+","+f
3796 trans_ids = trans_obj.search(cr, uid, [
3797 ('name', '=', trans_name),
3798 ('res_id','=',data['id'])
3801 trans_data.extend(trans_obj.read(cr,uid,trans_ids,context=context))
3805 for v in self._inherits:
3806 del data[self._inherits[v]]
3807 return data, trans_data
3809 def copy(self, cr, uid, id, default=None, context=None):
3811 Duplicate record with given id updating it with default values
3813 :param cr: database cursor
3814 :param uid: current user id
3815 :param id: id of the record to copy
3816 :param default: dictionary of field values to update before saving the duplicate object
3817 :type default: dictionary (example {'field_name': field_value, ...})
3818 :param context: context arguments, like lang, time zone
3822 trans_obj = self.pool.get('ir.translation')
3823 data, trans_data = self.copy_data(cr, uid, id, default, context)
3824 new_id = self.create(cr, uid, data, context)
3825 for record in trans_data:
3827 record['res_id'] = new_id
3828 trans_obj.create(cr, uid, record, context)
3831 def exists(self, cr, uid, ids, context=None):
3832 if type(ids) in (int,long):
3834 query = 'SELECT count(1) FROM "%s"' % (self._table)
3835 cr.execute(query + "WHERE ID IN %s", (tuple(ids),))
3836 return cr.fetchone()[0] == len(ids)
3838 def check_recursion(self, cr, uid, ids, parent=None):
3840 Check recursion in records
3842 :param cr: database cursor
3843 :param uid: current user id
3844 :param ids: list of ids of records
3845 :param parent: parent field name
3846 :return: True or False based on recursion detection
3850 parent = self._parent_name
3852 while len(ids_parent):
3854 for i in range(0, len(ids), cr.IN_MAX):
3855 sub_ids_parent = ids_parent[i:i+cr.IN_MAX]
3856 cr.execute('SELECT distinct "'+parent+'"'+
3857 ' FROM "'+self._table+'" ' \
3858 'WHERE id = ANY(%s)',(sub_ids_parent,))
3859 ids_parent2.extend(filter(None, map(lambda x: x[0], cr.fetchall())))
3860 ids_parent = ids_parent2
3861 for i in ids_parent:
3866 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: