Confirm message support translation and export
[odoo/odoo.git] / bin / osv / orm.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 ##############################################################################
21
22 #
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
31 #    . Datas conversions
32 #    . Multi-level caching system
33 #    . 2 different inheritancies
34 #    . Fields:
35 #         - classicals (varchar, integer, boolean, ...)
36 #         - relations (one2many, many2one, many2many)
37 #         - functions
38 #
39 #
40 import calendar
41 import copy
42 import datetime
43 import logging
44 import operator
45 import pickle
46 import re
47 import time
48 import traceback
49 import types
50
51 import netsvc
52 from lxml import etree
53 from tools.config import config
54 from tools.translate import _
55
56 import fields
57 from query import Query
58 import tools
59 from tools.safe_eval import safe_eval as eval
60
61 regex_order = re.compile('^(([a-z0-9_]+|"[a-z0-9_]+")( *desc| *asc)?( *, *|))+$', re.I)
62
63
64 POSTGRES_CONFDELTYPES = {
65     'RESTRICT': 'r',
66     'NO ACTION': 'a',
67     'CASCADE': 'c',
68     'SET NULL': 'n',
69     'SET DEFAULT': 'd',
70 }
71
72 def last_day_of_current_month():
73     today = datetime.date.today()
74     last_day = str(calendar.monthrange(today.year, today.month)[1])
75     return time.strftime('%Y-%m-' + last_day)
76
77 def intersect(la, lb):
78     return filter(lambda x: x in lb, la)
79
80 class except_orm(Exception):
81     def __init__(self, name, value):
82         self.name = name
83         self.value = value
84         self.args = (name, value)
85
86 class BrowseRecordError(Exception):
87     pass
88
89 # Readonly python database object browser
90 class browse_null(object):
91
92     def __init__(self):
93         self.id = False
94
95     def __getitem__(self, name):
96         return None
97
98     def __getattr__(self, name):
99         return None  # XXX: return self ?
100
101     def __int__(self):
102         return False
103
104     def __str__(self):
105         return ''
106
107     def __nonzero__(self):
108         return False
109
110     def __unicode__(self):
111         return u''
112
113
114 #
115 # TODO: execute an object method on browse_record_list
116 #
117 class browse_record_list(list):
118
119     def __init__(self, lst, context=None):
120         if not context:
121             context = {}
122         super(browse_record_list, self).__init__(lst)
123         self.context = context
124
125
126 class browse_record(object):
127     logger = netsvc.Logger()
128
129     def __init__(self, cr, uid, id, table, cache, context=None, list_class=None, fields_process=None):
130         '''
131         table : the object (inherited from orm)
132         context : dictionary with an optional context
133         '''
134         if fields_process is None:
135             fields_process = {}
136         if context is None:
137             context = {}
138         self._list_class = list_class or browse_record_list
139         self._cr = cr
140         self._uid = uid
141         self._id = id
142         self._table = table
143         self._table_name = self._table._name
144         self.__logger = logging.getLogger(
145             'osv.browse_record.' + self._table_name)
146         self._context = context
147         self._fields_process = fields_process
148
149         cache.setdefault(table._name, {})
150         self._data = cache[table._name]
151
152         if not (id and isinstance(id, (int, long,))):
153             raise BrowseRecordError(_('Wrong ID for the browse record, got %r, expected an integer.') % (id,))
154 #        if not table.exists(cr, uid, id, context):
155 #            raise BrowseRecordError(_('Object %s does not exists') % (self,))
156
157         if id not in self._data:
158             self._data[id] = {'id': id}
159
160         self._cache = cache
161
162     def __getitem__(self, name):
163         if name == 'id':
164             return self._id
165
166         if name not in self._data[self._id]:
167             # build the list of fields we will fetch
168
169             # fetch the definition of the field which was asked for
170             if name in self._table._columns:
171                 col = self._table._columns[name]
172             elif name in self._table._inherit_fields:
173                 col = self._table._inherit_fields[name][2]
174             elif hasattr(self._table, str(name)):
175                 attr = getattr(self._table, name)
176
177                 if isinstance(attr, (types.MethodType, types.LambdaType, types.FunctionType)):
178                     return lambda *args, **argv: attr(self._cr, self._uid, [self._id], *args, **argv)
179                 else:
180                     return attr
181             else:
182                 self.logger.notifyChannel("browse_record", netsvc.LOG_WARNING,
183                     "Field '%s' does not exist in object '%s': \n%s" % (
184                         name, self, ''.join(traceback.format_exc())))
185                 raise KeyError("Field '%s' does not exist in object '%s'" % (
186                     name, self))
187
188             # if the field is a classic one or a many2one, we'll fetch all classic and many2one fields
189             if col._prefetch:
190                 # gen the list of "local" (ie not inherited) fields which are classic or many2one
191                 fields_to_fetch = filter(lambda x: x[1]._classic_write, self._table._columns.items())
192                 # gen the list of inherited fields
193                 inherits = map(lambda x: (x[0], x[1][2]), self._table._inherit_fields.items())
194                 # complete the field list with the inherited fields which are classic or many2one
195                 fields_to_fetch += filter(lambda x: x[1]._classic_write, inherits)
196             # otherwise we fetch only that field
197             else:
198                 fields_to_fetch = [(name, col)]
199             ids = filter(lambda id: name not in self._data[id], self._data.keys())
200             # read the results
201             field_names = map(lambda x: x[0], fields_to_fetch)
202             field_values = self._table.read(self._cr, self._uid, ids, field_names, context=self._context, load="_classic_write")
203             if self._fields_process:
204                 lang = self._context.get('lang', 'en_US') or 'en_US'
205                 lang_obj_ids = self.pool.get('res.lang').search(self._cr, self._uid, [('code', '=', lang)])
206                 if not lang_obj_ids:
207                     raise Exception(_('Language with code "%s" is not defined in your system !\nDefine it through the Administration menu.') % (lang,))
208                 lang_obj = self.pool.get('res.lang').browse(self._cr, self._uid, lang_obj_ids[0])
209
210                 for field_name, field_column in fields_to_fetch:
211                     if field_column._type in self._fields_process:
212                         for result_line in field_values:
213                             result_line[field_name] = self._fields_process[field_column._type](result_line[field_name])
214                             if result_line[field_name]:
215                                 result_line[field_name].set_value(self._cr, self._uid, result_line[field_name], self, field_column, lang_obj)
216
217             if not field_values:
218                 # Where did those ids come from? Perhaps old entries in ir_model_dat?
219                 self.__logger.warn("No field_values found for ids %s in %s", ids, self)
220                 raise KeyError('Field %s not found in %s'%(name, self))
221             # create browse records for 'remote' objects
222             for result_line in field_values:
223                 new_data = {}
224                 for field_name, field_column in fields_to_fetch:
225                     if field_column._type in ('many2one', 'one2one'):
226                         if result_line[field_name]:
227                             obj = self._table.pool.get(field_column._obj)
228                             if isinstance(result_line[field_name], (list, tuple)):
229                                 value = result_line[field_name][0]
230                             else:
231                                 value = result_line[field_name]
232                             if value:
233                                 # FIXME: this happen when a _inherits object
234                                 #        overwrite a field of it parent. Need
235                                 #        testing to be sure we got the right
236                                 #        object and not the parent one.
237                                 if not isinstance(value, browse_record):
238                                     new_data[field_name] = browse_record(self._cr,
239                                         self._uid, value, obj, self._cache,
240                                         context=self._context,
241                                         list_class=self._list_class,
242                                         fields_process=self._fields_process)
243                                 else:
244                                     new_data[field_name] = value
245                             else:
246                                 new_data[field_name] = browse_null()
247                         else:
248                             new_data[field_name] = browse_null()
249                     elif field_column._type in ('one2many', 'many2many') and len(result_line[field_name]):
250                         new_data[field_name] = self._list_class([browse_record(self._cr, self._uid, id, self._table.pool.get(field_column._obj), self._cache, context=self._context, list_class=self._list_class, fields_process=self._fields_process) for id in result_line[field_name]], self._context)
251                     elif field_column._type in ('reference'):
252                         if result_line[field_name]:
253                             if isinstance(result_line[field_name], browse_record):
254                                 new_data[field_name] = result_line[field_name]
255                             else:
256                                 ref_obj, ref_id = result_line[field_name].split(',')
257                                 ref_id = long(ref_id)
258                                 obj = self._table.pool.get(ref_obj)
259                                 new_data[field_name] = browse_record(self._cr, self._uid, ref_id, obj, self._cache, context=self._context, list_class=self._list_class, fields_process=self._fields_process)
260                         else:
261                             new_data[field_name] = browse_null()
262                     else:
263                         new_data[field_name] = result_line[field_name]
264                 self._data[result_line['id']].update(new_data)
265
266         if not name in self._data[self._id]:
267             #how did this happen?
268             self.logger.notifyChannel("browse_record", netsvc.LOG_ERROR,
269                     "Fields to fetch: %s, Field values: %s"%(field_names, field_values))
270             self.logger.notifyChannel("browse_record", netsvc.LOG_ERROR,
271                     "Cached: %s, Table: %s"%(self._data[self._id], self._table))
272             raise KeyError(_('Unknown attribute %s in %s ') % (name, self))
273         return self._data[self._id][name]
274
275     def __getattr__(self, name):
276         try:
277             return self[name]
278         except KeyError, e:
279             raise AttributeError(e)
280
281     def __contains__(self, name):
282         return (name in self._table._columns) or (name in self._table._inherit_fields) or hasattr(self._table, name)
283
284     def __hasattr__(self, name):
285         return name in self
286
287     def __int__(self):
288         return self._id
289
290     def __str__(self):
291         return "browse_record(%s, %d)" % (self._table_name, self._id)
292
293     def __eq__(self, other):
294         if not isinstance(other, browse_record):
295             return False
296         return (self._table_name, self._id) == (other._table_name, other._id)
297
298     def __ne__(self, other):
299         if not isinstance(other, browse_record):
300             return True
301         return (self._table_name, self._id) != (other._table_name, other._id)
302
303     # we need to define __unicode__ even though we've already defined __str__
304     # because we have overridden __getattr__
305     def __unicode__(self):
306         return unicode(str(self))
307
308     def __hash__(self):
309         return hash((self._table_name, self._id))
310
311     __repr__ = __str__
312
313
314 def get_pg_type(f):
315     '''
316     returns a tuple
317     (type returned by postgres when the column was created, type expression to create the column)
318     '''
319
320     type_dict = {
321             fields.boolean: 'bool',
322             fields.integer: 'int4',
323             fields.integer_big: 'int8',
324             fields.text: 'text',
325             fields.date: 'date',
326             fields.time: 'time',
327             fields.datetime: 'timestamp',
328             fields.binary: 'bytea',
329             fields.many2one: 'int4',
330             }
331     if type(f) in type_dict:
332         f_type = (type_dict[type(f)], type_dict[type(f)])
333     elif isinstance(f, fields.float):
334         if f.digits:
335             f_type = ('numeric', 'NUMERIC')
336         else:
337             f_type = ('float8', 'DOUBLE PRECISION')
338     elif isinstance(f, (fields.char, fields.reference)):
339         f_type = ('varchar', 'VARCHAR(%d)' % (f.size,))
340     elif isinstance(f, fields.selection):
341         if isinstance(f.selection, list) and isinstance(f.selection[0][0], (str, unicode)):
342             f_size = reduce(lambda x, y: max(x, len(y[0])), f.selection, f.size or 16)
343         elif isinstance(f.selection, list) and isinstance(f.selection[0][0], int):
344             f_size = -1
345         else:
346             f_size = getattr(f, 'size', None) or 16
347
348         if f_size == -1:
349             f_type = ('int4', 'INTEGER')
350         else:
351             f_type = ('varchar', 'VARCHAR(%d)' % f_size)
352     elif isinstance(f, fields.function) and eval('fields.'+(f._type), globals()) in type_dict:
353         t = eval('fields.'+(f._type), globals())
354         f_type = (type_dict[t], type_dict[t])
355     elif isinstance(f, fields.function) and f._type == 'float':
356         if f.digits:
357             f_type = ('numeric', 'NUMERIC')
358         else:
359             f_type = ('float8', 'DOUBLE PRECISION')
360     elif isinstance(f, fields.function) and f._type == 'selection':
361         f_type = ('text', 'text')
362     elif isinstance(f, fields.function) and f._type == 'char':
363         f_type = ('varchar', 'VARCHAR(%d)' % (f.size))
364     else:
365         logger = netsvc.Logger()
366         logger.notifyChannel("init", netsvc.LOG_WARNING, '%s type not supported!' % (type(f)))
367         f_type = None
368     return f_type
369
370
371 class orm_template(object):
372     _name = None
373     _columns = {}
374     _constraints = []
375     _defaults = {}
376     _rec_name = 'name'
377     _parent_name = 'parent_id'
378     _parent_store = False
379     _parent_order = False
380     _date_name = 'date'
381     _order = 'id'
382     _sequence = None
383     _description = None
384     _inherits = {}
385     _table = None
386     _invalids = set()
387     _log_create = False
388
389     CONCURRENCY_CHECK_FIELD = '__last_update'
390     def log(self, cr, uid, id, message, secondary=False, context=None):
391         return self.pool.get('res.log').create(cr, uid,
392                 {
393                     'name': message,
394                     'res_model': self._name,
395                     'secondary': secondary,
396                     'res_id': id,
397                 },
398                 context=context
399         )
400
401     def view_init(self, cr, uid, fields_list, context=None):
402         """Override this method to do specific things when a view on the object is opened."""
403         pass
404
405     def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None):
406         raise NotImplementedError(_('The read_group method is not implemented on this object !'))
407
408     def _field_create(self, cr, context=None):
409         if context is None:
410             context = {}
411         cr.execute("SELECT id FROM ir_model WHERE model=%s", (self._name,))
412         if not cr.rowcount:
413             cr.execute('SELECT nextval(%s)', ('ir_model_id_seq',))
414             model_id = cr.fetchone()[0]
415             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'))
416         else:
417             model_id = cr.fetchone()[0]
418         if 'module' in context:
419             name_id = 'model_'+self._name.replace('.', '_')
420             cr.execute('select * from ir_model_data where name=%s and res_id=%s and module=%s', (name_id, model_id, context['module']))
421             if not cr.rowcount:
422                 cr.execute("INSERT INTO ir_model_data (name,date_init,date_update,module,model,res_id) VALUES (%s, now(), now(), %s, %s, %s)", \
423                     (name_id, context['module'], 'ir.model', model_id)
424                 )
425
426         cr.commit()
427
428         cr.execute("SELECT * FROM ir_model_fields WHERE model=%s", (self._name,))
429         cols = {}
430         for rec in cr.dictfetchall():
431             cols[rec['name']] = rec
432
433         for (k, f) in self._columns.items():
434             vals = {
435                 'model_id': model_id,
436                 'model': self._name,
437                 'name': k,
438                 'field_description': f.string.replace("'", " "),
439                 'ttype': f._type,
440                 'relation': f._obj or '',
441                 'view_load': (f.view_load and 1) or 0,
442                 'select_level': tools.ustr(f.select or 0),
443                 'readonly': (f.readonly and 1) or 0,
444                 'required': (f.required and 1) or 0,
445                 'selectable': (f.selectable and 1) or 0,
446                 'relation_field': (f._type=='one2many' and isinstance(f, fields.one2many)) and f._fields_id or '',
447             }
448             # When its a custom field,it does not contain f.select
449             if context.get('field_state', 'base') == 'manual':
450                 if context.get('field_name', '') == k:
451                     vals['select_level'] = context.get('select', '0')
452                 #setting value to let the problem NOT occur next time
453                 elif k in cols:
454                     vals['select_level'] = cols[k]['select_level']
455
456             if k not in cols:
457                 cr.execute('select nextval(%s)', ('ir_model_fields_id_seq',))
458                 id = cr.fetchone()[0]
459                 vals['id'] = id
460                 cr.execute("""INSERT INTO ir_model_fields (
461                     id, model_id, model, name, field_description, ttype,
462                     relation,view_load,state,select_level,relation_field
463                 ) VALUES (
464                     %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s
465                 )""", (
466                     id, vals['model_id'], vals['model'], vals['name'], vals['field_description'], vals['ttype'],
467                      vals['relation'], bool(vals['view_load']), 'base',
468                     vals['select_level'], vals['relation_field']
469                 ))
470                 if 'module' in context:
471                     name1 = 'field_' + self._table + '_' + k
472                     cr.execute("select name from ir_model_data where name=%s", (name1,))
473                     if cr.fetchone():
474                         name1 = name1 + "_" + str(id)
475                     cr.execute("INSERT INTO ir_model_data (name,date_init,date_update,module,model,res_id) VALUES (%s, now(), now(), %s, %s, %s)", \
476                         (name1, context['module'], 'ir.model.fields', id)
477                     )
478             else:
479                 for key, val in vals.items():
480                     if cols[k][key] != vals[key]:
481                         cr.execute('update ir_model_fields set field_description=%s where model=%s and name=%s', (vals['field_description'], vals['model'], vals['name']))
482                         cr.commit()
483                         cr.execute("""UPDATE ir_model_fields SET
484                             model_id=%s, field_description=%s, ttype=%s, relation=%s,
485                             view_load=%s, select_level=%s, readonly=%s ,required=%s, selectable=%s, relation_field=%s
486                         WHERE
487                             model=%s AND name=%s""", (
488                                 vals['model_id'], vals['field_description'], vals['ttype'],
489                                 vals['relation'], bool(vals['view_load']),
490                                 vals['select_level'], bool(vals['readonly']), bool(vals['required']), bool(vals['selectable']), vals['relation_field'], vals['model'], vals['name']
491                             ))
492                         break
493         cr.commit()
494
495     def _auto_init(self, cr, context=None):
496         self._field_create(cr, context=context)
497
498     def __init__(self, cr):
499         if not self._name and not hasattr(self, '_inherit'):
500             name = type(self).__name__.split('.')[0]
501             msg = "The class %s has to have a _name attribute" % name
502
503             logger = netsvc.Logger()
504             logger.notifyChannel('orm', netsvc.LOG_ERROR, msg)
505             raise except_orm('ValueError', msg)
506
507         if not self._description:
508             self._description = self._name
509         if not self._table:
510             self._table = self._name.replace('.', '_')
511
512     def browse(self, cr, uid, select, context=None, list_class=None, fields_process=None):
513         """
514         Fetch records as objects allowing to use dot notation to browse fields and relations
515
516         :param cr: database cursor
517         :param user: current user id
518         :param select: id or list of ids
519         :param context: context arguments, like lang, time zone
520         :rtype: object or list of objects requested
521
522         """
523         self._list_class = list_class or browse_record_list
524         cache = {}
525         # need to accepts ints and longs because ids coming from a method
526         # launched by button in the interface have a type long...
527         if isinstance(select, (int, long)):
528             return browse_record(cr, uid, select, self, cache, context=context, list_class=self._list_class, fields_process=fields_process)
529         elif isinstance(select, list):
530             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=context)
531         else:
532             return browse_null()
533
534     def __export_row(self, cr, uid, row, fields, context=None):
535         if context is None:
536             context = {}
537
538         def check_type(field_type):
539             if field_type == 'float':
540                 return 0.0
541             elif field_type == 'integer':
542                 return 0
543             elif field_type == 'boolean':
544                 return False
545             return ''
546
547         def selection_field(in_field):
548             col_obj = self.pool.get(in_field.keys()[0])
549             if f[i] in col_obj._columns.keys():
550                 return  col_obj._columns[f[i]]
551             elif f[i] in col_obj._inherits.keys():
552                 selection_field(col_obj._inherits)
553             else:
554                 return False
555
556         lines = []
557         data = map(lambda x: '', range(len(fields)))
558         done = []
559         for fpos in range(len(fields)):
560             f = fields[fpos]
561             if f:
562                 r = row
563                 i = 0
564                 while i < len(f):
565                     if f[i] == 'db_id':
566                         r = r['id']
567                     elif f[i] == 'id':
568                         model_data = self.pool.get('ir.model.data')
569                         data_ids = model_data.search(cr, uid, [('model', '=', r._table_name), ('res_id', '=', r['id'])])
570                         if len(data_ids):
571                             d = model_data.read(cr, uid, data_ids, ['name', 'module'])[0]
572                             if d['module']:
573                                 r = '%s.%s' % (d['module'], d['name'])
574                             else:
575                                 r = d['name']
576                         else:
577                             break
578                     else:
579                         r = r[f[i]]
580                         # To display external name of selection field when its exported
581                         if not context.get('import_comp', False):# Allow external name only if its not import compatible
582                             cols = False
583                             if f[i] in self._columns.keys():
584                                 cols = self._columns[f[i]]
585                             elif f[i] in self._inherit_fields.keys():
586                                 cols = selection_field(self._inherits)
587                             if cols and cols._type == 'selection':
588                                 sel_list = cols.selection
589                                 if r and type(sel_list) == type([]):
590                                     r = [x[1] for x in sel_list if r==x[0]]
591                                     r = r and r[0] or False
592                     if not r:
593                         if f[i] in self._columns:
594                             r = check_type(self._columns[f[i]]._type)
595                         elif f[i] in self._inherit_fields:
596                             r = check_type(self._inherit_fields[f[i]][2]._type)
597                         data[fpos] = r
598                         break
599                     if isinstance(r, (browse_record_list, list)):
600                         first = True
601                         fields2 = map(lambda x: (x[:i+1]==f[:i+1] and x[i+1:]) \
602                                 or [], fields)
603                         if fields2 in done:
604                             if [x for x in fields2 if x]:
605                                 break
606                         done.append(fields2)
607                         for row2 in r:
608                             lines2 = self.__export_row(cr, uid, row2, fields2,
609                                     context)
610                             if first:
611                                 for fpos2 in range(len(fields)):
612                                     if lines2 and lines2[0][fpos2]:
613                                         data[fpos2] = lines2[0][fpos2]
614                                 if not data[fpos]:
615                                     dt = ''
616                                     for rr in r:
617                                         if isinstance(rr.name, browse_record):
618                                             rr = rr.name
619                                         rr_name = self.pool.get(rr._table_name).name_get(cr, uid, [rr.id], context=context)
620                                         rr_name = rr_name and rr_name[0] and rr_name[0][1] or ''
621                                         dt += tools.ustr(rr_name or '') + ','
622                                     data[fpos] = dt[:-1]
623                                     break
624                                 lines += lines2[1:]
625                                 first = False
626                             else:
627                                 lines += lines2
628                         break
629                     i += 1
630                 if i == len(f):
631                     if isinstance(r, browse_record):
632                         r = self.pool.get(r._table_name).name_get(cr, uid, [r.id], context=context)
633                         r = r and r[0] and r[0][1] or ''
634                     data[fpos] = tools.ustr(r or '')
635         return [data] + lines
636
637     def export_data(self, cr, uid, ids, fields_to_export, context=None):
638         """
639         Export fields for selected objects
640
641         :param cr: database cursor
642         :param uid: current user id
643         :param ids: list of ids
644         :param fields_to_export: list of fields
645         :param context: context arguments, like lang, time zone, may contain import_comp(default: False) to make exported data compatible with import_data()
646         :rtype: dictionary with a *datas* matrix
647
648         This method is used when exporting data via client menu
649
650         """
651         if context is None:
652             context = {}
653         imp_comp = context.get('import_comp', False)
654         cols = self._columns.copy()
655         for f in self._inherit_fields:
656             cols.update({f: self._inherit_fields[f][2]})
657         fields_to_export = map(lambda x: x.split('/'), fields_to_export)
658         fields_export = fields_to_export + []
659         warning = ''
660         warning_fields = []
661         for field in fields_export:
662             if imp_comp and len(field) > 1:
663                 warning_fields.append('/'.join(map(lambda x: x in cols and cols[x].string or x,field)))
664             elif len (field) <= 1:
665                 if imp_comp and cols.get(field and field[0], False):
666                     if ((isinstance(cols[field[0]], fields.function) and not cols[field[0]].store) \
667                                      or isinstance(cols[field[0]], fields.related)\
668                                      or isinstance(cols[field[0]], fields.one2many)):
669                         warning_fields.append('/'.join(map(lambda x: x in cols and cols[x].string or x,field)))
670         datas = []
671         if imp_comp and len(warning_fields):
672             warning = 'Following columns cannot be exported since you select to be import compatible.\n%s' % ('\n'.join(warning_fields))
673             cr.rollback()
674             return {'warning': warning}
675         for row in self.browse(cr, uid, ids, context):
676             datas += self.__export_row(cr, uid, row, fields_to_export, context)
677         return {'datas': datas}
678
679     def import_data(self, cr, uid, fields, datas, mode='init', current_module='', noupdate=False, context=None, filename=None):
680         """
681         Import given data in given module
682
683         :param cr: database cursor
684         :param uid: current user id
685         :param ids: list of ids
686         :param fields: list of fields
687         :param data: data to import
688         :param mode: 'init' or 'update' for record creation
689         :param current_module: module name
690         :param noupdate: flag for record creation
691         :param context: context arguments, like lang, time zone,
692         :param filename: optional file to store partial import state for recovery
693         :rtype: tuple
694
695         This method is used when importing data via client menu
696
697         """
698         if not context:
699             context = {}
700         fields = map(lambda x: x.split('/'), fields)
701         logger = netsvc.Logger()
702         ir_model_data_obj = self.pool.get('ir.model.data')
703
704         def _check_db_id(self, model_name, db_id):
705             obj_model = self.pool.get(model_name)
706             ids = obj_model.search(cr, uid, [('id', '=', int(db_id))])
707             if not len(ids):
708                 raise Exception(_("Database ID doesn't exist: %s : %s") %(model_name, db_id))
709             return True
710
711         def process_liness(self, datas, prefix, current_module, model_name, fields_def, position=0):
712             line = datas[position]
713             row = {}
714             translate = {}
715             todo = []
716             warning = []
717             data_id = False
718             data_res_id = False
719             is_xml_id = False
720             is_db_id = False
721             ir_model_data_obj = self.pool.get('ir.model.data')
722             #
723             # Import normal fields
724             #
725             for i in range(len(fields)):
726                 if i >= len(line):
727                     raise Exception(_('Please check that all your lines have %d columns.') % (len(fields),))
728                 if not line[i]:
729                     continue
730
731                 field = fields[i]
732                 if prefix and not prefix[0] in field:
733                     continue
734
735                 if (len(field)==len(prefix)+1) and field[len(prefix)].endswith(':db_id'):
736                         # Database ID
737                     res = False
738                     if line[i]:
739                         field_name = field[0].split(':')[0]
740                         model_rel = fields_def[field_name]['relation']
741
742                         if fields_def[field[len(prefix)][:-6]]['type'] == 'many2many':
743                             res_id = []
744                             for db_id in line[i].split(config.get('csv_internal_sep')):
745                                 try:
746                                     _check_db_id(self, model_rel, db_id)
747                                     res_id.append(db_id)
748                                 except Exception, e:
749                                     warning += [tools.exception_to_unicode(e)]
750                                     logger.notifyChannel("import", netsvc.LOG_ERROR,
751                                               tools.exception_to_unicode(e))
752                             if len(res_id):
753                                 res = [(6, 0, res_id)]
754                             else:
755                                 try:
756                                     _check_db_id(self, model_rel, line[i])
757                                     res = line[i]
758                                 except Exception, e:
759                                     warning += [tools.exception_to_unicode(e)]
760                                     logger.notifyChannel("import", netsvc.LOG_ERROR,
761                                               tools.exception_to_unicode(e))
762                         row[field_name] = res or False
763                         continue
764
765                 if (len(field)==len(prefix)+1) and field[len(prefix)].endswith(':id'):
766                     res_id = False
767                     if line[i]:
768                         if fields_def[field[len(prefix)][:-3]]['type'] == 'many2many':
769                             res_id = []
770                             for word in line[i].split(config.get('csv_internal_sep')):
771                                 if '.' in word:
772                                     module, xml_id = word.rsplit('.', 1)
773                                 else:
774                                     module, xml_id = current_module, word
775                                 id = ir_model_data_obj._get_id(cr, uid, module,
776                                         xml_id)
777                                 res_id2 = ir_model_data_obj.read(cr, uid, [id],
778                                         ['res_id'])[0]['res_id']
779                                 if res_id2:
780                                     res_id.append(res_id2)
781                             if len(res_id):
782                                 res_id = [(6, 0, res_id)]
783                         else:
784                             if '.' in line[i]:
785                                 module, xml_id = line[i].rsplit('.', 1)
786                             else:
787                                 module, xml_id = current_module, line[i]
788                             record_id = ir_model_data_obj._get_id(cr, uid, module, xml_id)
789                             ir_model_data = ir_model_data_obj.read(cr, uid, [record_id], ['res_id'])
790                             if ir_model_data:
791                                 res_id = ir_model_data[0]['res_id']
792                             else:
793                                 raise ValueError('No references to %s.%s' % (module, xml_id))
794                     row[field[-1][:-3]] = res_id or False
795                     continue
796                 if (len(field) == len(prefix)+1) and \
797                         len(field[len(prefix)].split(':lang=')) == 2:
798                     f, lang = field[len(prefix)].split(':lang=')
799                     translate.setdefault(lang, {})[f] = line[i] or False
800                     continue
801                 if (len(field) == len(prefix)+1) and \
802                         (prefix == field[0:len(prefix)]):
803                     if field[len(prefix)] == "id":
804                         # XML ID
805                         db_id = False
806                         is_xml_id = data_id = line[i]
807                         d = data_id.split('.')
808                         module = len(d) > 1 and d[0] or ''
809                         name = len(d) > 1 and d[1] or d[0]
810                         data_ids = ir_model_data_obj.search(cr, uid, [('module', '=', module), ('model', '=', model_name), ('name', '=', name)])
811                         if len(data_ids):
812                             d = ir_model_data_obj.read(cr, uid, data_ids, ['res_id'])[0]
813                             db_id = d['res_id']
814                         if is_db_id and not db_id:
815                             data_ids = ir_model_data_obj.search(cr, uid, [('module', '=', module), ('model', '=', model_name), ('res_id', '=', is_db_id)])
816                             if not len(data_ids):
817                                 ir_model_data_obj.create(cr, uid, {'module': module, 'model': model_name, 'name': name, 'res_id': is_db_id})
818                                 db_id = is_db_id
819                         if is_db_id and int(db_id) != int(is_db_id):
820                             warning += [_("Id is not the same than existing one: %s") % (is_db_id)]
821                             logger.notifyChannel("import", netsvc.LOG_ERROR,
822                                     _("Id is not the same than existing one: %s") % (is_db_id))
823                         continue
824
825                     if field[len(prefix)] == "db_id":
826                         # Database ID
827                         try:
828                             _check_db_id(self, model_name, line[i])
829                             data_res_id = is_db_id = int(line[i])
830                         except Exception, e:
831                             warning += [tools.exception_to_unicode(e)]
832                             logger.notifyChannel("import", netsvc.LOG_ERROR,
833                                       tools.exception_to_unicode(e))
834                             continue
835                         data_ids = ir_model_data_obj.search(cr, uid, [('model', '=', model_name), ('res_id', '=', line[i])])
836                         if len(data_ids):
837                             d = ir_model_data_obj.read(cr, uid, data_ids, ['name', 'module'])[0]
838                             data_id = d['name']
839                             if d['module']:
840                                 data_id = '%s.%s' % (d['module'], d['name'])
841                             else:
842                                 data_id = d['name']
843                         if is_xml_id and not data_id:
844                             data_id = is_xml_id
845                         if is_xml_id and is_xml_id != data_id:
846                             warning += [_("Id is not the same than existing one: %s") % (line[i])]
847                             logger.notifyChannel("import", netsvc.LOG_ERROR,
848                                     _("Id is not the same than existing one: %s") % (line[i]))
849
850                         continue
851                     if fields_def[field[len(prefix)]]['type'] == 'integer':
852                         res = line[i] and int(line[i])
853                     elif fields_def[field[len(prefix)]]['type'] == 'boolean':
854                         res = line[i].lower() not in ('0', 'false', 'off')
855                     elif fields_def[field[len(prefix)]]['type'] == 'float':
856                         res = line[i] and float(line[i])
857                     elif fields_def[field[len(prefix)]]['type'] == 'selection':
858                         res = False
859                         if isinstance(fields_def[field[len(prefix)]]['selection'],
860                                 (tuple, list)):
861                             sel = fields_def[field[len(prefix)]]['selection']
862                         else:
863                             sel = fields_def[field[len(prefix)]]['selection'](self,
864                                     cr, uid, context)
865                         for key, val in sel:
866                             if line[i] in [tools.ustr(key), tools.ustr(val)]: #Acepting key or value for selection field
867                                 res = key
868                                 break
869                         if line[i] and not res:
870                             logger.notifyChannel("import", netsvc.LOG_WARNING,
871                                     _("key '%s' not found in selection field '%s'") % \
872                                             (line[i], field[len(prefix)]))
873
874                             warning += [_("Key/value '%s' not found in selection field '%s'") % (line[i], field[len(prefix)])]
875
876                     elif fields_def[field[len(prefix)]]['type'] == 'many2one':
877                         res = False
878                         if line[i]:
879                             relation = fields_def[field[len(prefix)]]['relation']
880                             res2 = self.pool.get(relation).name_search(cr, uid,
881                                     line[i], [], operator='=', context=context)
882                             res = (res2 and res2[0][0]) or False
883                             if not res:
884                                 warning += [_("Relation not found: %s on '%s'") % (line[i], relation)]
885                                 logger.notifyChannel("import", netsvc.LOG_WARNING,
886                                         _("Relation not found: %s on '%s'") % (line[i], relation))
887                     elif fields_def[field[len(prefix)]]['type'] == 'many2many':
888                         res = []
889                         if line[i]:
890                             relation = fields_def[field[len(prefix)]]['relation']
891                             for word in line[i].split(config.get('csv_internal_sep')):
892                                 res2 = self.pool.get(relation).name_search(cr,
893                                         uid, word, [], operator='=', context=context)
894                                 res3 = (res2 and res2[0][0]) or False
895                                 if not res3:
896                                     warning += [_("Relation not found: %s on '%s'") % (line[i], relation)]
897                                     logger.notifyChannel("import",
898                                             netsvc.LOG_WARNING,
899                                             _("Relation not found: %s on '%s'") % (line[i], relation))
900                                 else:
901                                     res.append(res3)
902                             if len(res):
903                                 res = [(6, 0, res)]
904                     else:
905                         res = line[i] or False
906                     row[field[len(prefix)]] = res
907                 elif (prefix==field[0:len(prefix)]):
908                     if field[0] not in todo:
909                         todo.append(field[len(prefix)])
910             #
911             # Import one2many, many2many fields
912             #
913             nbrmax = 1
914             for field in todo:
915                 relation_obj = self.pool.get(fields_def[field]['relation'])
916                 newfd = relation_obj.fields_get(
917                         cr, uid, context=context)
918                 res = process_liness(self, datas, prefix + [field], current_module, relation_obj._name, newfd, position)
919                 (newrow, max2, w2, translate2, data_id2, data_res_id2) = res
920                 nbrmax = max(nbrmax, max2)
921                 warning = warning + w2
922                 reduce(lambda x, y: x and y, newrow)
923                 row[field] = newrow and (reduce(lambda x, y: x or y, newrow.values()) and \
924                         [(0, 0, newrow)]) or []
925                 i = max2
926                 while (position+i) < len(datas):
927                     ok = True
928                     for j in range(len(fields)):
929                         field2 = fields[j]
930                         if (len(field2) <= (len(prefix)+1)) and datas[position+i][j]:
931                             ok = False
932                     if not ok:
933                         break
934
935                     (newrow, max2, w2, translate2, data_id2, data_res_id2) = process_liness(
936                             self, datas, prefix+[field], current_module, relation_obj._name, newfd, position+i)
937                     warning = warning + w2
938                     if newrow and reduce(lambda x, y: x or y, newrow.values()):
939                         row[field].append((0, 0, newrow))
940                     i += max2
941                     nbrmax = max(nbrmax, i)
942
943             if len(prefix) == 0:
944                 for i in range(max(nbrmax, 1)):
945                     #if datas:
946                     datas.pop(0)
947             result = (row, nbrmax, warning, translate, data_id, data_res_id)
948             return result
949
950         fields_def = self.fields_get(cr, uid, context=context)
951         done = 0
952
953         initial_size = len(datas)
954         if config.get('import_partial', False) and filename:
955             data = pickle.load(file(config.get('import_partial')))
956             original_value = data.get(filename, 0)
957         counter = 0
958         while len(datas):
959             counter += 1
960             res = {}
961             #try:
962             (res, other, warning, translate, data_id, res_id) = \
963                     process_liness(self, datas, [], current_module, self._name, fields_def)
964             if len(warning):
965                 cr.rollback()
966                 return (-1, res, 'Line ' + str(counter) +' : ' + '!\n'.join(warning), '')
967
968             try:
969                 id = ir_model_data_obj._update(cr, uid, self._name,
970                      current_module, res, xml_id=data_id, mode=mode,
971                      noupdate=noupdate, res_id=res_id, context=context)
972             except Exception, e:
973                 import psycopg2
974                 import osv
975                 cr.rollback()
976                 if isinstance(e, psycopg2.IntegrityError):
977                     msg = _('Insertion Failed! ')
978                     for key in self.pool._sql_error.keys():
979                         if key in e[0]:
980                             msg = self.pool._sql_error[key]
981                             break
982                     return (-1, res, 'Line ' + str(counter) +' : ' + msg, '')
983                 if isinstance(e, osv.orm.except_orm):
984                     msg = _('Insertion Failed! ' + e[1])
985                     return (-1, res, 'Line ' + str(counter) +' : ' + msg, '')
986                 #Raising Uncaught exception
987                 return (-1, res, 'Line ' + str(counter) +' : ' + str(e), '')
988
989             for lang in translate:
990                 context2 = context.copy()
991                 context2['lang'] = lang
992                 self.write(cr, uid, [id], translate[lang], context2)
993             if config.get('import_partial', False) and filename and (not (counter%100)):
994                 data = pickle.load(file(config.get('import_partial')))
995                 data[filename] = initial_size - len(datas) + original_value
996                 pickle.dump(data, file(config.get('import_partial'), 'wb'))
997                 if context.get('defer_parent_store_computation'):
998                     self._parent_store_compute(cr)
999                 cr.commit()
1000
1001             #except Exception, e:
1002             #    logger.notifyChannel("import", netsvc.LOG_ERROR, e)
1003             #    cr.rollback()
1004             #    try:
1005             #        return (-1, res, e[0], warning)
1006             #    except:
1007             #        return (-1, res, e[0], '')
1008             done += 1
1009         #
1010         # TODO: Send a request with the result and multi-thread !
1011         #
1012         if context.get('defer_parent_store_computation'):
1013             self._parent_store_compute(cr)
1014         return (done, 0, 0, 0)
1015
1016     def read(self, cr, user, ids, fields=None, context=None, load='_classic_read'):
1017         """
1018         Read records with given ids with the given fields
1019
1020         :param cr: database cursor
1021         :param user: current user id
1022         :param ids: id or list of the ids of the records to read
1023         :param fields: optional list of field names to return (default: all fields would be returned)
1024         :type fields: list (example ['field_name_1', ...])
1025         :param context: optional context dictionary - it may contains keys for specifying certain options
1026                         like ``context_lang``, ``context_tz`` to alter the results of the call.
1027                         A special ``bin_size`` boolean flag may also be passed in the context to request the
1028                         value of all fields.binary columns to be returned as the size of the binary instead of its
1029                         contents. This can also be selectively overriden by passing a field-specific flag
1030                         in the form ``bin_size_XXX: True/False`` where ``XXX`` is the name of the field.
1031                         Note: The ``bin_size_XXX`` form is new in OpenERP v6.0.
1032         :return: list of dictionaries((dictionary per record asked)) with requested field values
1033         :rtype: [{‘name_of_the_field’: value, ...}, ...]
1034         :raise AccessError: * if user has no read rights on the requested object
1035                             * if user tries to bypass access rules for read on the requested object
1036
1037         """
1038         raise NotImplementedError(_('The read method is not implemented on this object !'))
1039
1040     def get_invalid_fields(self, cr, uid):
1041         return list(self._invalids)
1042
1043     def _validate(self, cr, uid, ids, context=None):
1044         context = context or {}
1045         lng = context.get('lang', False) or 'en_US'
1046         trans = self.pool.get('ir.translation')
1047         error_msgs = []
1048         for constraint in self._constraints:
1049             fun, msg, fields = constraint
1050             if not fun(self, cr, uid, ids):
1051                 # Check presence of __call__ directly instead of using
1052                 # callable() because it will be deprecated as of Python 3.0
1053                 if hasattr(msg, '__call__'):
1054                     txt_msg, params = msg(self, cr, uid, ids)
1055                     tmp_msg = trans._get_source(cr, uid, self._name, 'constraint', lng, source=txt_msg) or txt_msg
1056                     translated_msg = tmp_msg % params
1057                 else:
1058                     translated_msg = trans._get_source(cr, uid, self._name, 'constraint', lng, source=msg) or msg
1059                 error_msgs.append(
1060                         _("Error occurred while validating the field(s) %s: %s") % (','.join(fields), translated_msg)
1061                 )
1062                 self._invalids.update(fields)
1063         if error_msgs:
1064             cr.rollback()
1065             raise except_orm('ValidateError', '\n'.join(error_msgs))
1066         else:
1067             self._invalids.clear()
1068
1069     def default_get(self, cr, uid, fields_list, context=None):
1070         """
1071         Returns default values for the fields in fields_list.
1072
1073         :param fields_list: list of fields to get the default values for (example ['field1', 'field2',])
1074         :type fields_list: list
1075         :param context: optional context dictionary - it may contains keys for specifying certain options
1076                         like ``context_lang`` (language) or ``context_tz`` (timezone) to alter the results of the call.
1077                         It may contain keys in the form ``default_XXX`` (where XXX is a field name), to set
1078                         or override a default value for a field.
1079                         A special ``bin_size`` boolean flag may also be passed in the context to request the
1080                         value of all fields.binary columns to be returned as the size of the binary instead of its
1081                         contents. This can also be selectively overriden by passing a field-specific flag
1082                         in the form ``bin_size_XXX: True/False`` where ``XXX`` is the name of the field.
1083                         Note: The ``bin_size_XXX`` form is new in OpenERP v6.0.
1084         :return: dictionary of the default values (set on the object model class, through user preferences, or in the context)
1085         """
1086         # trigger view init hook
1087         self.view_init(cr, uid, fields_list, context)
1088
1089         if not context:
1090             context = {}
1091         defaults = {}
1092
1093         # get the default values for the inherited fields
1094         for t in self._inherits.keys():
1095             defaults.update(self.pool.get(t).default_get(cr, uid, fields_list,
1096                 context))
1097
1098         # get the default values defined in the object
1099         for f in fields_list:
1100             if f in self._defaults:
1101                 if callable(self._defaults[f]):
1102                     defaults[f] = self._defaults[f](self, cr, uid, context)
1103                 else:
1104                     defaults[f] = self._defaults[f]
1105
1106             fld_def = ((f in self._columns) and self._columns[f]) \
1107                     or ((f in self._inherit_fields) and self._inherit_fields[f][2]) \
1108                     or False
1109
1110             if isinstance(fld_def, fields.property):
1111                 property_obj = self.pool.get('ir.property')
1112                 prop_value = property_obj.get(cr, uid, f, self._name, context=context)
1113                 if prop_value:
1114                     if isinstance(prop_value, (browse_record, browse_null)):
1115                         defaults[f] = prop_value.id
1116                     else:
1117                         defaults[f] = prop_value
1118                 else:
1119                     if f not in defaults:
1120                         defaults[f] = False
1121
1122         # get the default values set by the user and override the default
1123         # values defined in the object
1124         ir_values_obj = self.pool.get('ir.values')
1125         res = ir_values_obj.get(cr, uid, 'default', False, [self._name])
1126         for id, field, field_value in res:
1127             if field in fields_list:
1128                 fld_def = (field in self._columns) and self._columns[field] or self._inherit_fields[field][2]
1129                 if fld_def._type in ('many2one', 'one2one'):
1130                     obj = self.pool.get(fld_def._obj)
1131                     if not obj.search(cr, uid, [('id', '=', field_value or False)]):
1132                         continue
1133                 if fld_def._type in ('many2many'):
1134                     obj = self.pool.get(fld_def._obj)
1135                     field_value2 = []
1136                     for i in range(len(field_value)):
1137                         if not obj.search(cr, uid, [('id', '=',
1138                             field_value[i])]):
1139                             continue
1140                         field_value2.append(field_value[i])
1141                     field_value = field_value2
1142                 if fld_def._type in ('one2many'):
1143                     obj = self.pool.get(fld_def._obj)
1144                     field_value2 = []
1145                     for i in range(len(field_value)):
1146                         field_value2.append({})
1147                         for field2 in field_value[i]:
1148                             if field2 in obj._columns.keys() and obj._columns[field2]._type in ('many2one', 'one2one'):
1149                                 obj2 = self.pool.get(obj._columns[field2]._obj)
1150                                 if not obj2.search(cr, uid,
1151                                         [('id', '=', field_value[i][field2])]):
1152                                     continue
1153                             elif field2 in obj._inherit_fields.keys() and obj._inherit_fields[field2][2]._type in ('many2one', 'one2one'):
1154                                 obj2 = self.pool.get(obj._inherit_fields[field2][2]._obj)
1155                                 if not obj2.search(cr, uid,
1156                                         [('id', '=', field_value[i][field2])]):
1157                                     continue
1158                             # TODO add test for many2many and one2many
1159                             field_value2[i][field2] = field_value[i][field2]
1160                     field_value = field_value2
1161                 defaults[field] = field_value
1162
1163         # get the default values from the context
1164         for key in context or {}:
1165             if key.startswith('default_') and (key[8:] in fields_list):
1166                 defaults[key[8:]] = context[key]
1167         return defaults
1168
1169
1170     def perm_read(self, cr, user, ids, context=None, details=True):
1171         raise NotImplementedError(_('The perm_read method is not implemented on this object !'))
1172
1173     def unlink(self, cr, uid, ids, context=None):
1174         raise NotImplementedError(_('The unlink method is not implemented on this object !'))
1175
1176     def write(self, cr, user, ids, vals, context=None):
1177         raise NotImplementedError(_('The write method is not implemented on this object !'))
1178
1179     def create(self, cr, user, vals, context=None):
1180         raise NotImplementedError(_('The create method is not implemented on this object !'))
1181
1182     def fields_get_keys(self, cr, user, context=None):
1183         res = self._columns.keys()
1184         for parent in self._inherits:
1185             res.extend(self.pool.get(parent).fields_get_keys(cr, user, context))
1186         return res
1187
1188     # returns the definition of each field in the object
1189     # the optional fields parameter can limit the result to some fields
1190     def fields_get(self, cr, user, allfields=None, context=None, write_access=True):
1191         if context is None:
1192             context = {}
1193         res = {}
1194         translation_obj = self.pool.get('ir.translation')
1195         for parent in self._inherits:
1196             res.update(self.pool.get(parent).fields_get(cr, user, allfields, context))
1197
1198         if self._columns.keys():
1199             for f in self._columns.keys():
1200                 if allfields and f not in allfields:
1201                     continue
1202                 res[f] = {'type': self._columns[f]._type}
1203                 # This additional attributes for M2M and function field is added
1204                 # because we need to display tooltip with this additional information
1205                 # when client is started in debug mode.
1206                 if isinstance(self._columns[f], fields.function):
1207                     res[f]['function'] = self._columns[f]._fnct and self._columns[f]._fnct.func_name or False
1208                     res[f]['store'] = self._columns[f].store
1209                     if isinstance(self._columns[f].store, dict):
1210                         res[f]['store'] = str(self._columns[f].store)
1211                     res[f]['fnct_search'] = self._columns[f]._fnct_search and self._columns[f]._fnct_search.func_name or False
1212                     res[f]['fnct_inv'] = self._columns[f]._fnct_inv and self._columns[f]._fnct_inv.func_name or False
1213                     res[f]['fnct_inv_arg'] = self._columns[f]._fnct_inv_arg or False
1214                     res[f]['func_obj'] = self._columns[f]._obj or False
1215                     res[f]['func_method'] = self._columns[f]._method
1216                 if isinstance(self._columns[f], fields.many2many):
1217                     res[f]['related_columns'] = list((self._columns[f]._id1, self._columns[f]._id2))
1218                     res[f]['third_table'] = self._columns[f]._rel
1219                 for arg in ('string', 'readonly', 'states', 'size', 'required', 'group_operator',
1220                         'change_default', 'translate', 'help', 'select', 'selectable'):
1221                     if getattr(self._columns[f], arg):
1222                         res[f][arg] = getattr(self._columns[f], arg)
1223                 if not write_access:
1224                     res[f]['readonly'] = True
1225                     res[f]['states'] = {}
1226                 for arg in ('digits', 'invisible', 'filters'):
1227                     if getattr(self._columns[f], arg, None):
1228                         res[f][arg] = getattr(self._columns[f], arg)
1229
1230                 res_trans = translation_obj._get_source(cr, user, self._name + ',' + f, 'field', context.get('lang', False) or 'en_US', self._columns[f].string)
1231                 if res_trans:
1232                     res[f]['string'] = res_trans
1233                 help_trans = translation_obj._get_source(cr, user, self._name + ',' + f, 'help', context.get('lang', False) or 'en_US')
1234                 if help_trans:
1235                     res[f]['help'] = help_trans
1236
1237                 if hasattr(self._columns[f], 'selection'):
1238                     if isinstance(self._columns[f].selection, (tuple, list)):
1239                         sel = self._columns[f].selection
1240                         # translate each selection option
1241                         sel2 = []
1242                         for (key, val) in sel:
1243                             val2 = None
1244                             if val:
1245                                 val2 = translation_obj._get_source(cr, user, self._name + ',' + f, 'selection', context.get('lang', False) or 'en_US', val)
1246                             sel2.append((key, val2 or val))
1247                         sel = sel2
1248                         res[f]['selection'] = sel
1249                     else:
1250                         # call the 'dynamic selection' function
1251                         res[f]['selection'] = self._columns[f].selection(self, cr,
1252                                 user, context)
1253                 if res[f]['type'] in ('one2many', 'many2many', 'many2one', 'one2one'):
1254                     res[f]['relation'] = self._columns[f]._obj
1255                     res[f]['domain'] = self._columns[f]._domain
1256                     res[f]['context'] = self._columns[f]._context
1257         else:
1258             #TODO : read the fields from the database
1259             pass
1260
1261         if allfields:
1262             # filter out fields which aren't in the fields list
1263             for r in res.keys():
1264                 if r not in allfields:
1265                     del res[r]
1266         return res
1267
1268     #
1269     # Overload this method if you need a window title which depends on the context
1270     #
1271     def view_header_get(self, cr, user, view_id=None, view_type='form', context=None):
1272         return False
1273
1274     def __view_look_dom(self, cr, user, node, view_id, context=None):
1275         if not context:
1276             context = {}
1277         result = False
1278         fields = {}
1279         children = True
1280
1281         def encode(s):
1282             if isinstance(s, unicode):
1283                 return s.encode('utf8')
1284             return s
1285
1286         # return True if node can be displayed to current user
1287         def check_group(node):
1288             if node.get('groups'):
1289                 groups = node.get('groups').split(',')
1290                 access_pool = self.pool.get('ir.model.access')
1291                 can_see = any(access_pool.check_groups(cr, user, group) for group in groups)
1292                 if not can_see:
1293                     node.set('invisible', '1')
1294                     if 'attrs' in node.attrib:
1295                         del(node.attrib['attrs']) #avoid making field visible later
1296                 del(node.attrib['groups'])
1297                 return can_see
1298             else:
1299                 return True
1300
1301         if node.tag in ('field', 'node', 'arrow'):
1302             if node.get('object'):
1303                 attrs = {}
1304                 views = {}
1305                 xml = "<form>"
1306                 for f in node:
1307                     if f.tag in ('field'):
1308                         xml += etree.tostring(f, encoding="utf-8")
1309                 xml += "</form>"
1310                 new_xml = etree.fromstring(encode(xml))
1311                 ctx = context.copy()
1312                 ctx['base_model_name'] = self._name
1313                 xarch, xfields = self.pool.get(node.get('object', False)).__view_look_dom_arch(cr, user, new_xml, view_id, ctx)
1314                 views[str(f.tag)] = {
1315                     'arch': xarch,
1316                     'fields': xfields
1317                 }
1318                 attrs = {'views': views}
1319                 fields = views.get('field', False) and views['field'].get('fields', False)
1320             if node.get('name'):
1321                 attrs = {}
1322                 try:
1323                     if node.get('name') in self._columns:
1324                         column = self._columns[node.get('name')]
1325                     else:
1326                         column = self._inherit_fields[node.get('name')][2]
1327                 except:
1328                     column = False
1329
1330                 if column:
1331                     relation = self.pool.get(column._obj)
1332
1333                     children = False
1334                     views = {}
1335                     for f in node:
1336                         if f.tag in ('form', 'tree', 'graph'):
1337                             node.remove(f)
1338                             ctx = context.copy()
1339                             ctx['base_model_name'] = self._name
1340                             xarch, xfields = relation.__view_look_dom_arch(cr, user, f, view_id, ctx)
1341                             views[str(f.tag)] = {
1342                                 'arch': xarch,
1343                                 'fields': xfields
1344                             }
1345                     attrs = {'views': views}
1346                     if node.get('widget') and node.get('widget') == 'selection':
1347                         # Prepare the cached selection list for the client. This needs to be
1348                         # done even when the field is invisible to the current user, because
1349                         # other events could need to change its value to any of the selectable ones
1350                         # (such as on_change events, refreshes, etc.)
1351
1352                         # If domain and context are strings, we keep them for client-side, otherwise
1353                         # we evaluate them server-side to consider them when generating the list of
1354                         # possible values
1355                         # TODO: find a way to remove this hack, by allow dynamic domains
1356                         dom = []
1357                         if column._domain and not isinstance(column._domain, basestring):
1358                             dom = column._domain
1359                         dom += eval(node.get('domain', '[]'), {'uid': user, 'time': time})
1360                         search_context = dict(context)
1361                         if column._context and not isinstance(column._context, basestring):
1362                             search_context.update(column._context)
1363                         attrs['selection'] = relation._name_search(cr, user, '', dom, context=search_context, limit=None, name_get_uid=1)
1364                         if (node.get('required') and not int(node.get('required'))) or not column.required:
1365                             attrs['selection'].append((False, ''))
1366                 fields[node.get('name')] = attrs
1367
1368         elif node.tag in ('form', 'tree'):
1369             result = self.view_header_get(cr, user, False, node.tag, context)
1370             if result:
1371                 node.set('string', result)
1372
1373         elif node.tag == 'calendar':
1374             for additional_field in ('date_start', 'date_delay', 'date_stop', 'color'):
1375                 if node.get(additional_field):
1376                     fields[node.get(additional_field)] = {}
1377
1378         if 'groups' in node.attrib:
1379             check_group(node)
1380
1381         # translate view
1382         if ('lang' in context) and not result:
1383             if node.get('string'):
1384                 trans = self.pool.get('ir.translation')._get_source(cr, user, self._name, 'view', context['lang'], node.get('string'))
1385                 if not trans and ('base_model_name' in context):
1386                     trans = self.pool.get('ir.translation')._get_source(cr, user, context['base_model_name'], 'view', context['lang'], node.get('string'))
1387                 if trans:
1388                     node.set('string', trans)
1389             if node.get('confirm'):
1390                 trans = self.pool.get('ir.translation')._get_source(cr, user, self._name, 'view', context['lang'], node.get('confirm').encode('utf8'))
1391                 if not trans and ('base_model_name' in context):
1392                     trans = self.pool.get('ir.translation')._get_source(cr, user, context['base_model_name'], 'view', context['lang'], node.get('confirm').encode('utf8'))
1393                 if trans:
1394                     node.set('confirm', trans)
1395             if node.get('sum'):
1396                 trans = self.pool.get('ir.translation')._get_source(cr, user, self._name, 'view', context['lang'], node.get('sum'))
1397                 if trans:
1398                     node.set('sum', trans)
1399
1400         for f in node:
1401             if children or (node.tag == 'field' and f.tag in ('filter','separator')):
1402                 fields.update(self.__view_look_dom(cr, user, f, view_id, context))
1403
1404         return fields
1405
1406     def _disable_workflow_buttons(self, cr, user, node):
1407         if user == 1:
1408             # admin user can always activate workflow buttons
1409             return node
1410
1411         # TODO handle the case of more than one workflow for a model or multiple
1412         # transitions with different groups and same signal
1413         usersobj = self.pool.get('res.users')
1414         buttons = (n for n in node.getiterator('button') if n.get('type') != 'object')
1415         for button in buttons:
1416             user_groups = usersobj.read(cr, user, [user], ['groups_id'])[0]['groups_id']
1417             cr.execute("""SELECT DISTINCT t.group_id
1418                         FROM wkf
1419                   INNER JOIN wkf_activity a ON a.wkf_id = wkf.id
1420                   INNER JOIN wkf_transition t ON (t.act_to = a.id)
1421                        WHERE wkf.osv = %s
1422                          AND t.signal = %s
1423                          AND t.group_id is NOT NULL
1424                    """, (self._name, button.get('name')))
1425             group_ids = [x[0] for x in cr.fetchall() if x[0]]
1426             can_click = not group_ids or bool(set(user_groups).intersection(group_ids))
1427             button.set('readonly', str(int(not can_click)))
1428         return node
1429
1430     def __view_look_dom_arch(self, cr, user, node, view_id, context=None):
1431         fields_def = self.__view_look_dom(cr, user, node, view_id, context=context)
1432         node = self._disable_workflow_buttons(cr, user, node)
1433         arch = etree.tostring(node, encoding="utf-8").replace('\t', '')
1434         fields = {}
1435         if node.tag == 'diagram':
1436             if node.getchildren()[0].tag == 'node':
1437                 node_fields = self.pool.get(node.getchildren()[0].get('object')).fields_get(cr, user, fields_def.keys(), context)
1438             if node.getchildren()[1].tag == 'arrow':
1439                 arrow_fields = self.pool.get(node.getchildren()[1].get('object')).fields_get(cr, user, fields_def.keys(), context)
1440             for key, value in node_fields.items():
1441                 fields[key] = value
1442             for key, value in arrow_fields.items():
1443                 fields[key] = value
1444         else:
1445             fields = self.fields_get(cr, user, fields_def.keys(), context)
1446         for field in fields_def:
1447             if field == 'id':
1448                 # sometime, the view may contain the (invisible) field 'id' needed for a domain (when 2 objects have cross references)
1449                 fields['id'] = {'readonly': True, 'type': 'integer', 'string': 'ID'}
1450             elif field in fields:
1451                 fields[field].update(fields_def[field])
1452             else:
1453                 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))
1454                 res = cr.fetchall()[:]
1455                 model = res[0][1]
1456                 res.insert(0, ("Can't find field '%s' in the following view parts composing the view of object model '%s':" % (field, model), None))
1457                 msg = "\n * ".join([r[0] for r in res])
1458                 msg += "\n\nEither you wrongly customized this view, or some modules bringing those views are not compatible with your current data model"
1459                 netsvc.Logger().notifyChannel('orm', netsvc.LOG_ERROR, msg)
1460                 raise except_orm('View error', msg)
1461         return arch, fields
1462
1463     def __get_default_calendar_view(self):
1464         """Generate a default calendar view (For internal use only).
1465         """
1466
1467         arch = ('<?xml version="1.0" encoding="utf-8"?>\n'
1468                 '<calendar string="%s"') % (self._description)
1469
1470         if (self._date_name not in self._columns):
1471             date_found = False
1472             for dt in ['date', 'date_start', 'x_date', 'x_date_start']:
1473                 if dt in self._columns:
1474                     self._date_name = dt
1475                     date_found = True
1476                     break
1477
1478             if not date_found:
1479                 raise except_orm(_('Invalid Object Architecture!'), _("Insufficient fields for Calendar View!"))
1480
1481         if self._date_name:
1482             arch += ' date_start="%s"' % (self._date_name)
1483
1484         for color in ["user_id", "partner_id", "x_user_id", "x_partner_id"]:
1485             if color in self._columns:
1486                 arch += ' color="' + color + '"'
1487                 break
1488
1489         dt_stop_flag = False
1490
1491         for dt_stop in ["date_stop", "date_end", "x_date_stop", "x_date_end"]:
1492             if dt_stop in self._columns:
1493                 arch += ' date_stop="' + dt_stop + '"'
1494                 dt_stop_flag = True
1495                 break
1496
1497         if not dt_stop_flag:
1498             for dt_delay in ["date_delay", "planned_hours", "x_date_delay", "x_planned_hours"]:
1499                 if dt_delay in self._columns:
1500                     arch += ' date_delay="' + dt_delay + '"'
1501                     break
1502
1503         arch += ('>\n'
1504                  '  <field name="%s"/>\n'
1505                  '</calendar>') % (self._rec_name)
1506
1507         return arch
1508
1509     def __get_default_search_view(self, cr, uid, context=None):
1510
1511         def encode(s):
1512             if isinstance(s, unicode):
1513                 return s.encode('utf8')
1514             return s
1515
1516         view = self.fields_view_get(cr, uid, False, 'form', context=context)
1517
1518         root = etree.fromstring(encode(view['arch']))
1519         res = etree.XML("""<search string="%s"></search>""" % root.get("string", ""))
1520         node = etree.Element("group")
1521         res.append(node)
1522
1523         fields = root.xpath("//field[@select=1]")
1524         for field in fields:
1525             node.append(field)
1526
1527         return etree.tostring(res, encoding="utf-8").replace('\t', '')
1528
1529     #
1530     # if view_id, view_type is not required
1531     #
1532     def fields_view_get(self, cr, user, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
1533         """
1534         Get the detailed composition of the requested view like fields, model, view architecture
1535
1536         :param cr: database cursor
1537         :param user: current user id
1538         :param view_id: id of the view or None
1539         :param view_type: type of the view to return if view_id is None ('form', tree', ...)
1540         :param context: context arguments, like lang, time zone
1541         :param toolbar: true to include contextual actions
1542         :param submenu: example (portal_project module)
1543         :return: dictionary describing the composition of the requested view (including inherited views and extensions)
1544         :raise AttributeError:
1545                             * if the inherited view has unknown position to work with other than 'before', 'after', 'inside', 'replace'
1546                             * if some tag other than 'position' is found in parent view
1547         :raise Invalid ArchitectureError: if there is view type other than form, tree, calendar, search etc defined on the structure
1548
1549         """
1550         if not context:
1551             context = {}
1552
1553         def encode(s):
1554             if isinstance(s, unicode):
1555                 return s.encode('utf8')
1556             return s
1557
1558         def _inherit_apply(src, inherit):
1559             def _find(node, node2):
1560                 if node2.tag == 'xpath':
1561                     res = node.xpath(node2.get('expr'))
1562                     if res:
1563                         return res[0]
1564                     else:
1565                         return None
1566                 else:
1567                     for n in node.getiterator(node2.tag):
1568                         res = True
1569                         if node2.tag == 'field':
1570                             # only compare field names, a field can be only once in a given view
1571                             # at a given level (and for multilevel expressions, we should use xpath
1572                             # inheritance spec anyway)
1573                             if node2.get('name') == n.get('name'):
1574                                 return n
1575                             else:
1576                                 continue
1577                         for attr in node2.attrib:
1578                             if attr == 'position':
1579                                 continue
1580                             if n.get(attr):
1581                                 if n.get(attr) == node2.get(attr):
1582                                     continue
1583                             res = False
1584                         if res:
1585                             return n
1586                 return None
1587
1588             # End: _find(node, node2)
1589
1590             doc_dest = etree.fromstring(encode(inherit))
1591             toparse = [doc_dest]
1592
1593             while len(toparse):
1594                 node2 = toparse.pop(0)
1595                 if node2.tag == 'data':
1596                     toparse += [ c for c in doc_dest ]
1597                     continue
1598                 node = _find(src, node2)
1599                 if node is not None:
1600                     pos = 'inside'
1601                     if node2.get('position'):
1602                         pos = node2.get('position')
1603                     if pos == 'replace':
1604                         parent = node.getparent()
1605                         if parent is None:
1606                             src = copy.deepcopy(node2[0])
1607                         else:
1608                             for child in node2:
1609                                 node.addprevious(child)
1610                             node.getparent().remove(node)
1611                     elif pos == 'attributes':
1612                         for child in node2.getiterator('attribute'):
1613                             attribute = (child.get('name'), child.text and child.text.encode('utf8') or None)
1614                             if attribute[1]:
1615                                 node.set(attribute[0], attribute[1])
1616                             else:
1617                                 del(node.attrib[attribute[0]])
1618                     else:
1619                         sib = node.getnext()
1620                         for child in node2:
1621                             if pos == 'inside':
1622                                 node.append(child)
1623                             elif pos == 'after':
1624                                 if sib is None:
1625                                     node.addnext(child)
1626                                 else:
1627                                     sib.addprevious(child)
1628                             elif pos == 'before':
1629                                 node.addprevious(child)
1630                             else:
1631                                 raise AttributeError(_('Unknown position in inherited view %s !') % pos)
1632                 else:
1633                     attrs = ''.join([
1634                         ' %s="%s"' % (attr, node2.get(attr))
1635                         for attr in node2.attrib
1636                         if attr != 'position'
1637                     ])
1638                     tag = "<%s%s>" % (node2.tag, attrs)
1639                     raise AttributeError(_("Couldn't find tag '%s' in parent view !") % tag)
1640             return src
1641         # End: _inherit_apply(src, inherit)
1642
1643         result = {'type': view_type, 'model': self._name}
1644
1645         ok = True
1646         model = True
1647         sql_res = False
1648         while ok:
1649             view_ref = context.get(view_type + '_view_ref', False)
1650             if view_ref and not view_id:
1651                 if '.' in view_ref:
1652                     module, view_ref = view_ref.split('.', 1)
1653                     cr.execute("SELECT res_id FROM ir_model_data WHERE model='ir.ui.view' AND module=%s AND name=%s", (module, view_ref))
1654                     view_ref_res = cr.fetchone()
1655                     if view_ref_res:
1656                         view_id = view_ref_res[0]
1657
1658             if view_id:
1659                 query = "SELECT arch,name,field_parent,id,type,inherit_id FROM ir_ui_view WHERE id=%s"
1660                 params = (view_id,)
1661                 if model:
1662                     query += " AND model=%s"
1663                     params += (self._name,)
1664                 cr.execute(query, params)
1665             else:
1666                 cr.execute('''SELECT
1667                         arch,name,field_parent,id,type,inherit_id
1668                     FROM
1669                         ir_ui_view
1670                     WHERE
1671                         model=%s AND
1672                         type=%s AND
1673                         inherit_id IS NULL
1674                     ORDER BY priority''', (self._name, view_type))
1675             sql_res = cr.fetchone()
1676
1677             if not sql_res:
1678                 break
1679
1680             ok = sql_res[5]
1681             view_id = ok or sql_res[3]
1682             model = False
1683
1684         # if a view was found
1685         if sql_res:
1686             result['type'] = sql_res[4]
1687             result['view_id'] = sql_res[3]
1688             result['arch'] = sql_res[0]
1689
1690             def _inherit_apply_rec(result, inherit_id):
1691                 # get all views which inherit from (ie modify) this view
1692                 cr.execute('select arch,id from ir_ui_view where inherit_id=%s and model=%s order by priority', (inherit_id, self._name))
1693                 sql_inherit = cr.fetchall()
1694                 for (inherit, id) in sql_inherit:
1695                     result = _inherit_apply(result, inherit)
1696                     result = _inherit_apply_rec(result, id)
1697                 return result
1698
1699             inherit_result = etree.fromstring(encode(result['arch']))
1700             result['arch'] = _inherit_apply_rec(inherit_result, sql_res[3])
1701
1702             result['name'] = sql_res[1]
1703             result['field_parent'] = sql_res[2] or False
1704         else:
1705
1706             # otherwise, build some kind of default view
1707             if view_type == 'form':
1708                 res = self.fields_get(cr, user, context=context)
1709                 xml = '<?xml version="1.0" encoding="utf-8"?> ' \
1710                      '<form string="%s">' % (self._description,)
1711                 for x in res:
1712                     if res[x]['type'] not in ('one2many', 'many2many'):
1713                         xml += '<field name="%s"/>' % (x,)
1714                         if res[x]['type'] == 'text':
1715                             xml += "<newline/>"
1716                 xml += "</form>"
1717
1718             elif view_type == 'tree':
1719                 _rec_name = self._rec_name
1720                 if _rec_name not in self._columns:
1721                     _rec_name = self._columns.keys()[0]
1722                 xml = '<?xml version="1.0" encoding="utf-8"?>' \
1723                        '<tree string="%s"><field name="%s"/></tree>' \
1724                        % (self._description, self._rec_name)
1725
1726             elif view_type == 'calendar':
1727                 xml = self.__get_default_calendar_view()
1728
1729             elif view_type == 'search':
1730                 xml = self.__get_default_search_view(cr, user, context)
1731
1732             else:
1733                 xml = '<?xml version="1.0"?>' # what happens here, graph case?
1734                 raise except_orm(_('Invalid Architecture!'), _("There is no view of type '%s' defined for the structure!") % view_type)
1735             result['arch'] = etree.fromstring(encode(xml))
1736             result['name'] = 'default'
1737             result['field_parent'] = False
1738             result['view_id'] = 0
1739
1740         xarch, xfields = self.__view_look_dom_arch(cr, user, result['arch'], view_id, context=context)
1741         result['arch'] = xarch
1742         result['fields'] = xfields
1743
1744         if submenu:
1745             if context and context.get('active_id', False):
1746                 data_menu = self.pool.get('ir.ui.menu').browse(cr, user, context['active_id'], context).action
1747                 if data_menu:
1748                     act_id = data_menu.id
1749                     if act_id:
1750                         data_action = self.pool.get('ir.actions.act_window').browse(cr, user, [act_id], context)[0]
1751                         result['submenu'] = getattr(data_action, 'menus', False)
1752         if toolbar:
1753             def clean(x):
1754                 x = x[2]
1755                 for key in ('report_sxw_content', 'report_rml_content',
1756                         'report_sxw', 'report_rml',
1757                         'report_sxw_content_data', 'report_rml_content_data'):
1758                     if key in x:
1759                         del x[key]
1760                 return x
1761             ir_values_obj = self.pool.get('ir.values')
1762             resprint = ir_values_obj.get(cr, user, 'action',
1763                     'client_print_multi', [(self._name, False)], False,
1764                     context)
1765             resaction = ir_values_obj.get(cr, user, 'action',
1766                     'client_action_multi', [(self._name, False)], False,
1767                     context)
1768
1769             resrelate = ir_values_obj.get(cr, user, 'action',
1770                     'client_action_relate', [(self._name, False)], False,
1771                     context)
1772             resprint = map(clean, resprint)
1773             resaction = map(clean, resaction)
1774             resaction = filter(lambda x: not x.get('multi', False), resaction)
1775             resprint = filter(lambda x: not x.get('multi', False), resprint)
1776             resrelate = map(lambda x: x[2], resrelate)
1777
1778             for x in resprint + resaction + resrelate:
1779                 x['string'] = x['name']
1780
1781             result['toolbar'] = {
1782                 'print': resprint,
1783                 'action': resaction,
1784                 'relate': resrelate
1785             }
1786         return result
1787
1788     _view_look_dom_arch = __view_look_dom_arch
1789
1790     def search_count(self, cr, user, args, context=None):
1791         if not context:
1792             context = {}
1793         res = self.search(cr, user, args, context=context, count=True)
1794         if isinstance(res, list):
1795             return len(res)
1796         return res
1797
1798     def search(self, cr, user, args, offset=0, limit=None, order=None, context=None, count=False):
1799         """
1800         Search for records based on a search domain.
1801
1802         :param cr: database cursor
1803         :param user: current user id
1804         :param args: list of tuples specifying the search domain [('field_name', 'operator', value), ...]. Pass an empty list to match all records.
1805         :param offset: optional number of results to skip in the returned values (default: 0)
1806         :param limit: optional max number of records to return (default: **None**)
1807         :param order: optional columns to sort by (default: self._order=id )
1808         :param context: optional context arguments, like lang, time zone
1809         :type context: dictionary
1810         :param count: optional (default: **False**), if **True**, returns only the number of records matching the criteria, not their ids
1811         :return: id or list of ids of records matching the criteria
1812         :rtype: integer or list of integers
1813         :raise AccessError: * if user tries to bypass access rules for read on the requested object.
1814
1815         **Expressing a search domain (args)**
1816
1817         Each tuple in the search domain needs to have 3 elements, in the form: **('field_name', 'operator', value)**, where:
1818
1819             * **field_name** must be a valid name of field of the object model, possibly following many-to-one relationships using dot-notation, e.g 'street' or 'partner_id.country' are valid values.
1820             * **operator** must be a string with a valid comparison operator from this list: ``=, !=, >, >=, <, <=, like, ilike, in, not in, child_of, parent_left, parent_right``
1821               The semantics of most of these operators are obvious.
1822               The ``child_of`` operator will look for records who are children or grand-children of a given record,
1823               according to the semantics of this model (i.e following the relationship field named by
1824               ``self._parent_name``, by default ``parent_id``.
1825             * **value** must be a valid value to compare with the values of **field_name**, depending on its type.
1826
1827         Domain criteria can be combined using 3 logical operators than can be added between tuples:  '**&**' (logical AND, default), '**|**' (logical OR), '**!**' (logical NOT).
1828         These are **prefix** operators and the arity of the '**&**' and '**|**' operator is 2, while the arity of the '**!**' is just 1.
1829         Be very careful about this when you combine them the first time.
1830
1831         Here is an example of searching for Partners named *ABC* from Belgium and Germany whose language is not english ::
1832
1833             [('name','=','ABC'),'!',('language.code','=','en_US'),'|',('country_id.code','=','be'),('country_id.code','=','de'))
1834
1835         The '&' is omitted as it is the default, and of course we could have used '!=' for the language, but what this domain really represents is::
1836
1837             (name is 'ABC' AND (language is NOT english) AND (country is Belgium OR Germany))
1838
1839         """
1840         return self._search(cr, user, args, offset=offset, limit=limit, order=order, context=context, count=count)
1841
1842     def _search(self, cr, user, args, offset=0, limit=None, order=None, context=None, count=False, access_rights_uid=None):
1843         """
1844         Private implementation of search() method, allowing specifying the uid to use for the access right check.
1845         This is useful for example when filling in the selection list for a drop-down and avoiding access rights errors,
1846         by specifying ``access_rights_uid=1`` to bypass access rights check, but not ir.rules!
1847
1848         :param access_rights_uid: optional user ID to use when checking access rights
1849                                   (not for ir.rules, this is only for ir.model.access)
1850         """
1851         raise NotImplementedError(_('The search method is not implemented on this object !'))
1852
1853     def name_get(self, cr, user, ids, context=None):
1854         """
1855
1856         :param cr: database cursor
1857         :param user: current user id
1858         :type user: integer
1859         :param ids: list of ids
1860         :param context: context arguments, like lang, time zone
1861         :type context: dictionary
1862         :return: tuples with the text representation of requested objects for to-many relationships
1863
1864         """
1865         if not context:
1866             context = {}
1867         if not ids:
1868             return []
1869         if isinstance(ids, (int, long)):
1870             ids = [ids]
1871         return [(r['id'], tools.ustr(r[self._rec_name])) for r in self.read(cr, user, ids,
1872             [self._rec_name], context, load='_classic_write')]
1873
1874     def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100):
1875         """
1876         Search for records and their display names according to a search domain.
1877
1878         :param cr: database cursor
1879         :param user: current user id
1880         :param name: object name to search
1881         :param args: list of tuples specifying search criteria [('field_name', 'operator', 'value'), ...]
1882         :param operator: operator for search criterion
1883         :param context: context arguments, like lang, time zone
1884         :type context: dictionary
1885         :param limit: optional max number of records to return
1886         :return: list of object names matching the search criteria, used to provide completion for to-many relationships
1887
1888         This method is equivalent of :py:meth:`~osv.osv.osv.search` on **name** + :py:meth:`~osv.osv.osv.name_get` on the result.
1889         See :py:meth:`~osv.osv.osv.search` for an explanation of the possible values for the search domain specified in **args**.
1890
1891         """
1892         return self._name_search(cr, user, name, args, operator, context, limit)
1893
1894     # private implementation of name_search, allows passing a dedicated user for the name_get part to
1895     # solve some access rights issues
1896     def _name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100, name_get_uid=None):
1897         if args is None:
1898             args = []
1899         if context is None:
1900             context = {}
1901         args = args[:]
1902         if name:
1903             args += [(self._rec_name, operator, name)]
1904         access_rights_uid = name_get_uid or user
1905         ids = self._search(cr, user, args, limit=limit, context=context, access_rights_uid=access_rights_uid)
1906         res = self.name_get(cr, access_rights_uid, ids, context)
1907         return res
1908
1909     def copy(self, cr, uid, id, default=None, context=None):
1910         raise NotImplementedError(_('The copy method is not implemented on this object !'))
1911
1912     def exists(self, cr, uid, id, context=None):
1913         raise NotImplementedError(_('The exists method is not implemented on this object !'))
1914
1915     def read_string(self, cr, uid, id, langs, fields=None, context=None):
1916         res = {}
1917         res2 = {}
1918         self.pool.get('ir.model.access').check(cr, uid, 'ir.translation', 'read', context=context)
1919         if not fields:
1920             fields = self._columns.keys() + self._inherit_fields.keys()
1921         #FIXME: collect all calls to _get_source into one SQL call.
1922         for lang in langs:
1923             res[lang] = {'code': lang}
1924             for f in fields:
1925                 if f in self._columns:
1926                     res_trans = self.pool.get('ir.translation')._get_source(cr, uid, self._name+','+f, 'field', lang)
1927                     if res_trans:
1928                         res[lang][f] = res_trans
1929                     else:
1930                         res[lang][f] = self._columns[f].string
1931         for table in self._inherits:
1932             cols = intersect(self._inherit_fields.keys(), fields)
1933             res2 = self.pool.get(table).read_string(cr, uid, id, langs, cols, context)
1934         for lang in res2:
1935             if lang in res:
1936                 res[lang]['code'] = lang
1937             for f in res2[lang]:
1938                 res[lang][f] = res2[lang][f]
1939         return res
1940
1941     def write_string(self, cr, uid, id, langs, vals, context=None):
1942         self.pool.get('ir.model.access').check(cr, uid, 'ir.translation', 'write', context=context)
1943         #FIXME: try to only call the translation in one SQL
1944         for lang in langs:
1945             for field in vals:
1946                 if field in self._columns:
1947                     src = self._columns[field].string
1948                     self.pool.get('ir.translation')._set_ids(cr, uid, self._name+','+field, 'field', lang, [0], vals[field], src)
1949         for table in self._inherits:
1950             cols = intersect(self._inherit_fields.keys(), vals)
1951             if cols:
1952                 self.pool.get(table).write_string(cr, uid, id, langs, vals, context)
1953         return True
1954
1955     def _check_removed_columns(self, cr, log=False):
1956         raise NotImplementedError()
1957
1958     def _add_missing_default_values(self, cr, uid, values, context=None):
1959         missing_defaults = []
1960         avoid_tables = [] # avoid overriding inherited values when parent is set
1961         for tables, parent_field in self._inherits.items():
1962             if parent_field in values:
1963                 avoid_tables.append(tables)
1964         for field in self._columns.keys():
1965             if not field in values:
1966                 missing_defaults.append(field)
1967         for field in self._inherit_fields.keys():
1968             if (field not in values) and (self._inherit_fields[field][0] not in avoid_tables):
1969                 missing_defaults.append(field)
1970
1971         if len(missing_defaults):
1972             # override defaults with the provided values, never allow the other way around
1973             defaults = self.default_get(cr, uid, missing_defaults, context)
1974             for dv in defaults:
1975                 if (dv in self._columns and self._columns[dv]._type == 'many2many') \
1976                      or (dv in self._inherit_fields and self._inherit_fields[dv][2]._type == 'many2many') \
1977                         and defaults[dv] and isinstance(defaults[dv][0], (int, long)):
1978                     defaults[dv] = [(6, 0, defaults[dv])]
1979                 if dv in self._columns and self._columns[dv]._type == 'one2many' \
1980                     or (dv in self._inherit_fields and self._inherit_fields[dv][2]._type == 'one2many') \
1981                         and isinstance(defaults[dv], (list, tuple)) and isinstance(defaults[dv][0], dict):
1982                     defaults[dv] = [(0, 0, x) for x in defaults[dv]]
1983             defaults.update(values)
1984             values = defaults
1985         return values
1986
1987 class orm_memory(orm_template):
1988
1989     _protected = ['read', 'write', 'create', 'default_get', 'perm_read', 'unlink', 'fields_get', 'fields_view_get', 'search', 'name_get', 'distinct_field_get', 'name_search', 'copy', 'import_data', 'search_count', 'exists']
1990     _inherit_fields = {}
1991     _max_count = 200
1992     _max_hours = 1
1993     _check_time = 20
1994
1995     def __init__(self, cr):
1996         super(orm_memory, self).__init__(cr)
1997         self.datas = {}
1998         self.next_id = 0
1999         self.check_id = 0
2000         cr.execute('delete from wkf_instance where res_type=%s', (self._name,))
2001
2002     def _check_access(self, uid, object_id, mode):
2003         if uid != 1 and self.datas[object_id]['internal.create_uid'] != uid:
2004             raise except_orm(_('AccessError'), '%s access is only allowed on your own records for osv_memory objects except for the super-user' % mode.capitalize())
2005
2006     def vaccum(self, cr, uid):
2007         self.check_id += 1
2008         if self.check_id % self._check_time:
2009             return True
2010         tounlink = []
2011         max = time.time() - self._max_hours * 60 * 60
2012         for id in self.datas:
2013             if self.datas[id]['internal.date_access'] < max:
2014                 tounlink.append(id)
2015         self.unlink(cr, 1, tounlink)
2016         if len(self.datas) > self._max_count:
2017             sorted = map(lambda x: (x[1]['internal.date_access'], x[0]), self.datas.items())
2018             sorted.sort()
2019             ids = map(lambda x: x[1], sorted[:len(self.datas)-self._max_count])
2020             self.unlink(cr, uid, ids)
2021         return True
2022
2023     def read(self, cr, user, ids, fields_to_read=None, context=None, load='_classic_read'):
2024         if not context:
2025             context = {}
2026         if not fields_to_read:
2027             fields_to_read = self._columns.keys()
2028         result = []
2029         if self.datas:
2030             ids_orig = ids
2031             if isinstance(ids, (int, long)):
2032                 ids = [ids]
2033             for id in ids:
2034                 r = {'id': id}
2035                 for f in fields_to_read:
2036                     record = self.datas.get(id)
2037                     if record:
2038                         self._check_access(user, id, 'read')
2039                         r[f] = record.get(f, False)
2040                         if r[f] and isinstance(self._columns[f], fields.binary) and context.get('bin_size', False):
2041                             r[f] = len(r[f])
2042                 result.append(r)
2043                 if id in self.datas:
2044                     self.datas[id]['internal.date_access'] = time.time()
2045             fields_post = filter(lambda x: x in self._columns and not getattr(self._columns[x], load), fields_to_read)
2046             for f in fields_post:
2047                 res2 = self._columns[f].get_memory(cr, self, ids, f, user, context=context, values=result)
2048                 for record in result:
2049                     record[f] = res2[record['id']]
2050             if isinstance(ids_orig, (int, long)):
2051                 return result[0]
2052         return result
2053
2054     def write(self, cr, user, ids, vals, context=None):
2055         if not ids:
2056             return True
2057         vals2 = {}
2058         upd_todo = []
2059         for field in vals:
2060             if self._columns[field]._classic_write:
2061                 vals2[field] = vals[field]
2062             else:
2063                 upd_todo.append(field)
2064         for object_id in ids:
2065             self._check_access(user, object_id, mode='write')
2066             self.datas[object_id].update(vals2)
2067             self.datas[object_id]['internal.date_access'] = time.time()
2068             for field in upd_todo:
2069                 self._columns[field].set_memory(cr, self, object_id, field, vals[field], user, context)
2070         self._validate(cr, user, [object_id], context)
2071         wf_service = netsvc.LocalService("workflow")
2072         wf_service.trg_write(user, self._name, object_id, cr)
2073         return object_id
2074
2075     def create(self, cr, user, vals, context=None):
2076         self.vaccum(cr, user)
2077         self.next_id += 1
2078         id_new = self.next_id
2079
2080         vals = self._add_missing_default_values(cr, user, vals, context)
2081
2082         vals2 = {}
2083         upd_todo = []
2084         for field in vals:
2085             if self._columns[field]._classic_write:
2086                 vals2[field] = vals[field]
2087             else:
2088                 upd_todo.append(field)
2089         self.datas[id_new] = vals2
2090         self.datas[id_new]['internal.date_access'] = time.time()
2091         self.datas[id_new]['internal.create_uid'] = user
2092
2093         for field in upd_todo:
2094             self._columns[field].set_memory(cr, self, id_new, field, vals[field], user, context)
2095         self._validate(cr, user, [id_new], context)
2096         if self._log_create and not (context and context.get('no_store_function', False)):
2097             message = self._description + \
2098                 " '" + \
2099                 self.name_get(cr, user, [id_new], context=context)[0][1] + \
2100                 "' "+ _("created.")
2101             self.log(cr, user, id_new, message, True, context=context)
2102         wf_service = netsvc.LocalService("workflow")
2103         wf_service.trg_create(user, self._name, id_new, cr)
2104         return id_new
2105
2106     def _where_calc(self, cr, user, args, active_test=True, context=None):
2107         if not context:
2108             context = {}
2109         args = args[:]
2110         res = []
2111         # if the object has a field named 'active', filter out all inactive
2112         # records unless they were explicitely asked for
2113         if 'active' in self._columns and (active_test and context.get('active_test', True)):
2114             if args:
2115                 active_in_args = False
2116                 for a in args:
2117                     if a[0] == 'active':
2118                         active_in_args = True
2119                 if not active_in_args:
2120                     args.insert(0, ('active', '=', 1))
2121             else:
2122                 args = [('active', '=', 1)]
2123         if args:
2124             import expression
2125             e = expression.expression(args)
2126             e.parse(cr, user, self, context)
2127             res = e.exp
2128         return res or []
2129
2130     def _search(self, cr, user, args, offset=0, limit=None, order=None, context=None, count=False, access_rights_uid=None):
2131         if not context:
2132             context = {}
2133
2134         # implicit filter on current user except for superuser
2135         if user != 1:
2136             if not args:
2137                 args = []
2138             args.insert(0, ('internal.create_uid', '=', user))
2139
2140         result = self._where_calc(cr, user, args, context=context)
2141         if result == []:
2142             return self.datas.keys()
2143
2144         res = []
2145         counter = 0
2146         #Find the value of dict
2147         f = False
2148         if result:
2149             for id, data in self.datas.items():
2150                 counter = counter + 1
2151                 data['id'] = id
2152                 if limit and (counter > int(limit)):
2153                     break
2154                 f = True
2155                 for arg in result:
2156                     if arg[1] == '=':
2157                         val = eval('data[arg[0]]'+'==' +' arg[2]', locals())
2158                     elif arg[1] in ['<', '>', 'in', 'not in', '<=', '>=', '<>']:
2159                         val = eval('data[arg[0]]'+arg[1] +' arg[2]', locals())
2160                     elif arg[1] in ['ilike']:
2161                         val = (str(data[arg[0]]).find(str(arg[2]))!=-1)
2162
2163                     f = f and val
2164
2165                 if f:
2166                     res.append(id)
2167         if count:
2168             return len(res)
2169         return res or []
2170
2171     def unlink(self, cr, uid, ids, context=None):
2172         for id in ids:
2173             self._check_access(uid, id, 'unlink')
2174             self.datas.pop(id, None)
2175         if len(ids):
2176             cr.execute('delete from wkf_instance where res_type=%s and res_id IN %s', (self._name, tuple(ids)))
2177         return True
2178
2179     def perm_read(self, cr, user, ids, context=None, details=True):
2180         result = []
2181         credentials = self.pool.get('res.users').name_get(cr, user, [user])[0]
2182         create_date = time.strftime('%Y-%m-%d %H:%M:%S')
2183         for id in ids:
2184             self._check_access(user, id, 'read')
2185             result.append({
2186                 'create_uid': credentials,
2187                 'create_date': create_date,
2188                 'write_uid': False,
2189                 'write_date': False,
2190                 'id': id,
2191                 'xmlid' : False,
2192             })
2193         return result
2194
2195     def _check_removed_columns(self, cr, log=False):
2196         # nothing to check in memory...
2197         pass
2198
2199     def exists(self, cr, uid, id, context=None):
2200         return id in self.datas
2201
2202 class orm(orm_template):
2203     _sql_constraints = []
2204     _table = None
2205     _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']
2206     __logger = logging.getLogger('orm')
2207     __schema = logging.getLogger('orm.schema')
2208     def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None):
2209         """
2210         Get the list of records in list view grouped by the given ``groupby`` fields
2211
2212         :param cr: database cursor
2213         :param uid: current user id
2214         :param domain: list specifying search criteria [['field_name', 'operator', 'value'], ...]
2215         :param fields: list of fields present in the list view specified on the object
2216         :param groupby: list of fields on which to groupby the records
2217         :type fields_list: list (example ['field_name_1', ...])
2218         :param offset: optional number of records to skip
2219         :param limit: optional max number of records to return
2220         :param context: context arguments, like lang, time zone
2221         :return: list of dictionaries(one dictionary for each record) containing:
2222
2223                     * the values of fields grouped by the fields in ``groupby`` argument
2224                     * __domain: list of tuples specifying the search criteria
2225                     * __context: dictionary with argument like ``groupby``
2226         :rtype: [{'field_name_1': value, ...]
2227         :raise AccessError: * if user has no read rights on the requested object
2228                             * if user tries to bypass access rules for read on the requested object
2229
2230         """
2231         context = context or {}
2232         self.pool.get('ir.model.access').check(cr, uid, self._name, 'read', context=context)
2233         if not fields:
2234             fields = self._columns.keys()
2235
2236         query = self._where_calc(cr, uid, domain, context=context)
2237         self._apply_ir_rules(cr, uid, query, 'read', context=context)
2238
2239         # Take care of adding join(s) if groupby is an '_inherits'ed field
2240         groupby_list = groupby
2241         if groupby:
2242             if isinstance(groupby, list):
2243                 groupby = groupby[0]
2244             self._inherits_join_calc(groupby, query)
2245
2246         assert not groupby or groupby in fields, "Fields in 'groupby' must appear in the list of fields to read (perhaps it's missing in the list view?)"
2247
2248         fget = self.fields_get(cr, uid, fields)
2249         float_int_fields = filter(lambda x: fget[x]['type'] in ('float', 'integer'), fields)
2250         flist = ''
2251         group_by = groupby
2252         if groupby:
2253             if fget.get(groupby):
2254                 if fget[groupby]['type'] in ('date', 'datetime'):
2255                     flist = "to_char(%s,'yyyy-mm') as %s " % (groupby, groupby)
2256                     groupby = "to_char(%s,'yyyy-mm')" % (groupby)
2257                 else:
2258                     flist = groupby
2259             else:
2260                 # Don't allow arbitrary values, as this would be a SQL injection vector!
2261                 raise except_orm(_('Invalid group_by'),
2262                                  _('Invalid group_by specification: "%s".\nA group_by specification must be a list of valid fields.')%(groupby,))
2263
2264
2265         fields_pre = [f for f in float_int_fields if
2266                    f == self.CONCURRENCY_CHECK_FIELD
2267                 or (f in self._columns and getattr(self._columns[f], '_classic_write'))]
2268         for f in fields_pre:
2269             if f not in ['id', 'sequence']:
2270                 group_operator = fget[f].get('group_operator', 'sum')
2271                 if flist:
2272                     flist += ','
2273                 flist += group_operator+'('+f+') as '+f
2274
2275         gb = groupby and (' GROUP BY '+groupby) or ''
2276
2277         from_clause, where_clause, where_clause_params = query.get_sql()
2278         where_clause = where_clause and ' WHERE ' + where_clause
2279         limit_str = limit and ' limit %d' % limit or ''
2280         offset_str = offset and ' offset %d' % offset or ''
2281         cr.execute('SELECT min(%s.id) AS id,' % self._table + flist + ' FROM ' + from_clause + where_clause + gb + limit_str + offset_str, where_clause_params)
2282         alldata = {}
2283         groupby = group_by
2284         for r in cr.dictfetchall():
2285             for fld, val in r.items():
2286                 if val == None: r[fld] = False
2287             alldata[r['id']] = r
2288             del r['id']
2289         if groupby and fget[groupby]['type'] == 'many2one':
2290             data_ids = self.search(cr, uid, [('id', 'in', alldata.keys())], order=groupby, context=context)
2291             # the IDS of the records that has groupby field value = False or ''
2292             # should be added too
2293             data_ids += filter(lambda x:x not in data_ids, alldata.keys())
2294             data = self.read(cr, uid, data_ids, groupby and [groupby] or ['id'], context=context)
2295             # restore order of the search as read() uses the default _order (this is only for groups, so the size of data_read shoud be small):
2296             data.sort(lambda x,y: cmp(data_ids.index(x['id']), data_ids.index(y['id'])))
2297         else:
2298             data = self.read(cr, uid, alldata.keys(), groupby and [groupby] or ['id'], context=context)
2299             if groupby:
2300                 data.sort(lambda x,y:cmp(x[groupby],y[groupby]))
2301         for d in data:
2302             if groupby:
2303                 d['__domain'] = [(groupby, '=', alldata[d['id']][groupby] or False)] + domain
2304                 if not isinstance(groupby_list, (str, unicode)):
2305                     if groupby or not context.get('group_by_no_leaf', False):
2306                         d['__context'] = {'group_by': groupby_list[1:]}
2307             if groupby and groupby in fget:
2308                 if d[groupby] and fget[groupby]['type'] in ('date', 'datetime'):
2309                     dt = datetime.datetime.strptime(alldata[d['id']][groupby][:7], '%Y-%m')
2310                     days = calendar.monthrange(dt.year, dt.month)[1]
2311
2312                     d[groupby] = datetime.datetime.strptime(d[groupby][:10], '%Y-%m-%d').strftime('%B %Y')
2313                     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),\
2314                                      (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
2315                 del alldata[d['id']][groupby]
2316             d.update(alldata[d['id']])
2317             del d['id']
2318         return data
2319
2320     def _inherits_join_add(self, parent_model_name, query):
2321         """
2322         Add missing table SELECT and JOIN clause to ``query`` for reaching the parent table (no duplicates)
2323
2324         :param parent_model_name: name of the parent model for which the clauses should be added
2325         :param query: query object on which the JOIN should be added
2326         """
2327         inherits_field = self._inherits[parent_model_name]
2328         parent_model = self.pool.get(parent_model_name)
2329         parent_table_name = parent_model._table
2330         quoted_parent_table_name = '"%s"' % parent_table_name
2331         if quoted_parent_table_name not in query.tables:
2332             query.tables.append(quoted_parent_table_name)
2333             query.where_clause.append('("%s".%s = %s.id)' % (self._table, inherits_field, parent_table_name))
2334
2335     def _inherits_join_calc(self, field, query):
2336         """
2337         Adds missing table select and join clause(s) to ``query`` for reaching
2338         the field coming from an '_inherits' parent table (no duplicates).
2339
2340         :param field: name of inherited field to reach
2341         :param query: query object on which the JOIN should be added
2342         :return: qualified name of field, to be used in SELECT clause
2343         """
2344         current_table = self
2345         while field in current_table._inherit_fields and not field in current_table._columns:
2346             parent_model_name = current_table._inherit_fields[field][0]
2347             parent_table = self.pool.get(parent_model_name)
2348             self._inherits_join_add(parent_model_name, query)
2349             current_table = parent_table
2350         return '"%s".%s' % (current_table._table, field)
2351
2352     def _parent_store_compute(self, cr):
2353         if not self._parent_store:
2354             return
2355         logger = netsvc.Logger()
2356         logger.notifyChannel('data', netsvc.LOG_INFO, 'Computing parent left and right for table %s...' % (self._table, ))
2357         def browse_rec(root, pos=0):
2358 # TODO: set order
2359             where = self._parent_name+'='+str(root)
2360             if not root:
2361                 where = self._parent_name+' IS NULL'
2362             if self._parent_order:
2363                 where += ' order by '+self._parent_order
2364             cr.execute('SELECT id FROM '+self._table+' WHERE '+where)
2365             pos2 = pos + 1
2366             for id in cr.fetchall():
2367                 pos2 = browse_rec(id[0], pos2)
2368             cr.execute('update '+self._table+' set parent_left=%s, parent_right=%s where id=%s', (pos, pos2, root))
2369             return pos2 + 1
2370         query = 'SELECT id FROM '+self._table+' WHERE '+self._parent_name+' IS NULL'
2371         if self._parent_order:
2372             query += ' order by ' + self._parent_order
2373         pos = 0
2374         cr.execute(query)
2375         for (root,) in cr.fetchall():
2376             pos = browse_rec(root, pos)
2377         return True
2378
2379     def _update_store(self, cr, f, k):
2380         logger = netsvc.Logger()
2381         logger.notifyChannel('data', netsvc.LOG_INFO, "storing computed values of fields.function '%s'" % (k,))
2382         ss = self._columns[k]._symbol_set
2383         update_query = 'UPDATE "%s" SET "%s"=%s WHERE id=%%s' % (self._table, k, ss[0])
2384         cr.execute('select id from '+self._table)
2385         ids_lst = map(lambda x: x[0], cr.fetchall())
2386         while ids_lst:
2387             iids = ids_lst[:40]
2388             ids_lst = ids_lst[40:]
2389             res = f.get(cr, self, iids, k, 1, {})
2390             for key, val in res.items():
2391                 if f._multi:
2392                     val = val[k]
2393                 # if val is a many2one, just write the ID
2394                 if type(val) == tuple:
2395                     val = val[0]
2396                 if (val<>False) or (type(val)<>bool):
2397                     cr.execute(update_query, (ss[1](val), key))
2398
2399     def _check_removed_columns(self, cr, log=False):
2400         # iterate on the database columns to drop the NOT NULL constraints
2401         # of fields which were required but have been removed (or will be added by another module)
2402         columns = [c for c in self._columns if not (isinstance(self._columns[c], fields.function) and not self._columns[c].store)]
2403         columns += ('id', 'write_uid', 'write_date', 'create_uid', 'create_date') # openerp access columns
2404         cr.execute("SELECT a.attname, a.attnotnull"
2405                    "  FROM pg_class c, pg_attribute a"
2406                    " WHERE c.relname=%s"
2407                    "   AND c.oid=a.attrelid"
2408                    "   AND a.attisdropped=%s"
2409                    "   AND pg_catalog.format_type(a.atttypid, a.atttypmod) NOT IN ('cid', 'tid', 'oid', 'xid')"
2410                    "   AND a.attname NOT IN %s", (self._table, False, tuple(columns))),
2411
2412         for column in cr.dictfetchall():
2413             if log:
2414                 self.__logger.debug("column %s is in the table %s but not in the corresponding object %s",
2415                                     column['attname'], self._table, self._name)
2416             if column['attnotnull']:
2417                 cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" DROP NOT NULL' % (self._table, column['attname']))
2418                 self.__schema.debug("Table '%s': column '%s': dropped NOT NULL constraint",
2419                                     self._table, column['attname'])
2420
2421     def _auto_init(self, cr, context=None):
2422         if context is None:
2423             context = {}
2424         store_compute = False
2425         create = False
2426         todo_end = []
2427         self._field_create(cr, context=context)
2428         if getattr(self, '_auto', True):
2429             cr.execute("SELECT relname FROM pg_class WHERE relkind IN ('r','v') AND relname=%s", (self._table,))
2430             if not cr.rowcount:
2431                 cr.execute('CREATE TABLE "%s" (id SERIAL NOT NULL, PRIMARY KEY(id)) WITHOUT OIDS' % (self._table,))
2432                 cr.execute("COMMENT ON TABLE \"%s\" IS '%s'" % (self._table, self._description.replace("'", "''")))
2433                 create = True
2434                 self.__schema.debug("Table '%s': created", self._table)
2435
2436             cr.commit()
2437             if self._parent_store:
2438                 cr.execute("""SELECT c.relname
2439                     FROM pg_class c, pg_attribute a
2440                     WHERE c.relname=%s AND a.attname=%s AND c.oid=a.attrelid
2441                     """, (self._table, 'parent_left'))
2442                 if not cr.rowcount:
2443                     cr.execute('ALTER TABLE "%s" ADD COLUMN "parent_left" INTEGER' % (self._table,))
2444                     cr.execute('ALTER TABLE "%s" ADD COLUMN "parent_right" INTEGER' % (self._table,))
2445                     if 'parent_left' not in self._columns:
2446                         self.__logger.error('create a column parent_left on object %s: fields.integer(\'Left Parent\', select=1)',
2447                                             self._table)
2448                         self.__schema.debug("Table '%s': added column '%s' with definition=%s",
2449                                             self._table, 'parent_left', 'INTEGER')
2450                     if 'parent_right' not in self._columns:
2451                         self.__logger.error('create a column parent_right on object %s: fields.integer(\'Right Parent\', select=1)',
2452                                             self._table)
2453                         self.__schema.debug("Table '%s': added column '%s' with definition=%s",
2454                                             self._table, 'parent_right', 'INTEGER')
2455                     if self._columns[self._parent_name].ondelete != 'cascade':
2456                         self.__logger.error("The column %s on object %s must be set as ondelete='cascade'",
2457                                             self._parent_name, self._name)
2458
2459                     cr.commit()
2460                     store_compute = True
2461
2462             if self._log_access:
2463                 logs = {
2464                     'create_uid': 'INTEGER REFERENCES res_users ON DELETE SET NULL',
2465                     'create_date': 'TIMESTAMP',
2466                     'write_uid': 'INTEGER REFERENCES res_users ON DELETE SET NULL',
2467                     'write_date': 'TIMESTAMP'
2468                 }
2469                 for k in logs:
2470                     cr.execute("""
2471                         SELECT c.relname
2472                           FROM pg_class c, pg_attribute a
2473                          WHERE c.relname=%s AND a.attname=%s AND c.oid=a.attrelid
2474                         """, (self._table, k))
2475                     if not cr.rowcount:
2476                         cr.execute('ALTER TABLE "%s" ADD COLUMN "%s" %s' % (self._table, k, logs[k]))
2477                         cr.commit()
2478                         self.__schema.debug("Table '%s': added column '%s' with definition=%s",
2479                                             self._table, k, logs[k])
2480
2481             self._check_removed_columns(cr, log=False)
2482
2483             # iterate on the "object columns"
2484             todo_update_store = []
2485             update_custom_fields = context.get('update_custom_fields', False)
2486
2487             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 " \
2488                "FROM pg_class c,pg_attribute a,pg_type t " \
2489                "WHERE c.relname=%s " \
2490                "AND c.oid=a.attrelid " \
2491                "AND a.atttypid=t.oid", (self._table,))
2492             col_data = dict(map(lambda x: (x['attname'], x),cr.dictfetchall()))
2493
2494
2495             for k in self._columns:
2496                 if k in ('id', 'write_uid', 'write_date', 'create_uid', 'create_date'):
2497                     continue
2498                 #Not Updating Custom fields
2499                 if k.startswith('x_') and not update_custom_fields:
2500                     continue
2501
2502                 f = self._columns[k]
2503
2504                 if isinstance(f, fields.one2many):
2505                     cr.execute("SELECT relname FROM pg_class WHERE relkind='r' AND relname=%s", (f._obj,))
2506
2507                     if self.pool.get(f._obj):
2508                         if f._fields_id not in self.pool.get(f._obj)._columns.keys():
2509                             if not self.pool.get(f._obj)._inherits or (f._fields_id not in self.pool.get(f._obj)._inherit_fields.keys()):
2510                                 raise except_orm('Programming Error', ("There is no reference field '%s' found for '%s'") % (f._fields_id, f._obj,))
2511
2512                     if cr.fetchone():
2513                         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))
2514                         res = cr.fetchone()[0]
2515                         if not res:
2516                             cr.execute('ALTER TABLE "%s" ADD FOREIGN KEY (%s) REFERENCES "%s" ON DELETE SET NULL' % (self._obj, f._fields_id, f._table))
2517                             self.__schema.debug("Table '%s': added foreign key '%s' with definition=REFERENCES \"%s\" ON DELETE SET NULL",
2518                                 self._obj, f._fields_id, f._table)
2519                 elif isinstance(f, fields.many2many):
2520                     cr.execute("SELECT relname FROM pg_class WHERE relkind IN ('r','v') AND relname=%s", (f._rel,))
2521                     if not cr.dictfetchall():
2522                         if not self.pool.get(f._obj):
2523                             raise except_orm('Programming Error', ('There is no reference available for %s') % (f._obj,))
2524                         ref = self.pool.get(f._obj)._table
2525 #                        ref = f._obj.replace('.', '_')
2526                         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))
2527                         cr.execute('CREATE INDEX "%s_%s_index" ON "%s" ("%s")' % (f._rel, f._id1, f._rel, f._id1))
2528                         cr.execute('CREATE INDEX "%s_%s_index" ON "%s" ("%s")' % (f._rel, f._id2, f._rel, f._id2))
2529                         cr.execute("COMMENT ON TABLE \"%s\" IS 'RELATION BETWEEN %s AND %s'" % (f._rel, self._table, ref))
2530                         cr.commit()
2531                         self.__schema.debug("Create table '%s': relation between '%s' and '%s'",
2532                                             f._rel, self._table, ref)
2533                 else:
2534                     res = col_data.get(k, [])
2535                     res = res and [res] or []
2536                     if not res and hasattr(f, 'oldname'):
2537                         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 " \
2538                             "FROM pg_class c,pg_attribute a,pg_type t " \
2539                             "WHERE c.relname=%s " \
2540                             "AND a.attname=%s " \
2541                             "AND c.oid=a.attrelid " \
2542                             "AND a.atttypid=t.oid", (self._table, f.oldname))
2543                         res_old = cr.dictfetchall()
2544                         if res_old and len(res_old) == 1:
2545                             cr.execute('ALTER TABLE "%s" RENAME "%s" TO "%s"' % (self._table, f.oldname, k))
2546                             res = res_old
2547                             res[0]['attname'] = k
2548                             self.__schema.debug("Table '%s': renamed column '%s' to '%s'",
2549                                                 self._table, f.oldname, k)
2550
2551                     if len(res) == 1:
2552                         f_pg_def = res[0]
2553                         f_pg_type = f_pg_def['typname']
2554                         f_pg_size = f_pg_def['size']
2555                         f_pg_notnull = f_pg_def['attnotnull']
2556                         if isinstance(f, fields.function) and not f.store and\
2557                                 not getattr(f, 'nodrop', False):
2558                             self.__logger.info('column %s (%s) in table %s removed: converted to a function !\n',
2559                                                k, f.string, self._table)
2560                             cr.execute('ALTER TABLE "%s" DROP COLUMN "%s" CASCADE' % (self._table, k))
2561                             cr.commit()
2562                             self.__schema.debug("Table '%s': dropped column '%s' with cascade",
2563                                                  self._table, k)
2564                             f_obj_type = None
2565                         else:
2566                             f_obj_type = get_pg_type(f) and get_pg_type(f)[0]
2567
2568                         if f_obj_type:
2569                             ok = False
2570                             casts = [
2571                                 ('text', 'char', 'VARCHAR(%d)' % (f.size or 0,), '::VARCHAR(%d)'%(f.size or 0,)),
2572                                 ('varchar', 'text', 'TEXT', ''),
2573                                 ('int4', 'float', get_pg_type(f)[1], '::'+get_pg_type(f)[1]),
2574                                 ('date', 'datetime', 'TIMESTAMP', '::TIMESTAMP'),
2575                                 ('timestamp', 'date', 'date', '::date'),
2576                                 ('numeric', 'float', get_pg_type(f)[1], '::'+get_pg_type(f)[1]),
2577                                 ('float8', 'float', get_pg_type(f)[1], '::'+get_pg_type(f)[1]),
2578                             ]
2579                             if f_pg_type == 'varchar' and f._type == 'char' and f_pg_size < f.size:
2580                                 cr.execute('ALTER TABLE "%s" RENAME COLUMN "%s" TO temp_change_size' % (self._table, k))
2581                                 cr.execute('ALTER TABLE "%s" ADD COLUMN "%s" VARCHAR(%d)' % (self._table, k, f.size))
2582                                 cr.execute('UPDATE "%s" SET "%s"=temp_change_size::VARCHAR(%d)' % (self._table, k, f.size))
2583                                 cr.execute('ALTER TABLE "%s" DROP COLUMN temp_change_size CASCADE' % (self._table,))
2584                                 cr.commit()
2585                                 self.__schema.debug("Table '%s': column '%s' (type varchar) changed size from %s to %s",
2586                                     self._table, k, f_pg_size, f.size)
2587                             for c in casts:
2588                                 if (f_pg_type==c[0]) and (f._type==c[1]):
2589                                     if f_pg_type != f_obj_type:
2590                                         ok = True
2591                                         cr.execute('ALTER TABLE "%s" RENAME COLUMN "%s" TO temp_change_size' % (self._table, k))
2592                                         cr.execute('ALTER TABLE "%s" ADD COLUMN "%s" %s' % (self._table, k, c[2]))
2593                                         cr.execute(('UPDATE "%s" SET "%s"=temp_change_size'+c[3]) % (self._table, k))
2594                                         cr.execute('ALTER TABLE "%s" DROP COLUMN temp_change_size CASCADE' % (self._table,))
2595                                         cr.commit()
2596                                         self.__schema.debug("Table '%s': column '%s' changed type from %s to %s",
2597                                             self._table, k, c[0], c[1])
2598                                     break
2599
2600                             if f_pg_type != f_obj_type:
2601                                 if not ok:
2602                                     i = 0
2603                                     while True:
2604                                         newname = self._table + '_moved' + str(i)
2605                                         cr.execute("SELECT count(1) FROM pg_class c,pg_attribute a " \
2606                                             "WHERE c.relname=%s " \
2607                                             "AND a.attname=%s " \
2608                                             "AND c.oid=a.attrelid ", (self._table, newname))
2609                                         if not cr.fetchone()[0]:
2610                                             break
2611                                         i += 1
2612                                     if f_pg_notnull:
2613                                         cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" DROP NOT NULL' % (self._table, k))
2614                                     cr.execute('ALTER TABLE "%s" RENAME COLUMN "%s" TO "%s"' % (self._table, k, newname))
2615                                     cr.execute('ALTER TABLE "%s" ADD COLUMN "%s" %s' % (self._table, k, get_pg_type(f)[1]))
2616                                     cr.execute("COMMENT ON COLUMN %s.%s IS '%s'" % (self._table, k, f.string.replace("'", "''")))
2617                                     self.__schema.debug("Table '%s': column '%s' has changed type (DB=%s, def=%s), data moved to column %s !",
2618                                         self._table, k, f_pg_type, f._type, newname)
2619
2620                             # if the field is required and hasn't got a NOT NULL constraint
2621                             if f.required and f_pg_notnull == 0:
2622                                 # set the field to the default value if any
2623                                 if k in self._defaults:
2624                                     if callable(self._defaults[k]):
2625                                         default = self._defaults[k](self, cr, 1, context)
2626                                     else:
2627                                         default = self._defaults[k]
2628
2629                                     if (default is not None):
2630                                         ss = self._columns[k]._symbol_set
2631                                         query = 'UPDATE "%s" SET "%s"=%s WHERE "%s" is NULL' % (self._table, k, ss[0], k)
2632                                         cr.execute(query, (ss[1](default),))
2633                                 # add the NOT NULL constraint
2634                                 cr.commit()
2635                                 try:
2636                                     cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" SET NOT NULL' % (self._table, k))
2637                                     cr.commit()
2638                                     self.__schema.debug("Table '%s': column '%s': added NOT NULL constraint",
2639                                                         self._table, k)
2640                                 except Exception:
2641                                     msg = "Table '%s': unable to set a NOT NULL constraint on column '%s' !\n"\
2642                                         "If you want to have it, you should update the records and execute manually:\n"\
2643                                         "ALTER TABLE %s ALTER COLUMN %s SET NOT NULL"
2644                                     self.__schema.warn(msg, self._table, k, self._table, k)
2645                                 cr.commit()
2646                             elif not f.required and f_pg_notnull == 1:
2647                                 cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" DROP NOT NULL' % (self._table, k))
2648                                 cr.commit()
2649                                 self.__schema.debug("Table '%s': column '%s': dropped NOT NULL constraint",
2650                                                     self._table, k)
2651                             # Verify index
2652                             indexname = '%s_%s_index' % (self._table, k)
2653                             cr.execute("SELECT indexname FROM pg_indexes WHERE indexname = %s and tablename = %s", (indexname, self._table))
2654                             res2 = cr.dictfetchall()
2655                             if not res2 and f.select:
2656                                 cr.execute('CREATE INDEX "%s_%s_index" ON "%s" ("%s")' % (self._table, k, self._table, k))
2657                                 cr.commit()
2658                                 if f._type == 'text':
2659                                     # FIXME: for fields.text columns we should try creating GIN indexes instead (seems most suitable for an ERP context)
2660                                     msg = "Table '%s': Adding (b-tree) index for text column '%s'."\
2661                                         "This is probably useless (does not work for fulltext search) and prevents INSERTs of long texts"\
2662                                         " because there is a length limit for indexable btree values!\n"\
2663                                         "Use a search view instead if you simply want to make the field searchable."
2664                                     self.__schema.warn(msg, self._table, k, f._type)
2665                             if res2 and not f.select:
2666                                 cr.execute('DROP INDEX "%s_%s_index"' % (self._table, k))
2667                                 cr.commit()
2668                                 msg = "Table '%s': dropping index for column '%s' of type '%s' as it is not required anymore"
2669                                 self.__schema.warn(msg, self._table, k, f._type)
2670
2671                             if isinstance(f, fields.many2one):
2672                                 ref = self.pool.get(f._obj)._table
2673                                 if ref != 'ir_actions':
2674                                     cr.execute('SELECT confdeltype, conname FROM pg_constraint as con, pg_class as cl1, pg_class as cl2, '
2675                                                 'pg_attribute as att1, pg_attribute as att2 '
2676                                             'WHERE con.conrelid = cl1.oid '
2677                                                 'AND cl1.relname = %s '
2678                                                 'AND con.confrelid = cl2.oid '
2679                                                 'AND cl2.relname = %s '
2680                                                 'AND array_lower(con.conkey, 1) = 1 '
2681                                                 'AND con.conkey[1] = att1.attnum '
2682                                                 'AND att1.attrelid = cl1.oid '
2683                                                 'AND att1.attname = %s '
2684                                                 'AND array_lower(con.confkey, 1) = 1 '
2685                                                 'AND con.confkey[1] = att2.attnum '
2686                                                 'AND att2.attrelid = cl2.oid '
2687                                                 'AND att2.attname = %s '
2688                                                 "AND con.contype = 'f'", (self._table, ref, k, 'id'))
2689                                     res2 = cr.dictfetchall()
2690                                     if res2:
2691                                         if res2[0]['confdeltype'] != POSTGRES_CONFDELTYPES.get(f.ondelete.upper(), 'a'):
2692                                             cr.execute('ALTER TABLE "' + self._table + '" DROP CONSTRAINT "' + res2[0]['conname'] + '"')
2693                                             cr.execute('ALTER TABLE "' + self._table + '" ADD FOREIGN KEY ("' + k + '") REFERENCES "' + ref + '" ON DELETE ' + f.ondelete)
2694                                             cr.commit()
2695                                             self.__schema.debug("Table '%s': column '%s': XXX",
2696                                                 self._table, k)
2697                     elif len(res) > 1:
2698                         netsvc.Logger().notifyChannel('orm', netsvc.LOG_ERROR, "Programming error, column %s->%s has multiple instances !" % (self._table, k))
2699                     if not res:
2700                         if not isinstance(f, fields.function) or f.store:
2701                             # add the missing field
2702                             cr.execute('ALTER TABLE "%s" ADD COLUMN "%s" %s' % (self._table, k, get_pg_type(f)[1]))
2703                             cr.execute("COMMENT ON COLUMN %s.%s IS '%s'" % (self._table, k, f.string.replace("'", "''")))
2704                             self.__schema.debug("Table '%s': added column '%s' with definition=%s",
2705                                 self._table, k, get_pg_type(f)[1])
2706
2707                             # initialize it
2708                             if not create and k in self._defaults:
2709                                 if callable(self._defaults[k]):
2710                                     default = self._defaults[k](self, cr, 1, context)
2711                                 else:
2712                                     default = self._defaults[k]
2713
2714                                 ss = self._columns[k]._symbol_set
2715                                 query = 'UPDATE "%s" SET "%s"=%s' % (self._table, k, ss[0])
2716                                 cr.execute(query, (ss[1](default),))
2717                                 cr.commit()
2718                                 netsvc.Logger().notifyChannel('data', netsvc.LOG_DEBUG, "Table '%s': setting default value of new column %s" % (self._table, k))
2719
2720                             if isinstance(f, fields.function):
2721                                 order = 10
2722                                 if f.store is not True:
2723                                     order = f.store[f.store.keys()[0]][2]
2724                                 todo_update_store.append((order, f, k))
2725
2726                             # and add constraints if needed
2727                             if isinstance(f, fields.many2one):
2728                                 if not self.pool.get(f._obj):
2729                                     raise except_orm('Programming Error', ('There is no reference available for %s') % (f._obj,))
2730                                 ref = self.pool.get(f._obj)._table
2731 #                                ref = f._obj.replace('.', '_')
2732                                 # ir_actions is inherited so foreign key doesn't work on it
2733                                 if ref != 'ir_actions':
2734                                     cr.execute('ALTER TABLE "%s" ADD FOREIGN KEY ("%s") REFERENCES "%s" ON DELETE %s' % (self._table, k, ref, f.ondelete))
2735                                     self.__schema.debug("Table '%s': added foreign key '%s' with definition=REFERENCES \"%s\" ON DELETE %s",
2736                                         self._table, k, ref, f.ondelete)
2737                             if f.select:
2738                                 cr.execute('CREATE INDEX "%s_%s_index" ON "%s" ("%s")' % (self._table, k, self._table, k))
2739                             if f.required:
2740                                 try:
2741                                     cr.commit()
2742                                     cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" SET NOT NULL' % (self._table, k))
2743                                     self.__schema.debug("Table '%s': column '%s': added a NOT NULL constraint",
2744                                         self._table, k)
2745                                 except Exception:
2746                                     msg = "WARNING: unable to set column %s of table %s not null !\n"\
2747                                         "Try to re-run: openerp-server.py --update=module\n"\
2748                                         "If it doesn't work, update records and execute manually:\n"\
2749                                         "ALTER TABLE %s ALTER COLUMN %s SET NOT NULL"
2750                                     self.__logger.warn(msg, k, self._table, self._table, k)
2751                             cr.commit()
2752             for order, f, k in todo_update_store:
2753                 todo_end.append((order, self._update_store, (f, k)))
2754
2755         else:
2756             cr.execute("SELECT relname FROM pg_class WHERE relkind IN ('r','v') AND relname=%s", (self._table,))
2757             create = not bool(cr.fetchone())
2758
2759         cr.commit()     # start a new transaction
2760
2761         for (key, con, _) in self._sql_constraints:
2762             conname = '%s_%s' % (self._table, key)
2763
2764             cr.execute("SELECT conname, pg_catalog.pg_get_constraintdef(oid, true) as condef FROM pg_constraint where conname=%s", (conname,))
2765             existing_constraints = cr.dictfetchall()
2766
2767             sql_actions = {
2768                 'drop': {
2769                     'execute': False,
2770                     'query': 'ALTER TABLE "%s" DROP CONSTRAINT "%s"' % (self._table, conname, ),
2771                     'msg_ok': "Table '%s': dropped constraint '%s'. Reason: its definition changed from '%%s' to '%s'" % (
2772                         self._table, conname, con),
2773                     'msg_err': "Table '%s': unable to drop \'%s\' constraint !" % (self._table, con),
2774                     'order': 1,
2775                 },
2776                 'add': {
2777                     'execute': False,
2778                     'query': 'ALTER TABLE "%s" ADD CONSTRAINT "%s" %s' % (self._table, conname, con,),
2779                     'msg_ok': "Table '%s': added constraint '%s' with definition=%s" % (self._table, conname, con),
2780                     'msg_err': "Table '%s': unable to add \'%s\' constraint !\n If you want to have it, you should update the records and execute manually:\n%%s" % (
2781                         self._table, con),
2782                     'order': 2,
2783                 },
2784             }
2785
2786             if not existing_constraints:
2787                 # constraint does not exists:
2788                 sql_actions['add']['execute'] = True
2789                 sql_actions['add']['msg_err'] = sql_actions['add']['msg_err'] % (sql_actions['add']['query'], )
2790             elif con.lower() not in [item['condef'].lower() for item in existing_constraints]:
2791                 # constraint exists but its definition has changed:
2792                 sql_actions['drop']['execute'] = True
2793                 sql_actions['drop']['msg_ok'] = sql_actions['drop']['msg_ok'] % (existing_constraints[0]['condef'].lower(), )
2794                 sql_actions['add']['execute'] = True
2795                 sql_actions['add']['msg_err'] = sql_actions['add']['msg_err'] % (sql_actions['add']['query'], )
2796
2797             # we need to add the constraint:
2798             sql_actions = [item for item in sql_actions.values()]
2799             sql_actions.sort(key=lambda x: x['order'])
2800             for sql_action in [action for action in sql_actions if action['execute']]:
2801                 try:
2802                     cr.execute(sql_action['query'])
2803                     cr.commit()
2804                     self.__schema.debug(sql_action['msg_ok'])
2805                 except:
2806                     self.__schema.warn(sql_action['msg_err'])
2807                     cr.rollback()
2808
2809         if create:
2810             if hasattr(self, "_sql"):
2811                 for line in self._sql.split(';'):
2812                     line2 = line.replace('\n', '').strip()
2813                     if line2:
2814                         cr.execute(line2)
2815                         cr.commit()
2816         if store_compute:
2817             self._parent_store_compute(cr)
2818             cr.commit()
2819         return todo_end
2820
2821     def __init__(self, cr):
2822         super(orm, self).__init__(cr)
2823
2824         if not hasattr(self, '_log_access'):
2825             # if not access is not specify, it is the same value as _auto
2826             self._log_access = getattr(self, "_auto", True)
2827
2828         self._columns = self._columns.copy()
2829         for store_field in self._columns:
2830             f = self._columns[store_field]
2831             if hasattr(f, 'digits_change'):
2832                 f.digits_change(cr)
2833             if not isinstance(f, fields.function):
2834                 continue
2835             if not f.store:
2836                 continue
2837             if self._columns[store_field].store is True:
2838                 sm = {self._name: (lambda self, cr, uid, ids, c={}: ids, None, 10, None)}
2839             else:
2840                 sm = self._columns[store_field].store
2841             for object, aa in sm.items():
2842                 if len(aa) == 4:
2843                     (fnct, fields2, order, length) = aa
2844                 elif len(aa) == 3:
2845                     (fnct, fields2, order) = aa
2846                     length = None
2847                 else:
2848                     raise except_orm('Error',
2849                         ('Invalid function definition %s in object %s !\nYou must use the definition: store={object:(fnct, fields, priority, time length)}.' % (store_field, self._name)))
2850                 self.pool._store_function.setdefault(object, [])
2851                 ok = True
2852                 for x, y, z, e, f, l in self.pool._store_function[object]:
2853                     if (x==self._name) and (y==store_field) and (e==fields2):
2854                         if f == order:
2855                             ok = False
2856                 if ok:
2857                     self.pool._store_function[object].append( (self._name, store_field, fnct, fields2, order, length))
2858                     self.pool._store_function[object].sort(lambda x, y: cmp(x[4], y[4]))
2859
2860         for (key, _, msg) in self._sql_constraints:
2861             self.pool._sql_error[self._table+'_'+key] = msg
2862
2863         # Load manual fields
2864
2865         cr.execute("SELECT id FROM ir_model_fields WHERE name=%s AND model=%s", ('state', 'ir.model.fields'))
2866         if cr.fetchone():
2867             cr.execute('SELECT * FROM ir_model_fields WHERE model=%s AND state=%s', (self._name, 'manual'))
2868             for field in cr.dictfetchall():
2869                 if field['name'] in self._columns:
2870                     continue
2871                 attrs = {
2872                     'string': field['field_description'],
2873                     'required': bool(field['required']),
2874                     'readonly': bool(field['readonly']),
2875                     'domain': field['domain'] or None,
2876                     'size': field['size'],
2877                     'ondelete': field['on_delete'],
2878                     'translate': (field['translate']),
2879                     #'select': int(field['select_level'])
2880                 }
2881
2882                 if field['ttype'] == 'selection':
2883                     self._columns[field['name']] = getattr(fields, field['ttype'])(eval(field['selection']), **attrs)
2884                 elif field['ttype'] == 'reference':
2885                     self._columns[field['name']] = getattr(fields, field['ttype'])(selection=eval(field['selection']), **attrs)
2886                 elif field['ttype'] == 'many2one':
2887                     self._columns[field['name']] = getattr(fields, field['ttype'])(field['relation'], **attrs)
2888                 elif field['ttype'] == 'one2many':
2889                     self._columns[field['name']] = getattr(fields, field['ttype'])(field['relation'], field['relation_field'], **attrs)
2890                 elif field['ttype'] == 'many2many':
2891                     _rel1 = field['relation'].replace('.', '_')
2892                     _rel2 = field['model'].replace('.', '_')
2893                     _rel_name = 'x_%s_%s_%s_rel' % (_rel1, _rel2, field['name'])
2894                     self._columns[field['name']] = getattr(fields, field['ttype'])(field['relation'], _rel_name, 'id1', 'id2', **attrs)
2895                 else:
2896                     self._columns[field['name']] = getattr(fields, field['ttype'])(**attrs)
2897         self._inherits_check()
2898         self._inherits_reload()
2899         if not self._sequence:
2900             self._sequence = self._table + '_id_seq'
2901         for k in self._defaults:
2902             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,)
2903         for f in self._columns:
2904             self._columns[f].restart()
2905
2906     #
2907     # Update objects that uses this one to update their _inherits fields
2908     #
2909
2910     def _inherits_reload_src(self):
2911         for obj in self.pool.obj_pool.values():
2912             if self._name in obj._inherits:
2913                 obj._inherits_reload()
2914
2915     def _inherits_reload(self):
2916         res = {}
2917         for table in self._inherits:
2918             res.update(self.pool.get(table)._inherit_fields)
2919             for col in self.pool.get(table)._columns.keys():
2920                 res[col] = (table, self._inherits[table], self.pool.get(table)._columns[col])
2921             for col in self.pool.get(table)._inherit_fields.keys():
2922                 res[col] = (table, self._inherits[table], self.pool.get(table)._inherit_fields[col][2])
2923         self._inherit_fields = res
2924         self._inherits_reload_src()
2925
2926     def _inherits_check(self):
2927         for table, field_name in self._inherits.items():
2928             if field_name not in self._columns:
2929                 logging.getLogger('init').info('Missing many2one field definition for _inherits reference "%s" in "%s", using default one.' % (field_name, self._name))
2930                 self._columns[field_name] = fields.many2one(table, string="Automatically created field to link to parent %s" % table,
2931                                                              required=True, ondelete="cascade")
2932             elif not self._columns[field_name].required or self._columns[field_name].ondelete.lower() != "cascade":
2933                 logging.getLogger('init').warning('Field definition for _inherits reference "%s" in "%s" must be marked as "required" with ondelete="cascade", forcing it.' % (field_name, self._name))
2934                 self._columns[field_name].required = True
2935                 self._columns[field_name].ondelete = "cascade"
2936
2937     #def __getattr__(self, name):
2938     #    """
2939     #    Proxies attribute accesses to the `inherits` parent so we can call methods defined on the inherited parent
2940     #    (though inherits doesn't use Python inheritance).
2941     #    Handles translating between local ids and remote ids.
2942     #    Known issue: doesn't work correctly when using python's own super(), don't involve inherit-based inheritance
2943     #                 when you have inherits.
2944     #    """
2945     #    for model, field in self._inherits.iteritems():
2946     #        proxy = self.pool.get(model)
2947     #        if hasattr(proxy, name):
2948     #            attribute = getattr(proxy, name)
2949     #            if not hasattr(attribute, '__call__'):
2950     #                return attribute
2951     #            break
2952     #    else:
2953     #        return super(orm, self).__getattr__(name)
2954
2955     #    def _proxy(cr, uid, ids, *args, **kwargs):
2956     #        objects = self.browse(cr, uid, ids, kwargs.get('context', None))
2957     #        lst = [obj[field].id for obj in objects if obj[field]]
2958     #        return getattr(proxy, name)(cr, uid, lst, *args, **kwargs)
2959
2960     #    return _proxy
2961
2962
2963     def fields_get(self, cr, user, fields=None, context=None):
2964         """
2965         Get the description of list of fields
2966
2967         :param cr: database cursor
2968         :param user: current user id
2969         :param fields: list of fields
2970         :param context: context arguments, like lang, time zone
2971         :return: dictionary of field dictionaries, each one describing a field of the business object
2972         :raise AccessError: * if user has no create/write rights on the requested object
2973
2974         """
2975         ira = self.pool.get('ir.model.access')
2976         write_access = ira.check(cr, user, self._name, 'write', raise_exception=False, context=context) or \
2977                        ira.check(cr, user, self._name, 'create', raise_exception=False, context=context)
2978         return super(orm, self).fields_get(cr, user, fields, context, write_access)
2979
2980     def read(self, cr, user, ids, fields=None, context=None, load='_classic_read'):
2981         if not context:
2982             context = {}
2983         self.pool.get('ir.model.access').check(cr, user, self._name, 'read', context=context)
2984         if not fields:
2985             fields = self._columns.keys() + self._inherit_fields.keys()
2986         if isinstance(ids, (int, long)):
2987             select = [ids]
2988         else:
2989             select = ids
2990         select = map(lambda x: isinstance(x, dict) and x['id'] or x, select)
2991         result = self._read_flat(cr, user, select, fields, context, load)
2992
2993         for r in result:
2994             for key, v in r.items():
2995                 if v is None:
2996                     r[key] = False
2997
2998         if isinstance(ids, (int, long, dict)):
2999             return result and result[0] or False
3000         return result
3001
3002     def _read_flat(self, cr, user, ids, fields_to_read, context=None, load='_classic_read'):
3003         if not context:
3004             context = {}
3005         if not ids:
3006             return []
3007         if fields_to_read == None:
3008             fields_to_read = self._columns.keys()
3009
3010         # Construct a clause for the security rules.
3011         # 'tables' hold the list of tables necessary for the SELECT including the ir.rule clauses,
3012         # or will at least contain self._table.
3013         rule_clause, rule_params, tables = self.pool.get('ir.rule').domain_get(cr, user, self._name, 'read', context=context)
3014
3015         # all inherited fields + all non inherited fields for which the attribute whose name is in load is True
3016         fields_pre = [f for f in fields_to_read if
3017                            f == self.CONCURRENCY_CHECK_FIELD
3018                         or (f in self._columns and getattr(self._columns[f], '_classic_write'))
3019                      ] + self._inherits.values()
3020
3021         res = []
3022         if len(fields_pre):
3023             def convert_field(f):
3024                 f_qual = "%s.%s" % (self._table, f) # need fully-qualified references in case len(tables) > 1
3025                 if f in ('create_date', 'write_date'):
3026                     return "date_trunc('second', %s) as %s" % (f_qual, f)
3027                 if f == self.CONCURRENCY_CHECK_FIELD:
3028                     if self._log_access:
3029                         return "COALESCE(%s.write_date, %s.create_date, now())::timestamp AS %s" % (self._table, self._table, f,)
3030                     return "now()::timestamp AS %s" % (f,)
3031                 if isinstance(self._columns[f], fields.binary) and context.get('bin_size', False):
3032                     return 'length(%s) as "%s"' % (f_qual, f)
3033                 return f_qual
3034
3035             fields_pre2 = map(convert_field, fields_pre)
3036             order_by = self._parent_order or self._order
3037             select_fields = ','.join(fields_pre2 + [self._table + '.id'])
3038             query = 'SELECT %s FROM %s WHERE %s.id IN %%s' % (select_fields, ','.join(tables), self._table)
3039             if rule_clause:
3040                 query += " AND " + (' OR '.join(rule_clause))
3041             query += " ORDER BY " + order_by
3042             for sub_ids in cr.split_for_in_conditions(ids):
3043                 if rule_clause:
3044                     cr.execute(query, [tuple(sub_ids)] + rule_params)
3045                     if cr.rowcount != len(sub_ids):
3046                         raise except_orm(_('AccessError'),
3047                                          _('Operation prohibited by access rules, or performed on an already deleted document (Operation: read, Document type: %s).')
3048                                          % (self._description,))
3049                 else:
3050                     cr.execute(query, (tuple(sub_ids),))
3051                 res.extend(cr.dictfetchall())
3052         else:
3053             res = map(lambda x: {'id': x}, ids)
3054
3055 #        if not res:
3056 #            res = map(lambda x: {'id': x}, ids)
3057 #            for record in res:
3058 #                for f in fields_to_read:
3059 #                    field_val = False
3060 #                    if f in self._columns.keys():
3061 #                        ftype = self._columns[f]._type
3062 #                    elif f in self._inherit_fields.keys():
3063 #                        ftype = self._inherit_fields[f][2]._type
3064 #                    else:
3065 #                        continue
3066 #                    if ftype in ('one2many', 'many2many'):
3067 #                        field_val = []
3068 #                    record.update({f:field_val})
3069
3070         for f in fields_pre:
3071             if f == self.CONCURRENCY_CHECK_FIELD:
3072                 continue
3073             if self._columns[f].translate:
3074                 ids = [x['id'] for x in res]
3075                 #TODO: optimize out of this loop
3076                 res_trans = self.pool.get('ir.translation')._get_ids(cr, user, self._name+','+f, 'model', context.get('lang', False) or 'en_US', ids)
3077                 for r in res:
3078                     r[f] = res_trans.get(r['id'], False) or r[f]
3079
3080         for table in self._inherits:
3081             col = self._inherits[table]
3082             cols = intersect(self._inherit_fields.keys(), set(fields_to_read) - set(self._columns.keys()))
3083             if not cols:
3084                 continue
3085             res2 = self.pool.get(table).read(cr, user, [x[col] for x in res], cols, context, load)
3086
3087             res3 = {}
3088             for r in res2:
3089                 res3[r['id']] = r
3090                 del r['id']
3091
3092             for record in res:
3093                 if not record[col]: # if the record is deleted from _inherits table?
3094                     continue
3095                 record.update(res3[record[col]])
3096                 if col not in fields_to_read:
3097                     del record[col]
3098
3099         # all fields which need to be post-processed by a simple function (symbol_get)
3100         fields_post = filter(lambda x: x in self._columns and self._columns[x]._symbol_get, fields_to_read)
3101         if fields_post:
3102             for r in res:
3103                 for f in fields_post:
3104                     r[f] = self._columns[f]._symbol_get(r[f])
3105         ids = [x['id'] for x in res]
3106
3107         # all non inherited fields for which the attribute whose name is in load is False
3108         fields_post = filter(lambda x: x in self._columns and not getattr(self._columns[x], load), fields_to_read)
3109
3110         # Compute POST fields
3111         todo = {}
3112         for f in fields_post:
3113             todo.setdefault(self._columns[f]._multi, [])
3114             todo[self._columns[f]._multi].append(f)
3115         for key, val in todo.items():
3116             if key:
3117                 res2 = self._columns[val[0]].get(cr, self, ids, val, user, context=context, values=res)
3118                 for pos in val:
3119                     for record in res:
3120                         if isinstance(res2[record['id']], str): res2[record['id']] = eval(res2[record['id']]) #TOCHECK : why got string instend of dict in python2.6
3121                         multi_fields = res2.get(record['id'],{})
3122                         if multi_fields:
3123                             record[pos] = multi_fields.get(pos,[])
3124             else:
3125                 for f in val:
3126                     res2 = self._columns[f].get(cr, self, ids, f, user, context=context, values=res)
3127                     for record in res:
3128                         if res2:
3129                             record[f] = res2[record['id']]
3130                         else:
3131                             record[f] = []
3132         readonly = None
3133         for vals in res:
3134             for field in vals.copy():
3135                 fobj = None
3136                 if field in self._columns:
3137                     fobj = self._columns[field]
3138
3139                 if not fobj:
3140                     continue
3141                 groups = fobj.read
3142                 if groups:
3143                     edit = False
3144                     for group in groups:
3145                         module = group.split(".")[0]
3146                         grp = group.split(".")[1]
3147                         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"  \
3148                                    (grp, module, 'res.groups', user))
3149                         readonly = cr.fetchall()
3150                         if readonly[0][0] >= 1:
3151                             edit = True
3152                             break
3153                         elif readonly[0][0] == 0:
3154                             edit = False
3155                         else:
3156                             edit = False
3157
3158                     if not edit:
3159                         if type(vals[field]) == type([]):
3160                             vals[field] = []
3161                         elif type(vals[field]) == type(0.0):
3162                             vals[field] = 0
3163                         elif type(vals[field]) == type(''):
3164                             vals[field] = '=No Permission='
3165                         else:
3166                             vals[field] = False
3167         return res
3168
3169     def perm_read(self, cr, user, ids, context=None, details=True):
3170         """
3171         Returns some metadata about the given records.
3172
3173         :param details: if True, \*_uid fields are replaced with the name of the user
3174         :return: list of ownership dictionaries for each requested record
3175         :rtype: list of dictionaries with the following keys:
3176
3177                     * id: object id
3178                     * create_uid: user who created the record
3179                     * create_date: date when the record was created
3180                     * write_uid: last user who changed the record
3181                     * write_date: date of the last change to the record
3182                     * xmlid: XML ID to use to refer to this record (if there is one), in format ``module.name``
3183         """
3184         if not context:
3185             context = {}
3186         if not ids:
3187             return []
3188         fields = ''
3189         uniq = isinstance(ids, (int, long))
3190         if uniq:
3191             ids = [ids]
3192         fields = ['id']
3193         if self._log_access:
3194             fields += ['create_uid', 'create_date', 'write_uid', 'write_date']
3195         quoted_table = '"%s"' % self._table
3196         fields_str = ",".join('%s.%s'%(quoted_table, field) for field in fields)
3197         query = '''SELECT %s, __imd.module, __imd.name
3198                    FROM %s LEFT JOIN ir_model_data __imd
3199                        ON (__imd.model = %%s and __imd.res_id = %s.id)
3200                    WHERE %s.id IN %%s''' % (fields_str, quoted_table, quoted_table, quoted_table)
3201         cr.execute(query, (self._name, tuple(ids)))
3202         res = cr.dictfetchall()
3203         for r in res:
3204             for key in r:
3205                 r[key] = r[key] or False
3206                 if details and key in ('write_uid', 'create_uid'):
3207                     if r[key]:
3208                         r[key] = self.pool.get('res.users').name_get(cr, user, [r[key]])[0]
3209             r['xmlid'] = ("%(module)s.%(name)s" % r) if r['name'] else False
3210             del r['name'], r['module']
3211         if uniq:
3212             return res[ids[0]]
3213         return res
3214
3215     def _check_concurrency(self, cr, ids, context):
3216         if not context:
3217             return
3218         if not (context.get(self.CONCURRENCY_CHECK_FIELD) and self._log_access):
3219             return
3220         check_clause = "(id = %s AND %s < COALESCE(write_date, create_date, now())::timestamp)"
3221         for sub_ids in cr.split_for_in_conditions(ids):
3222             ids_to_check = []
3223             for id in sub_ids:
3224                 id_ref = "%s,%s" % (self._name, id)
3225                 update_date = context[self.CONCURRENCY_CHECK_FIELD].pop(id_ref, None)
3226                 if update_date:
3227                     ids_to_check.extend([id, update_date])
3228             if not ids_to_check:
3229                 continue
3230             cr.execute("SELECT id FROM %s WHERE %s" % (self._table, " OR ".join([check_clause]*(len(ids_to_check)/2))), tuple(ids_to_check))
3231             res = cr.fetchone()
3232             if res:
3233                 # mention the first one only to keep the error message readable
3234                 raise except_orm('ConcurrencyException', _('A document was modified since you last viewed it (%s:%d)') % (self._description, res[0]))
3235
3236     def check_access_rule(self, cr, uid, ids, operation, context=None):
3237         """Verifies that the operation given by ``operation`` is allowed for the user
3238            according to ir.rules.
3239
3240            :param operation: one of ``write``, ``unlink``
3241            :raise except_orm: * if current ir.rules do not permit this operation.
3242            :return: None if the operation is allowed
3243         """
3244         where_clause, where_params, tables = self.pool.get('ir.rule').domain_get(cr, uid, self._name, operation, context=context)
3245         if where_clause:
3246             where_clause = ' and ' + ' and '.join(where_clause)
3247             for sub_ids in cr.split_for_in_conditions(ids):
3248                 cr.execute('SELECT ' + self._table + '.id FROM ' + ','.join(tables) +
3249                            ' WHERE ' + self._table + '.id IN %s' + where_clause,
3250                            [sub_ids] + where_params)
3251                 if cr.rowcount != len(sub_ids):
3252                     raise except_orm(_('AccessError'),
3253                                      _('Operation prohibited by access rules, or performed on an already deleted document (Operation: %s, Document type: %s).')
3254                                      % (operation, self._description))
3255
3256     def unlink(self, cr, uid, ids, context=None):
3257         """
3258         Delete records with given ids
3259
3260         :param cr: database cursor
3261         :param uid: current user id
3262         :param ids: id or list of ids
3263         :param context: (optional) context arguments, like lang, time zone
3264         :return: True
3265         :raise AccessError: * if user has no unlink rights on the requested object
3266                             * if user tries to bypass access rules for unlink on the requested object
3267         :raise UserError: if the record is default property for other records
3268
3269         """
3270         if not ids:
3271             return True
3272         if isinstance(ids, (int, long)):
3273             ids = [ids]
3274
3275         result_store = self._store_get_values(cr, uid, ids, None, context)
3276
3277         self._check_concurrency(cr, ids, context)
3278
3279         self.pool.get('ir.model.access').check(cr, uid, self._name, 'unlink', context=context)
3280
3281         properties = self.pool.get('ir.property')
3282         domain = [('res_id', '=', False),
3283                   ('value_reference', 'in', ['%s,%s' % (self._name, i) for i in ids]),
3284                  ]
3285         if properties.search(cr, uid, domain, context=context):
3286             raise except_orm(_('Error'), _('Unable to delete this document because it is used as a default property'))
3287
3288         wf_service = netsvc.LocalService("workflow")
3289         for oid in ids:
3290             wf_service.trg_delete(uid, self._name, oid, cr)
3291
3292
3293         self.check_access_rule(cr, uid, ids, 'unlink', context=context)
3294         for sub_ids in cr.split_for_in_conditions(ids):
3295             cr.execute('delete from ' + self._table + ' ' \
3296                        'where id IN %s', (sub_ids,))
3297         for order, object, store_ids, fields in result_store:
3298             if object != self._name:
3299                 obj = self.pool.get(object)
3300                 cr.execute('select id from '+obj._table+' where id IN %s', (tuple(store_ids),))
3301                 rids = map(lambda x: x[0], cr.fetchall())
3302                 if rids:
3303                     obj._store_set_values(cr, uid, rids, fields, context)
3304         return True
3305
3306     #
3307     # TODO: Validate
3308     #
3309     def write(self, cr, user, ids, vals, context=None):
3310         """
3311         Update records with given ids with the given field values
3312
3313         :param cr: database cursor
3314         :param user: current user id
3315         :type user: integer
3316         :param ids: object id or list of object ids to update according to **vals**
3317         :param vals: field values to update, e.g {'field_name': new_field_value, ...}
3318         :type vals: dictionary
3319         :param context: (optional) context arguments, e.g. {'lang': 'en_us', 'tz': 'UTC', ...}
3320         :type context: dictionary
3321         :return: True
3322         :raise AccessError: * if user has no write rights on the requested object
3323                             * if user tries to bypass access rules for write on the requested object
3324         :raise ValidateError: if user tries to enter invalid value for a field that is not in selection
3325         :raise UserError: if a loop would be created in a hierarchy of objects a result of the operation (such as setting an object as its own parent)
3326
3327         **Note**: The type of field values to pass in ``vals`` for relationship fields is specific:
3328
3329             + For a many2many field, a list of tuples is expected.
3330               Here is the list of tuple that are accepted, with the corresponding semantics ::
3331
3332                  (0, 0,  { values })    link to a new record that needs to be created with the given values dictionary
3333                  (1, ID, { values })    update the linked record with id = ID (write *values* on it)
3334                  (2, ID)                remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well)
3335                  (3, ID)                cut the link to the linked record with id = ID (delete the relationship between the two objects but does not delete the target object itself)
3336                  (4, ID)                link to existing record with id = ID (adds a relationship)
3337                  (5)                    unlink all (like using (3,ID) for all linked records)
3338                  (6, 0, [IDs])          replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs)
3339
3340                  Example:
3341                     [(6, 0, [8, 5, 6, 4])] sets the many2many to ids [8, 5, 6, 4]
3342
3343             + For a one2many field, a lits of tuples is expected.
3344               Here is the list of tuple that are accepted, with the corresponding semantics ::
3345
3346                  (0, 0,  { values })    link to a new record that needs to be created with the given values dictionary
3347                  (1, ID, { values })    update the linked record with id = ID (write *values* on it)
3348                  (2, ID)                remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well)
3349
3350                  Example:
3351                     [(0, 0, {'field_name':field_value_record1, ...}), (0, 0, {'field_name':field_value_record2, ...})]
3352
3353             + For a many2one field, simply use the ID of target record, which must already exist, or ``False`` to remove the link.
3354             + For a reference field, use a string with the model name, a comma, and the target object id (example: ``'product.product, 5'``)
3355
3356         """
3357         readonly = None
3358         for field in vals.copy():
3359             fobj = None
3360             if field in self._columns:
3361                 fobj = self._columns[field]
3362             else:
3363                 fobj = self._inherit_fields[field][2]
3364             if not fobj:
3365                 continue
3366             groups = fobj.write
3367
3368             if groups:
3369                 edit = False
3370                 for group in groups:
3371                     module = group.split(".")[0]
3372                     grp = group.split(".")[1]
3373                     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", \
3374                                (grp, module, 'res.groups', user))
3375                     readonly = cr.fetchall()
3376                     if readonly[0][0] >= 1:
3377                         edit = True
3378                         break
3379                     elif readonly[0][0] == 0:
3380                         edit = False
3381                     else:
3382                         edit = False
3383
3384                 if not edit:
3385                     vals.pop(field)
3386
3387         if not context:
3388             context = {}
3389         if not ids:
3390             return True
3391         if isinstance(ids, (int, long)):
3392             ids = [ids]
3393
3394         self._check_concurrency(cr, ids, context)
3395         self.pool.get('ir.model.access').check(cr, user, self._name, 'write', context=context)
3396
3397         result = self._store_get_values(cr, user, ids, vals.keys(), context) or []
3398
3399         # No direct update of parent_left/right
3400         vals.pop('parent_left', None)
3401         vals.pop('parent_right', None)
3402
3403         parents_changed = []
3404         if self._parent_store and (self._parent_name in vals):
3405             # The parent_left/right computation may take up to
3406             # 5 seconds. No need to recompute the values if the
3407             # parent is the same. Get the current value of the parent
3408             parent_val = vals[self._parent_name]
3409             if parent_val:
3410                 query = "SELECT id FROM %s WHERE id IN %%s AND (%s != %%s OR %s IS NULL)" % \
3411                                 (self._table, self._parent_name, self._parent_name)
3412                 cr.execute(query, (tuple(ids), parent_val))
3413             else:
3414                 query = "SELECT id FROM %s WHERE id IN %%s AND (%s IS NOT NULL)" % \
3415                                 (self._table, self._parent_name)
3416                 cr.execute(query, (tuple(ids),))
3417             parents_changed = map(operator.itemgetter(0), cr.fetchall())
3418
3419         upd0 = []
3420         upd1 = []
3421         upd_todo = []
3422         updend = []
3423         direct = []
3424         totranslate = context.get('lang', False) and (context['lang'] != 'en_US')
3425         for field in vals:
3426             if field in self._columns:
3427                 if self._columns[field]._classic_write and not (hasattr(self._columns[field], '_fnct_inv')):
3428                     if (not totranslate) or not self._columns[field].translate:
3429                         upd0.append('"'+field+'"='+self._columns[field]._symbol_set[0])
3430                         upd1.append(self._columns[field]._symbol_set[1](vals[field]))
3431                     direct.append(field)
3432                 else:
3433                     upd_todo.append(field)
3434             else:
3435                 updend.append(field)
3436             if field in self._columns \
3437                     and hasattr(self._columns[field], 'selection') \
3438                     and vals[field]:
3439                 if self._columns[field]._type == 'reference':
3440                     val = vals[field].split(',')[0]
3441                 else:
3442                     val = vals[field]
3443                 if isinstance(self._columns[field].selection, (tuple, list)):
3444                     if val not in dict(self._columns[field].selection):
3445                         raise except_orm(_('ValidateError'),
3446                         _('The value "%s" for the field "%s" is not in the selection') \
3447                                 % (vals[field], field))
3448                 else:
3449                     if val not in dict(self._columns[field].selection(
3450                         self, cr, user, context=context)):
3451                         raise except_orm(_('ValidateError'),
3452                         _('The value "%s" for the field "%s" is not in the selection') \
3453                                 % (vals[field], field))
3454
3455         if self._log_access:
3456             upd0.append('write_uid=%s')
3457             upd0.append('write_date=now()')
3458             upd1.append(user)
3459
3460         if len(upd0):
3461             self.check_access_rule(cr, user, ids, 'write', context=context)
3462             for sub_ids in cr.split_for_in_conditions(ids):
3463                 cr.execute('update ' + self._table + ' set ' + ','.join(upd0) + ' ' \
3464                            'where id IN %s', upd1 + [sub_ids])
3465                 if cr.rowcount != len(sub_ids):
3466                     raise except_orm(_('AccessError'),
3467                                      _('One of the records you are trying to modify has already been deleted (Document type: %s).') % self._description)
3468
3469             if totranslate:
3470                 # TODO: optimize
3471                 for f in direct:
3472                     if self._columns[f].translate:
3473                         src_trans = self.pool.get(self._name).read(cr, user, ids, [f])[0][f]
3474                         if not src_trans:
3475                             src_trans = vals[f]
3476                             # Inserting value to DB
3477                             self.write(cr, user, ids, {f: vals[f]})
3478                         self.pool.get('ir.translation')._set_ids(cr, user, self._name+','+f, 'model', context['lang'], ids, vals[f], src_trans)
3479
3480
3481         # call the 'set' method of fields which are not classic_write
3482         upd_todo.sort(lambda x, y: self._columns[x].priority-self._columns[y].priority)
3483
3484         # default element in context must be removed when call a one2many or many2many
3485         rel_context = context.copy()
3486         for c in context.items():
3487             if c[0].startswith('default_'):
3488                 del rel_context[c[0]]
3489
3490         for field in upd_todo:
3491             for id in ids:
3492                 result += self._columns[field].set(cr, self, id, field, vals[field], user, context=rel_context) or []
3493
3494         for table in self._inherits:
3495             col = self._inherits[table]
3496             nids = []
3497             for sub_ids in cr.split_for_in_conditions(ids):
3498                 cr.execute('select distinct "'+col+'" from "'+self._table+'" ' \
3499                            'where id IN %s', (sub_ids,))
3500                 nids.extend([x[0] for x in cr.fetchall()])
3501
3502             v = {}
3503             for val in updend:
3504                 if self._inherit_fields[val][0] == table:
3505                     v[val] = vals[val]
3506             if v:
3507                 self.pool.get(table).write(cr, user, nids, v, context)
3508
3509         self._validate(cr, user, ids, context)
3510
3511         # TODO: use _order to set dest at the right position and not first node of parent
3512         # We can't defer parent_store computation because the stored function
3513         # fields that are computer may refer (directly or indirectly) to
3514         # parent_left/right (via a child_of domain)
3515         if parents_changed:
3516             if self.pool._init:
3517                 self.pool._init_parent[self._name] = True
3518             else:
3519                 order = self._parent_order or self._order
3520                 parent_val = vals[self._parent_name]
3521                 if parent_val:
3522                     clause, params = '%s=%%s' % (self._parent_name,), (parent_val,)
3523                 else:
3524                     clause, params = '%s IS NULL' % (self._parent_name,), ()
3525                 cr.execute('SELECT parent_right, id FROM %s WHERE %s ORDER BY %s' % (self._table, clause, order), params)
3526                 parents = cr.fetchall()
3527
3528                 for id in parents_changed:
3529                     cr.execute('SELECT parent_left, parent_right FROM %s WHERE id=%%s' % (self._table,), (id,))
3530                     pleft, pright = cr.fetchone()
3531                     distance = pright - pleft + 1
3532
3533                     # Find Position of the element
3534                     position = None
3535                     for (parent_pright, parent_id) in parents:
3536                         if parent_id == id:
3537                             break
3538                         position = parent_pright + 1
3539
3540                     # It's the first node of the parent
3541                     if not position:
3542                         if not parent_val:
3543                             position = 1
3544                         else:
3545                             cr.execute('select parent_left from '+self._table+' where id=%s', (parent_val,))
3546                             position = cr.fetchone()[0] + 1
3547
3548                     if pleft < position <= pright:
3549                         raise except_orm(_('UserError'), _('Recursivity Detected.'))
3550
3551                     if pleft < position:
3552                         cr.execute('update '+self._table+' set parent_left=parent_left+%s where parent_left>=%s', (distance, position))
3553                         cr.execute('update '+self._table+' set parent_right=parent_right+%s where parent_right>=%s', (distance, position))
3554                         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))
3555                     else:
3556                         cr.execute('update '+self._table+' set parent_left=parent_left+%s where parent_left>=%s', (distance, position))
3557                         cr.execute('update '+self._table+' set parent_right=parent_right+%s where parent_right>=%s', (distance, position))
3558                         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))
3559
3560         result += self._store_get_values(cr, user, ids, vals.keys(), context)
3561         result.sort()
3562
3563         done = {}
3564         for order, object, ids, fields in result:
3565             key = (object, tuple(fields))
3566             done.setdefault(key, {})
3567             # avoid to do several times the same computation
3568             todo = []
3569             for id in ids:
3570                 if id not in done[key]:
3571                     done[key][id] = True
3572                     todo.append(id)
3573             self.pool.get(object)._store_set_values(cr, user, todo, fields, context)
3574
3575         wf_service = netsvc.LocalService("workflow")
3576         for id in ids:
3577             wf_service.trg_write(user, self._name, id, cr)
3578         return True
3579
3580     #
3581     # TODO: Should set perm to user.xxx
3582     #
3583     def create(self, cr, user, vals, context=None):
3584         """
3585         Create new record with specified value
3586
3587         :param cr: database cursor
3588         :param user: current user id
3589         :type user: integer
3590         :param vals: field values for new record, e.g {'field_name': field_value, ...}
3591         :type vals: dictionary
3592         :param context: optional context arguments, e.g. {'lang': 'en_us', 'tz': 'UTC', ...}
3593         :type context: dictionary
3594         :return: id of new record created
3595         :raise AccessError: * if user has no create rights on the requested object
3596                             * if user tries to bypass access rules for create on the requested object
3597         :raise ValidateError: if user tries to enter invalid value for a field that is not in selection
3598         :raise UserError: if a loop would be created in a hierarchy of objects a result of the operation (such as setting an object as its own parent)
3599
3600         **Note**: The type of field values to pass in ``vals`` for relationship fields is specific.
3601         Please see the description of the :py:meth:`~osv.osv.osv.write` method for details about the possible values and how
3602         to specify them.
3603
3604         """
3605         if not context:
3606             context = {}
3607         self.pool.get('ir.model.access').check(cr, user, self._name, 'create', context=context)
3608
3609         vals = self._add_missing_default_values(cr, user, vals, context)
3610
3611         tocreate = {}
3612         for v in self._inherits:
3613             if self._inherits[v] not in vals:
3614                 tocreate[v] = {}
3615             else:
3616                 tocreate[v] = {'id': vals[self._inherits[v]]}
3617         (upd0, upd1, upd2) = ('', '', [])
3618         upd_todo = []
3619         for v in vals.keys():
3620             if v in self._inherit_fields:
3621                 (table, col, col_detail) = self._inherit_fields[v]
3622                 tocreate[table][v] = vals[v]
3623                 del vals[v]
3624             else:
3625                 if (v not in self._inherit_fields) and (v not in self._columns):
3626                     del vals[v]
3627
3628         # Try-except added to filter the creation of those records whose filds are readonly.
3629         # Example : any dashboard which has all the fields readonly.(due to Views(database views))
3630         try:
3631             cr.execute("SELECT nextval('"+self._sequence+"')")
3632         except:
3633             raise except_orm(_('UserError'),
3634                         _('You cannot perform this operation. New Record Creation is not allowed for this object as this object is for reporting purpose.'))
3635
3636         id_new = cr.fetchone()[0]
3637         for table in tocreate:
3638             if self._inherits[table] in vals:
3639                 del vals[self._inherits[table]]
3640
3641             record_id = tocreate[table].pop('id', None)
3642
3643             if record_id is None or not record_id:
3644                 record_id = self.pool.get(table).create(cr, user, tocreate[table], context=context)
3645             else:
3646                 self.pool.get(table).write(cr, user, [record_id], tocreate[table], context=context)
3647
3648             upd0 += ',' + self._inherits[table]
3649             upd1 += ',%s'
3650             upd2.append(record_id)
3651
3652         #Start : Set bool fields to be False if they are not touched(to make search more powerful)
3653         bool_fields = [x for x in self._columns.keys() if self._columns[x]._type=='boolean']
3654
3655         for bool_field in bool_fields:
3656             if bool_field not in vals:
3657                 vals[bool_field] = False
3658         #End
3659         for field in vals.copy():
3660             fobj = None
3661             if field in self._columns:
3662                 fobj = self._columns[field]
3663             else:
3664                 fobj = self._inherit_fields[field][2]
3665             if not fobj:
3666                 continue
3667             groups = fobj.write
3668             if groups:
3669                 edit = False
3670                 for group in groups:
3671                     module = group.split(".")[0]
3672                     grp = group.split(".")[1]
3673                     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" % \
3674                                (grp, module, 'res.groups', user))
3675                     readonly = cr.fetchall()
3676                     if readonly[0][0] >= 1:
3677                         edit = True
3678                         break
3679                     elif readonly[0][0] == 0:
3680                         edit = False
3681                     else:
3682                         edit = False
3683
3684                 if not edit:
3685                     vals.pop(field)
3686         for field in vals:
3687             if self._columns[field]._classic_write:
3688                 upd0 = upd0 + ',"' + field + '"'
3689                 upd1 = upd1 + ',' + self._columns[field]._symbol_set[0]
3690                 upd2.append(self._columns[field]._symbol_set[1](vals[field]))
3691             else:
3692                 if not isinstance(self._columns[field], fields.related):
3693                     upd_todo.append(field)
3694             if field in self._columns \
3695                     and hasattr(self._columns[field], 'selection') \
3696                     and vals[field]:
3697                 if self._columns[field]._type == 'reference':
3698                     val = vals[field].split(',')[0]
3699                 else:
3700                     val = vals[field]
3701                 if isinstance(self._columns[field].selection, (tuple, list)):
3702                     if val not in dict(self._columns[field].selection):
3703                         raise except_orm(_('ValidateError'),
3704                         _('The value "%s" for the field "%s" is not in the selection') \
3705                                 % (vals[field], field))
3706                 else:
3707                     if val not in dict(self._columns[field].selection(
3708                         self, cr, user, context=context)):
3709                         raise except_orm(_('ValidateError'),
3710                         _('The value "%s" for the field "%s" is not in the selection') \
3711                                 % (vals[field], field))
3712         if self._log_access:
3713             upd0 += ',create_uid,create_date'
3714             upd1 += ',%s,now()'
3715             upd2.append(user)
3716         cr.execute('insert into "'+self._table+'" (id'+upd0+") values ("+str(id_new)+upd1+')', tuple(upd2))
3717         self.check_access_rule(cr, user, [id_new], 'create', context=context)
3718         upd_todo.sort(lambda x, y: self._columns[x].priority-self._columns[y].priority)
3719
3720         if self._parent_store and not context.get('defer_parent_store_computation'):
3721             if self.pool._init:
3722                 self.pool._init_parent[self._name] = True
3723             else:
3724                 parent = vals.get(self._parent_name, False)
3725                 if parent:
3726                     cr.execute('select parent_right from '+self._table+' where '+self._parent_name+'=%s order by '+(self._parent_order or self._order), (parent,))
3727                     pleft_old = None
3728                     result_p = cr.fetchall()
3729                     for (pleft,) in result_p:
3730                         if not pleft:
3731                             break
3732                         pleft_old = pleft
3733                     if not pleft_old:
3734                         cr.execute('select parent_left from '+self._table+' where id=%s', (parent,))
3735                         pleft_old = cr.fetchone()[0]
3736                     pleft = pleft_old
3737                 else:
3738                     cr.execute('select max(parent_right) from '+self._table)
3739                     pleft = cr.fetchone()[0] or 0
3740                 cr.execute('update '+self._table+' set parent_left=parent_left+2 where parent_left>%s', (pleft,))
3741                 cr.execute('update '+self._table+' set parent_right=parent_right+2 where parent_right>%s', (pleft,))
3742                 cr.execute('update '+self._table+' set parent_left=%s,parent_right=%s where id=%s', (pleft+1, pleft+2, id_new))
3743
3744         # default element in context must be remove when call a one2many or many2many
3745         rel_context = context.copy()
3746         for c in context.items():
3747             if c[0].startswith('default_'):
3748                 del rel_context[c[0]]
3749
3750         result = []
3751         for field in upd_todo:
3752             result += self._columns[field].set(cr, self, id_new, field, vals[field], user, rel_context) or []
3753         self._validate(cr, user, [id_new], context)
3754
3755         if not context.get('no_store_function', False):
3756             result += self._store_get_values(cr, user, [id_new], vals.keys(), context)
3757             result.sort()
3758             done = []
3759             for order, object, ids, fields2 in result:
3760                 if not (object, ids, fields2) in done:
3761                     self.pool.get(object)._store_set_values(cr, user, ids, fields2, context)
3762                     done.append((object, ids, fields2))
3763
3764         if self._log_create and not (context and context.get('no_store_function', False)):
3765             message = self._description + \
3766                 " '" + \
3767                 self.name_get(cr, user, [id_new], context=context)[0][1] + \
3768                 "' " + _("created.")
3769             self.log(cr, user, id_new, message, True, context=context)
3770         wf_service = netsvc.LocalService("workflow")
3771         wf_service.trg_create(user, self._name, id_new, cr)
3772         return id_new
3773
3774     def _store_get_values(self, cr, uid, ids, fields, context):
3775         result = {}
3776         fncts = self.pool._store_function.get(self._name, [])
3777         for fnct in range(len(fncts)):
3778             if fncts[fnct][3]:
3779                 ok = False
3780                 if not fields:
3781                     ok = True
3782                 for f in (fields or []):
3783                     if f in fncts[fnct][3]:
3784                         ok = True
3785                         break
3786                 if not ok:
3787                     continue
3788
3789             result.setdefault(fncts[fnct][0], {})
3790
3791             # uid == 1 for accessing objects having rules defined on store fields
3792             ids2 = fncts[fnct][2](self, cr, 1, ids, context)
3793             for id in filter(None, ids2):
3794                 result[fncts[fnct][0]].setdefault(id, [])
3795                 result[fncts[fnct][0]][id].append(fnct)
3796         dict = {}
3797         for object in result:
3798             k2 = {}
3799             for id, fnct in result[object].items():
3800                 k2.setdefault(tuple(fnct), [])
3801                 k2[tuple(fnct)].append(id)
3802             for fnct, id in k2.items():
3803                 dict.setdefault(fncts[fnct[0]][4], [])
3804                 dict[fncts[fnct[0]][4]].append((fncts[fnct[0]][4], object, id, map(lambda x: fncts[x][1], fnct)))
3805         result2 = []
3806         tmp = dict.keys()
3807         tmp.sort()
3808         for k in tmp:
3809             result2 += dict[k]
3810         return result2
3811
3812     def _store_set_values(self, cr, uid, ids, fields, context):
3813         if not ids:
3814             return True
3815         field_flag = False
3816         field_dict = {}
3817         if self._log_access:
3818             cr.execute('select id,write_date from '+self._table+' where id IN %s', (tuple(ids),))
3819             res = cr.fetchall()
3820             for r in res:
3821                 if r[1]:
3822                     field_dict.setdefault(r[0], [])
3823                     res_date = time.strptime((r[1])[:19], '%Y-%m-%d %H:%M:%S')
3824                     write_date = datetime.datetime.fromtimestamp(time.mktime(res_date))
3825                     for i in self.pool._store_function.get(self._name, []):
3826                         if i[5]:
3827                             up_write_date = write_date + datetime.timedelta(hours=i[5])
3828                             if datetime.datetime.now() < up_write_date:
3829                                 if i[1] in fields:
3830                                     field_dict[r[0]].append(i[1])
3831                                     if not field_flag:
3832                                         field_flag = True
3833         todo = {}
3834         keys = []
3835         for f in fields:
3836             if self._columns[f]._multi not in keys:
3837                 keys.append(self._columns[f]._multi)
3838             todo.setdefault(self._columns[f]._multi, [])
3839             todo[self._columns[f]._multi].append(f)
3840         for key in keys:
3841             val = todo[key]
3842             if key:
3843                 # uid == 1 for accessing objects having rules defined on store fields
3844                 result = self._columns[val[0]].get(cr, self, ids, val, 1, context=context)
3845                 for id, value in result.items():
3846                     if field_flag:
3847                         for f in value.keys():
3848                             if f in field_dict[id]:
3849                                 value.pop(f)
3850                     upd0 = []
3851                     upd1 = []
3852                     for v in value:
3853                         if v not in val:
3854                             continue
3855                         if self._columns[v]._type in ('many2one', 'one2one'):
3856                             try:
3857                                 value[v] = value[v][0]
3858                             except:
3859                                 pass
3860                         upd0.append('"'+v+'"='+self._columns[v]._symbol_set[0])
3861                         upd1.append(self._columns[v]._symbol_set[1](value[v]))
3862                     upd1.append(id)
3863                     if upd0 and upd1:
3864                         cr.execute('update "' + self._table + '" set ' + \
3865                             ','.join(upd0) + ' where id = %s', upd1)
3866
3867             else:
3868                 for f in val:
3869                     # uid == 1 for accessing objects having rules defined on store fields
3870                     result = self._columns[f].get(cr, self, ids, f, 1, context=context)
3871                     for r in result.keys():
3872                         if field_flag:
3873                             if r in field_dict.keys():
3874                                 if f in field_dict[r]:
3875                                     result.pop(r)
3876                     for id, value in result.items():
3877                         if self._columns[f]._type in ('many2one', 'one2one'):
3878                             try:
3879                                 value = value[0]
3880                             except:
3881                                 pass
3882                         cr.execute('update "' + self._table + '" set ' + \
3883                             '"'+f+'"='+self._columns[f]._symbol_set[0] + ' where id = %s', (self._columns[f]._symbol_set[1](value), id))
3884         return True
3885
3886     #
3887     # TODO: Validate
3888     #
3889     def perm_write(self, cr, user, ids, fields, context=None):
3890         raise NotImplementedError(_('This method does not exist anymore'))
3891
3892     # TODO: ameliorer avec NULL
3893     def _where_calc(self, cr, user, domain, active_test=True, context=None):
3894         """Computes the WHERE clause needed to implement an OpenERP domain.
3895         :param domain: the domain to compute
3896         :type domain: list
3897         :param active_test: whether the default filtering of records with ``active``
3898                             field set to ``False`` should be applied.
3899         :return: the query expressing the given domain as provided in domain
3900         :rtype: osv.query.Query
3901         """
3902         if not context:
3903             context = {}
3904         domain = domain[:]
3905         # if the object has a field named 'active', filter out all inactive
3906         # records unless they were explicitely asked for
3907         if 'active' in self._columns and (active_test and context.get('active_test', True)):
3908             if domain:
3909                 active_in_args = False
3910                 for a in domain:
3911                     if a[0] == 'active':
3912                         active_in_args = True
3913                 if not active_in_args:
3914                     domain.insert(0, ('active', '=', 1))
3915             else:
3916                 domain = [('active', '=', 1)]
3917
3918         if domain:
3919             import expression
3920             e = expression.expression(domain)
3921             e.parse(cr, user, self, context)
3922             tables = e.get_tables()
3923             where_clause, where_params = e.to_sql()
3924             where_clause = where_clause and [where_clause] or []
3925         else:
3926             where_clause, where_params, tables = [], [], ['"%s"' % self._table]
3927
3928         return Query(tables, where_clause, where_params)
3929
3930     def _check_qorder(self, word):
3931         if not regex_order.match(word):
3932             raise except_orm(_('AccessError'), _('Invalid "order" specified. A valid "order" specification is a comma-separated list of valid field names (optionally followed by asc/desc for the direction)'))
3933         return True
3934
3935     def _apply_ir_rules(self, cr, uid, query, mode='read', context=None):
3936         """Add what's missing in ``query`` to implement all appropriate ir.rules
3937           (using the ``model_name``'s rules or the current model's rules if ``model_name`` is None)
3938
3939            :param query: the current query object
3940         """
3941         def apply_rule(added_clause, added_params, added_tables, parent_model=None, child_object=None):
3942             if added_clause:
3943                 if parent_model and child_object:
3944                     # as inherited rules are being applied, we need to add the missing JOIN
3945                     # to reach the parent table (if it was not JOINed yet in the query)
3946                     child_object._inherits_join_add(parent_model, query)
3947                 query.where_clause += added_clause
3948                 query.where_clause_params += added_params
3949                 for table in added_tables:
3950                     if table not in query.tables:
3951                         query.tables.append(table)
3952                 return True
3953             return False
3954
3955         # apply main rules on the object
3956         rule_obj = self.pool.get('ir.rule')
3957         apply_rule(*rule_obj.domain_get(cr, uid, self._name, mode, context=context))
3958
3959         # apply ir.rules from the parents (through _inherits)
3960         for inherited_model in self._inherits:
3961             kwargs = dict(parent_model=inherited_model, child_object=self) #workaround for python2.5
3962             apply_rule(*rule_obj.domain_get(cr, uid, inherited_model, mode, context=context), **kwargs)
3963
3964     def _generate_m2o_order_by(self, order_field, query):
3965         """
3966         Add possibly missing JOIN to ``query`` and generate the ORDER BY clause for m2o fields,
3967         either native m2o fields or function/related fields that are stored, including
3968         intermediate JOINs for inheritance if required.
3969
3970         :return: the qualified field name to use in an ORDER BY clause to sort by ``order_field``
3971         """
3972         if order_field not in self._columns and order_field in self._inherit_fields:
3973             # also add missing joins for reaching the table containing the m2o field
3974             qualified_field = self._inherits_join_calc(order_field, query)
3975             order_field_column = self._inherit_fields[order_field][2]
3976         else:
3977             qualified_field = '"%s"."%s"' % (self._table, order_field)
3978             order_field_column = self._columns[order_field]
3979
3980         assert order_field_column._type == 'many2one', 'Invalid field passed to _generate_m2o_order_by()'
3981         assert order_field_column._classic_write or getattr(order_field_column, 'store', False), "Many2one function/related fields must be stored to be used as ordering fields"
3982
3983         # figure out the applicable order_by for the m2o
3984         dest_model = self.pool.get(order_field_column._obj)
3985         m2o_order = dest_model._order
3986         if not regex_order.match(m2o_order):
3987             # _order is complex, can't use it here, so we default to _rec_name
3988             m2o_order = dest_model._rec_name
3989         else:
3990             # extract the first field name, to be able to qualify it and add desc/asc
3991             m2o_order = m2o_order.split(",",1)[0].strip().split(" ",1)[0]
3992
3993         # Join the dest m2o table if it's not joined yet. We use [LEFT] OUTER join here
3994         # as we don't want to exclude results that have NULL values for the m2o
3995         src_table, src_field = qualified_field.replace('"','').split('.', 1)
3996         query.join((src_table, dest_model._table, src_field, 'id'), outer=True)
3997         return '"%s"."%s"' % (dest_model._table, m2o_order)
3998
3999
4000     def _generate_order_by(self, order_spec, query):
4001         """
4002         Attempt to consruct an appropriate ORDER BY clause based on order_spec, which must be
4003         a comma-separated list of valid field names, optionally followed by an ASC or DESC direction.
4004
4005         :raise" except_orm in case order_spec is malformed
4006         """
4007         order_by_clause = self._order
4008         if order_spec:
4009             order_by_elements = []
4010             self._check_qorder(order_spec)
4011             for order_part in order_spec.split(','):
4012                 order_split = order_part.strip().split(' ')
4013                 order_field = order_split[0].strip()
4014                 order_direction = order_split[1].strip() if len(order_split) == 2 else ''
4015                 if order_field in self._columns:
4016                     order_column = self._columns[order_field]
4017                     if order_column._classic_read:
4018                         order_by_clause = '"%s"."%s"' % (self._table, order_field)
4019                     elif order_column._type == 'many2one':
4020                         order_by_clause = self._generate_m2o_order_by(order_field, query)
4021                     else:
4022                         continue # ignore non-readable or "non-joignable" fields
4023                 elif order_field in self._inherit_fields:
4024                     parent_obj = self.pool.get(self._inherit_fields[order_field][0])
4025                     order_column = parent_obj._columns[order_field]
4026                     if order_column._classic_read:
4027                         order_by_clause = self._inherits_join_calc(order_field, query)
4028                     elif order_column._type == 'many2one':
4029                         order_by_clause = self._generate_m2o_order_by(order_field, query)
4030                     else:
4031                         continue # ignore non-readable or "non-joignable" fields
4032                 order_by_elements.append("%s %s" % (order_by_clause, order_direction))
4033             order_by_clause = ",".join(order_by_elements)
4034
4035         return order_by_clause and (' ORDER BY %s ' % order_by_clause) or ''
4036
4037     def _search(self, cr, user, args, offset=0, limit=None, order=None, context=None, count=False, access_rights_uid=None):
4038         """
4039         Private implementation of search() method, allowing specifying the uid to use for the access right check.
4040         This is useful for example when filling in the selection list for a drop-down and avoiding access rights errors,
4041         by specifying ``access_rights_uid=1`` to bypass access rights check, but not ir.rules!
4042         This is ok at the security level because this method is private and not callable through XML-RPC.
4043
4044         :param access_rights_uid: optional user ID to use when checking access rights
4045                                   (not for ir.rules, this is only for ir.model.access)
4046         """
4047         if context is None:
4048             context = {}
4049         self.pool.get('ir.model.access').check(cr, access_rights_uid or user, self._name, 'read', context=context)
4050
4051         query = self._where_calc(cr, user, args, context=context)
4052         self._apply_ir_rules(cr, user, query, 'read', context=context)
4053         order_by = self._generate_order_by(order, query)
4054         from_clause, where_clause, where_clause_params = query.get_sql()
4055
4056         limit_str = limit and ' limit %d' % limit or ''
4057         offset_str = offset and ' offset %d' % offset or ''
4058         where_str = where_clause and (" WHERE %s" % where_clause) or ''
4059
4060         if count:
4061             cr.execute('SELECT count("%s".id) FROM ' % self._table + from_clause + where_str + limit_str + offset_str, where_clause_params)
4062             res = cr.fetchall()
4063             return res[0][0]
4064         cr.execute('SELECT "%s".id FROM ' % self._table + from_clause + where_str + order_by + limit_str + offset_str, where_clause_params)
4065         res = cr.fetchall()
4066         return [x[0] for x in res]
4067
4068     # returns the different values ever entered for one field
4069     # this is used, for example, in the client when the user hits enter on
4070     # a char field
4071     def distinct_field_get(self, cr, uid, field, value, args=None, offset=0, limit=None):
4072         if not args:
4073             args = []
4074         if field in self._inherit_fields:
4075             return self.pool.get(self._inherit_fields[field][0]).distinct_field_get(cr, uid, field, value, args, offset, limit)
4076         else:
4077             return self._columns[field].search(cr, self, args, field, value, offset, limit, uid)
4078
4079     def copy_data(self, cr, uid, id, default=None, context=None):
4080         """
4081         Copy given record's data with all its fields values
4082
4083         :param cr: database cursor
4084         :param user: current user id
4085         :param id: id of the record to copy
4086         :param default: field values to override in the original values of the copied record
4087         :type default: dictionary
4088         :param context: context arguments, like lang, time zone
4089         :type context: dictionary
4090         :return: dictionary containing all the field values
4091         """
4092
4093         if context is None:
4094             context = {}
4095         if default is None:
4096             default = {}
4097         if 'state' not in default:
4098             if 'state' in self._defaults:
4099                 if callable(self._defaults['state']):
4100                     default['state'] = self._defaults['state'](self, cr, uid, context)
4101                 else:
4102                     default['state'] = self._defaults['state']
4103
4104         context_wo_lang = context
4105         if 'lang' in context:
4106             del context_wo_lang['lang']
4107         data = self.read(cr, uid, [id], context=context_wo_lang)[0]
4108
4109         fields = self.fields_get(cr, uid, context=context)
4110         for f in fields:
4111             ftype = fields[f]['type']
4112
4113             if self._log_access and f in ('create_date', 'create_uid', 'write_date', 'write_uid'):
4114                 del data[f]
4115
4116             if f in default:
4117                 data[f] = default[f]
4118             elif ftype == 'function':
4119                 del data[f]
4120             elif ftype == 'many2one':
4121                 try:
4122                     data[f] = data[f] and data[f][0]
4123                 except:
4124                     pass
4125             elif ftype in ('one2many', 'one2one'):
4126                 res = []
4127                 rel = self.pool.get(fields[f]['relation'])
4128                 if data[f]:
4129                     # duplicate following the order of the ids
4130                     # because we'll rely on it later for copying
4131                     # translations in copy_translation()!
4132                     data[f].sort()
4133                     for rel_id in data[f]:
4134                         # the lines are first duplicated using the wrong (old)
4135                         # parent but then are reassigned to the correct one thanks
4136                         # to the (0, 0, ...)
4137                         d = rel.copy_data(cr, uid, rel_id, context=context)
4138                         res.append((0, 0, d))
4139                 data[f] = res
4140             elif ftype == 'many2many':
4141                 data[f] = [(6, 0, data[f])]
4142
4143         del data['id']
4144
4145         # make sure we don't break the current parent_store structure and
4146         # force a clean recompute!
4147         for parent_column in ['parent_left', 'parent_right']:
4148             data.pop(parent_column, None)
4149
4150         for v in self._inherits:
4151             del data[self._inherits[v]]
4152         return data
4153
4154     def copy_translations(self, cr, uid, old_id, new_id, context=None):
4155         trans_obj = self.pool.get('ir.translation')
4156         fields = self.fields_get(cr, uid, context=context)
4157
4158         translation_records = []
4159         for field_name, field_def in fields.items():
4160             # we must recursively copy the translations for o2o and o2m
4161             if field_def['type'] in ('one2one', 'one2many'):
4162                 target_obj = self.pool.get(field_def['relation'])
4163                 old_record, new_record = self.read(cr, uid, [old_id, new_id], [field_name], context=context)
4164                 # here we rely on the order of the ids to match the translations
4165                 # as foreseen in copy_data()
4166                 old_children = sorted(old_record[field_name])
4167                 new_children = sorted(new_record[field_name])
4168                 for (old_child, new_child) in zip(old_children, new_children):
4169                     # recursive copy of translations here
4170                     target_obj.copy_translations(cr, uid, old_child, new_child, context=context)
4171             # and for translatable fields we keep them for copy
4172             elif field_def.get('translate'):
4173                 trans_name = ''
4174                 if field_name in self._columns:
4175                     trans_name = self._name + "," + field_name
4176                 elif field_name in self._inherit_fields:
4177                     trans_name = self._inherit_fields[field_name][0] + "," + field_name
4178                 if trans_name:
4179                     trans_ids = trans_obj.search(cr, uid, [
4180                             ('name', '=', trans_name),
4181                             ('res_id', '=', old_id)
4182                     ])
4183                     translation_records.extend(trans_obj.read(cr, uid, trans_ids, context=context))
4184
4185         for record in translation_records:
4186             del record['id']
4187             record['res_id'] = new_id
4188             trans_obj.create(cr, uid, record, context=context)
4189
4190
4191     def copy(self, cr, uid, id, default=None, context=None):
4192         """
4193         Duplicate record with given id updating it with default values
4194
4195         :param cr: database cursor
4196         :param uid: current user id
4197         :param id: id of the record to copy
4198         :param default: dictionary of field values to override in the original values of the copied record, e.g: ``{'field_name': overriden_value, ...}``
4199         :type default: dictionary
4200         :param context: context arguments, like lang, time zone
4201         :type context: dictionary
4202         :return: True
4203
4204         """
4205         data = self.copy_data(cr, uid, id, default, context)
4206         new_id = self.create(cr, uid, data, context)
4207         self.copy_translations(cr, uid, id, new_id, context)
4208         return new_id
4209
4210     def exists(self, cr, uid, ids, context=None):
4211         if type(ids) in (int, long):
4212             ids = [ids]
4213         query = 'SELECT count(1) FROM "%s"' % (self._table)
4214         cr.execute(query + "WHERE ID IN %s", (tuple(ids),))
4215         return cr.fetchone()[0] == len(ids)
4216
4217     def check_recursion(self, cr, uid, ids, parent=None):
4218         """
4219         Verifies that there is no loop in a hierarchical structure of records,
4220         by following the parent relationship using the **parent** field until a loop
4221         is detected or until a top-level record is found.
4222
4223         :param cr: database cursor
4224         :param uid: current user id
4225         :param ids: list of ids of records to check
4226         :param parent: optional parent field name (default: ``self._parent_name = parent_id``)
4227         :return: **True** if the operation can proceed safely, or **False** if an infinite loop is detected.
4228         """
4229
4230         if not parent:
4231             parent = self._parent_name
4232         ids_parent = ids[:]
4233         query = 'SELECT distinct "%s" FROM "%s" WHERE id IN %%s' % (parent, self._table)
4234         while ids_parent:
4235             ids_parent2 = []
4236             for i in range(0, len(ids), cr.IN_MAX):
4237                 sub_ids_parent = ids_parent[i:i+cr.IN_MAX]
4238                 cr.execute(query, (tuple(sub_ids_parent),))
4239                 ids_parent2.extend(filter(None, map(lambda x: x[0], cr.fetchall())))
4240             ids_parent = ids_parent2
4241             for i in ids_parent:
4242                 if i in ids:
4243                     return False
4244         return True
4245
4246     def get_xml_id(self, cr, uid, ids, *args, **kwargs):
4247         """Find out the XML ID of any database record, if there
4248         is one. This method works as a possible implementation
4249         for a function field, to be able to add it to any
4250         model object easily, referencing it as ``osv.osv.get_xml_id``.
4251
4252         **Synopsis**: ``get_xml_id(cr, uid, ids) -> { 'id': 'module.xml_id' }``
4253
4254         :return: the fully qualified XML ID of the given object,
4255                  defaulting to an empty string when there's none
4256                  (to be usable as a function field).
4257         """
4258         result = dict.fromkeys(ids, '')
4259         model_data_obj = self.pool.get('ir.model.data')
4260         data_ids = model_data_obj.search(cr, uid,
4261                 [('model', '=', self._name), ('res_id', 'in', ids)])
4262         data_results = model_data_obj.read(cr, uid, data_ids,
4263                 ['name', 'module', 'res_id'])
4264         for record in data_results:
4265             result[record['res_id']] = '%(module)s.%(name)s' % record
4266         return result
4267
4268 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
4269