[IMP]: allow binary function fields to directly return size.
authorP. Christeas <p_christ@hol.gr>
Tue, 22 Sep 2009 09:30:46 +0000 (12:30 +0300)
committerP. Christeas <p_christ@hol.gr>
Tue, 22 Sep 2009 09:30:46 +0000 (12:30 +0300)
As used in document, function fields could have a shortcut method to
return the size instead of the contents. If done so and return a number,
use that instead.

bzr revid: p_christ@hol.gr-20090922093046-9r0gsj0qenapi06o

bin/osv/fields.py

index d6d23f7..0ca657e 100644 (file)
@@ -583,6 +583,16 @@ class many2many(_column):
                 obj.datas[id][name] = act[2]
 
 
+def get_nice_size(a):
+       (x,y) = a
+       if isinstance(y, (int,long)):
+               size = y
+       elif y:
+               y = len(y)
+       else:
+               y = 0
+       return (x, tools.human_size(size))
+
 # ---------------------------------------------------------
 # Function fields
 # ---------------------------------------------------------
@@ -653,7 +663,7 @@ class function(_column):
             
         if self._type == 'binary' and context.get('bin_size', False):
             # convert the data returned by the function with the size of that data...
-            res = dict(map(lambda (x, y): (x, tools.human_size(len(y or ''))), res.items()))
+            res = dict(map( get_nice_size, res.items()))
         return res
     get_memory = get