1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
24 # - relations (one2many, many2one, many2many)
28 # _classic_read: is a classic sql fields
34 from collections import defaultdict
39 from psycopg2 import Binary
43 from tools.translate import _
45 def _symbol_set(symb):
46 if symb == None or symb == False:
48 elif isinstance(symb, unicode):
49 return symb.encode('utf-8')
53 class _column(object):
62 _symbol_f = _symbol_set
63 _symbol_set = (_symbol_c, _symbol_f)
66 def __init__(self, string='unknown', required=False, readonly=False, domain=None, context='', states=None, priority=0, change_default=False, size=None, ondelete="set null", translate=False, select=False, **args):
67 self.states = states or {}
69 self.readonly = readonly
70 self.required = required
72 self.help = args.get('help', '')
73 self.priority = priority
74 self.change_default = change_default
75 self.ondelete = ondelete
76 self.translate = translate
77 self._domain = domain or []
78 self._context = context
83 self.selectable = True
84 self.group_operator = args.get('group_operator', False)
87 setattr(self, a, args[a])
92 def set(self, cr, obj, id, name, value, user=None, context=None):
93 cr.execute('update '+obj._table+' set '+name+'='+self._symbol_set[0]+' where id=%s', (self._symbol_set[1](value), id))
95 def set_memory(self, cr, obj, id, name, value, user=None, context=None):
96 raise Exception(_('Not implemented set_memory method !'))
98 def get_memory(self, cr, obj, ids, name, user=None, context=None, values=None):
99 raise Exception(_('Not implemented get_memory method !'))
101 def get(self, cr, obj, ids, name, user=None, offset=0, context=None, values=None):
102 raise Exception(_('undefined get method !'))
104 def search(self, cr, obj, args, name, value, offset=0, limit=None, uid=None, context=None):
105 ids = obj.search(cr, uid, args+self._domain+[(name, 'ilike', value)], offset, limit, context=context)
106 res = obj.read(cr, uid, ids, [name], context=context)
107 return [x[name] for x in res]
109 def search_memory(self, cr, obj, args, name, value, offset=0, limit=None, uid=None, context=None):
110 raise Exception(_('Not implemented search_memory method !'))
113 # ---------------------------------------------------------
115 # ---------------------------------------------------------
116 class boolean(_column):
119 _symbol_f = lambda x: x and 'True' or 'False'
120 _symbol_set = (_symbol_c, _symbol_f)
122 class integer_big(_column):
123 _type = 'integer_big'
125 _symbol_f = lambda x: int(x or 0)
126 _symbol_set = (_symbol_c, _symbol_f)
127 _symbol_get = lambda self,x: x or 0
129 class integer(_column):
132 _symbol_f = lambda x: int(x or 0)
133 _symbol_set = (_symbol_c, _symbol_f)
134 _symbol_get = lambda self,x: x or 0
137 class reference(_column):
139 def __init__(self, string, selection, size, **args):
140 _column.__init__(self, string=string, size=size, selection=selection, **args)
146 def __init__(self, string, size, **args):
147 _column.__init__(self, string=string, size=size, **args)
148 self._symbol_set = (self._symbol_c, self._symbol_set_char)
150 # takes a string (encoded in utf8) and returns a string (encoded in utf8)
151 def _symbol_set_char(self, symb):
153 # * we need to remove the "symb==False" from the next line BUT
154 # for now too many things rely on this broken behavior
155 # * the symb==None test should be common to all data types
156 if symb == None or symb == False:
159 # we need to convert the string to a unicode object to be able
160 # to evaluate its length (and possibly truncate it) reliably
161 u_symb = tools.ustr(symb)
163 return u_symb[:self.size].encode('utf8')
171 class float(_column):
174 _symbol_f = lambda x: __builtin__.float(x or 0.0)
175 _symbol_set = (_symbol_c, _symbol_f)
176 _symbol_get = lambda self,x: x or 0.0
178 def __init__(self, string='unknown', digits=None, digits_compute=None, **args):
179 _column.__init__(self, string=string, **args)
181 self.digits_compute = digits_compute
184 def digits_change(self, cr):
185 if self.digits_compute:
186 t = self.digits_compute(cr)
187 self._symbol_set=('%s', lambda x: ('%.'+str(t[1])+'f') % (__builtin__.float(x or 0.0),))
194 class datetime(_column):
201 class binary(_column):
204 _symbol_f = lambda symb: symb and Binary(symb) or None
205 _symbol_set = (_symbol_c, _symbol_f)
206 _symbol_get = lambda self, x: x and str(x)
208 _classic_read = False
211 def __init__(self, string='unknown', filters=None, **args):
212 _column.__init__(self, string=string, **args)
213 self.filters = filters
215 def get_memory(self, cr, obj, ids, name, user=None, context=None, values=None):
227 if context.get('bin_size', False) and val:
228 res[i] = tools.human_size(long(val))
236 class selection(_column):
239 def __init__(self, selection, string='unknown', **args):
240 _column.__init__(self, string=string, **args)
241 self.selection = selection
243 # ---------------------------------------------------------
245 # ---------------------------------------------------------
248 # Values: (0, 0, { fields }) create
249 # (1, ID, { fields }) modification
250 # (2, ID) remove (delete)
251 # (3, ID) unlink one (target id or target of relation)
253 # (5) unlink all (only valid for one2many)
255 #CHECKME: dans la pratique c'est quoi la syntaxe utilisee pour le 5? (5) ou (5, 0)?
256 class one2one(_column):
257 _classic_read = False
258 _classic_write = True
261 def __init__(self, obj, string='unknown', **args):
262 warnings.warn("The one2one field doesn't work anymore", DeprecationWarning)
263 _column.__init__(self, string=string, **args)
266 def set(self, cr, obj_src, id, field, act, user=None, context=None):
269 obj = obj_src.pool.get(self._obj)
270 self._table = obj_src.pool.get(self._obj)._table
272 id_new = obj.create(cr, user, act[1])
273 cr.execute('update '+obj_src._table+' set '+field+'=%s where id=%s', (id_new, id))
275 cr.execute('select '+field+' from '+obj_src._table+' where id=%s', (act[0],))
276 id = cr.fetchone()[0]
277 obj.write(cr, user, [id], act[1], context=context)
279 def search(self, cr, obj, args, name, value, offset=0, limit=None, uid=None, context=None):
280 return obj.pool.get(self._obj).search(cr, uid, args+self._domain+[('name', 'like', value)], offset, limit, context=context)
283 class many2one(_column):
284 _classic_read = False
285 _classic_write = True
288 _symbol_f = lambda x: x or None
289 _symbol_set = (_symbol_c, _symbol_f)
291 def __init__(self, obj, string='unknown', **args):
292 _column.__init__(self, string=string, **args)
295 def set_memory(self, cr, obj, id, field, values, user=None, context=None):
296 obj.datas.setdefault(id, {})
297 obj.datas[id][field] = values
299 def get_memory(self, cr, obj, ids, name, user=None, context=None, values=None):
302 result[id] = obj.datas[id][name]
305 def get(self, cr, obj, ids, name, user=None, context=None, values=None):
312 res[r['id']] = r[name]
314 res.setdefault(id, '')
315 obj = obj.pool.get(self._obj)
317 # build a dictionary of the form {'id_of_distant_resource': name_of_distant_resource}
318 from orm import except_orm
320 for record in list(set(filter(None, res.values()))):
322 record_name = dict(obj.name_get(cr, user, [record], context))
325 record_name[record] = '// Access Denied //'
326 names.update(record_name)
329 if res[r] and res[r] in names:
330 res[r] = (res[r], names[res[r]])
335 def set(self, cr, obj_src, id, field, values, user=None, context=None):
338 obj = obj_src.pool.get(self._obj)
339 self._table = obj_src.pool.get(self._obj)._table
340 if type(values) == type([]):
343 id_new = obj.create(cr, act[2])
344 cr.execute('update '+obj_src._table+' set '+field+'=%s where id=%s', (id_new, id))
346 obj.write(cr, [act[1]], act[2], context=context)
348 cr.execute('delete from '+self._table+' where id=%s', (act[1],))
349 elif act[0] == 3 or act[0] == 5:
350 cr.execute('update '+obj_src._table+' set '+field+'=null where id=%s', (id,))
352 cr.execute('update '+obj_src._table+' set '+field+'=%s where id=%s', (act[1], id))
355 cr.execute('update '+obj_src._table+' set '+field+'=%s where id=%s', (values, id))
357 cr.execute('update '+obj_src._table+' set '+field+'=null where id=%s', (id,))
359 def search(self, cr, obj, args, name, value, offset=0, limit=None, uid=None, context=None):
360 return obj.pool.get(self._obj).search(cr, uid, args+self._domain+[('name', 'like', value)], offset, limit, context=context)
363 class one2many(_column):
364 _classic_read = False
365 _classic_write = False
369 def __init__(self, obj, fields_id, string='unknown', limit=None, **args):
370 _column.__init__(self, string=string, **args)
372 self._fields_id = fields_id
374 #one2many can't be used as condition for defaults
375 assert(self.change_default != True)
377 def get_memory(self, cr, obj, ids, name, user=None, offset=0, context=None, values=None):
381 context = context.copy()
382 context.update(self._context)
388 ids2 = obj.pool.get(self._obj).search(cr, user, [(self._fields_id, 'in', ids)], limit=self._limit, context=context)
389 for r in obj.pool.get(self._obj).read(cr, user, ids2, [self._fields_id], context=context, load='_classic_write'):
390 if r[self._fields_id] in res:
391 res[r[self._fields_id]].append(r['id'])
394 def set_memory(self, cr, obj, id, field, values, user=None, context=None):
398 context = context.copy()
399 context.update(self._context)
402 obj = obj.pool.get(self._obj)
405 act[2][self._fields_id] = id
406 obj.create(cr, user, act[2], context=context)
408 obj.write(cr, user, [act[1]], act[2], context=context)
410 obj.unlink(cr, user, [act[1]], context=context)
412 obj.datas[act[1]][self._fields_id] = False
414 obj.datas[act[1]] = id
416 for o in obj.datas.values():
417 if o[self._fields_id] == id:
418 o[self._fields_id] = False
420 for id2 in (act[2] or []):
421 obj.datas[id2][self._fields_id] = id
423 def search_memory(self, cr, obj, args, name, value, offset=0, limit=None, uid=None, operator='like', context=None):
424 raise _('Not Implemented')
426 def get(self, cr, obj, ids, name, user=None, offset=0, context=None, values=None):
430 context = context.copy()
431 context.update(self._context)
435 res = defaultdict(list)
437 ids2 = obj.pool.get(self._obj).search(cr, user, self._domain + [(self._fields_id, 'in', ids)], limit=self._limit, context=context)
438 for r in obj.pool.get(self._obj)._read_flat(cr, user, ids2, [self._fields_id], context=context, load='_classic_write'):
439 res[r[self._fields_id]].append(r['id'])
442 def set(self, cr, obj, id, field, values, user=None, context=None):
447 context = context.copy()
448 context.update(self._context)
449 context['no_store_function'] = True
452 _table = obj.pool.get(self._obj)._table
453 obj = obj.pool.get(self._obj)
456 act[2][self._fields_id] = id
457 id_new = obj.create(cr, user, act[2], context=context)
458 result += obj._store_get_values(cr, user, [id_new], act[2].keys(), context)
460 obj.write(cr, user, [act[1]], act[2], context=context)
462 obj.unlink(cr, user, [act[1]], context=context)
464 cr.execute('update '+_table+' set '+self._fields_id+'=null where id=%s', (act[1],))
466 cr.execute('update '+_table+' set '+self._fields_id+'=%s where id=%s', (id, act[1]))
468 cr.execute('update '+_table+' set '+self._fields_id+'=null where '+self._fields_id+'=%s', (id,))
470 obj.write(cr, user, act[2], {self._fields_id:id}, context=context or {})
472 cr.execute('select id from '+_table+' where '+self._fields_id+'=%s and id <> ALL (%s)', (id,ids2))
473 ids3 = map(lambda x:x[0], cr.fetchall())
474 obj.write(cr, user, ids3, {self._fields_id:False}, context=context or {})
477 def search(self, cr, obj, args, name, value, offset=0, limit=None, uid=None, operator='like', context=None):
478 return obj.pool.get(self._obj).name_search(cr, uid, value, self._domain, operator, context=context,limit=limit)
482 # Values: (0, 0, { fields }) create
483 # (1, ID, { fields }) modification
488 # (6, ?, ids) set a list of links
490 class many2many(_column):
491 _classic_read = False
492 _classic_write = False
496 def __init__(self, obj, rel, id1, id2, string='unknown', limit=None, **args):
497 _column.__init__(self, string=string, **args)
500 raise Exception(_('The second argument of the many2many field %s must be a SQL table !'\
501 'You used %s, which is not a valid SQL table name.')% (string,rel))
507 def get(self, cr, obj, ids, name, user=None, offset=0, context=None, values=None):
517 limit_str = self._limit is not None and ' limit %d' % self._limit or ''
518 obj = obj.pool.get(self._obj)
520 d1, d2, tables = obj.pool.get('ir.rule').domain_get(cr, user, obj._name, context=context)
522 d1 = ' and ' + ' and '.join(d1)
525 cr.execute('SELECT '+self._rel+'.'+self._id2+','+self._rel+'.'+self._id1+' \
526 FROM '+self._rel+' , '+(','.join(tables))+' \
527 WHERE '+self._rel+'.'+self._id1+' = ANY (%s) \
528 AND '+self._rel+'.'+self._id2+' = '+obj._table+'.id '+d1
529 +limit_str+' order by '+obj._table+'.'+obj._order+' offset %s',
531 for r in cr.fetchall():
532 res[r[1]].append(r[0])
535 def set(self, cr, obj, id, name, values, user=None, context=None):
540 obj = obj.pool.get(self._obj)
542 if not (isinstance(act, list) or isinstance(act, tuple)) or not act:
545 idnew = obj.create(cr, user, act[2])
546 cr.execute('insert into '+self._rel+' ('+self._id1+','+self._id2+') values (%s,%s)', (id, idnew))
548 obj.write(cr, user, [act[1]], act[2], context=context)
550 obj.unlink(cr, user, [act[1]], context=context)
552 cr.execute('delete from '+self._rel+' where ' + self._id1 + '=%s and '+ self._id2 + '=%s', (id, act[1]))
554 cr.execute('insert into '+self._rel+' ('+self._id1+','+self._id2+') values (%s,%s)', (id, act[1]))
556 cr.execute('update '+self._rel+' set '+self._id2+'=null where '+self._id2+'=%s', (id,))
559 d1, d2,tables = obj.pool.get('ir.rule').domain_get(cr, user, obj._name, context=context)
561 d1 = ' and ' + ' and '.join(d1)
564 cr.execute('delete from '+self._rel+' where '+self._id1+'=%s AND '+self._id2+' IN (SELECT '+self._rel+'.'+self._id2+' FROM '+self._rel+', '+','.join(tables)+' WHERE '+self._rel+'.'+self._id1+'=%s AND '+self._rel+'.'+self._id2+' = '+obj._table+'.id '+ d1 +')', [id, id]+d2)
566 for act_nbr in act[2]:
567 cr.execute('insert into '+self._rel+' ('+self._id1+','+self._id2+') values (%s, %s)', (id, act_nbr))
570 # TODO: use a name_search
572 def search(self, cr, obj, args, name, value, offset=0, limit=None, uid=None, operator='like', context=None):
573 return obj.pool.get(self._obj).search(cr, uid, args+self._domain+[('name', operator, value)], offset, limit, context=context)
575 def get_memory(self, cr, obj, ids, name, user=None, offset=0, context=None, values=None):
578 result[id] = obj.datas[id].get(name, [])
581 def set_memory(self, cr, obj, id, name, values, user=None, context=None):
585 # TODO: use constants instead of these magic numbers
587 raise _('Not Implemented')
589 raise _('Not Implemented')
591 raise _('Not Implemented')
593 raise _('Not Implemented')
595 raise _('Not Implemented')
597 raise _('Not Implemented')
599 obj.datas[id][name] = act[2]
602 def get_nice_size(a):
604 if isinstance(y, (int,long)):
610 return (x, tools.human_size(size))
612 # ---------------------------------------------------------
614 # ---------------------------------------------------------
615 class function(_column):
616 _classic_read = False
617 _classic_write = False
623 # multi: compute several fields in one call
625 def __init__(self, fnct, arg=None, fnct_inv=None, fnct_inv_arg=None, type='float', fnct_search=None, obj=None, method=False, store=False, multi=False, **args):
626 _column.__init__(self, **args)
628 self._method = method
630 self._fnct_inv = fnct_inv
633 if 'relation' in args:
634 self._obj = args['relation']
636 self.digits = args.get('digits', (16,2))
637 self.digits_compute = args.get('digits_compute', None)
639 self._fnct_inv_arg = fnct_inv_arg
643 self._fnct_search = fnct_search
646 if not fnct_search and not store:
647 self.selectable = False
650 self._classic_read = True
651 self._classic_write = True
653 self._symbol_get=lambda x:x and str(x)
656 self._symbol_c = float._symbol_c
657 self._symbol_f = float._symbol_f
658 self._symbol_set = float._symbol_set
660 if type == 'boolean':
661 self._symbol_c = boolean._symbol_c
662 self._symbol_f = boolean._symbol_f
663 self._symbol_set = boolean._symbol_set
665 def digits_change(self, cr):
666 if self.digits_compute:
667 t = self.digits_compute(cr)
668 self._symbol_set=('%s', lambda x: ('%.'+str(t[1])+'f') % (__builtin__.float(x or 0.0),))
672 def search(self, cr, uid, obj, name, args, context=None):
673 if not self._fnct_search:
674 #CHECKME: should raise an exception
676 return self._fnct_search(obj, cr, uid, obj, name, args, context=context)
678 def get(self, cr, obj, ids, name, user=None, context=None, values=None):
685 res = self._fnct(obj, cr, user, ids, name, self._arg, context)
687 res = self._fnct(cr, obj._table, ids, name, self._arg, context)
689 if self._type == "many2one" :
690 # Filtering only integer/long values if passed
691 res_ids = [x for x in res.values() if x and isinstance(x, (int,long))]
694 obj_model = obj.pool.get(self._obj)
695 dict_names = dict(obj_model.name_get(cr, user, res_ids, context))
697 if res[r] and res[r] in dict_names:
698 res[r] = (res[r], dict_names[res[r]])
700 if self._type == 'binary' and context.get('bin_size', False):
701 # convert the data returned by the function with the size of that data...
702 res = dict(map( get_nice_size, res.items()))
703 if self._type == "integer":
705 # Converting value into string so that it does not affect XML-RPC Limits
706 if isinstance(res[r],dict): # To treat integer values with _multi attribute
707 for record in res[r].keys():
708 res[r][record] = str(res[r][record])
714 def set(self, cr, obj, id, name, value, user=None, context=None):
718 self._fnct_inv(obj, cr, user, id, name, value, self._fnct_inv_arg, context)
721 # ---------------------------------------------------------
723 # ---------------------------------------------------------
725 class related(function):
727 def _fnct_search(self, tobj, cr, uid, obj=None, name=None, domain=None, context={}):
728 self._field_get2(cr, uid, obj, context)
732 if type(sarg) in [type([]), type( (1,) )]:
733 where = [(self._arg[i], 'in', sarg)]
735 where = [(self._arg[i], '=', sarg)]
737 where = map(lambda x: (self._arg[i],x[1], x[2]), domain)
739 sarg = obj.pool.get(self._relations[i]['object']).search(cr, uid, where, context=context)
741 return [(self._arg[0], 'in', sarg)]
743 def _fnct_write(self,obj,cr, uid, ids, field_name, values, args, context=None):
744 if values and field_name:
745 self._field_get2(cr, uid, obj, context)
748 if type(ids) != type([]):
750 objlst = obj.browse(cr, uid, ids)
755 for i in range(len(self.arg)):
756 field_detail = self._relations[i]
757 relation = field_detail['object']
758 if not t_data[self.arg[i]]:
759 if self._type not in ('one2many', 'many2many'):
763 if field_detail['type'] in ('one2many', 'many2many'):
764 if self._type != "many2one":
766 t_data = t_data[self.arg[i]][0]
772 t_data = t_data[self.arg[i]]
775 obj.pool.get(field_detail['object']).write(cr,uid,[t_id],{args[-1]:values}, context=context)
777 def _fnct_read(self, obj, cr, uid, ids, field_name, args, context=None):
778 self._field_get2(cr, uid, obj, context)
779 if not ids: return {}
781 res = {}.fromkeys(ids, False)
783 objlst = obj.browse(cr, uid, ids, context=context)
789 for i in range(len(self.arg)):
790 field_detail = self._relations[i]
791 relation = field_detail['object']
793 if not t_data[self.arg[i]]:
799 if field_detail['type'] in ('one2many', 'many2many') and i != len(self.arg) - 1:
800 t_data = t_data[self.arg[i]][0]
802 t_data = t_data[self.arg[i]]
803 if type(t_data) == type(objlst[0]):
804 res[data.id] = t_data.id
806 res[data.id] = t_data
807 if self._type=='many2one':
808 ids = filter(None, res.values())
810 ng = dict(obj.pool.get(self._obj).name_get(cr, uid, ids, context=context))
813 res[r] = (res[r], ng[res[r]])
814 elif self._type in ('one2many', 'many2many'):
817 res[r] = [x.id for x in res[r]]
820 def __init__(self, *arg, **args):
823 super(related, self).__init__(self._fnct_read, arg, self._fnct_write, fnct_inv_arg=arg, method=True, fnct_search=self._fnct_search, **args)
824 if self.store is True:
825 # TODO: improve here to change self.store = {...} according to related objects
828 def _field_get2(self, cr, uid, obj, context={}):
832 for i in range(len(self._arg)):
833 f = obj.pool.get(obj_name).fields_get(cr, uid, [self._arg[i]], context=context)[self._arg[i]]
834 self._relations.append({
839 if f.get('relation',False):
840 obj_name = f['relation']
841 self._relations[-1]['relation'] = f['relation']
843 # ---------------------------------------------------------
845 # ---------------------------------------------------------
847 class dummy(function):
848 def _fnct_search(self, tobj, cr, uid, obj=None, name=None, domain=None, context={}):
851 def _fnct_write(self,obj,cr, uid, ids, field_name, values, args, context=None):
854 def _fnct_read(self, obj, cr, uid, ids, field_name, args, context=None):
857 def __init__(self, *arg, **args):
860 super(dummy, self).__init__(self._fnct_read, arg, self._fnct_write, fnct_inv_arg=arg, method=True, fnct_search=None, **args)
862 # ---------------------------------------------------------
864 # ---------------------------------------------------------
865 class serialized(_column):
866 def __init__(self, string='unknown', serialize_func=repr, deserialize_func=eval, type='text', **args):
867 self._serialize_func = serialize_func
868 self._deserialize_func = deserialize_func
870 self._symbol_set = (self._symbol_c, self._serialize_func)
871 self._symbol_get = self._deserialize_func
872 super(serialized, self).__init__(string=string, **args)
875 class property(function):
877 def _get_default(self, obj, cr, uid, prop_name, context=None):
878 from orm import browse_record
879 prop = obj.pool.get('ir.property')
880 domain = prop._get_domain_default(cr, uid, prop_name, obj._name, context)
881 ids = prop.search(cr, uid, domain, order='company_id', context=context)
885 default_value = prop.get_by_id(cr, uid, ids, context=context)
886 if isinstance(default_value, browse_record):
887 return default_value.id
890 def _get_by_id(self, obj, cr, uid, prop_name, ids, context=None):
891 prop = obj.pool.get('ir.property')
892 vids = [obj._name + ',' + str(oid) for oid in ids]
894 domain = prop._get_domain(cr, uid, prop_name, obj._name, context)
895 if domain is not None:
896 domain = [('res_id', 'in', vids)] + domain
897 return prop.search(cr, uid, domain, context=context)
902 def _fnct_write(self, obj, cr, uid, id, prop_name, id_val, obj_dest, context=None):
906 nids = self._get_by_id(obj, cr, uid, prop_name, [id], context)
908 cr.execute('DELETE FROM ir_property WHERE id IN %s', (tuple(nids),))
910 default_val = self._get_default(obj, cr, uid, prop_name, context)
912 if id_val is not default_val:
913 def_id = self._field_get(cr, uid, obj._name, prop_name)
914 company = obj.pool.get('res.company')
915 cid = company._company_default_get(cr, uid, obj._name, def_id,
917 propdef = obj.pool.get('ir.model.fields').browse(cr, uid, def_id,
919 prop = obj.pool.get('ir.property')
920 return prop.create(cr, uid, {
921 'name': propdef.name,
923 'res_id': obj._name+','+str(id),
931 def _fnct_read(self, obj, cr, uid, ids, prop_name, obj_dest, context=None):
932 from orm import browse_record
933 properties = obj.pool.get('ir.property')
935 default_val = self._get_default(obj, cr, uid, prop_name, context)
937 nids = self._get_by_id(obj, cr, uid, prop_name, ids, context)
941 res[id] = default_val
942 for prop in properties.browse(cr, uid, nids):
943 value = prop.get_by_id(context=context)
944 if isinstance(value, browse_record):
945 if not value.exists():
946 cr.execute('DELETE FROM ir_property WHERE id=%s', (prop.id,))
949 res[prop.res_id.id] = value or False
953 def _field_get(self, cr, uid, model_name, prop):
954 if not self.field_id.get(cr.dbname):
955 cr.execute('SELECT id \
956 FROM ir_model_fields \
957 WHERE name=%s AND model=%s', (prop, model_name))
959 self.field_id[cr.dbname] = res and res[0]
960 return self.field_id[cr.dbname]
962 def __init__(self, obj_prop, **args):
963 # TODO remove obj_prop parameter (use many2one type)
965 function.__init__(self, self._fnct_read, False, self._fnct_write,
972 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: