2009-10-20 10:52:23 +00:00
# -*- coding: utf-8 -*-
2006-12-07 13:41:40 +00:00
##############################################################################
2010-03-08 09:40:36 +00:00
#
2008-11-25 07:07:40 +00:00
# OpenERP, Open Source Management Solution
2009-11-27 07:23:48 +00:00
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
2008-11-03 18:27:16 +00:00
#
# This program is free software: you can redistribute it and/or modify
2009-11-27 07:23:48 +00:00
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
2008-11-03 18:27:16 +00:00
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2009-11-27 07:23:48 +00:00
# GNU Affero General Public License for more details.
2008-11-03 18:27:16 +00:00
#
2009-11-27 07:23:48 +00:00
# You should have received a copy of the GNU Affero General Public License
2010-03-08 09:40:36 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2006-12-07 13:41:40 +00:00
#
##############################################################################
# . Fields:
# - simple
# - relations (one2many, many2one, many2many)
# - function
#
# Fields Attributes:
# _classic_read: is a classic sql fields
# _type : field type
# readonly
# required
# size
#
2010-10-12 16:01:30 +00:00
import datetime as DT
2006-12-07 13:41:40 +00:00
import string
2009-02-05 23:43:52 +00:00
import sys
2010-10-21 13:00:45 +00:00
import warnings
2010-12-09 23:07:42 +00:00
import xmlrpclib
2008-12-08 14:03:42 +00:00
from psycopg2 import Binary
2006-12-07 13:41:40 +00:00
2011-02-07 12:57:23 +00:00
import openerp . netsvc as netsvc
import openerp . tools as tools
from openerp . tools . translate import _
2008-08-12 14:44:56 +00:00
2006-12-07 13:41:40 +00:00
def _symbol_set ( symb ) :
2008-08-12 14:44:56 +00:00
if symb == None or symb == False :
2008-07-22 14:24:36 +00:00
return None
elif isinstance ( symb , unicode ) :
return symb . encode ( ' utf-8 ' )
return str ( symb )
2006-12-07 13:41:40 +00:00
2008-08-12 14:44:56 +00:00
2006-12-07 13:41:40 +00:00
class _column ( object ) :
2008-07-22 14:24:36 +00:00
_classic_read = True
_classic_write = True
2009-08-20 15:29:21 +00:00
_prefetch = True
2008-07-22 14:24:36 +00:00
_properties = False
_type = ' unknown '
_obj = None
2008-08-17 18:28:29 +00:00
_multi = False
2008-07-22 14:24:36 +00:00
_symbol_c = ' %s '
_symbol_f = _symbol_set
_symbol_set = ( _symbol_c , _symbol_f )
_symbol_get = None
2011-05-25 13:26:12 +00:00
def __init__ ( self , string = ' unknown ' , required = False , readonly = False , domain = None , context = None , states = None , priority = 0 , change_default = False , size = None , ondelete = " set null " , translate = False , select = False , manual = False , * * args ) :
"""
2011-05-25 14:59:09 +00:00
The ' manual ' keyword argument specifies if the field is a custom one .
It corresponds to the ' state ' column in ir_model_fields .
2011-05-25 13:26:12 +00:00
"""
2010-11-12 13:42:14 +00:00
if domain is None :
domain = [ ]
if context is None :
context = { }
2008-07-22 14:24:36 +00:00
self . states = states or { }
self . string = string
self . readonly = readonly
self . required = required
self . size = size
self . help = args . get ( ' help ' , ' ' )
self . priority = priority
self . change_default = change_default
self . ondelete = ondelete
self . translate = translate
2010-11-12 13:42:14 +00:00
self . _domain = domain
self . _context = context
2008-07-22 14:24:36 +00:00
self . write = False
self . read = False
self . view_load = 0
2008-08-12 14:44:56 +00:00
self . select = select
2011-05-25 13:26:12 +00:00
self . manual = manual
2009-09-17 07:27:12 +00:00
self . selectable = True
2010-03-08 09:40:36 +00:00
self . group_operator = args . get ( ' group_operator ' , False )
2008-07-22 14:24:36 +00:00
for a in args :
if args [ a ] :
setattr ( self , a , args [ a ] )
def restart ( self ) :
pass
def set ( self , cr , obj , id , name , value , user = None , context = None ) :
2008-12-09 13:35:40 +00:00
cr . execute ( ' update ' + obj . _table + ' set ' + name + ' = ' + self . _symbol_set [ 0 ] + ' where id= %s ' , ( self . _symbol_set [ 1 ] ( value ) , id ) )
2008-08-12 14:44:56 +00:00
2008-07-22 14:24:36 +00:00
def set_memory ( self , cr , obj , id , name , value , user = None , context = None ) :
2008-08-12 14:44:56 +00:00
raise Exception ( _ ( ' Not implemented set_memory method ! ' ) )
2008-07-22 14:24:36 +00:00
2008-08-29 13:08:14 +00:00
def get_memory ( self , cr , obj , ids , name , user = None , context = None , values = None ) :
2008-08-12 14:44:56 +00:00
raise Exception ( _ ( ' Not implemented get_memory method ! ' ) )
2008-08-29 13:08:14 +00:00
def get ( self , cr , obj , ids , name , user = None , offset = 0 , context = None , values = None ) :
2008-08-12 14:44:56 +00:00
raise Exception ( _ ( ' undefined get method ! ' ) )
2008-07-22 14:24:36 +00:00
2010-01-05 12:03:39 +00:00
def search ( self , cr , obj , args , name , value , offset = 0 , limit = None , uid = None , context = None ) :
ids = obj . search ( cr , uid , args + self . _domain + [ ( name , ' ilike ' , value ) ] , offset , limit , context = context )
res = obj . read ( cr , uid , ids , [ name ] , context = context )
2008-07-22 14:24:36 +00:00
return [ x [ name ] for x in res ]
2008-08-12 14:44:56 +00:00
2008-07-22 14:24:36 +00:00
def search_memory ( self , cr , obj , args , name , value , offset = 0 , limit = None , uid = None , context = None ) :
2008-08-12 14:44:56 +00:00
raise Exception ( _ ( ' Not implemented search_memory method ! ' ) )
2006-12-07 13:41:40 +00:00
# ---------------------------------------------------------
# Simple fields
# ---------------------------------------------------------
class boolean ( _column ) :
2008-07-22 14:24:36 +00:00
_type = ' boolean '
_symbol_c = ' %s '
_symbol_f = lambda x : x and ' True ' or ' False '
_symbol_set = ( _symbol_c , _symbol_f )
2006-12-07 13:41:40 +00:00
class integer ( _column ) :
2008-07-22 14:24:36 +00:00
_type = ' integer '
2008-12-09 13:35:40 +00:00
_symbol_c = ' %s '
2008-07-22 14:24:36 +00:00
_symbol_f = lambda x : int ( x or 0 )
_symbol_set = ( _symbol_c , _symbol_f )
2010-04-18 20:07:32 +00:00
_symbol_get = lambda self , x : x or 0
2006-12-07 13:41:40 +00:00
2011-01-12 13:28:28 +00:00
class integer_big ( _column ) :
_type = ' integer_big '
2011-01-12 17:39:01 +00:00
# do not reference the _symbol_* of integer class, as that would possibly
# unbind the lambda functions
_symbol_c = ' %s '
_symbol_f = lambda x : int ( x or 0 )
_symbol_set = ( _symbol_c , _symbol_f )
_symbol_get = lambda self , x : x or 0
2008-08-12 14:44:56 +00:00
2006-12-07 13:41:40 +00:00
class reference ( _column ) :
2008-07-22 14:24:36 +00:00
_type = ' reference '
def __init__ ( self , string , selection , size , * * args ) :
_column . __init__ ( self , string = string , size = size , selection = selection , * * args )
2006-12-07 13:41:40 +00:00
2008-08-12 14:44:56 +00:00
2006-12-07 13:41:40 +00:00
class char ( _column ) :
2008-07-22 14:24:36 +00:00
_type = ' char '
2008-08-12 14:44:56 +00:00
2008-07-22 14:24:36 +00:00
def __init__ ( self , string , size , * * args ) :
_column . __init__ ( self , string = string , size = size , * * args )
self . _symbol_set = ( self . _symbol_c , self . _symbol_set_char )
2008-08-12 14:44:56 +00:00
2008-07-22 14:24:36 +00:00
# takes a string (encoded in utf8) and returns a string (encoded in utf8)
def _symbol_set_char ( self , symb ) :
2008-08-12 14:44:56 +00:00
#TODO:
# * we need to remove the "symb==False" from the next line BUT
2008-07-22 14:24:36 +00:00
# for now too many things rely on this broken behavior
# * the symb==None test should be common to all data types
2008-08-12 14:44:56 +00:00
if symb == None or symb == False :
2008-07-22 14:24:36 +00:00
return None
2008-08-12 14:44:56 +00:00
# we need to convert the string to a unicode object to be able
2008-07-22 14:24:36 +00:00
# to evaluate its length (and possibly truncate it) reliably
2009-02-12 13:32:52 +00:00
u_symb = tools . ustr ( symb )
2008-11-24 16:22:47 +00:00
return u_symb [ : self . size ] . encode ( ' utf8 ' )
2006-12-07 13:41:40 +00:00
2008-08-12 14:44:56 +00:00
2006-12-07 13:41:40 +00:00
class text ( _column ) :
2008-07-22 14:24:36 +00:00
_type = ' text '
2006-12-07 13:41:40 +00:00
import __builtin__
class float ( _column ) :
2008-07-22 14:24:36 +00:00
_type = ' float '
2008-12-09 13:35:40 +00:00
_symbol_c = ' %s '
2008-07-22 14:24:36 +00:00
_symbol_f = lambda x : __builtin__ . float ( x or 0.0 )
_symbol_set = ( _symbol_c , _symbol_f )
2010-04-18 20:07:32 +00:00
_symbol_get = lambda self , x : x or 0.0
2008-08-12 14:44:56 +00:00
2010-03-06 19:59:55 +00:00
def __init__ ( self , string = ' unknown ' , digits = None , digits_compute = None , * * args ) :
2008-07-22 14:24:36 +00:00
_column . __init__ ( self , string = string , * * args )
self . digits = digits
2010-03-06 19:59:55 +00:00
self . digits_compute = digits_compute
2006-12-07 13:41:40 +00:00
2010-03-08 09:40:36 +00:00
2010-03-06 19:59:55 +00:00
def digits_change ( self , cr ) :
if self . digits_compute :
t = self . digits_compute ( cr )
self . _symbol_set = ( ' %s ' , lambda x : ( ' % . ' + str ( t [ 1 ] ) + ' f ' ) % ( __builtin__ . float ( x or 0.0 ) , ) )
self . digits = t
2008-08-12 14:44:56 +00:00
2006-12-07 13:41:40 +00:00
class date ( _column ) :
2008-07-22 14:24:36 +00:00
_type = ' date '
2010-10-12 16:01:30 +00:00
@staticmethod
def today ( * args ) :
""" Returns the current date in a format fit for being a
default value to a ` ` date ` ` field .
2006-12-07 13:41:40 +00:00
2010-10-12 16:01:30 +00:00
This method should be provided as is to the _defaults dict , it
should not be called .
"""
return DT . date . today ( ) . strftime (
tools . DEFAULT_SERVER_DATE_FORMAT )
2008-08-12 14:44:56 +00:00
2006-12-07 13:41:40 +00:00
class datetime ( _column ) :
2008-07-22 14:24:36 +00:00
_type = ' datetime '
2010-10-12 16:01:30 +00:00
@staticmethod
def now ( * args ) :
""" Returns the current datetime in a format fit for being a
default value to a ` ` datetime ` ` field .
2006-12-07 13:41:40 +00:00
2010-10-12 16:01:30 +00:00
This method should be provided as is to the _defaults dict , it
should not be called .
"""
return DT . datetime . now ( ) . strftime (
tools . DEFAULT_SERVER_DATETIME_FORMAT )
2008-08-12 14:44:56 +00:00
2006-12-07 13:41:40 +00:00
class time ( _column ) :
2008-07-22 14:24:36 +00:00
_type = ' time '
2010-10-12 16:01:30 +00:00
@staticmethod
def now ( * args ) :
""" Returns the current time in a format fit for being a
default value to a ` ` time ` ` field .
This method should be proivided as is to the _defaults dict ,
it should not be called .
"""
return DT . datetime . now ( ) . strftime (
tools . DEFAULT_SERVER_TIME_FORMAT )
2006-12-07 13:41:40 +00:00
class binary ( _column ) :
2008-07-22 14:24:36 +00:00
_type = ' binary '
_symbol_c = ' %s '
2008-12-08 14:03:42 +00:00
_symbol_f = lambda symb : symb and Binary ( symb ) or None
2008-07-22 14:24:36 +00:00
_symbol_set = ( _symbol_c , _symbol_f )
2008-12-14 16:46:47 +00:00
_symbol_get = lambda self , x : x and str ( x )
2006-12-07 13:41:40 +00:00
2008-08-29 13:08:14 +00:00
_classic_read = False
2009-08-20 15:29:21 +00:00
_prefetch = False
2008-09-16 07:31:23 +00:00
def __init__ ( self , string = ' unknown ' , filters = None , * * args ) :
_column . __init__ ( self , string = string , * * args )
self . filters = filters
2008-08-29 13:08:14 +00:00
def get_memory ( self , cr , obj , ids , name , user = None , context = None , values = None ) :
if not context :
context = { }
if not values :
values = [ ]
res = { }
for i in ids :
val = None
for v in values :
if v [ ' id ' ] == i :
val = v [ name ]
break
2010-11-04 17:05:23 +00:00
# If client is requesting only the size of the field, we return it instead
# of the content. Presumably a separate request will be done to read the actual
# content if it's needed at some point.
# TODO: after 6.0 we should consider returning a dict with size and content instead of
# having an implicit convention for the value
if val and context . get ( ' bin_size_ %s ' % name , context . get ( ' bin_size ' ) ) :
2009-02-03 21:58:14 +00:00
res [ i ] = tools . human_size ( long ( val ) )
2008-12-14 16:46:47 +00:00
else :
res [ i ] = val
2008-08-29 13:08:14 +00:00
return res
get = get_memory
2008-08-12 14:44:56 +00:00
2008-10-07 14:19:11 +00:00
2006-12-07 13:41:40 +00:00
class selection ( _column ) :
2008-07-22 14:24:36 +00:00
_type = ' selection '
2008-08-12 14:44:56 +00:00
2008-07-22 14:24:36 +00:00
def __init__ ( self , selection , string = ' unknown ' , * * args ) :
_column . __init__ ( self , string = string , * * args )
self . selection = selection
2006-12-07 13:41:40 +00:00
# ---------------------------------------------------------
# Relationals fields
# ---------------------------------------------------------
#
# Values: (0, 0, { fields }) create
2010-06-16 17:58:37 +00:00
# (1, ID, { fields }) update
2006-12-07 13:41:40 +00:00
# (2, ID) remove (delete)
# (3, ID) unlink one (target id or target of relation)
# (4, ID) link
# (5) unlink all (only valid for one2many)
#
#CHECKME: dans la pratique c'est quoi la syntaxe utilisee pour le 5? (5) ou (5, 0)?
class one2one ( _column ) :
2008-07-22 14:24:36 +00:00
_classic_read = False
_classic_write = True
_type = ' one2one '
2008-08-12 14:44:56 +00:00
2008-07-22 14:24:36 +00:00
def __init__ ( self , obj , string = ' unknown ' , * * args ) :
warnings . warn ( " The one2one field doesn ' t work anymore " , DeprecationWarning )
_column . __init__ ( self , string = string , * * args )
self . _obj = obj
def set ( self , cr , obj_src , id , field , act , user = None , context = None ) :
if not context :
2008-08-12 14:44:56 +00:00
context = { }
2008-07-22 14:24:36 +00:00
obj = obj_src . pool . get ( self . _obj )
self . _table = obj_src . pool . get ( self . _obj ) . _table
2008-08-12 14:44:56 +00:00
if act [ 0 ] == 0 :
2008-07-22 14:24:36 +00:00
id_new = obj . create ( cr , user , act [ 1 ] )
2008-12-09 13:35:40 +00:00
cr . execute ( ' update ' + obj_src . _table + ' set ' + field + ' = %s where id= %s ' , ( id_new , id ) )
2008-07-22 14:24:36 +00:00
else :
2008-12-09 13:35:40 +00:00
cr . execute ( ' select ' + field + ' from ' + obj_src . _table + ' where id= %s ' , ( act [ 0 ] , ) )
2008-08-12 14:44:56 +00:00
id = cr . fetchone ( ) [ 0 ]
obj . write ( cr , user , [ id ] , act [ 1 ] , context = context )
2008-07-22 14:24:36 +00:00
2010-01-05 12:03:39 +00:00
def search ( self , cr , obj , args , name , value , offset = 0 , limit = None , uid = None , context = None ) :
return obj . pool . get ( self . _obj ) . search ( cr , uid , args + self . _domain + [ ( ' name ' , ' like ' , value ) ] , offset , limit , context = context )
2008-08-12 14:44:56 +00:00
2006-12-07 13:41:40 +00:00
class many2one ( _column ) :
2008-07-22 14:24:36 +00:00
_classic_read = False
_classic_write = True
_type = ' many2one '
2008-12-14 16:46:47 +00:00
_symbol_c = ' %s '
_symbol_f = lambda x : x or None
_symbol_set = ( _symbol_c , _symbol_f )
2008-08-12 14:44:56 +00:00
2008-07-22 14:24:36 +00:00
def __init__ ( self , obj , string = ' unknown ' , * * args ) :
_column . __init__ ( self , string = string , * * args )
self . _obj = obj
2008-12-18 09:50:58 +00:00
def set_memory ( self , cr , obj , id , field , values , user = None , context = None ) :
obj . datas . setdefault ( id , { } )
obj . datas [ id ] [ field ] = values
2008-07-22 14:24:36 +00:00
def get_memory ( self , cr , obj , ids , name , user = None , context = None , values = None ) :
result = { }
for id in ids :
2010-08-17 13:28:29 +00:00
result [ id ] = obj . datas [ id ] . get ( name , False )
2011-02-02 09:15:46 +00:00
# build a dictionary of the form {'id_of_distant_resource': name_of_distant_resource}
# we use uid=1 because the visibility of a many2one field value (just id and name)
# must be the access right of the parent form and not the linked object itself.
obj = obj . pool . get ( self . _obj )
records = dict ( obj . name_get ( cr , 1 ,
list ( set ( [ x for x in result . values ( ) if x and isinstance ( x , ( int , long ) ) ] ) ) ,
context = context ) )
for id in ids :
if result [ id ] in records :
result [ id ] = ( result [ id ] , records [ result [ id ] ] )
else :
result [ id ] = False
2008-07-22 14:24:36 +00:00
return result
def get ( self , cr , obj , ids , name , user = None , context = None , values = None ) :
2010-11-23 15:44:08 +00:00
if context is None :
context = { }
if values is None :
values = { }
2010-09-18 14:47:32 +00:00
2008-07-22 14:24:36 +00:00
res = { }
for r in values :
res [ r [ ' id ' ] ] = r [ name ]
for id in ids :
res . setdefault ( id , ' ' )
obj = obj . pool . get ( self . _obj )
2010-01-28 13:26:53 +00:00
2008-07-22 14:24:36 +00:00
# build a dictionary of the form {'id_of_distant_resource': name_of_distant_resource}
2010-09-19 09:45:18 +00:00
# we use uid=1 because the visibility of a many2one field value (just id and name)
# must be the access right of the parent form and not the linked object itself.
2011-01-13 01:09:01 +00:00
records = dict ( obj . name_get ( cr , 1 ,
list ( set ( [ x for x in res . values ( ) if isinstance ( x , ( int , long ) ) ] ) ) ,
context = context ) )
2010-09-18 14:47:32 +00:00
for id in res :
2010-09-19 09:45:18 +00:00
if res [ id ] in records :
res [ id ] = ( res [ id ] , records [ res [ id ] ] )
2008-07-22 14:24:36 +00:00
else :
2010-09-18 14:47:32 +00:00
res [ id ] = False
2008-07-22 14:24:36 +00:00
return res
def set ( self , cr , obj_src , id , field , values , user = None , context = None ) :
if not context :
2008-08-12 14:44:56 +00:00
context = { }
2008-07-22 14:24:36 +00:00
obj = obj_src . pool . get ( self . _obj )
self . _table = obj_src . pool . get ( self . _obj ) . _table
2008-10-07 14:19:11 +00:00
if type ( values ) == type ( [ ] ) :
2008-07-22 14:24:36 +00:00
for act in values :
2008-08-12 14:44:56 +00:00
if act [ 0 ] == 0 :
2008-07-22 14:24:36 +00:00
id_new = obj . create ( cr , act [ 2 ] )
2008-12-09 13:35:40 +00:00
cr . execute ( ' update ' + obj_src . _table + ' set ' + field + ' = %s where id= %s ' , ( id_new , id ) )
2008-08-12 14:44:56 +00:00
elif act [ 0 ] == 1 :
2008-07-22 14:24:36 +00:00
obj . write ( cr , [ act [ 1 ] ] , act [ 2 ] , context = context )
2008-08-12 14:44:56 +00:00
elif act [ 0 ] == 2 :
2008-12-09 13:35:40 +00:00
cr . execute ( ' delete from ' + self . _table + ' where id= %s ' , ( act [ 1 ] , ) )
2008-08-12 14:44:56 +00:00
elif act [ 0 ] == 3 or act [ 0 ] == 5 :
2008-12-09 13:35:40 +00:00
cr . execute ( ' update ' + obj_src . _table + ' set ' + field + ' =null where id= %s ' , ( id , ) )
2008-08-12 14:44:56 +00:00
elif act [ 0 ] == 4 :
2008-12-09 13:35:40 +00:00
cr . execute ( ' update ' + obj_src . _table + ' set ' + field + ' = %s where id= %s ' , ( act [ 1 ] , id ) )
2008-07-22 14:24:36 +00:00
else :
if values :
2008-12-09 13:35:40 +00:00
cr . execute ( ' update ' + obj_src . _table + ' set ' + field + ' = %s where id= %s ' , ( values , id ) )
2008-07-22 14:24:36 +00:00
else :
2008-12-09 13:35:40 +00:00
cr . execute ( ' update ' + obj_src . _table + ' set ' + field + ' =null where id= %s ' , ( id , ) )
2008-07-22 14:24:36 +00:00
2010-01-05 12:03:39 +00:00
def search ( self , cr , obj , args , name , value , offset = 0 , limit = None , uid = None , context = None ) :
return obj . pool . get ( self . _obj ) . search ( cr , uid , args + self . _domain + [ ( ' name ' , ' like ' , value ) ] , offset , limit , context = context )
2008-08-12 14:44:56 +00:00
2006-12-07 13:41:40 +00:00
2008-06-15 14:59:23 +00:00
class one2many ( _column ) :
2008-07-22 14:24:36 +00:00
_classic_read = False
_classic_write = False
2009-08-20 15:29:21 +00:00
_prefetch = False
2008-07-22 14:24:36 +00:00
_type = ' one2many '
2008-08-12 14:44:56 +00:00
2010-03-18 16:53:53 +00:00
def __init__ ( self , obj , fields_id , string = ' unknown ' , limit = None , * * args ) :
2008-07-22 14:24:36 +00:00
_column . __init__ ( self , string = string , * * args )
self . _obj = obj
self . _fields_id = fields_id
self . _limit = limit
#one2many can't be used as condition for defaults
assert ( self . change_default != True )
def get_memory ( self , cr , obj , ids , name , user = None , offset = 0 , context = None , values = None ) :
2010-07-22 13:49:48 +00:00
if context is None :
2008-07-22 14:24:36 +00:00
context = { }
2009-04-06 16:05:07 +00:00
if self . _context :
context = context . copy ( )
context . update ( self . _context )
2008-07-22 14:24:36 +00:00
if not values :
values = { }
res = { }
for id in ids :
res [ id ] = [ ]
2009-04-06 16:05:07 +00:00
ids2 = obj . pool . get ( self . _obj ) . search ( cr , user , [ ( self . _fields_id , ' in ' , ids ) ] , limit = self . _limit , context = context )
2008-07-22 14:24:36 +00:00
for r in obj . pool . get ( self . _obj ) . read ( cr , user , ids2 , [ self . _fields_id ] , context = context , load = ' _classic_write ' ) :
if r [ self . _fields_id ] in res :
2008-08-12 14:44:56 +00:00
res [ r [ self . _fields_id ] ] . append ( r [ ' id ' ] )
2008-07-22 14:24:36 +00:00
return res
def set_memory ( self , cr , obj , id , field , values , user = None , context = None ) :
if not context :
2008-08-12 14:44:56 +00:00
context = { }
2009-04-06 16:05:07 +00:00
if self . _context :
context = context . copy ( )
context . update ( self . _context )
2008-07-22 14:24:36 +00:00
if not values :
return
obj = obj . pool . get ( self . _obj )
for act in values :
2008-08-12 14:44:56 +00:00
if act [ 0 ] == 0 :
2008-07-22 14:24:36 +00:00
act [ 2 ] [ self . _fields_id ] = id
obj . create ( cr , user , act [ 2 ] , context = context )
2008-08-12 14:44:56 +00:00
elif act [ 0 ] == 1 :
obj . write ( cr , user , [ act [ 1 ] ] , act [ 2 ] , context = context )
elif act [ 0 ] == 2 :
2008-07-22 14:24:36 +00:00
obj . unlink ( cr , user , [ act [ 1 ] ] , context = context )
2008-08-12 14:44:56 +00:00
elif act [ 0 ] == 3 :
2008-07-22 14:24:36 +00:00
obj . datas [ act [ 1 ] ] [ self . _fields_id ] = False
2008-08-12 14:44:56 +00:00
elif act [ 0 ] == 4 :
2010-12-28 15:53:47 +00:00
obj . datas [ act [ 1 ] ] [ self . _fields_id ] = id
2008-08-12 14:44:56 +00:00
elif act [ 0 ] == 5 :
2008-07-22 14:24:36 +00:00
for o in obj . datas . values ( ) :
2008-08-12 14:44:56 +00:00
if o [ self . _fields_id ] == id :
2008-07-22 14:24:36 +00:00
o [ self . _fields_id ] = False
2008-08-12 14:44:56 +00:00
elif act [ 0 ] == 6 :
2008-07-22 14:24:36 +00:00
for id2 in ( act [ 2 ] or [ ] ) :
obj . datas [ id2 ] [ self . _fields_id ] = id
def search_memory ( self , cr , obj , args , name , value , offset = 0 , limit = None , uid = None , operator = ' like ' , context = None ) :
raise _ ( ' Not Implemented ' )
def get ( self , cr , obj , ids , name , user = None , offset = 0 , context = None , values = None ) :
2010-11-23 15:44:08 +00:00
if context is None :
2008-07-22 14:24:36 +00:00
context = { }
2009-04-06 16:05:07 +00:00
if self . _context :
context = context . copy ( )
context . update ( self . _context )
2010-11-23 15:44:08 +00:00
if values is None :
2008-07-22 14:24:36 +00:00
values = { }
2010-06-11 15:53:17 +00:00
2010-10-13 15:00:40 +00:00
res = { }
for id in ids :
res [ id ] = [ ]
2010-06-11 15:53:17 +00:00
2010-06-11 15:52:14 +00:00
ids2 = obj . pool . get ( self . _obj ) . search ( cr , user , self . _domain + [ ( self . _fields_id , ' in ' , ids ) ] , limit = self . _limit , context = context )
2008-07-22 14:24:36 +00:00
for r in obj . pool . get ( self . _obj ) . _read_flat ( cr , user , ids2 , [ self . _fields_id ] , context = context , load = ' _classic_write ' ) :
2010-10-13 16:39:27 +00:00
if r [ self . _fields_id ] in res :
2010-10-13 15:00:40 +00:00
res [ r [ self . _fields_id ] ] . append ( r [ ' id ' ] )
2008-07-22 14:24:36 +00:00
return res
def set ( self , cr , obj , id , field , values , user = None , context = None ) :
2009-08-20 15:29:21 +00:00
result = [ ]
2008-07-22 14:24:36 +00:00
if not context :
2008-08-12 14:44:56 +00:00
context = { }
2009-04-06 16:05:07 +00:00
if self . _context :
context = context . copy ( )
context . update ( self . _context )
2009-08-20 15:29:21 +00:00
context [ ' no_store_function ' ] = True
2008-07-22 14:24:36 +00:00
if not values :
return
_table = obj . pool . get ( self . _obj ) . _table
obj = obj . pool . get ( self . _obj )
for act in values :
2008-08-12 14:44:56 +00:00
if act [ 0 ] == 0 :
2008-07-22 14:24:36 +00:00
act [ 2 ] [ self . _fields_id ] = id
2009-08-20 15:29:21 +00:00
id_new = obj . create ( cr , user , act [ 2 ] , context = context )
result + = obj . _store_get_values ( cr , user , [ id_new ] , act [ 2 ] . keys ( ) , context )
2008-08-12 14:44:56 +00:00
elif act [ 0 ] == 1 :
obj . write ( cr , user , [ act [ 1 ] ] , act [ 2 ] , context = context )
elif act [ 0 ] == 2 :
2008-07-22 14:24:36 +00:00
obj . unlink ( cr , user , [ act [ 1 ] ] , context = context )
2008-08-12 14:44:56 +00:00
elif act [ 0 ] == 3 :
2008-12-09 13:35:40 +00:00
cr . execute ( ' update ' + _table + ' set ' + self . _fields_id + ' =null where id= %s ' , ( act [ 1 ] , ) )
2008-08-12 14:44:56 +00:00
elif act [ 0 ] == 4 :
2011-02-05 00:43:16 +00:00
# Must use write() to recompute parent_store structure if needed
2011-02-07 10:26:52 +00:00
obj . write ( cr , user , [ act [ 1 ] ] , { self . _fields_id : id } , context = context or { } )
2008-08-12 14:44:56 +00:00
elif act [ 0 ] == 5 :
2008-12-09 13:35:40 +00:00
cr . execute ( ' update ' + _table + ' set ' + self . _fields_id + ' =null where ' + self . _fields_id + ' = %s ' , ( id , ) )
2008-08-12 14:44:56 +00:00
elif act [ 0 ] == 6 :
2011-02-05 00:43:16 +00:00
# Must use write() to recompute parent_store structure if needed
2009-04-27 18:17:01 +00:00
obj . write ( cr , user , act [ 2 ] , { self . _fields_id : id } , context = context or { } )
ids2 = act [ 2 ] or [ 0 ]
2010-06-25 08:49:06 +00:00
cr . execute ( ' select id from ' + _table + ' where ' + self . _fields_id + ' = %s and id <> ALL ( %s ) ' , ( id , ids2 ) )
2009-04-27 18:17:01 +00:00
ids3 = map ( lambda x : x [ 0 ] , cr . fetchall ( ) )
obj . write ( cr , user , ids3 , { self . _fields_id : False } , context = context or { } )
2009-08-20 15:29:21 +00:00
return result
2008-08-12 14:44:56 +00:00
2010-01-05 12:03:39 +00:00
def search ( self , cr , obj , args , name , value , offset = 0 , limit = None , uid = None , operator = ' like ' , context = None ) :
return obj . pool . get ( self . _obj ) . name_search ( cr , uid , value , self . _domain , operator , context = context , limit = limit )
2006-12-07 13:41:40 +00:00
2008-08-12 14:44:56 +00:00
2006-12-07 13:41:40 +00:00
#
# Values: (0, 0, { fields }) create
2010-06-16 17:58:37 +00:00
# (1, ID, { fields }) update (write fields to ID)
# (2, ID) remove (calls unlink on ID, that will also delete the relationship because of the ondelete)
# (3, ID) unlink (delete the relationship between the two objects but does not delete ID)
# (4, ID) link (add a relationship)
2006-12-07 13:41:40 +00:00
# (5, ID) unlink all
# (6, ?, ids) set a list of links
#
class many2many ( _column ) :
2008-07-22 14:24:36 +00:00
_classic_read = False
_classic_write = False
2009-08-20 15:29:21 +00:00
_prefetch = False
2008-07-22 14:24:36 +00:00
_type = ' many2many '
def __init__ ( self , obj , rel , id1 , id2 , string = ' unknown ' , limit = None , * * args ) :
_column . __init__ ( self , string = string , * * args )
self . _obj = obj
2008-10-28 23:33:20 +00:00
if ' . ' in rel :
raise Exception ( _ ( ' The second argument of the many2many field %s must be a SQL table ! ' \
' You used %s , which is not a valid SQL table name. ' ) % ( string , rel ) )
2008-07-22 14:24:36 +00:00
self . _rel = rel
self . _id1 = id1
self . _id2 = id2
self . _limit = limit
def get ( self , cr , obj , ids , name , user = None , offset = 0 , context = None , values = None ) :
if not context :
2008-08-12 14:44:56 +00:00
context = { }
2008-07-22 14:24:36 +00:00
if not values :
2008-08-12 14:44:56 +00:00
values = { }
2008-07-22 14:24:36 +00:00
res = { }
if not ids :
return res
for id in ids :
res [ id ] = [ ]
2010-10-21 13:00:45 +00:00
if offset :
warnings . warn ( " Specifying offset at a many2many.get() may produce unpredictable results. " ,
DeprecationWarning , stacklevel = 2 )
2008-07-22 14:24:36 +00:00
obj = obj . pool . get ( self . _obj )
2010-12-31 06:13:14 +00:00
# static domains are lists, and are evaluated both here and on client-side, while string
2010-10-21 16:31:31 +00:00
# domains supposed by dynamic and evaluated on client-side only (thus ignored here)
# FIXME: make this distinction explicit in API!
domain = isinstance ( self . _domain , list ) and self . _domain or [ ]
wquery = obj . _where_calc ( cr , user , domain , context = context )
2010-10-21 13:00:45 +00:00
obj . _apply_ir_rules ( cr , user , wquery , ' read ' , context = context )
from_c , where_c , where_params = wquery . get_sql ( )
if where_c :
where_c = ' AND ' + where_c
if offset or self . _limit :
order_by = ' ORDER BY " %s " . %s ' % ( obj . _table , obj . _order . split ( ' , ' ) [ 0 ] )
else :
order_by = ' '
limit_str = ' '
if self . _limit is not None :
limit_str = ' LIMIT %d ' % self . _limit
2010-06-15 13:27:22 +00:00
query = ' SELECT %(rel)s . %(id2)s , %(rel)s . %(id1)s \
2010-10-21 13:00:45 +00:00
FROM % ( rel ) s , % ( from_c ) s \
WHERE % ( rel ) s . % ( id1 ) s IN % % s \
2010-06-15 13:27:22 +00:00
AND % ( rel ) s . % ( id2 ) s = % ( tbl ) s . id \
2010-10-21 13:00:45 +00:00
% ( where_c ) s \
% ( order_by ) s \
2010-06-15 13:27:22 +00:00
% ( limit ) s \
OFFSET % ( offset ) d ' \
% { ' rel ' : self . _rel ,
2010-10-21 13:00:45 +00:00
' from_c ' : from_c ,
2010-06-15 13:27:22 +00:00
' tbl ' : obj . _table ,
' id1 ' : self . _id1 ,
' id2 ' : self . _id2 ,
2010-10-21 13:00:45 +00:00
' where_c ' : where_c ,
2010-06-15 13:27:22 +00:00
' limit ' : limit_str ,
2010-10-21 13:00:45 +00:00
' order_by ' : order_by ,
2010-06-15 13:27:22 +00:00
' offset ' : offset ,
}
2010-10-21 13:00:45 +00:00
cr . execute ( query , [ tuple ( ids ) , ] + where_params )
2008-07-22 14:24:36 +00:00
for r in cr . fetchall ( ) :
res [ r [ 1 ] ] . append ( r [ 0 ] )
return res
def set ( self , cr , obj , id , name , values , user = None , context = None ) :
if not context :
2008-08-12 14:44:56 +00:00
context = { }
2008-07-22 14:24:36 +00:00
if not values :
return
obj = obj . pool . get ( self . _obj )
for act in values :
2009-11-11 05:50:28 +00:00
if not ( isinstance ( act , list ) or isinstance ( act , tuple ) ) or not act :
continue
2008-08-12 14:44:56 +00:00
if act [ 0 ] == 0 :
2008-07-22 14:24:36 +00:00
idnew = obj . create ( cr , user , act [ 2 ] )
2008-12-09 13:35:40 +00:00
cr . execute ( ' insert into ' + self . _rel + ' ( ' + self . _id1 + ' , ' + self . _id2 + ' ) values ( %s , %s ) ' , ( id , idnew ) )
2008-08-12 14:44:56 +00:00
elif act [ 0 ] == 1 :
obj . write ( cr , user , [ act [ 1 ] ] , act [ 2 ] , context = context )
elif act [ 0 ] == 2 :
2008-07-22 14:24:36 +00:00
obj . unlink ( cr , user , [ act [ 1 ] ] , context = context )
2008-08-12 14:44:56 +00:00
elif act [ 0 ] == 3 :
2008-12-09 13:35:40 +00:00
cr . execute ( ' delete from ' + self . _rel + ' where ' + self . _id1 + ' = %s and ' + self . _id2 + ' = %s ' , ( id , act [ 1 ] ) )
2008-08-12 14:44:56 +00:00
elif act [ 0 ] == 4 :
2010-12-14 13:28:40 +00:00
# following queries are in the same transaction - so should be relatively safe
cr . execute ( ' SELECT 1 FROM ' + self . _rel + ' WHERE ' + self . _id1 + ' = %s and ' + self . _id2 + ' = %s ' , ( id , act [ 1 ] ) )
if not cr . fetchone ( ) :
cr . execute ( ' insert into ' + self . _rel + ' ( ' + self . _id1 + ' , ' + self . _id2 + ' ) values ( %s , %s ) ' , ( id , act [ 1 ] ) )
2008-08-12 14:44:56 +00:00
elif act [ 0 ] == 5 :
2008-12-09 13:35:40 +00:00
cr . execute ( ' update ' + self . _rel + ' set ' + self . _id2 + ' =null where ' + self . _id2 + ' = %s ' , ( id , ) )
2008-08-12 14:44:56 +00:00
elif act [ 0 ] == 6 :
2008-07-22 14:24:36 +00:00
2009-12-22 20:56:00 +00:00
d1 , d2 , tables = obj . pool . get ( ' ir.rule ' ) . domain_get ( cr , user , obj . _name , context = context )
2008-07-22 14:24:36 +00:00
if d1 :
2009-12-22 21:22:43 +00:00
d1 = ' and ' + ' and ' . join ( d1 )
2009-12-22 21:24:26 +00:00
else :
d1 = ' '
2009-12-22 19:01:58 +00:00
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 )
2008-07-22 14:24:36 +00:00
2008-08-12 14:44:56 +00:00
for act_nbr in act [ 2 ] :
2008-12-09 13:35:40 +00:00
cr . execute ( ' insert into ' + self . _rel + ' ( ' + self . _id1 + ' , ' + self . _id2 + ' ) values ( %s , %s ) ' , ( id , act_nbr ) )
2008-07-22 14:24:36 +00:00
#
# TODO: use a name_search
#
2010-01-05 12:03:39 +00:00
def search ( self , cr , obj , args , name , value , offset = 0 , limit = None , uid = None , operator = ' like ' , context = None ) :
return obj . pool . get ( self . _obj ) . search ( cr , uid , args + self . _domain + [ ( ' name ' , operator , value ) ] , offset , limit , context = context )
2008-07-22 14:24:36 +00:00
def get_memory ( self , cr , obj , ids , name , user = None , offset = 0 , context = None , values = None ) :
result = { }
for id in ids :
result [ id ] = obj . datas [ id ] . get ( name , [ ] )
return result
def set_memory ( self , cr , obj , id , name , values , user = None , context = None ) :
if not values :
return
for act in values :
# TODO: use constants instead of these magic numbers
2008-08-12 14:44:56 +00:00
if act [ 0 ] == 0 :
2008-07-22 14:24:36 +00:00
raise _ ( ' Not Implemented ' )
2008-08-12 14:44:56 +00:00
elif act [ 0 ] == 1 :
2008-07-22 14:24:36 +00:00
raise _ ( ' Not Implemented ' )
2008-08-12 14:44:56 +00:00
elif act [ 0 ] == 2 :
2008-07-22 14:24:36 +00:00
raise _ ( ' Not Implemented ' )
2008-08-12 14:44:56 +00:00
elif act [ 0 ] == 3 :
2008-07-22 14:24:36 +00:00
raise _ ( ' Not Implemented ' )
2008-08-12 14:44:56 +00:00
elif act [ 0 ] == 4 :
2008-07-22 14:24:36 +00:00
raise _ ( ' Not Implemented ' )
2008-08-12 14:44:56 +00:00
elif act [ 0 ] == 5 :
2008-07-22 14:24:36 +00:00
raise _ ( ' Not Implemented ' )
2008-08-12 14:44:56 +00:00
elif act [ 0 ] == 6 :
2008-07-22 14:24:36 +00:00
obj . datas [ id ] [ name ] = act [ 2 ]
2008-06-15 14:59:23 +00:00
2009-09-22 09:30:46 +00:00
def get_nice_size ( a ) :
2009-11-24 14:44:05 +00:00
( x , y ) = a
if isinstance ( y , ( int , long ) ) :
size = y
elif y :
2009-12-01 10:27:53 +00:00
size = len ( y )
2009-11-24 14:44:05 +00:00
else :
2009-12-01 10:27:53 +00:00
size = 0
2009-11-24 14:44:05 +00:00
return ( x , tools . human_size ( size ) )
2009-09-22 09:30:46 +00:00
2010-12-09 23:07:42 +00:00
def sanitize_binary_value ( dict_item ) :
# binary fields should be 7-bit ASCII base64-encoded data,
# but we do additional sanity checks to make sure the values
2011-01-04 11:54:19 +00:00
# are not something else that won't pass via xmlrpc
2010-12-09 23:07:42 +00:00
index , value = dict_item
if isinstance ( value , ( xmlrpclib . Binary , tuple , list , dict ) ) :
# these builtin types are meant to pass untouched
return index , value
# For all other cases, handle the value as a binary string:
# it could be a 7-bit ASCII string (e.g base64 data), but also
# any 8-bit content from files, with byte values that cannot
# be passed inside XML!
# See for more info:
# - http://bugs.python.org/issue10066
# - http://www.w3.org/TR/2000/REC-xml-20001006#NT-Char
#
# One solution is to convert the byte-string to unicode,
# so it gets serialized as utf-8 encoded data (always valid XML)
# If invalid XML byte values were present, tools.ustr() uses
# the Latin-1 codec as fallback, which converts any 8-bit
# byte value, resulting in valid utf-8-encoded bytes
# in the end:
# >>> unicode('\xe1','latin1').encode('utf8') == '\xc3\xa1'
# Note: when this happens, decoding on the other endpoint
# is not likely to produce the expected output, but this is
# just a safety mechanism (in these cases base64 data or
2011-01-04 11:54:19 +00:00
# xmlrpc.Binary values should be used instead)
2010-12-09 23:07:42 +00:00
return index , tools . ustr ( value )
2006-12-07 13:41:40 +00:00
# ---------------------------------------------------------
# Function fields
# ---------------------------------------------------------
class function ( _column ) :
2008-07-22 14:24:36 +00:00
_classic_read = False
_classic_write = False
2009-08-20 15:29:21 +00:00
_prefetch = False
2008-07-22 14:24:36 +00:00
_type = ' function '
_properties = True
2008-08-12 14:44:56 +00:00
2008-08-17 18:28:29 +00:00
#
# multi: compute several fields in one call
#
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 ) :
2008-07-22 14:24:36 +00:00
_column . __init__ ( self , * * args )
self . _obj = obj
self . _method = method
self . _fnct = fnct
self . _fnct_inv = fnct_inv
self . _arg = arg
2008-08-17 18:28:29 +00:00
self . _multi = multi
2008-07-22 14:24:36 +00:00
if ' relation ' in args :
self . _obj = args [ ' relation ' ]
2010-03-08 09:40:36 +00:00
2010-03-06 20:51:54 +00:00
self . digits = args . get ( ' digits ' , ( 16 , 2 ) )
self . digits_compute = args . get ( ' digits_compute ' , None )
2008-07-22 14:24:36 +00:00
self . _fnct_inv_arg = fnct_inv_arg
if not fnct_inv :
self . readonly = 1
self . _type = type
self . _fnct_search = fnct_search
self . store = store
2009-09-17 07:27:12 +00:00
if not fnct_search and not store :
self . selectable = False
2010-03-08 09:40:36 +00:00
2008-12-07 02:16:54 +00:00
if store :
2010-08-13 00:23:21 +00:00
if self . _type != ' many2one ' :
# m2o fields need to return tuples with name_get, not just foreign keys
self . _classic_read = True
2008-12-07 02:16:54 +00:00
self . _classic_write = True
2008-12-16 08:55:33 +00:00
if type == ' binary ' :
self . _symbol_get = lambda x : x and str ( x )
2008-07-22 14:24:36 +00:00
if type == ' float ' :
2008-12-09 13:35:40 +00:00
self . _symbol_c = float . _symbol_c
self . _symbol_f = float . _symbol_f
self . _symbol_set = float . _symbol_set
2008-08-12 14:44:56 +00:00
2010-04-29 12:01:58 +00:00
if type == ' boolean ' :
self . _symbol_c = boolean . _symbol_c
self . _symbol_f = boolean . _symbol_f
self . _symbol_set = boolean . _symbol_set
2011-01-10 09:44:13 +00:00
if type in [ ' integer ' , ' integer_big ' ] :
self . _symbol_c = integer . _symbol_c
self . _symbol_f = integer . _symbol_f
self . _symbol_set = integer . _symbol_set
2010-03-06 20:51:54 +00:00
def digits_change ( self , cr ) :
if self . digits_compute :
t = self . digits_compute ( cr )
self . _symbol_set = ( ' %s ' , lambda x : ( ' % . ' + str ( t [ 1 ] ) + ' f ' ) % ( __builtin__ . float ( x or 0.0 ) , ) )
self . digits = t
2010-01-05 12:03:39 +00:00
def search ( self , cr , uid , obj , name , args , context = None ) :
2008-07-22 14:24:36 +00:00
if not self . _fnct_search :
#CHECKME: should raise an exception
return [ ]
2010-01-05 12:03:39 +00:00
return self . _fnct_search ( obj , cr , uid , obj , name , args , context = context )
2008-07-22 14:24:36 +00:00
def get ( self , cr , obj , ids , name , user = None , context = None , values = None ) :
2010-11-23 15:44:08 +00:00
if context is None :
2008-08-12 14:44:56 +00:00
context = { }
2010-11-23 15:44:08 +00:00
if values is None :
2008-08-12 14:44:56 +00:00
values = { }
2008-07-22 14:24:36 +00:00
res = { }
if self . _method :
2008-09-09 13:22:10 +00:00
res = self . _fnct ( obj , cr , user , ids , name , self . _arg , context )
2008-07-22 14:24:36 +00:00
else :
2008-09-09 13:22:10 +00:00
res = self . _fnct ( cr , obj . _table , ids , name , self . _arg , context )
2008-09-16 07:31:23 +00:00
2009-08-27 14:19:22 +00:00
if self . _type == " many2one " :
# Filtering only integer/long values if passed
res_ids = [ x for x in res . values ( ) if x and isinstance ( x , ( int , long ) ) ]
2010-03-08 09:40:36 +00:00
2009-08-27 14:19:22 +00:00
if res_ids :
obj_model = obj . pool . get ( self . _obj )
dict_names = dict ( obj_model . name_get ( cr , user , res_ids , context ) )
for r in res . keys ( ) :
if res [ r ] and res [ r ] in dict_names :
res [ r ] = ( res [ r ] , dict_names [ res [ r ] ] )
2010-03-08 09:40:36 +00:00
2010-12-09 23:07:42 +00:00
if self . _type == ' binary ' :
if context . get ( ' bin_size ' , False ) :
# client requests only the size of binary fields
res = dict ( map ( get_nice_size , res . items ( ) ) )
else :
res = dict ( map ( sanitize_binary_value , res . items ( ) ) )
2009-12-23 06:46:55 +00:00
if self . _type == " integer " :
for r in res . keys ( ) :
# Converting value into string so that it does not affect XML-RPC Limits
2010-04-02 12:49:05 +00:00
if isinstance ( res [ r ] , dict ) : # To treat integer values with _multi attribute
for record in res [ r ] . keys ( ) :
res [ r ] [ record ] = str ( res [ r ] [ record ] )
else :
res [ r ] = str ( res [ r ] )
2008-09-09 13:22:10 +00:00
return res
2009-03-27 12:56:20 +00:00
get_memory = get
2008-07-22 14:24:36 +00:00
def set ( self , cr , obj , id , name , value , user = None , context = None ) :
if not context :
2008-08-12 14:44:56 +00:00
context = { }
2008-07-22 14:24:36 +00:00
if self . _fnct_inv :
self . _fnct_inv ( obj , cr , user , id , name , value , self . _fnct_inv_arg , context )
2009-03-27 12:56:20 +00:00
set_memory = set
2006-12-07 13:41:40 +00:00
2008-09-18 11:28:57 +00:00
# ---------------------------------------------------------
# Related fields
# ---------------------------------------------------------
class related ( function ) :
2010-07-22 13:49:48 +00:00
def _fnct_search ( self , tobj , cr , uid , obj = None , name = None , domain = None , context = None ) :
2008-12-09 08:03:55 +00:00
self . _field_get2 ( cr , uid , obj , context )
i = len ( self . _arg ) - 1
sarg = name
while i > 0 :
if type ( sarg ) in [ type ( [ ] ) , type ( ( 1 , ) ) ] :
where = [ ( self . _arg [ i ] , ' in ' , sarg ) ]
2008-10-03 10:55:11 +00:00
else :
2008-12-09 08:03:55 +00:00
where = [ ( self . _arg [ i ] , ' = ' , sarg ) ]
if domain :
where = map ( lambda x : ( self . _arg [ i ] , x [ 1 ] , x [ 2 ] ) , domain )
domain = [ ]
sarg = obj . pool . get ( self . _relations [ i ] [ ' object ' ] ) . search ( cr , uid , where , context = context )
i - = 1
return [ ( self . _arg [ 0 ] , ' in ' , sarg ) ]
2008-12-12 13:05:11 +00:00
def _fnct_write ( self , obj , cr , uid , ids , field_name , values , args , context = None ) :
2010-11-12 14:10:41 +00:00
self . _field_get2 ( cr , uid , obj , context = context )
2010-08-10 09:59:48 +00:00
if type ( ids ) != type ( [ ] ) :
ids = [ ids ]
objlst = obj . browse ( cr , uid , ids )
for data in objlst :
t_id = data . id
t_data = data
for i in range ( len ( self . arg ) ) :
if not t_data : break
field_detail = self . _relations [ i ]
if not t_data [ self . arg [ i ] ] :
if self . _type not in ( ' one2many ' , ' many2many ' ) :
2010-01-25 12:34:53 +00:00
t_id = t_data [ ' id ' ]
2010-08-10 09:59:48 +00:00
t_data = False
elif field_detail [ ' type ' ] in ( ' one2many ' , ' many2many ' ) :
if self . _type != " many2one " :
t_id = t_data . id
t_data = t_data [ self . arg [ i ] ] [ 0 ]
else :
t_data = False
else :
t_id = t_data [ ' id ' ]
t_data = t_data [ self . arg [ i ] ]
else :
model = obj . pool . get ( self . _relations [ - 1 ] [ ' object ' ] )
model . write ( cr , uid , [ t_id ] , { args [ - 1 ] : values } , context = context )
2008-09-18 11:28:57 +00:00
2008-10-07 14:19:11 +00:00
def _fnct_read ( self , obj , cr , uid , ids , field_name , args , context = None ) :
2008-12-09 08:03:55 +00:00
self . _field_get2 ( cr , uid , obj , context )
2008-09-19 06:30:20 +00:00
if not ids : return { }
2008-10-07 14:19:11 +00:00
relation = obj . _name
2010-07-27 09:39:09 +00:00
if self . _type in ( ' one2many ' , ' many2many ' ) :
2010-09-22 09:23:49 +00:00
res = dict ( [ ( i , [ ] ) for i in ids ] )
2010-07-27 09:39:09 +00:00
else :
res = { } . fromkeys ( ids , False )
2009-12-22 20:56:00 +00:00
2010-06-12 22:35:30 +00:00
objlst = obj . browse ( cr , 1 , ids , context = context )
2008-09-19 06:30:20 +00:00
for data in objlst :
2008-12-17 05:44:27 +00:00
if not data :
continue
2008-10-07 14:19:11 +00:00
t_data = data
relation = obj . _name
2008-09-19 06:30:20 +00:00
for i in range ( len ( self . arg ) ) :
2008-12-09 08:03:55 +00:00
field_detail = self . _relations [ i ]
relation = field_detail [ ' object ' ]
2008-12-16 18:58:24 +00:00
try :
if not t_data [ self . arg [ i ] ] :
t_data = False
break
except :
2008-09-19 06:30:20 +00:00
t_data = False
break
2009-12-01 13:19:08 +00:00
if field_detail [ ' type ' ] in ( ' one2many ' , ' many2many ' ) and i != len ( self . arg ) - 1 :
2008-10-07 14:19:11 +00:00
t_data = t_data [ self . arg [ i ] ] [ 0 ]
2010-07-27 09:39:09 +00:00
elif t_data :
2008-10-07 14:19:11 +00:00
t_data = t_data [ self . arg [ i ] ]
2008-09-19 06:30:20 +00:00
if type ( t_data ) == type ( objlst [ 0 ] ) :
2009-01-02 12:01:53 +00:00
res [ data . id ] = t_data . id
2010-07-27 09:39:09 +00:00
elif t_data :
2008-10-07 14:19:11 +00:00
res [ data . id ] = t_data
2009-01-02 12:01:53 +00:00
if self . _type == ' many2one ' :
ids = filter ( None , res . values ( ) )
if ids :
2010-06-12 22:35:30 +00:00
ng = dict ( obj . pool . get ( self . _obj ) . name_get ( cr , 1 , ids , context = context ) )
2009-01-02 12:01:53 +00:00
for r in res :
if res [ r ] :
res [ r ] = ( res [ r ] , ng [ res [ r ] ] )
2009-12-01 13:19:08 +00:00
elif self . _type in ( ' one2many ' , ' many2many ' ) :
for r in res :
if res [ r ] :
res [ r ] = [ x . id for x in res [ r ] ]
2008-09-18 11:28:57 +00:00
return res
2008-09-19 06:30:20 +00:00
2008-10-07 14:19:11 +00:00
def __init__ ( self , * arg , * * args ) :
2008-09-19 06:30:20 +00:00
self . arg = arg
2008-12-09 08:03:55 +00:00
self . _relations = [ ]
2008-12-12 13:05:11 +00:00
super ( related , self ) . __init__ ( self . _fnct_read , arg , self . _fnct_write , fnct_inv_arg = arg , method = True , fnct_search = self . _fnct_search , * * args )
2009-12-09 09:52:19 +00:00
if self . store is True :
# TODO: improve here to change self.store = {...} according to related objects
pass
2008-09-18 11:28:57 +00:00
2010-07-22 13:49:48 +00:00
def _field_get2 ( self , cr , uid , obj , context = None ) :
2008-12-09 08:03:55 +00:00
if self . _relations :
return
obj_name = obj . _name
for i in range ( len ( self . _arg ) ) :
f = obj . pool . get ( obj_name ) . fields_get ( cr , uid , [ self . _arg [ i ] ] , context = context ) [ self . _arg [ i ] ]
self . _relations . append ( {
' object ' : obj_name ,
' type ' : f [ ' type ' ]
} )
if f . get ( ' relation ' , False ) :
obj_name = f [ ' relation ' ]
self . _relations [ - 1 ] [ ' relation ' ] = f [ ' relation ' ]
2008-08-12 14:44:56 +00:00
2009-09-17 07:27:12 +00:00
# ---------------------------------------------------------
# Dummy fields
# ---------------------------------------------------------
class dummy ( function ) :
2010-07-22 13:49:48 +00:00
def _fnct_search ( self , tobj , cr , uid , obj = None , name = None , domain = None , context = None ) :
2009-09-17 07:27:12 +00:00
return [ ]
2010-07-22 13:49:48 +00:00
def _fnct_write ( self , obj , cr , uid , ids , field_name , values , args , context = None ) :
2009-09-17 07:27:12 +00:00
return False
def _fnct_read ( self , obj , cr , uid , ids , field_name , args , context = None ) :
return { }
2010-03-08 09:40:36 +00:00
2009-09-17 07:27:12 +00:00
def __init__ ( self , * arg , * * args ) :
self . arg = arg
self . _relations = [ ]
super ( dummy , self ) . __init__ ( self . _fnct_read , arg , self . _fnct_write , fnct_inv_arg = arg , method = True , fnct_search = None , * * args )
2006-12-07 13:41:40 +00:00
# ---------------------------------------------------------
# Serialized fields
# ---------------------------------------------------------
class serialized ( _column ) :
2008-07-22 14:24:36 +00:00
def __init__ ( self , string = ' unknown ' , serialize_func = repr , deserialize_func = eval , type = ' text ' , * * args ) :
self . _serialize_func = serialize_func
self . _deserialize_func = deserialize_func
self . _type = type
self . _symbol_set = ( self . _symbol_c , self . _serialize_func )
self . _symbol_get = self . _deserialize_func
super ( serialized , self ) . __init__ ( string = string , * * args )
2006-12-07 13:41:40 +00:00
2007-08-30 14:22:03 +00:00
2010-09-18 10:19:58 +00:00
# TODO: review completly this class for speed improvement
2006-12-07 13:41:40 +00:00
class property ( function ) :
2007-08-30 14:22:03 +00:00
2010-05-04 14:46:42 +00:00
def _get_default ( self , obj , cr , uid , prop_name , context = None ) :
2010-09-19 12:34:45 +00:00
return self . _get_defaults ( obj , cr , uid , [ prop_name ] , context = None ) [ 0 ] [ prop_name ]
2010-06-10 16:20:17 +00:00
2010-09-18 11:07:31 +00:00
def _get_defaults ( self , obj , cr , uid , prop_name , context = None ) :
2010-05-04 14:46:42 +00:00
prop = obj . pool . get ( ' ir.property ' )
2010-09-18 11:07:31 +00:00
domain = [ ( ' fields_id.model ' , ' = ' , obj . _name ) , ( ' fields_id.name ' , ' in ' , prop_name ) , ( ' res_id ' , ' = ' , False ) ]
2010-09-19 13:32:09 +00:00
ids = prop . search ( cr , uid , domain , context = context )
2010-09-19 12:34:45 +00:00
replaces = { }
2010-09-18 11:07:31 +00:00
default_value = { } . fromkeys ( prop_name , False )
for prop_rec in prop . browse ( cr , uid , ids , context = context ) :
2010-09-19 12:34:45 +00:00
if default_value . get ( prop_rec . fields_id . name , False ) :
2010-09-18 11:07:31 +00:00
continue
2010-09-19 12:34:45 +00:00
value = prop . get_by_record ( cr , uid , prop_rec , context = context ) or False
default_value [ prop_rec . fields_id . name ] = value
if value and ( prop_rec . type == ' many2one ' ) :
replaces . setdefault ( value . _name , { } )
replaces [ value . _name ] [ value . id ] = True
return default_value , replaces
2010-05-04 14:46:42 +00:00
def _get_by_id ( self , obj , cr , uid , prop_name , ids , context = None ) :
prop = obj . pool . get ( ' ir.property ' )
vids = [ obj . _name + ' , ' + str ( oid ) for oid in ids ]
2010-09-19 12:34:45 +00:00
domain = [ ( ' fields_id.model ' , ' = ' , obj . _name ) , ( ' fields_id.name ' , ' in ' , prop_name ) ]
#domain = prop._get_domain(cr, uid, prop_name, obj._name, context)
2010-09-21 08:55:49 +00:00
if vids :
2010-05-05 10:02:41 +00:00
domain = [ ( ' res_id ' , ' in ' , vids ) ] + domain
2010-09-21 08:55:49 +00:00
return prop . search ( cr , uid , domain , context = context )
2010-05-04 14:46:42 +00:00
2010-09-18 14:35:59 +00:00
# TODO: to rewrite more clean
2010-05-04 14:46:42 +00:00
def _fnct_write ( self , obj , cr , uid , id , prop_name , id_val , obj_dest , context = None ) :
if context is None :
2008-08-12 14:44:56 +00:00
context = { }
2008-07-22 14:24:36 +00:00
2010-09-19 12:34:45 +00:00
nids = self . _get_by_id ( obj , cr , uid , [ prop_name ] , [ id ] , context )
2010-05-04 14:46:42 +00:00
if nids :
cr . execute ( ' DELETE FROM ir_property WHERE id IN %s ' , ( tuple ( nids ) , ) )
default_val = self . _get_default ( obj , cr , uid , prop_name , context )
2010-06-10 16:20:17 +00:00
if id_val is not default_val :
2010-05-04 14:46:42 +00:00
def_id = self . _field_get ( cr , uid , obj . _name , prop_name )
company = obj . pool . get ( ' res.company ' )
cid = company . _company_default_get ( cr , uid , obj . _name , def_id ,
context = context )
propdef = obj . pool . get ( ' ir.model.fields ' ) . browse ( cr , uid , def_id ,
context = context )
prop = obj . pool . get ( ' ir.property ' )
return prop . create ( cr , uid , {
2008-07-22 14:24:36 +00:00
' name ' : propdef . name ,
2010-05-04 14:46:42 +00:00
' value ' : id_val ,
2008-07-22 14:24:36 +00:00
' res_id ' : obj . _name + ' , ' + str ( id ) ,
2010-05-04 14:46:42 +00:00
' company_id ' : cid ,
' fields_id ' : def_id ,
2010-05-05 10:02:41 +00:00
' type ' : self . _type ,
2010-09-19 12:34:45 +00:00
} , context = context )
2010-05-04 14:46:42 +00:00
return False
2008-07-22 14:24:36 +00:00
2010-05-04 14:46:42 +00:00
def _fnct_read ( self , obj , cr , uid , ids , prop_name , obj_dest , context = None ) :
2010-06-10 16:20:17 +00:00
properties = obj . pool . get ( ' ir.property ' )
2010-09-18 11:07:31 +00:00
domain = [ ( ' fields_id.model ' , ' = ' , obj . _name ) , ( ' fields_id.name ' , ' in ' , prop_name ) ]
2010-09-18 10:19:58 +00:00
domain + = [ ( ' res_id ' , ' in ' , [ obj . _name + ' , ' + str ( oid ) for oid in ids ] ) ]
nids = properties . search ( cr , uid , domain , context = context )
2010-09-19 12:34:45 +00:00
default_val , replaces = self . _get_defaults ( obj , cr , uid , prop_name , context )
2008-07-22 14:24:36 +00:00
res = { }
for id in ids :
2010-09-18 11:07:31 +00:00
res [ id ] = default_val . copy ( )
2010-09-18 14:35:59 +00:00
brs = properties . browse ( cr , uid , nids , context = context )
for prop in brs :
2010-09-18 10:19:58 +00:00
value = properties . get_by_record ( cr , uid , prop , context = context )
2011-01-14 23:05:24 +00:00
res [ prop . res_id . id ] [ prop . fields_id . name ] = value or False
2010-09-19 12:34:45 +00:00
if value and ( prop . type == ' many2one ' ) :
2011-01-14 23:05:24 +00:00
record_exists = obj . pool . get ( value . _name ) . exists ( cr , uid , value . id )
if record_exists :
replaces . setdefault ( value . _name , { } )
replaces [ value . _name ] [ value . id ] = True
else :
res [ prop . res_id . id ] [ prop . fields_id . name ] = False
2010-09-18 14:35:59 +00:00
for rep in replaces :
2010-12-28 16:07:28 +00:00
nids = obj . pool . get ( rep ) . search ( cr , uid , [ ( ' id ' , ' in ' , replaces [ rep ] . keys ( ) ) ] , context = context )
replaces [ rep ] = dict ( obj . pool . get ( rep ) . name_get ( cr , uid , nids , context = context ) )
2010-09-18 14:35:59 +00:00
2010-09-19 12:34:45 +00:00
for prop in prop_name :
for id in ids :
if res [ id ] [ prop ] and hasattr ( res [ id ] [ prop ] , ' _name ' ) :
res [ id ] [ prop ] = ( res [ id ] [ prop ] . id , replaces [ res [ id ] [ prop ] . _name ] . get ( res [ id ] [ prop ] . id , False ) )
2010-09-18 14:35:59 +00:00
2008-07-22 14:24:36 +00:00
return res
2010-05-04 14:46:42 +00:00
2008-07-22 14:24:36 +00:00
def _field_get ( self , cr , uid , model_name , prop ) :
if not self . field_id . get ( cr . dbname ) :
cr . execute ( ' SELECT id \
FROM ir_model_fields \
WHERE name = % s AND model = % s ' , (prop, model_name))
res = cr . fetchone ( )
self . field_id [ cr . dbname ] = res and res [ 0 ]
return self . field_id [ cr . dbname ]
def __init__ ( self , obj_prop , * * args ) :
2010-05-05 10:02:41 +00:00
# TODO remove obj_prop parameter (use many2one type)
2008-07-22 14:24:36 +00:00
self . field_id = { }
function . __init__ ( self , self . _fnct_read , False , self . _fnct_write ,
2010-09-18 11:07:31 +00:00
obj_prop , multi = ' properties ' , * * args )
2008-07-22 14:24:36 +00:00
def restart ( self ) :
self . field_id = { }
2008-07-08 08:13:12 +00:00
2008-07-23 15:01:27 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: