~openerp-community/+junk/openerp-server-trunk-misc

« back to all changes in this revision

Viewing changes to openerp/osv/fields.py

[MERGE] `multi` support for function fields.

Show diffs side-by-side

added added

removed removed

Lines of Context:
692
692
                obj.datas[id][name] = act[2]
693
693
 
694
694
 
695
 
def get_nice_size(a):
696
 
    (x,y) = a
697
 
    if isinstance(y, (int,long)):
698
 
        size = y
699
 
    elif y:
700
 
        size = len(y)
701
 
    else:
702
 
        size = 0
703
 
    return (x, tools.human_size(size))
 
695
def get_nice_size(value):
 
696
    size = 0
 
697
    if isinstance(value, (int,long)):
 
698
        size = value
 
699
    elif value: # this is supposed to be a string
 
700
        size = len(value)
 
701
    return tools.human_size(size)
704
702
 
705
 
def sanitize_binary_value(dict_item):
 
703
def sanitize_binary_value(value):
706
704
    # binary fields should be 7-bit ASCII base64-encoded data,
707
705
    # but we do additional sanity checks to make sure the values
708
706
    # are not something else that won't pass via xmlrpc
709
 
    index, value = dict_item
710
707
    if isinstance(value, (xmlrpclib.Binary, tuple, list, dict)):
711
708
        # these builtin types are meant to pass untouched
712
 
        return index, value
 
709
        return value
713
710
 
714
711
    # For all other cases, handle the value as a binary string:
715
712
    # it could be a 7-bit ASCII string (e.g base64 data), but also
730
727
    # is not likely to produce the expected output, but this is
731
728
    # just a safety mechanism (in these cases base64 data or
732
729
    # xmlrpc.Binary values should be used instead)
733
 
    return index, tools.ustr(value)
 
730
    return tools.ustr(value)
734
731
 
735
732
 
736
733
# ---------------------------------------------------------
806
803
            return []
807
804
        return self._fnct_search(obj, cr, uid, obj, name, args, context=context)
808
805
 
809
 
    def get(self, cr, obj, ids, name, user=None, context=None, values=None):
 
806
    def postprocess(self, cr, uid, obj, field, value=None, context=None):
810
807
        if context is None:
811
808
            context = {}
812
 
        if values is None:
813
 
            values = {}
814
 
        res = {}
815
 
        if self._method:
816
 
            res = self._fnct(obj, cr, user, ids, name, self._arg, context)
817
 
        else:
818
 
            res = self._fnct(cr, obj._table, ids, name, self._arg, context)
819
 
 
820
 
        if self._type == "many2one" :
821
 
            # Filtering only integer/long values if passed
822
 
            res_ids = [x for x in res.values() if x and isinstance(x, (int,long))]
823
 
 
824
 
            if res_ids:
825
 
                obj_model = obj.pool.get(self._obj)
826
 
                dict_names = dict(obj_model.name_get(cr, user, res_ids, context))
827
 
                for r in res.keys():
828
 
                    if res[r] and res[r] in dict_names:
829
 
                        res[r] = (res[r], dict_names[res[r]])
830
 
 
831
 
        if self._type == 'binary':
 
809
        result = value
 
810
        field_type = obj._columns[field]._type
 
811
        if field_type == "many2one":
 
812
            # make the result a tuple if it is not already one
 
813
            if isinstance(value, (int,long)) and hasattr(obj._columns[field], 'relation'):
 
814
                obj_model = obj.pool.get(obj._columns[field].relation)
 
815
                dict_names = dict(obj_model.name_get(cr, uid, [value], context))
 
816
                result = (value, dict_names[value])
 
817
 
 
818
        if field_type == 'binary':
832
819
            if context.get('bin_size', False):
833
820
                # client requests only the size of binary fields
834
 
                res = dict(map(get_nice_size, res.items()))
 
821
                result = get_nice_size(value)
835
822
            else:
836
 
                res = dict(map(sanitize_binary_value, res.items()))
837
 
 
838
 
        if self._type == "integer":
839
 
            for r in res.keys():
840
 
                # Converting value into string so that it does not affect XML-RPC Limits
841
 
                if isinstance(res[r],dict): # To treat integer values with _multi attribute
842
 
                    for record in res[r].keys():
843
 
                        res[r][record] = str(res[r][record])
844
 
                else:
845
 
                    res[r] = str(res[r])
846
 
        return res
 
823
                result = sanitize_binary_value(value)
 
824
 
 
825
        if field_type == "integer":
 
826
            result = tools.ustr(value)
 
827
        return result
 
828
 
 
829
    def get(self, cr, obj, ids, name, uid=False, context=None, values=None):
 
830
        result = {}
 
831
        if self._method:
 
832
            result = self._fnct(obj, cr, uid, ids, name, self._arg, context)
 
833
        else:
 
834
            result = self._fnct(cr, obj._table, ids, name, self._arg, context)
 
835
        for id in ids:
 
836
            if self._multi and id in result:
 
837
                for field, value in result[id].iteritems():
 
838
                    if value:
 
839
                        result[id][field] = self.postprocess(cr, uid, obj, field, value, context)
 
840
            elif result.get(id):
 
841
                result[id] = self.postprocess(cr, uid, obj, name, result[id], context)
 
842
        return result
 
843
 
847
844
    get_memory = get
848
845
 
849
846
    def set(self, cr, obj, id, name, value, user=None, context=None):