From 1fc19ec86cd8ede60b7282d9e062925651459169 Mon Sep 17 00:00:00 2001 From: "Yogesh (OpenERP)" Date: Mon, 23 May 2011 14:31:13 +0530 Subject: [PATCH 1/4] [FIX] fields.function work in multi mode for binary type. return field name with size on multi mode in get_nice_size method. bzr revid: ysa@tinyerp.com-20110523090113-tryh2f1c760sf8er --- openerp/osv/fields.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/openerp/osv/fields.py b/openerp/osv/fields.py index 5d60d299d55..b33d44b7b60 100644 --- a/openerp/osv/fields.py +++ b/openerp/osv/fields.py @@ -687,12 +687,19 @@ class many2many(_column): def get_nice_size(a): (x,y) = a + size = 0 + if y and isinstance(y, dict): + if y.keys() and isinstance(y.get(y.keys()[0]), (int,long)): + size = y.get(y.keys()[0],0) + elif y.keys() and y.get(y.keys()[0]): + size = len(y.get(y.keys()[0])) + y.update({y.keys()[0]: tools.human_size(size)}) + return (x, y) + if isinstance(y, (int,long)): size = y elif y: size = len(y) - else: - size = 0 return (x, tools.human_size(size)) def sanitize_binary_value(dict_item): @@ -833,9 +840,9 @@ class function(_column): # Converting value into string so that it does not affect XML-RPC Limits 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]) + res[r][record] = tools.ustr(res[r][record]) else: - res[r] = str(res[r]) + res[r] = tools.ustr(res[r]) return res get_memory = get From a0eba446d9f6c16e13b2bd10729d616d6b18035f Mon Sep 17 00:00:00 2001 From: "Yogesh (OpenERP)" Date: Mon, 13 Jun 2011 16:49:49 +0530 Subject: [PATCH 2/4] [IMP] improve get method of function field. bzr revid: ysa@tinyerp.com-20110613111949-u71pez6qpdfwk1iy --- openerp/osv/fields.py | 71 ++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 42 deletions(-) diff --git a/openerp/osv/fields.py b/openerp/osv/fields.py index b33d44b7b60..be7b66ca4bb 100644 --- a/openerp/osv/fields.py +++ b/openerp/osv/fields.py @@ -688,14 +688,6 @@ class many2many(_column): def get_nice_size(a): (x,y) = a size = 0 - if y and isinstance(y, dict): - if y.keys() and isinstance(y.get(y.keys()[0]), (int,long)): - size = y.get(y.keys()[0],0) - elif y.keys() and y.get(y.keys()[0]): - size = len(y.get(y.keys()[0])) - y.update({y.keys()[0]: tools.human_size(size)}) - return (x, y) - if isinstance(y, (int,long)): size = y elif y: @@ -706,10 +698,9 @@ 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 # are not something else that won't pass via xmlrpc - index, value = dict_item - if isinstance(value, (xmlrpclib.Binary, tuple, list, dict)): + if isinstance(dict_item, (xmlrpclib.Binary, tuple, dict)): # these builtin types are meant to pass untouched - return index, value + return dict_item # 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 @@ -730,7 +721,7 @@ def sanitize_binary_value(dict_item): # is not likely to produce the expected output, but this is # just a safety mechanism (in these cases base64 data or # xmlrpc.Binary values should be used instead) - return index, tools.ustr(value) + return tools.ustr(dict_item) # --------------------------------------------------------- @@ -806,44 +797,40 @@ class function(_column): return [] return self._fnct_search(obj, cr, uid, obj, name, args, context=context) + def postprocess(self, cr, user, obj, type, value=None, context=None): + if context is None: + context = {} + result = value + if type == 'binary': + if context.get('bin_size', False): + # client requests only the size of binary fields + result = get_nice_size(value) + else: + result = sanitize_binary_value(value) + + if type == "integer": + result = tools.ustr(value) + return result + def get(self, cr, obj, ids, name, user=None, context=None, values=None): if context is None: context = {} if values is None: values = {} - res = {} + result = {} if self._method: - res = self._fnct(obj, cr, user, ids, name, self._arg, context) + result = self._fnct(obj, cr, user, ids, name, self._arg, context) else: - res = self._fnct(cr, obj._table, ids, name, self._arg, context) + result = self._fnct(cr, obj._table, ids, name, self._arg, context) + for id in ids: + if self._multi and result.get(id, {}): + for field, value in result[id].iteritems(): + if value: + result[id][field] = self.postprocess(cr, user, obj, obj._columns[field]._type, value) + elif result.get(id, False) and result[id]: + result[id] = self.postprocess(cr, user, obj, self._type, result[id]) + return result - 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))] - - 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]]) - - 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())) - - if self._type == "integer": - for r in res.keys(): - # Converting value into string so that it does not affect XML-RPC Limits - if isinstance(res[r],dict): # To treat integer values with _multi attribute - for record in res[r].keys(): - res[r][record] = tools.ustr(res[r][record]) - else: - res[r] = tools.ustr(res[r]) - return res get_memory = get def set(self, cr, obj, id, name, value, user=None, context=None): From 2f635092d29fc7132b3eb9ecf3e1c14644069b6a Mon Sep 17 00:00:00 2001 From: "Yogesh (OpenERP)" Date: Wed, 15 Jun 2011 14:17:17 +0530 Subject: [PATCH 3/4] Improve code. bzr revid: ysa@tinyerp.com-20110615084717-ya7v52ngikwn4zy8 --- openerp/osv/fields.py | 47 +++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/openerp/osv/fields.py b/openerp/osv/fields.py index be7b66ca4bb..74a19e7ca49 100644 --- a/openerp/osv/fields.py +++ b/openerp/osv/fields.py @@ -685,22 +685,21 @@ class many2many(_column): obj.datas[id][name] = act[2] -def get_nice_size(a): - (x,y) = a +def get_nice_size(value): size = 0 - if isinstance(y, (int,long)): - size = y - elif y: - size = len(y) - return (x, tools.human_size(size)) + if isinstance(value, (int,long)): + size = value + elif value: + size = len(value) + return tools.human_size(size) -def sanitize_binary_value(dict_item): +def sanitize_binary_value(value): # binary fields should be 7-bit ASCII base64-encoded data, # but we do additional sanity checks to make sure the values # are not something else that won't pass via xmlrpc - if isinstance(dict_item, (xmlrpclib.Binary, tuple, dict)): + if isinstance(value, (xmlrpclib.Binary, tuple, list, dict)): # these builtin types are meant to pass untouched - return dict_item + return 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 @@ -721,7 +720,7 @@ def sanitize_binary_value(dict_item): # is not likely to produce the expected output, but this is # just a safety mechanism (in these cases base64 data or # xmlrpc.Binary values should be used instead) - return tools.ustr(dict_item) + return tools.ustr(value) # --------------------------------------------------------- @@ -797,38 +796,42 @@ class function(_column): return [] return self._fnct_search(obj, cr, uid, obj, name, args, context=context) - def postprocess(self, cr, user, obj, type, value=None, context=None): + def postprocess(self, cr, user, obj, field, value=None, context=None): if context is None: context = {} result = value - if type == 'binary': + field_type = obj._columns[field]._type + if field_type == "many2one" : + if isinstance(value, (int,long)) and hasattr(obj._columns[field], 'relation'): + obj_model = obj.pool.get(obj._columns[field].relation) + dict_names = dict(obj_model.name_get(cr, user, [value], context)) + if value in dict_names: + result = (value, dict_names[value]) + + if field_type == 'binary': if context.get('bin_size', False): # client requests only the size of binary fields result = get_nice_size(value) else: result = sanitize_binary_value(value) - if type == "integer": + if field_type == "integer": result = tools.ustr(value) return result def get(self, cr, obj, ids, name, user=None, context=None, values=None): - if context is None: - context = {} - if values is None: - values = {} result = {} if self._method: result = self._fnct(obj, cr, user, ids, name, self._arg, context) else: result = self._fnct(cr, obj._table, ids, name, self._arg, context) for id in ids: - if self._multi and result.get(id, {}): + if self._multi and id in result: for field, value in result[id].iteritems(): if value: - result[id][field] = self.postprocess(cr, user, obj, obj._columns[field]._type, value) - elif result.get(id, False) and result[id]: - result[id] = self.postprocess(cr, user, obj, self._type, result[id]) + result[id][field] = self.postprocess(cr, user, obj, field, value, context) + elif result.get(id,False): + result[id] = self.postprocess(cr, user, obj, name, result[id], context) return result get_memory = get From 45e8ba84746a5d0f5d890143ed28b1217f56ff9f Mon Sep 17 00:00:00 2001 From: "Yogesh (OpenERP)" Date: Wed, 15 Jun 2011 15:55:51 +0530 Subject: [PATCH 4/4] [IMP] improve code. bzr revid: ysa@tinyerp.com-20110615102551-np3mfev3eq5z4iwp --- openerp/osv/fields.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/openerp/osv/fields.py b/openerp/osv/fields.py index 74a19e7ca49..6663186ea6d 100644 --- a/openerp/osv/fields.py +++ b/openerp/osv/fields.py @@ -796,15 +796,15 @@ class function(_column): return [] return self._fnct_search(obj, cr, uid, obj, name, args, context=context) - def postprocess(self, cr, user, obj, field, value=None, context=None): + def postprocess(self, cr, uid, obj, field, value=None, context=None): if context is None: context = {} result = value field_type = obj._columns[field]._type - if field_type == "many2one" : + if field_type == "many2one": if isinstance(value, (int,long)) and hasattr(obj._columns[field], 'relation'): obj_model = obj.pool.get(obj._columns[field].relation) - dict_names = dict(obj_model.name_get(cr, user, [value], context)) + dict_names = dict(obj_model.name_get(cr, uid, [value], context)) if value in dict_names: result = (value, dict_names[value]) @@ -819,19 +819,19 @@ class function(_column): result = tools.ustr(value) return result - def get(self, cr, obj, ids, name, user=None, context=None, values=None): + def get(self, cr, obj, ids, name, uid=False, context=None, values=None): result = {} if self._method: - result = self._fnct(obj, cr, user, ids, name, self._arg, context) + result = self._fnct(obj, cr, uid, ids, name, self._arg, context) else: result = self._fnct(cr, obj._table, ids, name, self._arg, context) for id in ids: if self._multi and id in result: for field, value in result[id].iteritems(): if value: - result[id][field] = self.postprocess(cr, user, obj, field, value, context) - elif result.get(id,False): - result[id] = self.postprocess(cr, user, obj, name, result[id], context) + result[id][field] = self.postprocess(cr, uid, obj, field, value, context) + elif result.get(id): + result[id] = self.postprocess(cr, uid, obj, name, result[id], context) return result get_memory = get