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):
173 if name not in self._data[self._id]:
174 # build the list of fields we will fetch
176 # fetch the definition of the field which was asked for
177 if name in self._table._columns:
178 col = self._table._columns[name]
179 elif name in self._table._inherit_fields:
180 col = self._table._inherit_fields[name][2]
181 elif hasattr(self._table, str(name)):
182 if isinstance(getattr(self._table, name), (types.MethodType, types.LambdaType, types.FunctionType)):
183 return lambda *args, **argv: getattr(self._table, name)(self._cr, self._uid, [self._id], *args, **argv)
185 return getattr(self._table, name)
187 self.logger.notifyChannel("browse_record", netsvc.LOG_WARNING,
188 "Field '%s' does not exist in object '%s': \n%s" % (
189 name, self, ''.join(traceback.format_exc())))
190 raise KeyError("Field '%s' does not exist in object '%s'" % (
193 # if the field is a classic one or a many2one, we'll fetch all classic and many2one fields
195 # gen the list of "local" (ie not inherited) fields which are classic or many2one
196 ffields = filter(lambda x: x[1]._classic_write, self._table._columns.items())
197 # gen the list of inherited fields
198 inherits = map(lambda x: (x[0], x[1][2]), self._table._inherit_fields.items())
199 # complete the field list with the inherited fields which are classic or many2one
200 ffields += filter(lambda x: x[1]._classic_write, inherits)
201 # otherwise we fetch only that field
203 ffields = [(name, col)]
204 ids = filter(lambda id: name not in self._data[id], self._data.keys())
206 fffields = map(lambda x: x[0], ffields)
207 datas = self._table.read(self._cr, self._uid, ids, fffields, context=self._context, load="_classic_write")
208 if self._fields_process:
209 lang = self._context.get('lang', 'en_US') or 'en_US'
210 lang_obj_ids = self.pool.get('res.lang').search(self._cr, self._uid,[('code','=',lang)])
212 raise Exception(_('Language with code "%s" is not defined in your system !\nDefine it through the Administration menu.') % (lang,))
213 lang_obj = self.pool.get('res.lang').browse(self._cr, self._uid,lang_obj_ids[0])
216 if f._type in self._fields_process:
218 d[n] = self._fields_process[f._type](d[n])
220 d[n].set_value(self._cr, self._uid, d[n], self, f, lang_obj)
224 # Where did those ids come from? Perhaps old entries in ir_model_dat?
225 self.__logger.warn("No datas found for ids %s in %s",
227 raise KeyError('Field %s not found in %s'%(name,self))
228 # create browse records for 'remote' objects
230 if len(str(data['id']).split('-')) > 1:
231 data['id'] = int(str(data['id']).split('-')[0])
234 if f._type in ('many2one', 'one2one'):
236 obj = self._table.pool.get(f._obj)
238 if type(data[n]) in (type([]),type( (1,) )):
243 # FIXME: this happen when a _inherits object
244 # overwrite a field of it parent. Need
245 # testing to be sure we got the right
246 # object and not the parent one.
247 if not isinstance(ids2, browse_record):
248 new_data[n] = browse_record(self._cr,
249 self._uid, ids2, obj, self._cache,
250 context=self._context,
251 list_class=self._list_class,
252 fields_process=self._fields_process)
254 new_data[n] = browse_null()
256 new_data[n] = browse_null()
257 elif f._type in ('one2many', 'many2many') and len(data[n]):
258 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)
259 elif f._type in ('reference'):
261 if isinstance(data[n], browse_record):
262 new_data[n] = data[n]
264 ref_obj, ref_id = data[n].split(',')
265 ref_id = long(ref_id)
266 obj = self._table.pool.get(ref_obj)
268 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)
270 new_data[n] = browse_null()
272 new_data[n] = data[n]
273 self._data[data['id']].update(new_data)
274 if not name in self._data[self._id]:
275 #how did this happen?
276 self.logger.notifyChannel("browse_record", netsvc.LOG_ERROR,
277 "Ffields: %s, datas: %s"%(fffields, datas))
278 self.logger.notifyChannel("browse_record", netsvc.LOG_ERROR,
279 "Data: %s, Table: %s"%(self._data[self._id], self._table))
280 raise KeyError(_('Unknown attribute %s in %s ') % (name, self))
281 return self._data[self._id][name]
283 def __getattr__(self, name):
287 raise AttributeError(e)
289 def __contains__(self, name):
290 return (name in self._table._columns) or (name in self._table._inherit_fields) or hasattr(self._table, name)
292 def __hasattr__(self, name):
299 return "browse_record(%s, %d)" % (self._table_name, self._id)
301 def __eq__(self, other):
302 return (self._table_name, self._id) == (other._table_name, other._id)
304 def __ne__(self, other):
305 return (self._table_name, self._id) != (other._table_name, other._id)
307 # we need to define __unicode__ even though we've already defined __str__
308 # because we have overridden __getattr__
309 def __unicode__(self):
310 return unicode(str(self))
313 return hash((self._table_name, self._id))
321 (type returned by postgres when the column was created, type expression to create the column)
325 fields.boolean: 'bool',
326 fields.integer: 'int4',
327 fields.integer_big: 'int8',
331 fields.datetime: 'timestamp',
332 fields.binary: 'bytea',
333 fields.many2one: 'int4',
335 if type(f) in type_dict:
336 f_type = (type_dict[type(f)], type_dict[type(f)])
337 elif isinstance(f, fields.float):
339 f_type = ('numeric', 'NUMERIC')
341 f_type = ('float8', 'DOUBLE PRECISION')
342 elif isinstance(f, (fields.char, fields.reference)):
343 f_type = ('varchar', 'VARCHAR(%d)' % (f.size,))
344 elif isinstance(f, fields.selection):
345 if isinstance(f.selection, list) and isinstance(f.selection[0][0], (str, unicode)):
346 f_size = reduce(lambda x, y: max(x, len(y[0])), f.selection, f.size or 16)
347 elif isinstance(f.selection, list) and isinstance(f.selection[0][0], int):
350 f_size = getattr(f, 'size', None) or 16
353 f_type = ('int4', 'INTEGER')
355 f_type = ('varchar', 'VARCHAR(%d)' % f_size)
356 elif isinstance(f, fields.function) and eval('fields.'+(f._type)) in type_dict:
357 t = eval('fields.'+(f._type))
358 f_type = (type_dict[t], type_dict[t])
359 elif isinstance(f, fields.function) and f._type == 'float':
361 f_type = ('numeric', 'NUMERIC')
363 f_type = ('float8', 'DOUBLE PRECISION')
364 elif isinstance(f, fields.function) and f._type == 'selection':
365 f_type = ('text', 'text')
366 elif isinstance(f, fields.function) and f._type == 'char':
367 f_type = ('varchar', 'VARCHAR(%d)' % (f.size))
369 logger = netsvc.Logger()
370 logger.notifyChannel("init", netsvc.LOG_WARNING, '%s type not supported!' % (type(f)))
375 class orm_template(object):
381 _parent_name = 'parent_id'
382 _parent_store = False
383 _parent_order = False
392 CONCURRENCY_CHECK_FIELD = '__last_update'
394 def view_init(self, cr , uid , fields_list, context=None):
395 """Override this method to do specific things when a view on the object is opened."""
398 def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None):
399 raise _('The read_group method is not implemented on this object !')
401 def _field_create(self, cr, context={}):
402 cr.execute("SELECT id FROM ir_model WHERE model=%s", (self._name,))
404 cr.execute('SELECT nextval(%s)', ('ir_model_id_seq',))
405 model_id = cr.fetchone()[0]
406 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'))
408 model_id = cr.fetchone()[0]
409 if 'module' in context:
410 name_id = 'model_'+self._name.replace('.','_')
411 cr.execute('select * from ir_model_data where name=%s and res_id=%s and module=%s', (name_id,model_id,context['module']))
413 cr.execute("INSERT INTO ir_model_data (name,date_init,date_update,module,model,res_id) VALUES (%s, now(), now(), %s, %s, %s)", \
414 (name_id, context['module'], 'ir.model', model_id)
419 cr.execute("SELECT * FROM ir_model_fields WHERE model=%s", (self._name,))
421 for rec in cr.dictfetchall():
422 cols[rec['name']] = rec
424 for (k, f) in self._columns.items():
426 'model_id': model_id,
429 'field_description': f.string.replace("'", " "),
431 'relation': f._obj or 'NULL',
432 'view_load': (f.view_load and 1) or 0,
433 'select_level': tools.ustr(f.select or 0),
434 'readonly':(f.readonly and 1) or 0,
435 'required':(f.required and 1) or 0,
436 'selectable' : (f.selectable and 1) or 0,
437 'relation_field': (f._type=='one2many' and isinstance(f,fields.one2many)) and f._fields_id or '',
439 # When its a custom field,it does not contain f.select
440 if context.get('field_state','base') == 'manual':
441 if context.get('field_name','') == k:
442 vals['select_level'] = context.get('select','0')
443 #setting value to let the problem NOT occur next time
445 vals['select_level'] = cols[k]['select_level']
448 cr.execute('select nextval(%s)', ('ir_model_fields_id_seq',))
449 id = cr.fetchone()[0]
451 cr.execute("""INSERT INTO ir_model_fields (
452 id, model_id, model, name, field_description, ttype,
453 relation,view_load,state,select_level,relation_field
455 %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s
457 id, vals['model_id'], vals['model'], vals['name'], vals['field_description'], vals['ttype'],
458 vals['relation'], bool(vals['view_load']), 'base',
459 vals['select_level'],vals['relation_field']
461 if 'module' in context:
462 name1 = 'field_' + self._table + '_' + k
463 cr.execute("select name from ir_model_data where name=%s", (name1,))
465 name1 = name1 + "_" + str(id)
466 cr.execute("INSERT INTO ir_model_data (name,date_init,date_update,module,model,res_id) VALUES (%s, now(), now(), %s, %s, %s)", \
467 (name1, context['module'], 'ir.model.fields', id)
470 for key, val in vals.items():
471 if cols[k][key] != vals[key]:
472 cr.execute('update ir_model_fields set field_description=%s where model=%s and name=%s', (vals['field_description'], vals['model'], vals['name']))
474 cr.execute("""UPDATE ir_model_fields SET
475 model_id=%s, field_description=%s, ttype=%s, relation=%s,
476 view_load=%s, select_level=%s, readonly=%s ,required=%s, selectable=%s, relation_field=%s
478 model=%s AND name=%s""", (
479 vals['model_id'], vals['field_description'], vals['ttype'],
480 vals['relation'], bool(vals['view_load']),
481 vals['select_level'], bool(vals['readonly']),bool(vals['required']),bool(vals['selectable']),vals['relation_field'],vals['model'], vals['name']
486 def _auto_init(self, cr, context={}):
487 self._field_create(cr, context)
489 def __init__(self, cr):
490 if not self._name and not hasattr(self, '_inherit'):
491 name = type(self).__name__.split('.')[0]
492 msg = "The class %s has to have a _name attribute" % name
494 logger = netsvc.Logger()
495 logger.notifyChannel('orm', netsvc.LOG_ERROR, msg )
496 raise except_orm('ValueError', msg )
498 if not self._description:
499 self._description = self._name
501 self._table = self._name.replace('.', '_')
503 def browse(self, cr, uid, select, context=None, list_class=None, fields_process={}):
505 Fetch records as objects allowing to use dot notation to browse fields and relations
507 :param cr: database cursor
508 :param user: current user id
509 :param select: id or list of ids
510 :param context: context arguments, like lang, time zone
511 :rtype: object or list of objects requested
516 self._list_class = list_class or browse_record_list
518 # need to accepts ints and longs because ids coming from a method
519 # launched by button in the interface have a type long...
520 if isinstance(select, (int, long)):
521 return browse_record(cr, uid, select, self, cache, context=context, list_class=self._list_class, fields_process=fields_process)
522 elif isinstance(select, list):
523 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)
527 def __export_row(self, cr, uid, row, fields, context=None):
529 def check_type(field_type):
530 if field_type == 'float':
532 elif field_type == 'integer':
534 elif field_type == 'boolean':
538 def selection_field(in_field):
539 col_obj = self.pool.get(in_field.keys()[0])
540 if f[i] in col_obj._columns.keys():
541 return col_obj._columns[f[i]]
542 elif f[i] in col_obj._inherits.keys():
543 selection_field(col_obj._inherits)
549 data = map(lambda x: '', range(len(fields)))
551 for fpos in range(len(fields)):
560 model_data = self.pool.get('ir.model.data')
561 data_ids = model_data.search(cr, uid, [('model','=',r._table_name),('res_id','=',r['id'])])
563 d = model_data.read(cr, uid, data_ids, ['name','module'])[0]
565 r = '%s.%s'%(d['module'],d['name'])
572 # To display external name of selection field when its exported
573 if not context.get('import_comp',False):# Allow external name only if its not import compatible
575 if f[i] in self._columns.keys():
576 cols = self._columns[f[i]]
577 elif f[i] in self._inherit_fields.keys():
578 cols = selection_field(self._inherits)
579 if cols and cols._type == 'selection':
580 sel_list = cols.selection
581 if type(sel_list) == type([]):
582 r = [x[1] for x in sel_list if r==x[0]][0]
585 if f[i] in self._columns:
586 r = check_type(self._columns[f[i]]._type)
587 elif f[i] in self._inherit_fields:
588 r = check_type(self._inherit_fields[f[i]][2]._type)
591 if isinstance(r, (browse_record_list, list)):
593 fields2 = map(lambda x: (x[:i+1]==f[:i+1] and x[i+1:]) \
599 lines2 = self.__export_row(cr, uid, row2, fields2,
602 for fpos2 in range(len(fields)):
603 if lines2 and lines2[0][fpos2]:
604 data[fpos2] = lines2[0][fpos2]
608 if isinstance(rr.name, browse_record):
610 rr_name = self.pool.get(rr._table_name).name_get(cr, uid, [rr.id])
611 rr_name = rr_name and rr_name[0] and rr_name[0][1] or ''
612 dt += tools.ustr(rr_name or '') + ','
622 if isinstance(r, browse_record):
623 r = self.pool.get(r._table_name).name_get(cr, uid, [r.id])
624 r = r and r[0] and r[0][1] or ''
625 data[fpos] = tools.ustr(r or '')
626 return [data] + lines
628 def export_data(self, cr, uid, ids, fields_to_export, context=None):
630 Export fields for selected objects
632 :param cr: database cursor
633 :param uid: current user id
634 :param ids: list of ids
635 :param fields_to_export: list of fields
636 :param context: context arguments, like lang, time zone, may contain import_comp(default: False) to make exported data compatible with import_data()
637 :rtype: dictionary with a *datas* matrix
639 This method is used when exporting data via client menu
644 imp_comp = context.get('import_comp',False)
645 cols = self._columns.copy()
646 for f in self._inherit_fields:
647 cols.update({f: self._inherit_fields[f][2]})
648 fields_to_export = map(lambda x: x.split('/'), fields_to_export)
649 fields_export = fields_to_export+[]
652 for field in fields_export:
653 if imp_comp and len(field)>1:
654 warning_fields.append('/'.join(map(lambda x:x in cols and cols[x].string or x,field)))
655 elif len (field) <=1:
656 if imp_comp and cols.get(field and field[0],False):
657 if ((isinstance(cols[field[0]], fields.function) and not cols[field[0]].store) \
658 or isinstance(cols[field[0]], fields.related)\
659 or isinstance(cols[field[0]], fields.one2many)):
660 warning_fields.append('/'.join(map(lambda x:x in cols and cols[x].string or x,field)))
662 if imp_comp and len(warning_fields):
663 warning = 'Following columns cannot be exported since you select to be import compatible.\n%s' %('\n'.join(warning_fields))
665 return {'warning' : warning}
666 for row in self.browse(cr, uid, ids, context):
667 datas += self.__export_row(cr, uid, row, fields_to_export, context)
668 return {'datas':datas}
670 def import_data(self, cr, uid, fields, datas, mode='init', current_module='', noupdate=False, context=None, filename=None):
672 Import given data in given module
674 :param cr: database cursor
675 :param uid: current user id
676 :param ids: list of ids
677 :param fields: list of fields
678 :param data: data to import
679 :param mode: 'init' or 'update' for record creation
680 :param current_module: module name
681 :param noupdate: flag for record creation
682 :param context: context arguments, like lang, time zone,
683 :param filename: optional file to store partial import state for recovery
686 This method is used when importing data via client menu
691 fields = map(lambda x: x.split('/'), fields)
692 logger = netsvc.Logger()
693 ir_model_data_obj = self.pool.get('ir.model.data')
695 def _check_db_id(self, model_name, db_id):
696 obj_model = self.pool.get(model_name)
697 ids = obj_model.search(cr, uid, [('id','=',int(db_id))])
699 raise Exception(_("Database ID doesn't exist: %s : %s") %(model_name, db_id))
702 def process_liness(self, datas, prefix, current_module, model_name, fields_def, position=0):
703 line = datas[position]
712 ir_model_data_obj = self.pool.get('ir.model.data')
714 # Import normal fields
716 for i in range(len(fields)):
718 raise Exception(_('Please check that all your lines have %d columns.') % (len(fields),))
723 if prefix and not prefix[0] in field:
726 if (len(field)==len(prefix)+1) and field[len(prefix)].endswith(':db_id'):
730 field_name = field[0].split(':')[0]
731 model_rel = fields_def[field_name]['relation']
733 if fields_def[field[len(prefix)][:-6]]['type']=='many2many':
735 for db_id in line[i].split(config.get('csv_internal_sep')):
737 _check_db_id(self, model_rel, db_id)
740 warning += [tools.exception_to_unicode(e)]
741 logger.notifyChannel("import", netsvc.LOG_ERROR,
742 tools.exception_to_unicode(e))
744 res = [(6, 0, res_id)]
747 _check_db_id(self, model_rel, line[i])
750 warning += [tools.exception_to_unicode(e)]
751 logger.notifyChannel("import", netsvc.LOG_ERROR,
752 tools.exception_to_unicode(e))
753 row[field_name] = res or False
756 if (len(field)==len(prefix)+1) and field[len(prefix)].endswith(':id'):
759 if fields_def[field[len(prefix)][:-3]]['type']=='many2many':
761 for word in line[i].split(config.get('csv_internal_sep')):
763 module, xml_id = word.rsplit('.', 1)
765 module, xml_id = current_module, word
766 id = ir_model_data_obj._get_id(cr, uid, module,
768 res_id2 = ir_model_data_obj.read(cr, uid, [id],
769 ['res_id'])[0]['res_id']
771 res_id.append(res_id2)
773 res_id = [(6, 0, res_id)]
776 module, xml_id = line[i].rsplit('.', 1)
778 module, xml_id = current_module, line[i]
779 id = ir_model_data_obj._get_id(cr, uid, module, xml_id)
781 res_res_id = ir_model_data_obj.read(cr, uid, [id],
784 res_id = res_res_id[0]['res_id']
785 row[field[-1][:-3]] = res_id or False
787 if (len(field) == len(prefix)+1) and \
788 len(field[len(prefix)].split(':lang=')) == 2:
789 f, lang = field[len(prefix)].split(':lang=')
790 translate.setdefault(lang, {})[f]=line[i] or False
792 if (len(field) == len(prefix)+1) and \
793 (prefix == field[0:len(prefix)]):
794 if field[len(prefix)] == "id":
797 is_xml_id = data_id = line[i]
798 d = data_id.split('.')
799 module = len(d)>1 and d[0] or ''
800 name = len(d)>1 and d[1] or d[0]
801 data_ids = ir_model_data_obj.search(cr, uid, [('module','=',module),('model','=',model_name),('name','=',name)])
803 d = ir_model_data_obj.read(cr, uid, data_ids, ['res_id'])[0]
805 if is_db_id and not db_id:
806 data_ids = ir_model_data_obj.search(cr, uid, [('module','=',module),('model','=',model_name),('res_id','=',is_db_id)])
807 if not len(data_ids):
808 ir_model_data_obj.create(cr, uid, {'module':module, 'model':model_name, 'name':name, 'res_id':is_db_id})
810 if is_db_id and int(db_id) != int(is_db_id):
811 warning += [_("Id is not the same than existing one: %s")%(is_db_id)]
812 logger.notifyChannel("import", netsvc.LOG_ERROR,
813 _("Id is not the same than existing one: %s")%(is_db_id))
816 if field[len(prefix)] == "db_id":
819 _check_db_id(self, model_name, line[i])
820 data_res_id = is_db_id = int(line[i])
822 warning += [tools.exception_to_unicode(e)]
823 logger.notifyChannel("import", netsvc.LOG_ERROR,
824 tools.exception_to_unicode(e))
826 data_ids = ir_model_data_obj.search(cr, uid, [('model','=',model_name),('res_id','=',line[i])])
828 d = ir_model_data_obj.read(cr, uid, data_ids, ['name','module'])[0]
831 data_id = '%s.%s'%(d['module'],d['name'])
834 if is_xml_id and not data_id:
836 if is_xml_id and is_xml_id!=data_id:
837 warning += [_("Id is not the same than existing one: %s")%(line[i])]
838 logger.notifyChannel("import", netsvc.LOG_ERROR,
839 _("Id is not the same than existing one: %s")%(line[i]))
842 if fields_def[field[len(prefix)]]['type'] == 'integer':
843 res = line[i] and int(line[i])
844 elif fields_def[field[len(prefix)]]['type'] == 'boolean':
845 res = line[i].lower() not in ('0', 'false', 'off')
846 elif fields_def[field[len(prefix)]]['type'] == 'float':
847 res = line[i] and float(line[i])
848 elif fields_def[field[len(prefix)]]['type'] == 'selection':
850 if isinstance(fields_def[field[len(prefix)]]['selection'],
852 sel = fields_def[field[len(prefix)]]['selection']
854 sel = fields_def[field[len(prefix)]]['selection'](self,
857 if line[i] in [tools.ustr(key),tools.ustr(val)]: #Acepting key or value for selection field
860 if line[i] and not res:
861 logger.notifyChannel("import", netsvc.LOG_WARNING,
862 _("key '%s' not found in selection field '%s'") % \
863 (line[i], field[len(prefix)]))
865 warning += [_("Key/value '%s' not found in selection field '%s'")%(line[i],field[len(prefix)])]
867 elif fields_def[field[len(prefix)]]['type']=='many2one':
870 relation = fields_def[field[len(prefix)]]['relation']
871 res2 = self.pool.get(relation).name_search(cr, uid,
872 line[i], [], operator='=', context=context)
873 res = (res2 and res2[0][0]) or False
875 warning += [_("Relation not found: %s on '%s'")%(line[i],relation)]
876 logger.notifyChannel("import", netsvc.LOG_WARNING,
877 _("Relation not found: %s on '%s'")%(line[i],relation))
878 elif fields_def[field[len(prefix)]]['type']=='many2many':
881 relation = fields_def[field[len(prefix)]]['relation']
882 for word in line[i].split(config.get('csv_internal_sep')):
883 res2 = self.pool.get(relation).name_search(cr,
884 uid, word, [], operator='=', context=context)
885 res3 = (res2 and res2[0][0]) or False
887 warning += [_("Relation not found: %s on '%s'")%(line[i],relation)]
888 logger.notifyChannel("import",
890 _("Relation not found: %s on '%s'")%(line[i],relation))
896 res = line[i] or False
897 row[field[len(prefix)]] = res
898 elif (prefix==field[0:len(prefix)]):
899 if field[0] not in todo:
900 todo.append(field[len(prefix)])
902 # Import one2many, many2many fields
906 relation_obj = self.pool.get(fields_def[field]['relation'])
907 newfd = relation_obj.fields_get(
908 cr, uid, context=context)
909 res = process_liness(self, datas, prefix + [field], current_module, relation_obj._name, newfd, position)
910 (newrow, max2, w2, translate2, data_id2, data_res_id2) = res
911 nbrmax = max(nbrmax, max2)
912 warning = warning + w2
913 reduce(lambda x, y: x and y, newrow)
914 row[field] = (reduce(lambda x, y: x or y, newrow.values()) and \
915 [(0, 0, newrow)]) or []
917 while (position+i)<len(datas):
919 for j in range(len(fields)):
921 if (len(field2) <= (len(prefix)+1)) and datas[position+i][j]:
926 (newrow, max2, w2, translate2, data_id2, data_res_id2) = process_liness(
927 self, datas, prefix+[field], current_module, relation_obj._name, newfd, position+i)
929 if reduce(lambda x, y: x or y, newrow.values()):
930 row[field].append((0, 0, newrow))
932 nbrmax = max(nbrmax, i)
935 for i in range(max(nbrmax, 1)):
938 result = (row, nbrmax, warning, translate, data_id, data_res_id)
941 fields_def = self.fields_get(cr, uid, context=context)
944 initial_size = len(datas)
945 if config.get('import_partial', False) and filename:
946 data = pickle.load(file(config.get('import_partial')))
947 original_value = data.get(filename, 0)
953 (res, other, warning, translate, data_id, res_id) = \
954 process_liness(self, datas, [], current_module, self._name, fields_def)
957 return (-1, res, 'Line ' + str(counter) +' : ' + '!\n'.join(warning), '')
960 id = ir_model_data_obj._update(cr, uid, self._name,
961 current_module, res, xml_id=data_id, mode=mode,
962 noupdate=noupdate, res_id=res_id, context=context)
967 if isinstance(e,psycopg2.IntegrityError):
968 msg= _('Insertion Failed! ')
969 for key in self.pool._sql_error.keys():
971 msg = self.pool._sql_error[key]
973 return (-1, res, 'Line ' + str(counter) +' : ' + msg, '' )
974 if isinstance(e, osv.orm.except_orm ):
975 msg = _('Insertion Failed! ' + e[1])
976 return (-1, res, 'Line ' + str(counter) +' : ' + msg, '' )
977 #Raising Uncaught exception
979 for lang in translate:
980 context2 = context.copy()
981 context2['lang'] = lang
982 self.write(cr, uid, [id], translate[lang], context2)
983 if config.get('import_partial', False) and filename and (not (counter%100)) :
984 data = pickle.load(file(config.get('import_partial')))
985 data[filename] = initial_size - len(datas) + original_value
986 pickle.dump(data, file(config.get('import_partial'),'wb'))
989 #except Exception, e:
990 # logger.notifyChannel("import", netsvc.LOG_ERROR, e)
993 # return (-1, res, e[0], warning)
995 # return (-1, res, e[0], '')
998 # TODO: Send a request with the result and multi-thread !
1000 return (done, 0, 0, 0)
1002 def read(self, cr, user, ids, fields=None, context=None, load='_classic_read'):
1003 raise _('The read method is not implemented on this object !')
1005 def get_invalid_fields(self,cr,uid):
1006 return list(self._invalids)
1008 def _validate(self, cr, uid, ids, context=None):
1009 context = context or {}
1010 lng = context.get('lang', False) or 'en_US'
1011 trans = self.pool.get('ir.translation')
1013 for constraint in self._constraints:
1014 fun, msg, fields = constraint
1015 if not fun(self, cr, uid, ids):
1016 translated_msg = trans._get_source(cr, uid, self._name, 'constraint', lng, source=msg) or msg
1018 _("Error occurred while validating the field(s) %s: %s") % (','.join(fields), translated_msg)
1020 self._invalids.update(fields)
1023 raise except_orm('ValidateError', '\n'.join(error_msgs))
1025 self._invalids.clear()
1027 def default_get(self, cr, uid, fields_list, context=None):
1029 Set default values for the object's fields.
1031 :param fields_list: fields for which the object doesn't have any value yet, and default values need to be provided.
1032 If fields outside this list are returned, the user-provided values will be overwritten.
1033 :rtype: a dict of {field_name:default_value}
1038 def perm_read(self, cr, user, ids, context=None, details=True):
1039 raise _('The perm_read method is not implemented on this object !')
1041 def unlink(self, cr, uid, ids, context=None):
1042 raise _('The unlink method is not implemented on this object !')
1044 def write(self, cr, user, ids, vals, context=None):
1045 raise _('The write method is not implemented on this object !')
1047 def create(self, cr, user, vals, context=None):
1048 raise _('The create method is not implemented on this object !')
1050 # returns the definition of each field in the object
1051 # the optional fields parameter can limit the result to some fields
1052 def fields_get_keys(self, cr, user, context=None, read_access=True):
1055 res = self._columns.keys()
1056 for parent in self._inherits:
1057 res.extend(self.pool.get(parent).fields_get_keys(cr, user, fields, context))
1060 def fields_get(self, cr, user, fields=None, context=None, read_access=True):
1064 translation_obj = self.pool.get('ir.translation')
1065 model_access_obj = self.pool.get('ir.model.access')
1066 for parent in self._inherits:
1067 res.update(self.pool.get(parent).fields_get(cr, user, fields, context))
1069 if self._columns.keys():
1070 for f in self._columns.keys():
1071 if fields and f not in fields:
1073 res[f] = {'type': self._columns[f]._type}
1074 for arg in ('string', 'readonly', 'states', 'size', 'required', 'group_operator',
1075 'change_default', 'translate', 'help', 'select', 'selectable'):
1076 if getattr(self._columns[f], arg):
1077 res[f][arg] = getattr(self._columns[f], arg)
1079 res[f]['readonly'] = True
1080 res[f]['states'] = {}
1081 for arg in ('digits', 'invisible','filters'):
1082 if getattr(self._columns[f], arg, None):
1083 res[f][arg] = getattr(self._columns[f], arg)
1086 res_trans = translation_obj._get_source(cr, user, self._name + ',' + f, 'field', context.get('lang', False) or 'en_US')
1088 res[f]['string'] = res_trans
1089 help_trans = translation_obj._get_source(cr, user, self._name + ',' + f, 'help', context.get('lang', False) or 'en_US')
1091 res[f]['help'] = help_trans
1093 if hasattr(self._columns[f], 'selection'):
1094 if isinstance(self._columns[f].selection, (tuple, list)):
1095 sel = self._columns[f].selection
1096 # translate each selection option
1098 for (key, val) in sel:
1101 val2 = translation_obj._get_source(cr, user, self._name + ',' + f, 'selection', context.get('lang', False) or 'en_US', val)
1102 sel2.append((key, val2 or val))
1104 res[f]['selection'] = sel
1106 # call the 'dynamic selection' function
1107 res[f]['selection'] = self._columns[f].selection(self, cr,
1109 if res[f]['type'] in ('one2many', 'many2many', 'many2one', 'one2one'):
1110 res[f]['relation'] = self._columns[f]._obj
1111 res[f]['domain'] = self._columns[f]._domain
1112 res[f]['context'] = self._columns[f]._context
1114 #TODO : read the fields from the database
1118 # filter out fields which aren't in the fields list
1119 for r in res.keys():
1125 # Overload this method if you need a window title which depends on the context
1127 def view_header_get(self, cr, user, view_id=None, view_type='form', context=None):
1130 def __view_look_dom(self, cr, user, node, view_id, context=None):
1138 if isinstance(s, unicode):
1139 return s.encode('utf8')
1142 if node.tag in ('field', 'node', 'arrow'):
1143 if node.get('object'):
1148 if f.tag in ('field'):
1149 xml += etree.tostring(f, encoding="utf-8")
1151 new_xml = etree.fromstring(encode(xml))
1152 ctx = context.copy()
1153 ctx['base_model_name'] = self._name
1154 xarch, xfields = self.pool.get(node.get('object',False)).__view_look_dom_arch(cr, user, new_xml, view_id, ctx)
1155 views[str(f.tag)] = {
1159 attrs = {'views': views}
1161 fields = views.get('field',False) and views['field'].get('fields',False)
1162 if node.get('name'):
1165 if node.get('name') in self._columns:
1166 column = self._columns[node.get('name')]
1168 column = self._inherit_fields[node.get('name')][2]
1173 relation = column._obj
1177 if f.tag in ('form', 'tree', 'graph'):
1179 ctx = context.copy()
1180 ctx['base_model_name'] = self._name
1181 xarch, xfields = self.pool.get(relation).__view_look_dom_arch(cr, user, f, view_id, ctx)
1182 views[str(f.tag)] = {
1186 attrs = {'views': views}
1187 if node.get('widget') and node.get('widget') == 'selection':
1188 # We can not use the 'string' domain has it is defined according to the record !
1190 if column._domain and not isinstance(column._domain, (str, unicode)):
1191 dom = column._domain
1192 dom += eval(node.get('domain','[]'), {'uid':user, 'time':time})
1193 context.update(eval(node.get('context','{}')))
1194 attrs['selection'] = self.pool.get(relation).name_search(cr, user, '', dom, context=context,limit=None)
1195 if (node.get('required') and not int(node.get('required'))) or not column.required:
1196 attrs['selection'].append((False,''))
1197 fields[node.get('name')] = attrs
1199 elif node.tag in ('form', 'tree'):
1200 result = self.view_header_get(cr, user, False, node.tag, context)
1202 node.set('string', result)
1204 elif node.tag == 'calendar':
1205 for additional_field in ('date_start', 'date_delay', 'date_stop', 'color'):
1206 if node.get(additional_field):
1207 fields[node.get(additional_field)] = {}
1209 if 'groups' in node.attrib:
1210 if node.get('groups'):
1211 groups = node.get('groups').split(',')
1213 access_pool = self.pool.get('ir.model.access')
1214 for group in groups:
1215 readonly = readonly or access_pool.check_groups(cr, user, group)
1217 node.set('invisible', '1')
1218 del(node.attrib['groups'])
1221 if ('lang' in context) and not result:
1222 if node.get('string'):
1223 trans = self.pool.get('ir.translation')._get_source(cr, user, self._name, 'view', context['lang'], node.get('string').encode('utf8'))
1224 if not trans and ('base_model_name' in context):
1225 trans = self.pool.get('ir.translation')._get_source(cr, user, context['base_model_name'], 'view', context['lang'], node.get('string').encode('utf8'))
1227 node.set('string', trans)
1229 trans = self.pool.get('ir.translation')._get_source(cr, user, self._name, 'view', context['lang'], node.get('sum').encode('utf8'))
1231 node.set('sum', trans)
1235 fields.update(self.__view_look_dom(cr, user, f, view_id, context))
1239 def __view_look_dom_arch(self, cr, user, node, view_id, context=None):
1240 fields_def = self.__view_look_dom(cr, user, node, view_id, context=context)
1242 rolesobj = self.pool.get('res.roles')
1243 usersobj = self.pool.get('res.users')
1245 buttons = (n for n in node.getiterator('button') if n.get('type') != 'object')
1246 for button in buttons:
1248 if user != 1: # admin user has all roles
1249 user_roles = usersobj.read(cr, user, [user], ['roles_id'])[0]['roles_id']
1250 # TODO handle the case of more than one workflow for a model
1251 cr.execute("""SELECT DISTINCT t.role_id
1253 INNER JOIN wkf_activity a ON a.wkf_id = wkf.id
1254 INNER JOIN wkf_transition t ON (t.act_to = a.id)
1257 """, (self._name, button.get('name'),))
1258 roles = cr.fetchall()
1260 # draft -> valid = signal_next (role X)
1261 # draft -> cancel = signal_cancel (no role)
1263 # valid -> running = signal_next (role Y)
1264 # valid -> cancel = signal_cancel (role Z)
1266 # running -> done = signal_next (role Z)
1267 # running -> cancel = signal_cancel (role Z)
1269 # As we don't know the object state, in this scenario,
1270 # the button "signal_cancel" will be always shown as there is no restriction to cancel in draft
1271 # the button "signal_next" will be show if the user has any of the roles (X Y or Z)
1272 # The verification will be made later in workflow process...
1274 can_click = any((not role) or rolesobj.check(cr, user, user_roles, role) for (role,) in roles)
1276 button.set('readonly', str(int(not can_click)))
1278 arch = etree.tostring(node, encoding="utf-8").replace('\t', '')
1281 if node.tag=='diagram':
1282 if node.getchildren()[0].tag=='node':
1283 node_fields=self.pool.get(node.getchildren()[0].get('object')).fields_get(cr, user, fields_def.keys(), context)
1284 if node.getchildren()[1].tag=='arrow':
1285 arrow_fields = self.pool.get(node.getchildren()[1].get('object')).fields_get(cr, user, fields_def.keys(), context)
1286 for key,value in node_fields.items():
1288 for key,value in arrow_fields.items():
1291 fields = self.fields_get(cr, user, fields_def.keys(), context)
1292 for field in fields_def:
1294 # sometime, the view may containt the (invisible) field 'id' needed for a domain (when 2 objects have cross references)
1295 fields['id'] = {'readonly': True, 'type': 'integer', 'string': 'ID'}
1296 elif field in fields:
1297 fields[field].update(fields_def[field])
1299 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))
1300 res = cr.fetchall()[:]
1302 res.insert(0, ("Can't find field '%s' in the following view parts composing the view of object model '%s':" % (field, model), None))
1303 msg = "\n * ".join([r[0] for r in res])
1304 msg += "\n\nEither you wrongly customised this view, or some modules bringing those views are not compatible with your current data model"
1305 netsvc.Logger().notifyChannel('orm', netsvc.LOG_ERROR, msg)
1306 raise except_orm('View error', msg)
1309 def __get_default_calendar_view(self):
1310 """Generate a default calendar view (For internal use only).
1313 arch = ('<?xml version="1.0" encoding="utf-8"?>\n'
1314 '<calendar string="%s"') % (self._description)
1316 if (self._date_name not in self._columns):
1318 for dt in ['date','date_start','x_date','x_date_start']:
1319 if dt in self._columns:
1320 self._date_name = dt
1325 raise except_orm(_('Invalid Object Architecture!'),_("Insufficient fields for Calendar View!"))
1328 arch +=' date_start="%s"' % (self._date_name)
1330 for color in ["user_id","partner_id","x_user_id","x_partner_id"]:
1331 if color in self._columns:
1332 arch += ' color="' + color + '"'
1335 dt_stop_flag = False
1337 for dt_stop in ["date_stop","date_end","x_date_stop","x_date_end"]:
1338 if dt_stop in self._columns:
1339 arch += ' date_stop="' + dt_stop + '"'
1343 if not dt_stop_flag:
1344 for dt_delay in ["date_delay","planned_hours","x_date_delay","x_planned_hours"]:
1345 if dt_delay in self._columns:
1346 arch += ' date_delay="' + dt_delay + '"'
1350 ' <field name="%s"/>\n'
1351 '</calendar>') % (self._rec_name)
1355 def __get_default_search_view(self, cr, uid, context={}):
1358 if isinstance(s, unicode):
1359 return s.encode('utf8')
1362 view = self.fields_view_get(cr, uid, False, 'form', context)
1364 root = etree.fromstring(encode(view['arch']))
1365 res = etree.XML("<search string='%s'></search>" % root.get("string", ""))
1366 node = etree.Element("group")
1369 fields = root.xpath("//field[@select=1]")
1370 for field in fields:
1373 return etree.tostring(res, encoding="utf-8").replace('\t', '')
1376 # if view_id, view_type is not required
1378 def fields_view_get(self, cr, user, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
1380 Get the detailed composition of the requested view like fields, model, view architecture
1382 :param cr: database cursor
1383 :param user: current user id
1384 :param view_id: id of the view or None
1385 :param view_type: type of the view to return if view_id is None ('form', tree', ...)
1386 :param context: context arguments, like lang, time zone
1387 :param toolbar: true to include contextual actions
1388 :param submenu: example (portal_project module)
1389 :return: dictionary describing the composition of the requested view (including inherited views and extensions)
1390 :raise AttributeError:
1391 * if the inherited view has unknown position to work with other than 'before', 'after', 'inside', 'replace'
1392 * if some tag other than 'position' is found in parent view
1393 :raise Invalid ArchitectureError: if there is view type other than form, tree, calendar, search etc defined on the structure
1400 if isinstance(s, unicode):
1401 return s.encode('utf8')
1404 def _inherit_apply(src, inherit):
1405 def _find(node, node2):
1406 if node2.tag == 'xpath':
1407 res = node.xpath(node2.get('expr'))
1413 for n in node.getiterator(node2.tag):
1415 for attr in node2.attrib:
1416 if attr == 'position':
1419 if n.get(attr) == node2.get(attr):
1426 # End: _find(node, node2)
1428 doc_dest = etree.fromstring(encode(inherit))
1429 toparse = [ doc_dest ]
1432 node2 = toparse.pop(0)
1433 if node2.tag == 'data':
1434 toparse += [ c for c in doc_dest ]
1436 node = _find(src, node2)
1437 if node is not None:
1439 if node2.get('position'):
1440 pos = node2.get('position')
1441 if pos == 'replace':
1442 parent = node.getparent()
1444 src = copy.deepcopy(node2[0])
1447 node.addprevious(child)
1448 node.getparent().remove(node)
1449 elif pos == 'attributes':
1450 for child in node2.getiterator('attribute'):
1451 attribute = (child.get('name'), child.text and child.text.encode('utf8') or None)
1453 node.set(attribute[0], attribute[1])
1455 del(node.attrib[attribute[0]])
1457 sib = node.getnext()
1461 elif pos == 'after':
1465 sib.addprevious(child)
1466 elif pos == 'before':
1467 node.addprevious(child)
1469 raise AttributeError(_('Unknown position in inherited view %s !') % pos)
1472 ' %s="%s"' % (attr, node2.get(attr))
1473 for attr in node2.attrib
1474 if attr != 'position'
1476 tag = "<%s%s>" % (node2.tag, attrs)
1477 raise AttributeError(_("Couldn't find tag '%s' in parent view !") % tag)
1479 # End: _inherit_apply(src, inherit)
1481 result = {'type': view_type, 'model': self._name}
1487 view_ref = context.get(view_type + '_view_ref', False)
1490 module, view_ref = view_ref.split('.', 1)
1491 cr.execute("SELECT res_id FROM ir_model_data WHERE model='ir.ui.view' AND module=%s AND name=%s", (module, view_ref))
1492 view_ref_res = cr.fetchone()
1494 view_id = view_ref_res[0]
1497 where = (model and (" and model='%s'" % (self._name,))) or ''
1498 cr.execute('SELECT arch,name,field_parent,id,type,inherit_id FROM ir_ui_view WHERE id=%s'+where, (view_id,))
1500 cr.execute('''SELECT
1501 arch,name,field_parent,id,type,inherit_id
1508 ORDER BY priority''', (self._name, view_type))
1509 sql_res = cr.fetchone()
1515 view_id = ok or sql_res[3]
1518 # if a view was found
1520 result['type'] = sql_res[4]
1521 result['view_id'] = sql_res[3]
1522 result['arch'] = sql_res[0]
1524 def _inherit_apply_rec(result, inherit_id):
1525 # get all views which inherit from (ie modify) this view
1526 cr.execute('select arch,id from ir_ui_view where inherit_id=%s and model=%s order by priority', (inherit_id, self._name))
1527 sql_inherit = cr.fetchall()
1528 for (inherit, id) in sql_inherit:
1529 result = _inherit_apply(result, inherit)
1530 result = _inherit_apply_rec(result, id)
1533 inherit_result = etree.fromstring(encode(result['arch']))
1534 result['arch'] = _inherit_apply_rec(inherit_result, sql_res[3])
1536 result['name'] = sql_res[1]
1537 result['field_parent'] = sql_res[2] or False
1540 # otherwise, build some kind of default view
1541 if view_type == 'form':
1542 res = self.fields_get(cr, user, context=context)
1543 xml = '<?xml version="1.0" encoding="utf-8"?> ' \
1544 '<form string="%s">' % (self._description,)
1546 if res[x]['type'] not in ('one2many', 'many2many'):
1547 xml += '<field name="%s"/>' % (x,)
1548 if res[x]['type'] == 'text':
1552 elif view_type == 'tree':
1553 _rec_name = self._rec_name
1554 if _rec_name not in self._columns:
1555 _rec_name = self._columns.keys()[0]
1556 xml = '<?xml version="1.0" encoding="utf-8"?>' \
1557 '<tree string="%s"><field name="%s"/></tree>' \
1558 % (self._description, self._rec_name)
1560 elif view_type == 'calendar':
1561 xml = self.__get_default_calendar_view()
1563 elif view_type == 'search':
1564 xml = self.__get_default_search_view(cr, user, context)
1567 xml = '<?xml version="1.0"?>' # what happens here, graph case?
1568 raise except_orm(_('Invalid Architecture!'),_("There is no view of type '%s' defined for the structure!") % view_type)
1569 result['arch'] = etree.fromstring(encode(xml))
1570 result['name'] = 'default'
1571 result['field_parent'] = False
1572 result['view_id'] = 0
1574 xarch, xfields = self.__view_look_dom_arch(cr, user, result['arch'], view_id, context=context)
1575 result['arch'] = xarch
1576 result['fields'] = xfields
1579 if context and context.get('active_id',False):
1580 data_menu = self.pool.get('ir.ui.menu').browse(cr, user, context['active_id'], context).action
1582 act_id = data_menu.id
1584 data_action = self.pool.get('ir.actions.act_window').browse(cr, user, [act_id], context)[0]
1585 result['submenu'] = getattr(data_action,'menus', False)
1589 for key in ('report_sxw_content', 'report_rml_content',
1590 'report_sxw', 'report_rml',
1591 'report_sxw_content_data', 'report_rml_content_data'):
1595 ir_values_obj = self.pool.get('ir.values')
1596 resprint = ir_values_obj.get(cr, user, 'action',
1597 'client_print_multi', [(self._name, False)], False,
1599 resaction = ir_values_obj.get(cr, user, 'action',
1600 'client_action_multi', [(self._name, False)], False,
1603 resrelate = ir_values_obj.get(cr, user, 'action',
1604 'client_action_relate', [(self._name, False)], False,
1606 resprint = map(clean, resprint)
1607 resaction = map(clean, resaction)
1608 resaction = filter(lambda x: not x.get('multi', False), resaction)
1609 resprint = filter(lambda x: not x.get('multi', False), resprint)
1610 resrelate = map(lambda x: x[2], resrelate)
1612 for x in resprint+resaction+resrelate:
1613 x['string'] = x['name']
1615 result['toolbar'] = {
1617 'action': resaction,
1620 if result['type']=='form' and result['arch'].count("default_focus")>1:
1621 msg = "Form View contain more than one default_focus attribute"
1622 netsvc.Logger().notifyChannel('orm', netsvc.LOG_ERROR, msg)
1623 raise except_orm('View Error !',msg)
1626 _view_look_dom_arch = __view_look_dom_arch
1628 def search_count(self, cr, user, args, context=None):
1631 res = self.search(cr, user, args, context=context, count=True)
1632 if isinstance(res, list):
1636 def search(self, cr, user, args, offset=0, limit=None, order=None,
1637 context=None, count=False):
1638 raise _('The search method is not implemented on this object !')
1640 def name_get(self, cr, user, ids, context=None):
1641 raise _('The name_get method is not implemented on this object !')
1643 def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100):
1644 raise _('The name_search method is not implemented on this object !')
1646 def copy(self, cr, uid, id, default=None, context=None):
1647 raise _('The copy method is not implemented on this object !')
1649 def exists(self, cr, uid, id, context=None):
1650 raise _('The exists method is not implemented on this object !')
1652 def read_string(self, cr, uid, id, langs, fields=None, context=None):
1655 self.pool.get('ir.model.access').check(cr, uid, 'ir.translation', 'read', context=context)
1657 fields = self._columns.keys() + self._inherit_fields.keys()
1658 #FIXME: collect all calls to _get_source into one SQL call.
1660 res[lang] = {'code': lang}
1662 if f in self._columns:
1663 res_trans = self.pool.get('ir.translation')._get_source(cr, uid, self._name+','+f, 'field', lang)
1665 res[lang][f] = res_trans
1667 res[lang][f] = self._columns[f].string
1668 for table in self._inherits:
1669 cols = intersect(self._inherit_fields.keys(), fields)
1670 res2 = self.pool.get(table).read_string(cr, uid, id, langs, cols, context)
1673 res[lang]['code'] = lang
1674 for f in res2[lang]:
1675 res[lang][f] = res2[lang][f]
1678 def write_string(self, cr, uid, id, langs, vals, context=None):
1679 self.pool.get('ir.model.access').check(cr, uid, 'ir.translation', 'write', context=context)
1680 #FIXME: try to only call the translation in one SQL
1683 if field in self._columns:
1684 self.pool.get('ir.translation')._set_ids(cr, uid, self._name+','+field, 'field', lang, [0], vals[field])
1685 for table in self._inherits:
1686 cols = intersect(self._inherit_fields.keys(), vals)
1688 self.pool.get(table).write_string(cr, uid, id, langs, vals, context)
1691 def _check_removed_columns(self, cr, log=False):
1692 raise NotImplementedError()
1694 class orm_memory(orm_template):
1695 _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']
1696 _inherit_fields = {}
1701 def __init__(self, cr):
1702 super(orm_memory, self).__init__(cr)
1706 cr.execute('delete from wkf_instance where res_type=%s', (self._name,))
1708 def vaccum(self, cr, uid):
1710 if self.check_id % self._check_time:
1713 max = time.time() - self._max_hours * 60 * 60
1714 for id in self.datas:
1715 if self.datas[id]['internal.date_access'] < max:
1717 self.unlink(cr, uid, tounlink)
1718 if len(self.datas)>self._max_count:
1719 sorted = map(lambda x: (x[1]['internal.date_access'], x[0]), self.datas.items())
1721 ids = map(lambda x: x[1], sorted[:len(self.datas)-self._max_count])
1722 self.unlink(cr, uid, ids)
1725 def read(self, cr, user, ids, fields_to_read=None, context=None, load='_classic_read'):
1728 if not fields_to_read:
1729 fields_to_read = self._columns.keys()
1733 if isinstance(ids, (int, long)):
1737 for f in fields_to_read:
1738 if id in self.datas:
1739 r[f] = self.datas[id].get(f, False)
1740 if r[f] and isinstance(self._columns[f], fields.binary) and context.get('bin_size', False):
1743 if id in self.datas:
1744 self.datas[id]['internal.date_access'] = time.time()
1745 fields_post = filter(lambda x: x in self._columns and not getattr(self._columns[x], load), fields_to_read)
1746 for f in fields_post:
1747 res2 = self._columns[f].get_memory(cr, self, ids, f, user, context=context, values=result)
1748 for record in result:
1749 record[f] = res2[record['id']]
1750 if isinstance(ids_orig, (int, long)):
1754 def write(self, cr, user, ids, vals, context=None):
1760 if self._columns[field]._classic_write:
1761 vals2[field] = vals[field]
1763 upd_todo.append(field)
1765 self.datas[id_new].update(vals2)
1766 self.datas[id_new]['internal.date_access'] = time.time()
1767 for field in upd_todo:
1768 self._columns[field].set_memory(cr, self, id_new, field, vals[field], user, context)
1769 self._validate(cr, user, [id_new], context)
1770 wf_service = netsvc.LocalService("workflow")
1771 wf_service.trg_write(user, self._name, id_new, cr)
1774 def create(self, cr, user, vals, context=None):
1775 self.vaccum(cr, user)
1777 id_new = self.next_id
1779 for f in self._columns.keys():
1783 vals.update(self.default_get(cr, user, default, context))
1787 if self._columns[field]._classic_write:
1788 vals2[field] = vals[field]
1790 upd_todo.append(field)
1791 self.datas[id_new] = vals2
1792 self.datas[id_new]['internal.date_access'] = time.time()
1794 for field in upd_todo:
1795 self._columns[field].set_memory(cr, self, id_new, field, vals[field], user, context)
1796 self._validate(cr, user, [id_new], context)
1797 wf_service = netsvc.LocalService("workflow")
1798 wf_service.trg_create(user, self._name, id_new, cr)
1801 def default_get(self, cr, uid, fields_list, context=None):
1802 self.view_init(cr, uid, fields_list, context)
1806 # get the default values for the inherited fields
1807 for f in fields_list:
1808 if f in self._defaults:
1809 if callable(self._defaults[f]):
1810 value[f] = self._defaults[f](self, cr, uid, context)
1812 value[f] = self._defaults[f]
1814 fld_def = ((f in self._columns) and self._columns[f]) \
1815 or ((f in self._inherit_fields) and self._inherit_fields[f][2]) \
1818 # get the default values set by the user and override the default
1819 # values defined in the object
1820 ir_values_obj = self.pool.get('ir.values')
1821 res = ir_values_obj.get(cr, uid, 'default', False, [self._name])
1822 for id, field, field_value in res:
1823 if field in fields_list:
1824 fld_def = (field in self._columns) and self._columns[field] or self._inherit_fields[field][2]
1825 if fld_def._type in ('many2one', 'one2one'):
1826 obj = self.pool.get(fld_def._obj)
1827 if not obj.search(cr, uid, [('id', '=', field_value)]):
1829 if fld_def._type in ('many2many'):
1830 obj = self.pool.get(fld_def._obj)
1832 for i in range(len(field_value)):
1833 if not obj.search(cr, uid, [('id', '=',
1836 field_value2.append(field_value[i])
1837 field_value = field_value2
1838 if fld_def._type in ('one2many'):
1839 obj = self.pool.get(fld_def._obj)
1841 for i in range(len(field_value)):
1842 field_value2.append({})
1843 for field2 in field_value[i]:
1844 if obj._columns[field2]._type in ('many2one', 'one2one'):
1845 obj2 = self.pool.get(obj._columns[field2]._obj)
1846 if not obj2.search(cr, uid,
1847 [('id', '=', field_value[i][field2])]):
1849 # TODO add test for many2many and one2many
1850 field_value2[i][field2] = field_value[i][field2]
1851 field_value = field_value2
1852 value[field] = field_value
1854 # get the default values from the context
1855 for key in context or {}:
1856 if key.startswith('default_') and (key[8:] in fields_list):
1857 value[key[8:]] = context[key]
1860 def _where_calc(self, cr, user, args, active_test=True, context=None):
1865 # if the object has a field named 'active', filter out all inactive
1866 # records unless they were explicitely asked for
1867 if 'active' in self._columns and (active_test and context.get('active_test', True)):
1869 active_in_args = False
1871 if a[0] == 'active':
1872 active_in_args = True
1873 if not active_in_args:
1874 args.insert(0, ('active', '=', 1))
1876 args = [('active', '=', 1)]
1879 e = expression.expression(args)
1880 e.parse(cr, user, self, context)
1881 res=e.__dict__['_expression__exp']
1885 def search(self, cr, user, args, offset=0, limit=None, order=None,
1886 context=None, count=False):
1889 result = self._where_calc(cr, user, args, context=context)
1891 return self.datas.keys()
1895 #Find the value of dict
1898 for id, data in self.datas.items():
1901 if limit and (counter >int(limit)):
1906 val =eval('data[arg[0]]'+'==' +' arg[2]')
1907 elif arg[1] in ['<','>','in','not in','<=','>=','<>']:
1908 val =eval('data[arg[0]]'+arg[1] +' arg[2]')
1909 elif arg[1] in ['ilike']:
1910 if str(data[arg[0]]).find(str(arg[2]))!=-1:
1925 def unlink(self, cr, uid, ids, context=None):
1927 if id in self.datas:
1930 cr.execute('delete from wkf_instance where res_type=%s and res_id = ANY (%s)', (self._name,ids))
1933 def perm_read(self, cr, user, ids, context=None, details=True):
1937 'create_uid': (user, 'Root'),
1938 'create_date': time.strftime('%Y-%m-%d %H:%M:%S'),
1940 'write_date': False,
1945 def _check_removed_columns(self, cr, log=False):
1946 # nothing to check in memory...
1949 def exists(self, cr, uid, id, context=None):
1950 return id in self.datas
1952 class orm(orm_template):
1953 _sql_constraints = []
1955 _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']
1957 def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None):
1959 Get the list of records in list view grouped by the given ``groupby`` fields
1961 :param cr: database cursor
1962 :param uid: current user id
1963 :param domain: list specifying search criteria [['field_name', 'operator', 'value'], ...]
1964 :param fields: list of fields present in the list view specified on the object
1965 :param groupby: list of fields on which to groupby the records
1966 :type fields_list: list (example ['field_name_1', ...])
1967 :param offset: optional number of records to skip
1968 :param limit: optional max number of records to return
1969 :param context: context arguments, like lang, time zone
1970 :return: list of dictionaries(one dictionary for each record) containing:
1972 * the values of fields grouped by the fields in ``groupby`` argument
1973 * __domain: list of tuples specifying the search criteria
1974 * __context: dictionary with argument like ``groupby``
1975 :rtype: [{'field_name_1': value, ...]
1976 :raise AccessError: * if user has no read rights on the requested object
1977 * if user tries to bypass access rules for read on the requested object
1980 groupby_list = groupby
1981 if isinstance(groupby, list):
1982 groupby = groupby[0]
1983 context = context or {}
1984 self.pool.get('ir.model.access').check(cr, uid, self._name, 'read', context=context)
1986 fields = self._columns.keys()
1988 (where_clause, where_params, tables) = self._where_calc(cr, uid, domain, context=context)
1989 dom = self.pool.get('ir.rule').domain_get(cr, uid, self._name, 'read', context=context)
1990 where_clause = where_clause + dom[0]
1991 where_params = where_params + dom[1]
1996 # Take care of adding join(s) if groupby is an '_inherits'ed field
1997 tables, where_clause = self._inherits_join_calc(groupby,tables,where_clause)
1999 if len(where_clause):
2000 where_clause = ' where '+string.join(where_clause, ' and ')
2003 limit_str = limit and ' limit %d' % limit or ''
2004 offset_str = offset and ' offset %d' % offset or ''
2006 fget = self.fields_get(cr, uid, fields)
2007 float_int_fields = filter(lambda x: fget[x]['type'] in ('float','integer'), fields)
2011 if fget.get(groupby,False) and fget[groupby]['type'] in ('date','datetime'):
2012 flist = "to_char(%s,'yyyy-mm') as %s "%(groupby,groupby)
2013 groupby = "to_char(%s,'yyyy-mm')"%(groupby)
2017 fields_pre = [f for f in float_int_fields if
2018 f == self.CONCURRENCY_CHECK_FIELD
2019 or (f in self._columns and getattr(self._columns[f], '_classic_write'))]
2020 for f in fields_pre:
2021 if f not in ['id','sequence']:
2022 operator = fget[f].get('group_operator','sum')
2023 flist += ','+operator+'('+f+') as '+f
2025 cr.execute('select min(%s.id) as id,' % self._table + flist + ' from ' + ','.join(tables) + where_clause + ' group by '+ groupby + limit_str + offset_str, where_params)
2028 for r in cr.dictfetchall():
2029 for fld,val in r.items():
2030 if val == None:r[fld] = False
2031 alldata[r['id']] = r
2033 data = self.read(cr, uid, alldata.keys(), [groupby], context=context)
2034 today = datetime.date.today()
2037 d['__domain'] = [(groupby,'=',alldata[d['id']][groupby] or False)] + domain
2038 if not isinstance(groupby_list,(str, unicode)):
2039 d['__context'] = {'group_by':groupby_list[1:]}
2040 if fget.has_key(groupby):
2041 if d[groupby] and fget[groupby]['type'] in ('date','datetime'):
2042 dt = datetime.datetime.strptime(alldata[d['id']][groupby][:7],'%Y-%m')
2043 days = calendar.monthrange(dt.year, dt.month)[1]
2045 d[groupby] = datetime.datetime.strptime(d[groupby][:10],'%Y-%m-%d').strftime('%B %Y')
2046 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),\
2047 (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
2048 elif fget[groupby]['type'] == 'many2one':
2049 d[groupby] = d[groupby] and ((type(d[groupby])==type(1)) and d[groupby] or d[groupby][1]) or ''
2051 del alldata[d['id']][groupby]
2052 d.update(alldata[d['id']])
2056 def _inherits_join_calc(self, field, tables, where_clause):
2058 Adds missing table select and join clause(s) for reaching
2059 the field coming from an '_inherits' parent table.
2061 :param tables: list of table._table names enclosed in double quotes as returned
2065 current_table = self
2066 while field in current_table._inherit_fields and not field in current_table._columns:
2067 parent_table = self.pool.get(current_table._inherit_fields[field][0])
2068 parent_table_name = parent_table._table
2069 if '"%s"'%parent_table_name not in tables:
2070 tables.append('"%s"'%parent_table_name)
2071 where_clause.append('(%s.%s = %s.id)' % (current_table._table, current_table._inherits[parent_table._name], parent_table_name))
2072 current_table = parent_table
2073 return (tables, where_clause)
2075 def _parent_store_compute(self, cr):
2076 logger = netsvc.Logger()
2077 logger.notifyChannel('orm', netsvc.LOG_INFO, 'Computing parent left and right for table %s...' % (self._table, ))
2078 def browse_rec(root, pos=0):
2080 where = self._parent_name+'='+str(root)
2082 where = self._parent_name+' IS NULL'
2083 if self._parent_order:
2084 where += ' order by '+self._parent_order
2085 cr.execute('SELECT id FROM '+self._table+' WHERE '+where)
2087 childs = cr.fetchall()
2089 pos2 = browse_rec(id[0], pos2)
2090 cr.execute('update '+self._table+' set parent_left=%s, parent_right=%s where id=%s', (pos,pos2,root))
2092 query = 'SELECT id FROM '+self._table+' WHERE '+self._parent_name+' IS NULL'
2093 if self._parent_order:
2094 query += ' order by '+self._parent_order
2097 for (root,) in cr.fetchall():
2098 pos = browse_rec(root, pos)
2101 def _update_store(self, cr, f, k):
2102 logger = netsvc.Logger()
2103 logger.notifyChannel('orm', netsvc.LOG_INFO, "storing computed values of fields.function '%s'" % (k,))
2104 ss = self._columns[k]._symbol_set
2105 update_query = 'UPDATE "%s" SET "%s"=%s WHERE id=%%s' % (self._table, k, ss[0])
2106 cr.execute('select id from '+self._table)
2107 ids_lst = map(lambda x: x[0], cr.fetchall())
2110 ids_lst = ids_lst[40:]
2111 res = f.get(cr, self, iids, k, 1, {})
2112 for key,val in res.items():
2115 # if val is a many2one, just write the ID
2116 if type(val)==tuple:
2118 if (val<>False) or (type(val)<>bool):
2119 cr.execute(update_query, (ss[1](val), key))
2121 def _check_removed_columns(self, cr, log=False):
2122 logger = netsvc.Logger()
2123 # iterate on the database columns to drop the NOT NULL constraints
2124 # of fields which were required but have been removed (or will be added by another module)
2125 columns = [c for c in self._columns if not (isinstance(self._columns[c], fields.function) and not self._columns[c].store)]
2126 columns += ('id', 'write_uid', 'write_date', 'create_uid', 'create_date') # openerp access columns
2127 cr.execute("SELECT a.attname, a.attnotnull"
2128 " FROM pg_class c, pg_attribute a"
2129 " WHERE c.relname=%%s"
2130 " AND c.oid=a.attrelid"
2131 " AND a.attisdropped=%%s"
2132 " AND pg_catalog.format_type(a.atttypid, a.atttypmod) NOT IN ('cid', 'tid', 'oid', 'xid')"
2133 " AND a.attname NOT IN (%s)" % ",".join(['%s']*len(columns)),
2134 [self._table, False] + columns)
2135 for column in cr.dictfetchall():
2137 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))
2138 if column['attnotnull']:
2139 cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" DROP NOT NULL' % (self._table, column['attname']))
2141 def _auto_init(self, cr, context={}):
2142 store_compute = False
2143 logger = netsvc.Logger()
2146 self._field_create(cr, context=context)
2147 if getattr(self, '_auto', True):
2148 cr.execute("SELECT relname FROM pg_class WHERE relkind in ('r','v') AND relname='%s'" % self._table)
2150 cr.execute("CREATE TABLE \"%s\" (id SERIAL NOT NULL, PRIMARY KEY(id)) WITHOUT OIDS" % self._table)
2151 cr.execute("COMMENT ON TABLE \"%s\" IS '%s'" % (self._table, self._description.replace("'","''")))
2154 if self._parent_store:
2155 cr.execute("""SELECT c.relname
2156 FROM pg_class c, pg_attribute a
2157 WHERE c.relname=%s AND a.attname=%s AND c.oid=a.attrelid
2158 """, (self._table, 'parent_left'))
2160 if 'parent_left' not in self._columns:
2161 logger.notifyChannel('orm', netsvc.LOG_ERROR, 'create a column parent_left on object %s: fields.integer(\'Left Parent\', select=1)' % (self._table, ))
2162 if 'parent_right' not in self._columns:
2163 logger.notifyChannel('orm', netsvc.LOG_ERROR, 'create a column parent_right on object %s: fields.integer(\'Right Parent\', select=1)' % (self._table, ))
2164 if self._columns[self._parent_name].ondelete<>'cascade':
2165 logger.notifyChannel('orm', netsvc.LOG_ERROR, "the columns %s on object must be set as ondelete='cascasde'" % (self._name, self._parent_name))
2166 cr.execute('ALTER TABLE "%s" ADD COLUMN "parent_left" INTEGER' % (self._table,))
2167 cr.execute('ALTER TABLE "%s" ADD COLUMN "parent_right" INTEGER' % (self._table,))
2169 store_compute = True
2171 if self._log_access:
2173 'create_uid': 'INTEGER REFERENCES res_users ON DELETE SET NULL',
2174 'create_date': 'TIMESTAMP',
2175 'write_uid': 'INTEGER REFERENCES res_users ON DELETE SET NULL',
2176 'write_date': 'TIMESTAMP'
2181 FROM pg_class c, pg_attribute a
2182 WHERE c.relname=%s AND a.attname=%s AND c.oid=a.attrelid
2183 """, (self._table, k))
2185 cr.execute('ALTER TABLE "%s" ADD COLUMN "%s" %s' % (self._table, k, logs[k]))
2188 self._check_removed_columns(cr, log=False)
2190 # iterate on the "object columns"
2191 todo_update_store = []
2192 update_custom_fields = context.get('update_custom_fields', False)
2193 for k in self._columns:
2194 if k in ('id', 'write_uid', 'write_date', 'create_uid', 'create_date'):
2196 #raise _('Can not define a column %s. Reserved keyword !') % (k,)
2197 #Not Updating Custom fields
2198 if k.startswith('x_') and not update_custom_fields:
2200 f = self._columns[k]
2202 if isinstance(f, fields.one2many):
2203 cr.execute("SELECT relname FROM pg_class WHERE relkind='r' AND relname=%s", (f._obj,))
2205 if self.pool.get(f._obj):
2206 if f._fields_id not in self.pool.get(f._obj)._columns.keys():
2207 if not self.pool.get(f._obj)._inherits or (f._fields_id not in self.pool.get(f._obj)._inherit_fields.keys()):
2208 raise except_orm('Programming Error', ("There is no reference field '%s' found for '%s'") % (f._fields_id,f._obj,))
2211 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))
2212 res = cr.fetchone()[0]
2214 cr.execute('ALTER TABLE "%s" ADD FOREIGN KEY (%s) REFERENCES "%s" ON DELETE SET NULL' % (self._obj, f._fields_id, f._table))
2215 elif isinstance(f, fields.many2many):
2216 cr.execute("SELECT relname FROM pg_class WHERE relkind in ('r','v') AND relname=%s", (f._rel,))
2217 if not cr.dictfetchall():
2218 if not self.pool.get(f._obj):
2219 raise except_orm('Programming Error', ('There is no reference available for %s') % (f._obj,))
2220 ref = self.pool.get(f._obj)._table
2221 # ref = f._obj.replace('.', '_')
2222 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))
2223 cr.execute('CREATE INDEX "%s_%s_index" ON "%s" ("%s")' % (f._rel, f._id1, f._rel, f._id1))
2224 cr.execute('CREATE INDEX "%s_%s_index" ON "%s" ("%s")' % (f._rel, f._id2, f._rel, f._id2))
2225 cr.execute("COMMENT ON TABLE \"%s\" IS 'RELATION BETWEEN %s AND %s'" % (f._rel, self._table, ref))
2228 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 " \
2229 "FROM pg_class c,pg_attribute a,pg_type t " \
2230 "WHERE c.relname=%s " \
2231 "AND a.attname=%s " \
2232 "AND c.oid=a.attrelid " \
2233 "AND a.atttypid=t.oid", (self._table, k))
2234 res = cr.dictfetchall()
2235 if not res and hasattr(f,'oldname'):
2236 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 " \
2237 "FROM pg_class c,pg_attribute a,pg_type t " \
2238 "WHERE c.relname=%s " \
2239 "AND a.attname=%s " \
2240 "AND c.oid=a.attrelid " \
2241 "AND a.atttypid=t.oid", (self._table, f.oldname))
2242 res_old = cr.dictfetchall()
2243 logger.notifyChannel('orm', netsvc.LOG_DEBUG, 'trying to rename %s(%s) to %s'% (self._table, f.oldname, k))
2244 if res_old and len(res_old)==1:
2245 cr.execute('ALTER TABLE "%s" RENAME "%s" TO "%s"' % ( self._table,f.oldname, k))
2247 res[0]['attname'] = k
2250 if not isinstance(f, fields.function) or f.store:
2252 # add the missing field
2253 cr.execute('ALTER TABLE "%s" ADD COLUMN "%s" %s' % (self._table, k, get_pg_type(f)[1]))
2254 cr.execute("COMMENT ON COLUMN %s.%s IS '%s'" % (self._table, k, f.string.replace("'","''")))
2257 if not create and k in self._defaults:
2258 if callable(self._defaults[k]):
2259 default = self._defaults[k](self, cr, 1, context)
2261 default = self._defaults[k]
2263 ss = self._columns[k]._symbol_set
2264 query = 'UPDATE "%s" SET "%s"=%s' % (self._table, k, ss[0])
2265 cr.execute(query, (ss[1](default),))
2267 logger.notifyChannel('orm', netsvc.LOG_DEBUG, 'setting default value of new column %s of table %s'% (k, self._table))
2269 logger.notifyChannel('orm', netsvc.LOG_DEBUG, 'creating new column %s of table %s'% (k, self._table))
2271 if isinstance(f, fields.function):
2273 if f.store is not True:
2274 order = f.store[f.store.keys()[0]][2]
2275 todo_update_store.append((order, f,k))
2277 # and add constraints if needed
2278 if isinstance(f, fields.many2one):
2279 if not self.pool.get(f._obj):
2280 raise except_orm('Programming Error', ('There is no reference available for %s') % (f._obj,))
2281 ref = self.pool.get(f._obj)._table
2282 # ref = f._obj.replace('.', '_')
2283 # ir_actions is inherited so foreign key doesn't work on it
2284 if ref != 'ir_actions':
2285 cr.execute('ALTER TABLE "%s" ADD FOREIGN KEY ("%s") REFERENCES "%s" ON DELETE %s' % (self._table, k, ref, f.ondelete))
2287 cr.execute('CREATE INDEX "%s_%s_index" ON "%s" ("%s")' % (self._table, k, self._table, k))
2291 cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" SET NOT NULL' % (self._table, k))
2292 except Exception, e:
2293 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))
2297 f_pg_type = f_pg_def['typname']
2298 f_pg_size = f_pg_def['size']
2299 f_pg_notnull = f_pg_def['attnotnull']
2300 if isinstance(f, fields.function) and not f.store and\
2301 not getattr(f, 'nodrop', False):
2302 logger.notifyChannel('orm', netsvc.LOG_INFO, 'column %s (%s) in table %s removed: converted to a function !\n' % (k, f.string, self._table))
2303 cr.execute('ALTER TABLE "%s" DROP COLUMN "%s" CASCADE'% (self._table, k))
2307 f_obj_type = get_pg_type(f) and get_pg_type(f)[0]
2312 ('text', 'char', 'VARCHAR(%d)' % (f.size or 0,), '::VARCHAR(%d)'%(f.size or 0,)),
2313 ('varchar', 'text', 'TEXT', ''),
2314 ('int4', 'float', get_pg_type(f)[1], '::'+get_pg_type(f)[1]),
2315 ('date', 'datetime', 'TIMESTAMP', '::TIMESTAMP'),
2316 ('numeric', 'float', get_pg_type(f)[1], '::'+get_pg_type(f)[1]),
2317 ('float8', 'float', get_pg_type(f)[1], '::'+get_pg_type(f)[1]),
2319 # !!! Avoid reduction of varchar field !!!
2320 if f_pg_type == 'varchar' and f._type == 'char' and f_pg_size < f.size:
2321 # if f_pg_type == 'varchar' and f._type == 'char' and f_pg_size != f.size:
2322 logger.notifyChannel('orm', netsvc.LOG_INFO, "column '%s' in table '%s' changed size" % (k, self._table))
2323 cr.execute('ALTER TABLE "%s" RENAME COLUMN "%s" TO temp_change_size' % (self._table, k))
2324 cr.execute('ALTER TABLE "%s" ADD COLUMN "%s" VARCHAR(%d)' % (self._table, k, f.size))
2325 cr.execute('UPDATE "%s" SET "%s"=temp_change_size::VARCHAR(%d)' % (self._table, k, f.size))
2326 cr.execute('ALTER TABLE "%s" DROP COLUMN temp_change_size CASCADE' % (self._table,))
2329 if (f_pg_type==c[0]) and (f._type==c[1]):
2330 if f_pg_type != f_obj_type:
2331 if f_pg_type != f_obj_type:
2332 logger.notifyChannel('orm', netsvc.LOG_INFO, "column '%s' in table '%s' changed type to %s." % (k, self._table, c[1]))
2334 cr.execute('ALTER TABLE "%s" RENAME COLUMN "%s" TO temp_change_size' % (self._table, k))
2335 cr.execute('ALTER TABLE "%s" ADD COLUMN "%s" %s' % (self._table, k, c[2]))
2336 cr.execute(('UPDATE "%s" SET "%s"=temp_change_size'+c[3]) % (self._table, k))
2337 cr.execute('ALTER TABLE "%s" DROP COLUMN temp_change_size CASCADE' % (self._table,))
2341 if f_pg_type != f_obj_type:
2343 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))
2345 # if the field is required and hasn't got a NOT NULL constraint
2346 if f.required and f_pg_notnull == 0:
2347 # set the field to the default value if any
2348 if k in self._defaults:
2349 if callable(self._defaults[k]):
2350 default = self._defaults[k](self, cr, 1, context)
2352 default = self._defaults[k]
2354 if (default is not None):
2355 ss = self._columns[k]._symbol_set
2356 query = 'UPDATE "%s" SET "%s"=%s WHERE "%s" is NULL' % (self._table, k, ss[0], k)
2357 cr.execute(query, (ss[1](default),))
2358 # add the NOT NULL constraint
2361 cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" SET NOT NULL' % (self._table, k))
2363 except Exception, e:
2364 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))
2366 elif not f.required and f_pg_notnull == 1:
2367 cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" DROP NOT NULL' % (self._table, k))
2369 indexname = '%s_%s_index' % (self._table, k)
2370 cr.execute("SELECT indexname FROM pg_indexes WHERE indexname = %s and tablename = %s", (indexname, self._table))
2371 res = cr.dictfetchall()
2372 if not res and f.select:
2373 cr.execute('CREATE INDEX "%s_%s_index" ON "%s" ("%s")' % (self._table, k, self._table, k))
2375 if res and not f.select:
2376 cr.execute('DROP INDEX "%s_%s_index"' % (self._table, k))
2378 if isinstance(f, fields.many2one):
2379 ref = self.pool.get(f._obj)._table
2380 if ref != 'ir_actions':
2381 cr.execute('SELECT confdeltype, conname FROM pg_constraint as con, pg_class as cl1, pg_class as cl2, '
2382 'pg_attribute as att1, pg_attribute as att2 '
2383 'WHERE con.conrelid = cl1.oid '
2384 'AND cl1.relname = %s '
2385 'AND con.confrelid = cl2.oid '
2386 'AND cl2.relname = %s '
2387 'AND array_lower(con.conkey, 1) = 1 '
2388 'AND con.conkey[1] = att1.attnum '
2389 'AND att1.attrelid = cl1.oid '
2390 'AND att1.attname = %s '
2391 'AND array_lower(con.confkey, 1) = 1 '
2392 'AND con.confkey[1] = att2.attnum '
2393 'AND att2.attrelid = cl2.oid '
2394 'AND att2.attname = %s '
2395 "AND con.contype = 'f'", (self._table, ref, k, 'id'))
2396 res = cr.dictfetchall()
2398 if res[0]['confdeltype'] != POSTGRES_CONFDELTYPES.get(f.ondelete.upper(), 'a'):
2399 cr.execute('ALTER TABLE "' + self._table + '" DROP CONSTRAINT "' + res[0]['conname'] + '"')
2400 cr.execute('ALTER TABLE "' + self._table + '" ADD FOREIGN KEY ("' + k + '") REFERENCES "' + ref + '" ON DELETE ' + f.ondelete)
2403 logger.notifyChannel('orm', netsvc.LOG_ERROR, "Programming error, column %s->%s has multiple instances !"%(self._table,k))
2404 for order,f,k in todo_update_store:
2405 todo_end.append((order, self._update_store, (f, k)))
2408 cr.execute("SELECT relname FROM pg_class WHERE relkind in ('r','v') AND relname=%s", (self._table,))
2409 create = not bool(cr.fetchone())
2411 for (key, con, _) in self._sql_constraints:
2412 conname = '%s_%s' % (self._table, key)
2413 cr.execute("SELECT conname FROM pg_constraint where conname=%s", (conname,))
2414 if not cr.dictfetchall():
2416 cr.execute('alter table "%s" add constraint "%s_%s" %s' % (self._table, self._table, key, con,))
2419 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,))
2422 if hasattr(self, "_sql"):
2423 for line in self._sql.split(';'):
2424 line2 = line.replace('\n', '').strip()
2429 self._parent_store_compute(cr)
2432 def __init__(self, cr):
2433 super(orm, self).__init__(cr)
2435 if not hasattr(self, '_log_access'):
2436 # if not access is not specify, it is the same value as _auto
2437 self._log_access = getattr(self, "_auto", True)
2439 self._columns = self._columns.copy()
2440 for store_field in self._columns:
2441 f = self._columns[store_field]
2442 if hasattr(f, 'digits_change'):
2444 if not isinstance(f, fields.function):
2448 if self._columns[store_field].store is True:
2449 sm = {self._name:(lambda self,cr, uid, ids, c={}: ids, None, 10, None)}
2451 sm = self._columns[store_field].store
2452 for object, aa in sm.items():
2454 (fnct,fields2,order,length)=aa
2456 (fnct,fields2,order)=aa
2459 raise except_orm('Error',
2460 ('Invalid function definition %s in object %s !\nYou must use the definition: store={object:(fnct, fields, priority, time length)}.' % (store_field, self._name)))
2461 self.pool._store_function.setdefault(object, [])
2463 for x,y,z,e,f,l in self.pool._store_function[object]:
2464 if (x==self._name) and (y==store_field) and (e==fields2):
2468 self.pool._store_function[object].append( (self._name, store_field, fnct, fields2, order, length))
2469 self.pool._store_function[object].sort(lambda x,y: cmp(x[4],y[4]))
2471 for (key, _, msg) in self._sql_constraints:
2472 self.pool._sql_error[self._table+'_'+key] = msg
2474 # Load manual fields
2476 cr.execute("SELECT id FROM ir_model_fields WHERE name=%s AND model=%s", ('state', 'ir.model.fields'))
2478 cr.execute('SELECT * FROM ir_model_fields WHERE model=%s AND state=%s', (self._name, 'manual'))
2479 for field in cr.dictfetchall():
2480 if field['name'] in self._columns:
2483 'string': field['field_description'],
2484 'required': bool(field['required']),
2485 'readonly': bool(field['readonly']),
2486 'domain': field['domain'] or None,
2487 'size': field['size'],
2488 'ondelete': field['on_delete'],
2489 'translate': (field['translate']),
2490 #'select': int(field['select_level'])
2493 if field['ttype'] == 'selection':
2494 self._columns[field['name']] = getattr(fields, field['ttype'])(eval(field['selection']), **attrs)
2495 elif field['ttype'] == 'reference':
2496 self._columns[field['name']] = getattr(fields, field['ttype'])(selection=eval(field['selection']), **attrs)
2497 elif field['ttype'] == 'many2one':
2498 self._columns[field['name']] = getattr(fields, field['ttype'])(field['relation'], **attrs)
2499 elif field['ttype'] == 'one2many':
2500 self._columns[field['name']] = getattr(fields, field['ttype'])(field['relation'], field['relation_field'], **attrs)
2501 elif field['ttype'] == 'many2many':
2502 _rel1 = field['relation'].replace('.', '_')
2503 _rel2 = field['model'].replace('.', '_')
2504 _rel_name = 'x_%s_%s_%s_rel' %(_rel1, _rel2, field['name'])
2505 self._columns[field['name']] = getattr(fields, field['ttype'])(field['relation'], _rel_name, 'id1', 'id2', **attrs)
2507 self._columns[field['name']] = getattr(fields, field['ttype'])(**attrs)
2509 self._inherits_reload()
2510 if not self._sequence:
2511 self._sequence = self._table+'_id_seq'
2512 for k in self._defaults:
2513 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,)
2514 for f in self._columns:
2515 self._columns[f].restart()
2517 def default_get(self, cr, uid, fields_list, context=None):
2519 To Get default field values of given fields list of the model
2521 :param cr: database cursor
2522 :param uid: current user id
2523 :param fields_list: list of fields to get the default value
2524 :type fields_list: list (example ['field1', 'field2',])
2525 :param context: context arguments, like lang, time zone
2526 :return: dictionary of the default values for fields (set on the object class, by the user preferences, or via the context)
2532 # get the default values for the inherited fields
2533 for t in self._inherits.keys():
2534 value.update(self.pool.get(t).default_get(cr, uid, fields_list,
2537 # get the default values defined in the object
2538 for f in fields_list:
2539 if f in self._defaults:
2540 if callable(self._defaults[f]):
2541 value[f] = self._defaults[f](self, cr, uid, context)
2543 value[f] = self._defaults[f]
2545 fld_def = ((f in self._columns) and self._columns[f]) \
2546 or ((f in self._inherit_fields) and self._inherit_fields[f][2]) \
2548 if isinstance(fld_def, fields.property):
2549 property_obj = self.pool.get('ir.property')
2550 definition_id = fld_def._field_get(cr, uid, self._name, f)
2551 nid = property_obj.search(cr, uid, [('fields_id', '=',
2552 definition_id), ('res_id', '=', False)])
2554 prop_value = property_obj.browse(cr, uid, nid[0],
2555 context=context).value
2556 value[f] = (prop_value and int(prop_value.split(',')[1])) \
2559 # get the default values set by the user and override the default
2560 # values defined in the object
2561 ir_values_obj = self.pool.get('ir.values')
2562 res = ir_values_obj.get(cr, uid, 'default', False, [self._name])
2563 for id, field, field_value in res:
2564 if field in fields_list:
2565 fld_def = (field in self._columns) and self._columns[field] or self._inherit_fields[field][2]
2566 if fld_def._type in ('many2one', 'one2one'):
2567 obj = self.pool.get(fld_def._obj)
2568 if not obj.search(cr, uid, [('id', '=', field_value or False)]):
2570 if fld_def._type in ('many2many'):
2571 obj = self.pool.get(fld_def._obj)
2573 for i in range(len(field_value)):
2574 if not obj.search(cr, uid, [('id', '=',
2577 field_value2.append(field_value[i])
2578 field_value = field_value2
2579 if fld_def._type in ('one2many'):
2580 obj = self.pool.get(fld_def._obj)
2582 for i in range(len(field_value)):
2583 field_value2.append({})
2584 for field2 in field_value[i]:
2585 if obj._columns[field2]._type in ('many2one', 'one2one'):
2586 obj2 = self.pool.get(obj._columns[field2]._obj)
2587 if not obj2.search(cr, uid,
2588 [('id', '=', field_value[i][field2])]):
2590 # TODO add test for many2many and one2many
2591 field_value2[i][field2] = field_value[i][field2]
2592 field_value = field_value2
2593 value[field] = field_value
2594 for key in context or {}:
2595 if key.startswith('default_') and (key[8:] in fields_list):
2596 value[key[8:]] = context[key]
2600 # Update objects that uses this one to update their _inherits fields
2603 def _inherits_reload_src(self):
2604 for obj in self.pool.obj_pool.values():
2605 if self._name in obj._inherits:
2606 obj._inherits_reload()
2608 def _inherits_reload(self):
2610 for table in self._inherits:
2611 res.update(self.pool.get(table)._inherit_fields)
2612 for col in self.pool.get(table)._columns.keys():
2613 res[col] = (table, self._inherits[table], self.pool.get(table)._columns[col])
2614 for col in self.pool.get(table)._inherit_fields.keys():
2615 res[col] = (table, self._inherits[table], self.pool.get(table)._inherit_fields[col][2])
2616 self._inherit_fields = res
2617 self._inherits_reload_src()
2619 def fields_get(self, cr, user, fields=None, context=None):
2621 Get the description of list of fields
2623 :param cr: database cursor
2624 :param user: current user id
2625 :param fields: list of fields
2626 :param context: context arguments, like lang, time zone
2627 :return: dictionary of field dictionaries, each one describing a field of the business object
2628 :raise AccessError: * if user has no create/write rights on the requested object
2631 ira = self.pool.get('ir.model.access')
2632 read_access = ira.check(cr, user, self._name, 'write', raise_exception=False, context=context) or \
2633 ira.check(cr, user, self._name, 'create', raise_exception=False, context=context)
2634 return super(orm, self).fields_get(cr, user, fields, context, read_access)
2636 def read(self, cr, user, ids, fields=None, context=None, load='_classic_read'):
2638 Read records with given ids with the given fields
2640 :param cr: database cursor
2641 :param user: current user id
2642 :param ids: id or list of the ids of the records to read
2643 :param fields: optional list of field names to return (default: all fields would be returned)
2644 :type fields: list (example ['field_name_1', ...])
2645 :param context(optional, highly recommended): context arguments, like lang, time zone
2646 :return: list of dictionaries((dictionary per record asked)) with requested field values
2647 :rtype: [{‘name_of_the_field’: value, ...}, ...]
2648 :raise AccessError: * if user has no read rights on the requested object
2649 * if user tries to bypass access rules for read on the requested object
2654 self.pool.get('ir.model.access').check(cr, user, self._name, 'read', context=context)
2656 fields = self._columns.keys() + self._inherit_fields.keys()
2657 if isinstance(ids, (int, long)):
2661 select = map(lambda x: isinstance(x,dict) and x['id'] or x, select)
2662 result = self._read_flat(cr, user, select, fields, context, load)
2664 for key, v in r.items():
2667 if key in self._columns.keys():
2668 type = self._columns[key]._type
2669 elif key in self._inherit_fields.keys():
2670 type = self._inherit_fields[key][2]._type
2673 if type == 'reference' and v:
2674 model,ref_id = v.split(',')
2675 table = self.pool.get(model)._table
2676 cr.execute('select id from "%s" where id=%s' % (table,ref_id))
2677 id_exist = cr.fetchone()
2679 cr.execute('update "'+self._table+'" set "'+key+'"=NULL where "%s"=%s' %(key,''.join("'"+str(v)+"'")))
2681 if isinstance(ids, (int, long, dict)):
2682 return result and result[0] or False
2685 def _read_flat(self, cr, user, ids, fields_to_read, context=None, load='_classic_read'):
2688 #ids = map(lambda x:int(x), ids)
2691 if fields_to_read == None:
2692 fields_to_read = self._columns.keys()
2694 # construct a clause for the rules :
2695 d1, d2, tables = self.pool.get('ir.rule').domain_get(cr, user, self._name, 'read', context=context)
2696 # all inherited fields + all non inherited fields for which the attribute whose name is in load is True
2697 fields_pre = [f for f in fields_to_read if
2698 f == self.CONCURRENCY_CHECK_FIELD
2699 or (f in self._columns and getattr(self._columns[f], '_classic_write'))
2700 ] + self._inherits.values()
2704 def convert_field(f):
2705 if f in ('create_date', 'write_date'):
2706 return "date_trunc('second', %s) as %s" % (f, f)
2707 if f == self.CONCURRENCY_CHECK_FIELD:
2708 if self._log_access:
2709 return "COALESCE(%s.write_date, %s.create_date, now())::timestamp AS %s" % (self._table, self._table, f,)
2710 return "now()::timestamp AS %s" % (f,)
2711 if isinstance(self._columns[f], fields.binary) and context.get('bin_size', False):
2712 return 'length("%s") as "%s"' % (f, f)
2713 return '"%s"' % (f,)
2714 fields_pre2 = map(convert_field, fields_pre)
2715 order_by = self._parent_order or self._order
2716 for sub_ids in cr.split_for_in_conditions(ids):
2718 cr.execute('SELECT %s FROM %s WHERE %s.id IN %%s AND %s ORDER BY %s' % \
2719 (','.join(fields_pre2 + [self._table + '.id']), ','.join(tables), self._table, ' and '.join(d1),
2720 order_by),[sub_ids,]+d2)
2721 if cr.rowcount != len(sub_ids):
2722 raise except_orm(_('AccessError'),
2723 _('You try to bypass an access rule while reading (Document type: %s).') % self._description)
2725 cr.execute('SELECT %s FROM \"%s\" WHERE id IN %%s ORDER BY %s' %
2726 (','.join(fields_pre2 + ['id']), self._table,
2727 order_by), (sub_ids,))
2728 res.extend(cr.dictfetchall())
2730 res = map(lambda x: {'id': x}, ids)
2732 for f in fields_pre:
2733 if f == self.CONCURRENCY_CHECK_FIELD:
2735 if self._columns[f].translate:
2736 ids = map(lambda x: x['id'], res)
2737 #TODO: optimize out of this loop
2738 res_trans = self.pool.get('ir.translation')._get_ids(cr, user, self._name+','+f, 'model', context.get('lang', False) or 'en_US', ids)
2740 r[f] = res_trans.get(r['id'], False) or r[f]
2742 for table in self._inherits:
2743 col = self._inherits[table]
2744 cols = intersect(self._inherit_fields.keys(), fields_to_read)
2747 res2 = self.pool.get(table).read(cr, user, [x[col] for x in res], cols, context, load)
2755 if not record[col]:# if the record is deleted from _inherits table?
2757 record.update(res3[record[col]])
2758 if col not in fields_to_read:
2761 # all fields which need to be post-processed by a simple function (symbol_get)
2762 fields_post = filter(lambda x: x in self._columns and self._columns[x]._symbol_get, fields_to_read)
2765 for f in fields_post:
2766 r[f] = self._columns[f]._symbol_get(r[f])
2767 ids = map(lambda x: x['id'], res)
2769 # all non inherited fields for which the attribute whose name is in load is False
2770 fields_post = filter(lambda x: x in self._columns and not getattr(self._columns[x], load), fields_to_read)
2772 # Compute POST fields
2774 for f in fields_post:
2775 todo.setdefault(self._columns[f]._multi, [])
2776 todo[self._columns[f]._multi].append(f)
2777 for key,val in todo.items():
2779 res2 = self._columns[val[0]].get(cr, self, ids, val, user, context=context, values=res)
2782 if isinstance(res2[record['id']], str):res2[record['id']] = eval(res2[record['id']]) #TOCHECK : why got string instend of dict in python2.6
2783 record[pos] = res2[record['id']][pos]
2786 res2 = self._columns[f].get(cr, self, ids, f, user, context=context, values=res)
2789 record[f] = res2[record['id']]
2793 #for f in fields_post:
2794 # # get the value of that field for all records/ids
2795 # res2 = self._columns[f].get(cr, self, ids, f, user, context=context, values=res)
2796 # for record in res:
2797 # record[f] = res2[record['id']]
2801 for field in vals.copy():
2803 if field in self._columns:
2804 fobj = self._columns[field]
2811 for group in groups:
2812 module = group.split(".")[0]
2813 grp = group.split(".")[1]
2814 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" % \
2815 (grp, module, 'res.groups', user))
2816 readonly = cr.fetchall()
2817 if readonly[0][0] >= 1:
2820 elif readonly[0][0] == 0:
2826 if type(vals[field]) == type([]):
2828 elif type(vals[field]) == type(0.0):
2830 elif type(vals[field]) == type(''):
2831 vals[field] = '=No Permission='
2836 def perm_read(self, cr, user, ids, context=None, details=True):
2838 Read the permission for record of the given ids
2840 :param cr: database cursor
2841 :param user: current user id
2842 :param ids: id or list of ids
2843 :param context: context arguments, like lang, time zone
2844 :param details: if True, *_uid fields are replaced with the name of the user
2845 :return: list of ownership dictionaries for each requested record
2846 :rtype: list of dictionaries with the following keys:
2849 * create_uid: user who created the record
2850 * create_date: date when the record was created
2851 * write_uid: last user who changed the record
2852 * write_date: date of the last change to the record
2860 if self._log_access:
2861 fields = ', u.create_uid, u.create_date, u.write_uid, u.write_date'
2862 if isinstance(ids, (int, long)):
2865 ids_str = string.join(map(lambda x: str(x), ids), ',')
2866 cr.execute('select u.id'+fields+' from "'+self._table+'" u where u.id in ('+ids_str+')')
2867 res = cr.dictfetchall()
2870 r[key] = r[key] or False
2871 if key in ('write_uid', 'create_uid', 'uid') and details:
2873 r[key] = self.pool.get('res.users').name_get(cr, user, [r[key]])[0]
2874 if isinstance(ids, (int, long)):
2878 def _check_concurrency(self, cr, ids, context):
2881 if context.get(self.CONCURRENCY_CHECK_FIELD) and self._log_access:
2883 return "%s,%s" % (self._name, oid)
2884 santa = "(id = %s AND %s < COALESCE(write_date, create_date, now())::timestamp)"
2885 for i in range(0, len(ids), cr.IN_MAX):
2886 sub_ids = tools.flatten(((oid, context[self.CONCURRENCY_CHECK_FIELD][key(oid)])
2887 for oid in ids[i:i+cr.IN_MAX]
2888 if key(oid) in context[self.CONCURRENCY_CHECK_FIELD]))
2890 cr.execute("SELECT count(1) FROM %s WHERE %s" % (self._table, " OR ".join([santa]*(len(sub_ids)/2))), sub_ids)
2893 raise except_orm('ConcurrencyException', _('Records were modified in the meanwhile'))
2895 def check_access_rule(self, cr, uid, ids, operation, context=None):
2896 """Verifies that the operation given by ``operation`` is allowed for the user
2897 according to ir.rules.
2899 :param operation: one of ``write``, ``unlink``
2900 :raise except_orm: * if current ir.rules do not permit this operation.
2901 :return: None if the operation is allowed
2903 where_clause, where_params, tables = self.pool.get('ir.rule').domain_get(cr, uid, self._name, operation, context=context)
2905 where_clause = ' and ' + ' and '.join(where_clause)
2906 for sub_ids in cr.split_for_in_conditions(ids):
2907 cr.execute('SELECT ' + self._table + '.id FROM ' + ','.join(tables) +
2908 ' WHERE ' + self._table + '.id IN %s' + where_clause,
2909 [sub_ids] + where_params)
2910 if cr.rowcount != len(sub_ids):
2911 raise except_orm(_('AccessError'),
2912 _('Operation prohibited by access rules (Operation: %s, Document type: %s).')
2913 % (operation, self._name))
2915 def unlink(self, cr, uid, ids, context=None):
2917 Delete records with given ids
2919 :param cr: database cursor
2920 :param uid: current user id
2921 :param ids: id or list of ids
2922 :param context(optional, highly recommended): context arguments, like lang, time zone
2924 :raise AccessError: * if user has no unlink rights on the requested object
2925 * if user tries to bypass access rules for unlink on the requested object
2926 :raise UserError: if the record is default property for other records
2931 if isinstance(ids, (int, long)):
2934 result_store = self._store_get_values(cr, uid, ids, None, context)
2936 self._check_concurrency(cr, ids, context)
2938 self.pool.get('ir.model.access').check(cr, uid, self._name, 'unlink', context=context)
2940 properties = self.pool.get('ir.property')
2941 domain = [('res_id', '=', False),
2942 ('value', 'in', ['%s,%s' % (self._name, i) for i in ids]),
2944 if properties.search(cr, uid, domain, context=context):
2945 raise except_orm(_('Error'), _('Unable to delete this document because it is used as a default property'))
2947 wf_service = netsvc.LocalService("workflow")
2949 wf_service.trg_delete(uid, self._name, oid, cr)
2951 #cr.execute('select * from '+self._table+' where id in ('+str_d+')', ids)
2952 #res = cr.dictfetchall()
2953 #for key in self._inherits:
2954 # ids2 = [x[self._inherits[key]] for x in res]
2955 # self.pool.get(key).unlink(cr, uid, ids2)
2957 self.check_access_rule(cr, uid, ids, 'unlink', context=context)
2958 for sub_ids in cr.split_for_in_conditions(ids):
2959 cr.execute('delete from ' + self._table + ' ' \
2960 'where id in %s', (sub_ids,))
2961 for order, object, store_ids, fields in result_store:
2962 if object != self._name:
2963 obj = self.pool.get(object)
2964 cr.execute('select id from '+obj._table+' where id in ('+','.join(map(str, store_ids))+')')
2965 rids = map(lambda x: x[0], cr.fetchall())
2967 obj._store_set_values(cr, uid, rids, fields, context)
2973 def write(self, cr, user, ids, vals, context=None):
2975 Update records with given ids with the given field values
2977 :param cr: database cursor
2978 :param user: current user id
2979 :type user: integer (example 1)
2980 :param ids: id or list of ids
2981 :param vals: dictionary of field values to update
2982 :type vals: dictionary (example {'field_name': 'value', ...})
2983 :param context(optional, highly recommended): context arguments, like lang, time zone
2985 :raise AccessError: * if user has no write rights on the requested object
2986 * if user tries to bypass access rules for write on the requested object
2987 :raise ValidateError: if user tries to enter invalid value for a field that is not in selection
2988 :raise UserError: if recurssion is found
2990 vals format for relational field type.
2992 + many2many field : [(6, 0, list of ids)] (example: [(6, 0, [8, 5, 6, 4])])
2993 + one2many field : [(0, 0, dictionary of values)] (example: [(0, 0, {'field_name':field_value, ...})])
2994 + many2one field : ID of related record
2995 + reference field : model name, id (example: 'product.product, 5')
2999 for field in vals.copy():
3001 if field in self._columns:
3002 fobj = self._columns[field]
3004 fobj = self._inherit_fields[field][2]
3011 for group in groups:
3012 module = group.split(".")[0]
3013 grp = group.split(".")[1]
3014 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" % \
3015 (grp, module, 'res.groups', user))
3016 readonly = cr.fetchall()
3017 if readonly[0][0] >= 1:
3020 elif readonly[0][0] == 0:
3032 if isinstance(ids, (int, long)):
3035 self._check_concurrency(cr, ids, context)
3037 self.pool.get('ir.model.access').check(cr, user, self._name, 'write', context=context)
3045 totranslate = context.get('lang', False) and (context['lang'] != 'en_US')
3047 if field in self._columns:
3048 if self._columns[field]._classic_write and not (hasattr(self._columns[field], '_fnct_inv')):
3049 if (not totranslate) or not self._columns[field].translate:
3050 upd0.append('"'+field+'"='+self._columns[field]._symbol_set[0])
3051 upd1.append(self._columns[field]._symbol_set[1](vals[field]))
3052 direct.append(field)
3054 upd_todo.append(field)
3056 updend.append(field)
3057 if field in self._columns \
3058 and hasattr(self._columns[field], 'selection') \
3060 if self._columns[field]._type == 'reference':
3061 val = vals[field].split(',')[0]
3064 if isinstance(self._columns[field].selection, (tuple, list)):
3065 if val not in dict(self._columns[field].selection):
3066 raise except_orm(_('ValidateError'),
3067 _('The value "%s" for the field "%s" is not in the selection') \
3068 % (vals[field], field))
3070 if val not in dict(self._columns[field].selection(
3071 self, cr, user, context=context)):
3072 raise except_orm(_('ValidateError'),
3073 _('The value "%s" for the field "%s" is not in the selection') \
3074 % (vals[field], field))
3076 if self._log_access:
3077 upd0.append('write_uid=%s')
3078 upd0.append('write_date=now()')
3082 self.check_access_rule(cr, user, ids, 'write', context=context)
3083 for sub_ids in cr.split_for_in_conditions(ids):
3084 cr.execute('update ' + self._table + ' set ' + ','.join(upd0) + ' ' \
3085 'where id in %s', upd1 + [sub_ids])
3090 if self._columns[f].translate:
3091 src_trans = self.pool.get(self._name).read(cr,user,ids,[f])[0][f]
3094 # Inserting value to DB
3095 self.write(cr, user, ids, {f:vals[f]})
3096 self.pool.get('ir.translation')._set_ids(cr, user, self._name+','+f, 'model', context['lang'], ids, vals[f], src_trans)
3099 # call the 'set' method of fields which are not classic_write
3100 upd_todo.sort(lambda x, y: self._columns[x].priority-self._columns[y].priority)
3102 # default element in context must be removed when call a one2many or many2many
3103 rel_context = context.copy()
3104 for c in context.items():
3105 if c[0].startswith('default_'):
3106 del rel_context[c[0]]
3109 for field in upd_todo:
3111 result += self._columns[field].set(cr, self, id, field, vals[field], user, context=rel_context) or []
3113 for table in self._inherits:
3114 col = self._inherits[table]
3116 for sub_ids in cr.split_for_in_conditions(ids):
3117 cr.execute('select distinct "'+col+'" from "'+self._table+'" ' \
3118 'where id in %s', (sub_ids,))
3119 nids.extend([x[0] for x in cr.fetchall()])
3123 if self._inherit_fields[val][0] == table:
3125 self.pool.get(table).write(cr, user, nids, v, context)
3127 self._validate(cr, user, ids, context)
3128 # TODO: use _order to set dest at the right position and not first node of parent
3129 if self._parent_store and (self._parent_name in vals):
3131 self.pool._init_parent[self._name]=True
3134 # Find Position of the element
3135 if vals[self._parent_name]:
3136 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],))
3138 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))
3139 result_p = cr.fetchall()
3141 for (pleft,pright,pid) in result_p:
3146 # It's the first node of the parent: position = parent_left+1
3148 if not vals[self._parent_name]:
3151 cr.execute('select parent_left from '+self._table+' where id=%s', (vals[self._parent_name],))
3152 position = cr.fetchone()[0]+1
3154 # We have the new position !
3155 cr.execute('select parent_left,parent_right from '+self._table+' where id=%s', (id,))
3156 pleft,pright = cr.fetchone()
3157 distance = pright - pleft + 1
3159 if position>pleft and position<=pright:
3160 raise except_orm(_('UserError'), _('Recursivity Detected.'))
3163 cr.execute('update '+self._table+' set parent_left=parent_left+%s where parent_left>=%s', (distance, position))
3164 cr.execute('update '+self._table+' set parent_right=parent_right+%s where parent_right>=%s', (distance, position))
3165 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))
3167 cr.execute('update '+self._table+' set parent_left=parent_left+%s where parent_left>=%s', (distance, position))
3168 cr.execute('update '+self._table+' set parent_right=parent_right+%s where parent_right>=%s', (distance, position))
3169 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))
3171 result += self._store_get_values(cr, user, ids, vals.keys(), context)
3172 for order, object, ids, fields in result:
3173 self.pool.get(object)._store_set_values(cr, user, ids, fields, context)
3175 wf_service = netsvc.LocalService("workflow")
3177 wf_service.trg_write(user, self._name, id, cr)
3181 # TODO: Should set perm to user.xxx
3183 def create(self, cr, user, vals, context=None):
3185 Create new record with specified value
3187 :param cr: database cursor
3188 :param user: current user id
3189 :type user: integer (example 1)
3190 :param vals: dictionary for new record {'field_name': field_value, ...}
3191 :type vals: dictionary (example {'field_name': field_value, ...})
3192 :param context(optional, highly recommended): context arguments, like lang, time zone
3193 :type context: dictionary (example {'lang': 'en_us', ...})
3194 :return: id of new record created
3195 :raise AccessError: * if user has no create rights on the requested object
3196 * if user tries to bypass access rules for create on the requested object
3197 :raise ValidateError: if user tries to enter invalid value for a field that is not in selection
3199 vals format for relational field type.
3201 + many2many field : [(6, 0, list of ids)] (example: [(6, 0, [8, 5, 6, 4])])
3202 + one2many field : [(0, 0, dictionary of values)] (example: [(0, 0, {'field_name':field_value, ...})])
3203 + many2one field : ID of related record
3204 + reference field : model name, id (example: 'product.product, 5')
3209 self.pool.get('ir.model.access').check(cr, user, self._name, 'create')
3214 for (t, c) in self._inherits.items():
3216 avoid_table.append(t)
3217 for f in self._columns.keys(): # + self._inherit_fields.keys():
3221 for f in self._inherit_fields.keys():
3222 if (not f in vals) and (self._inherit_fields[f][0] not in avoid_table):
3226 default_values = self.default_get(cr, user, default, context)
3227 for dv in default_values:
3228 if dv in self._columns and self._columns[dv]._type == 'many2many':
3229 if default_values[dv] and isinstance(default_values[dv][0], (int, long)):
3230 default_values[dv] = [(6, 0, default_values[dv])]
3232 vals.update(default_values)
3235 for v in self._inherits:
3236 if self._inherits[v] not in vals:
3239 tocreate[v] = {'id' : vals[self._inherits[v]]}
3240 (upd0, upd1, upd2) = ('', '', [])
3242 for v in vals.keys():
3243 if v in self._inherit_fields:
3244 (table, col, col_detail) = self._inherit_fields[v]
3245 tocreate[table][v] = vals[v]
3248 if (v not in self._inherit_fields) and (v not in self._columns):
3251 # Try-except added to filter the creation of those records whose filds are readonly.
3252 # Example : any dashboard which has all the fields readonly.(due to Views(database views))
3254 cr.execute("SELECT nextval('"+self._sequence+"')")
3256 raise except_orm(_('UserError'),
3257 _('You cannot perform this operation. New Record Creation is not allowed for this object as this object is for reporting purpose.'))
3259 id_new = cr.fetchone()[0]
3260 for table in tocreate:
3261 if self._inherits[table] in vals:
3262 del vals[self._inherits[table]]
3264 record_id = tocreate[table].pop('id', None)
3266 if record_id is None:
3267 record_id = self.pool.get(table).create(cr, user, tocreate[table], context=context)
3269 self.pool.get(table).write(cr, user, [record_id], tocreate[table], context=context)
3271 upd0 += ','+self._inherits[table]
3273 upd2.append(record_id)
3275 #Start : Set bool fields to be False if they are not touched(to make search more powerful)
3276 bool_fields = [x for x in self._columns.keys() if self._columns[x]._type=='boolean']
3278 for bool_field in bool_fields:
3279 if bool_field not in vals:
3280 vals[bool_field] = False
3282 for field in vals.copy():
3284 if field in self._columns:
3285 fobj = self._columns[field]
3287 fobj = self._inherit_fields[field][2]
3293 for group in groups:
3294 module = group.split(".")[0]
3295 grp = group.split(".")[1]
3296 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" % \
3297 (grp, module, 'res.groups', user))
3298 readonly = cr.fetchall()
3299 if readonly[0][0] >= 1:
3302 elif readonly[0][0] == 0:
3310 if self._columns[field]._classic_write:
3311 upd0 = upd0 + ',"' + field + '"'
3312 upd1 = upd1 + ',' + self._columns[field]._symbol_set[0]
3313 upd2.append(self._columns[field]._symbol_set[1](vals[field]))
3315 if not isinstance(self._columns[field], fields.related):
3316 upd_todo.append(field)
3317 if field in self._columns \
3318 and hasattr(self._columns[field], 'selection') \
3320 if self._columns[field]._type == 'reference':
3321 val = vals[field].split(',')[0]
3324 if isinstance(self._columns[field].selection, (tuple, list)):
3325 if val not in dict(self._columns[field].selection):
3326 raise except_orm(_('ValidateError'),
3327 _('The value "%s" for the field "%s" is not in the selection') \
3328 % (vals[field], field))
3330 if val not in dict(self._columns[field].selection(
3331 self, cr, user, context=context)):
3332 raise except_orm(_('ValidateError'),
3333 _('The value "%s" for the field "%s" is not in the selection') \
3334 % (vals[field], field))
3335 if self._log_access:
3336 upd0 += ',create_uid,create_date'
3339 cr.execute('insert into "'+self._table+'" (id'+upd0+") values ("+str(id_new)+upd1+')', tuple(upd2))
3340 d1, d2, tables = self.pool.get('ir.rule').domain_get(cr, user, self._name, 'create', context=context)
3342 d1 = ' AND '+' AND '.join(d1)
3343 cr.execute('SELECT '+self._table+'.id FROM '+','.join(tables)+' ' \
3344 'WHERE '+self._table+'.id = ' +str(id_new)+d1,d2)
3346 raise except_orm(_('AccessError'),
3347 _('You try to bypass an access rule to create (Document type: %s).') \
3349 upd_todo.sort(lambda x, y: self._columns[x].priority-self._columns[y].priority)
3351 if self._parent_store:
3353 self.pool._init_parent[self._name]=True
3355 parent = vals.get(self._parent_name, False)
3357 cr.execute('select parent_right from '+self._table+' where '+self._parent_name+'=%s order by '+(self._parent_order or self._order), (parent,))
3359 result_p = cr.fetchall()
3360 for (pleft,) in result_p:
3365 cr.execute('select parent_left from '+self._table+' where id=%s', (parent,))
3366 pleft_old = cr.fetchone()[0]
3369 cr.execute('select max(parent_right) from '+self._table)
3370 pleft = cr.fetchone()[0] or 0
3371 cr.execute('update '+self._table+' set parent_left=parent_left+2 where parent_left>%s', (pleft,))
3372 cr.execute('update '+self._table+' set parent_right=parent_right+2 where parent_right>%s', (pleft,))
3373 cr.execute('update '+self._table+' set parent_left=%s,parent_right=%s where id=%s', (pleft+1,pleft+2,id_new))
3375 # default element in context must be remove when call a one2many or many2many
3376 rel_context = context.copy()
3377 for c in context.items():
3378 if c[0].startswith('default_'):
3379 del rel_context[c[0]]
3382 for field in upd_todo:
3383 result += self._columns[field].set(cr, self, id_new, field, vals[field], user, rel_context) or []
3384 self._validate(cr, user, [id_new], context)
3386 if not context.get('no_store_function', False):
3387 result += self._store_get_values(cr, user, [id_new], vals.keys(), context)
3390 for order, object, ids, fields2 in result:
3391 if not (object, ids, fields2) in done:
3392 self.pool.get(object)._store_set_values(cr, user, ids, fields2, context)
3393 done.append((object, ids, fields2))
3395 wf_service = netsvc.LocalService("workflow")
3396 wf_service.trg_create(user, self._name, id_new, cr)
3399 def _store_get_values(self, cr, uid, ids, fields, context):
3401 fncts = self.pool._store_function.get(self._name, [])
3402 for fnct in range(len(fncts)):
3407 for f in (fields or []):
3408 if f in fncts[fnct][3]:
3414 result.setdefault(fncts[fnct][0], {})
3416 # uid == 1 for accessing objects having rules defined on store fields
3417 ids2 = fncts[fnct][2](self,cr, 1, ids, context)
3418 for id in filter(None, ids2):
3419 result[fncts[fnct][0]].setdefault(id, [])
3420 result[fncts[fnct][0]][id].append(fnct)
3422 for object in result:
3424 for id,fnct in result[object].items():
3425 k2.setdefault(tuple(fnct), [])
3426 k2[tuple(fnct)].append(id)
3427 for fnct,id in k2.items():
3428 dict.setdefault(fncts[fnct[0]][4],[])
3429 dict[fncts[fnct[0]][4]].append((fncts[fnct[0]][4],object,id,map(lambda x: fncts[x][1], fnct)))
3437 def _store_set_values(self, cr, uid, ids, fields, context):
3440 if self._log_access:
3441 cr.execute('select id,write_date from '+self._table+' where id in ('+','.join(map(str, ids))+')')
3445 field_dict.setdefault(r[0], [])
3446 res_date = time.strptime((r[1])[:19], '%Y-%m-%d %H:%M:%S')
3447 write_date = datetime.datetime.fromtimestamp(time.mktime(res_date))
3448 for i in self.pool._store_function.get(self._name, []):
3450 up_write_date = write_date + datetime.timedelta(hours=i[5])
3451 if datetime.datetime.now() < up_write_date:
3453 field_dict[r[0]].append(i[1])
3459 if self._columns[f]._multi not in keys:
3460 keys.append(self._columns[f]._multi)
3461 todo.setdefault(self._columns[f]._multi, [])
3462 todo[self._columns[f]._multi].append(f)
3466 # uid == 1 for accessing objects having rules defined on store fields
3467 result = self._columns[val[0]].get(cr, self, ids, val, 1, context=context)
3468 for id,value in result.items():
3470 for f in value.keys():
3471 if f in field_dict[id]:
3478 if self._columns[v]._type in ('many2one', 'one2one'):
3480 value[v] = value[v][0]
3483 upd0.append('"'+v+'"='+self._columns[v]._symbol_set[0])
3484 upd1.append(self._columns[v]._symbol_set[1](value[v]))
3487 cr.execute('update "' + self._table + '" set ' + \
3488 string.join(upd0, ',') + ' where id = %s', upd1)
3492 # uid == 1 for accessing objects having rules defined on store fields
3493 result = self._columns[f].get(cr, self, ids, f, 1, context=context)
3494 for r in result.keys():
3496 if r in field_dict.keys():
3497 if f in field_dict[r]:
3499 for id,value in result.items():
3500 if self._columns[f]._type in ('many2one', 'one2one'):
3505 cr.execute('update "' + self._table + '" set ' + \
3506 '"'+f+'"='+self._columns[f]._symbol_set[0] + ' where id = %s', (self._columns[f]._symbol_set[1](value),id))
3512 def perm_write(self, cr, user, ids, fields, context=None):
3513 raise _('This method does not exist anymore')
3515 # TODO: ameliorer avec NULL
3516 def _where_calc(self, cr, user, args, active_test=True, context=None):
3520 # if the object has a field named 'active', filter out all inactive
3521 # records unless they were explicitely asked for
3522 if 'active' in self._columns and (active_test and context.get('active_test', True)):
3524 active_in_args = False
3526 if a[0] == 'active':
3527 active_in_args = True
3528 if not active_in_args:
3529 args.insert(0, ('active', '=', 1))
3531 args = [('active', '=', 1)]
3535 e = expression.expression(args)
3536 e.parse(cr, user, self, context)
3537 tables = e.get_tables()
3538 qu1, qu2 = e.to_sql()
3539 qu1 = qu1 and [qu1] or []
3541 qu1, qu2, tables = [], [], ['"%s"' % self._table]
3543 return (qu1, qu2, tables)
3545 def _check_qorder(self, word):
3546 if not regex_order.match(word):
3547 raise except_orm(_('AccessError'), _('Bad query.'))
3550 def search(self, cr, user, args, offset=0, limit=None, order=None,
3551 context=None, count=False):
3553 Search for record/s with or without domain
3555 :param cr: database cursor
3556 :param user: current user id
3557 :param args: list of tuples specifying search criteria [('field_name', 'operator', 'value'), ...]
3558 :param offset: optional number from search starts
3559 :param limit: optional max number of records to return
3560 :param order: optional columns to sort by (default: self._order=id )
3561 :param context(optional, highly recommended): context arguments, like lang, time zone
3562 :param count: if True, returns only the number of records matching the criteria, not their ids
3563 :return: id or list of ids of records matching the criteria
3564 :rtype: integer or list of integers
3565 :raise AccessError: * if user tries to bypass access rules for read on the requested object.
3568 * =, !=, >, >=, <, <=, like, ilike, in, not in, child_of, parent_left, parent_right
3570 * '&' (default), '|', '!'
3575 # compute the where, order by, limit and offset clauses
3576 (qu1, qu2, tables) = self._where_calc(cr, user, args, context=context)
3577 dom = self.pool.get('ir.rule').domain_get(cr, user, self._name, 'read', context=context)
3585 qu1 = ' where '+string.join(qu1, ' and ')
3590 order_by = self._order
3592 self._check_qorder(order)
3593 o = order.split(' ')[0]
3594 if (o in self._columns) and getattr(self._columns[o], '_classic_write'):
3597 limit_str = limit and ' limit %d' % limit or ''
3598 offset_str = offset and ' offset %d' % offset or ''
3602 cr.execute('select count(%s.id) from ' % self._table +
3603 ','.join(tables) +qu1 + limit_str + offset_str, qu2)
3606 cr.execute('select %s.id from ' % self._table + ','.join(tables) +qu1+' order by '+order_by+limit_str+offset_str, qu2)
3608 return [x[0] for x in res]
3610 # returns the different values ever entered for one field
3611 # this is used, for example, in the client when the user hits enter on
3613 def distinct_field_get(self, cr, uid, field, value, args=None, offset=0, limit=None):
3616 if field in self._inherit_fields:
3617 return self.pool.get(self._inherit_fields[field][0]).distinct_field_get(cr, uid, field, value, args, offset, limit)
3619 return self._columns[field].search(cr, self, args, field, value, offset, limit, uid)
3621 def name_get(self, cr, user, ids, context=None):
3624 :param cr: database cursor
3625 :param user: current user id
3626 :type user: integer (example 1)
3627 :param ids: list of ids
3628 :param context: context arguments, like lang, time zone
3629 :return: tuples with the text representation of requested objects for to-many relationships
3636 if isinstance(ids, (int, long)):
3638 return [(r['id'], tools.ustr(r[self._rec_name])) for r in self.read(cr, user, ids,
3639 [self._rec_name], context, load='_classic_write')]
3641 def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100):
3644 :param cr: database cursor
3645 :param user: current user id
3646 :param name: object name to search
3647 :param args: list of tuples specifying search criteria [('field_name', 'operator', 'value'), ...]
3648 :param operator: operator for search criterion
3649 :param context: context arguments, like lang, time zone
3650 :param limit: optional max number of records to return
3651 :return: list of object names matching the search criteria, used to provide completion for to-many relationships
3653 This method is equivalent of search() on name + name_get()
3662 args += [(self._rec_name, operator, name)]
3663 ids = self.search(cr, user, args, limit=limit, context=context)
3664 res = self.name_get(cr, user, ids, context)
3667 def copy_data(self, cr, uid, id, default=None, context=None):
3669 Copy given record's data with all its fields values
3671 :param cr: database cursor
3672 :param user: current user id
3673 :param ids: id of the record to copy
3674 :param default: dictionary of field values to update before saving the duplicate object
3675 :param context: context arguments, like lang, time zone
3676 :return: dictionary containing all the field values
3683 if 'state' not in default:
3684 if 'state' in self._defaults:
3685 if callable(self._defaults['state']):
3686 default['state'] = self._defaults['state'](self, cr, uid, context)
3688 default['state'] = self._defaults['state']
3690 data = self.read(cr, uid, [id], context=context)[0]
3691 fields = self.fields_get(cr, uid, context=context)
3694 ftype = fields[f]['type']
3696 if self._log_access and f in ('create_date', 'create_uid', 'write_date', 'write_uid'):
3700 data[f] = default[f]
3701 elif ftype == 'function':
3703 elif ftype == 'many2one':
3705 data[f] = data[f] and data[f][0]
3708 elif ftype in ('one2many', 'one2one'):
3710 rel = self.pool.get(fields[f]['relation'])
3711 if data[f] != False:
3712 for rel_id in data[f]:
3713 # the lines are first duplicated using the wrong (old)
3714 # parent but then are reassigned to the correct one thanks
3716 d,t = rel.copy_data(cr, uid, rel_id, context=context)
3717 res.append((0, 0, d))
3720 elif ftype == 'many2many':
3721 data[f] = [(6, 0, data[f])]
3723 trans_obj = self.pool.get('ir.translation')
3724 #TODO: optimize translations
3728 if f in self._columns and self._columns[f].translate:
3729 trans_name=self._name+","+f
3730 elif f in self._inherit_fields and self._inherit_fields[f][2].translate:
3731 trans_name=self._inherit_fields[f][0]+","+f
3736 trans_ids = trans_obj.search(cr, uid, [
3737 ('name', '=', trans_name),
3738 ('res_id','=',data['id'])
3741 trans_data.extend(trans_obj.read(cr,uid,trans_ids,context=context))
3745 for v in self._inherits:
3746 del data[self._inherits[v]]
3747 return data, trans_data
3749 def copy(self, cr, uid, id, default=None, context=None):
3751 Duplicate record with given id updating it with default values
3753 :param cr: database cursor
3754 :param uid: current user id
3755 :param id: id of the record to copy
3756 :param default: dictionary of field values to update before saving the duplicate object
3757 :type default: dictionary (example {'field_name': field_value, ...})
3758 :param context: context arguments, like lang, time zone
3762 trans_obj = self.pool.get('ir.translation')
3763 data, trans_data = self.copy_data(cr, uid, id, default, context)
3764 new_id = self.create(cr, uid, data, context)
3765 for record in trans_data:
3767 record['res_id'] = new_id
3768 trans_obj.create(cr, uid, record, context)
3771 def exists(self, cr, uid, id, context=None):
3772 cr.execute('SELECT count(1) FROM "%s" where id=%%s' % (self._table,), (id,))
3773 return bool(cr.fetchone()[0])
3775 def check_recursion(self, cr, uid, ids, parent=None):
3777 Check recursion in records
3779 :param cr: database cursor
3780 :param uid: current user id
3781 :param ids: list of ids of records
3782 :param parent: parent field name
3783 :return: True or False based on recursion detection
3787 parent = self._parent_name
3789 while len(ids_parent):
3791 for i in range(0, len(ids), cr.IN_MAX):
3792 sub_ids_parent = ids_parent[i:i+cr.IN_MAX]
3793 cr.execute('SELECT distinct "'+parent+'"'+
3794 ' FROM "'+self._table+'" ' \
3795 'WHERE id = ANY(%s)',(sub_ids_parent,))
3796 ids_parent2.extend(filter(None, map(lambda x: x[0], cr.fetchall())))
3797 ids_parent = ids_parent2
3798 for i in ids_parent:
3803 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: