From e33baecfa3eff21a74a55f3b48b7a5cec99b9ddf Mon Sep 17 00:00:00 2001 From: Nicolas Lempereur Date: Tue, 7 Jul 2015 09:50:18 +0200 Subject: [PATCH] [FIX] osv: binary size already in human format In version 8.0, postgresql's pg_size_pretty function is used (http://www.postgresql.org/docs/9.4/static/functions-admin.html) when getting the size of a binary field when reading if `bin_size` or `bin_size_[col_name]` is set in the context. So in 8.0 the size of a binary field get units bytes, kB, MB, GB and TB which was not taken into account. e.g: '5.3 GB'. This fix uses the size of the string to choose to differenciate the two. e.g: '10000 bytes' (the longest size) will be returned directly, but for something longer the human size of the content length will be returned. There is a corner case if a file is shorter than 12 bytes but it is an enough of a small scenario with small implications that it is deemed acceptable. closes #7485 opw-644085 --- openerp/addons/base/ir/ir_attachment.py | 3 --- openerp/osv/fields.py | 2 ++ 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/openerp/addons/base/ir/ir_attachment.py b/openerp/addons/base/ir/ir_attachment.py index 82ec746ec6b..a4927931d08 100644 --- a/openerp/addons/base/ir/ir_attachment.py +++ b/openerp/addons/base/ir/ir_attachment.py @@ -162,9 +162,6 @@ class ir_attachment(osv.osv): result[attach.id] = self._file_read(cr, uid, attach.store_fname, bin_size) else: result[attach.id] = attach.db_datas - if bin_size: - result[attach.id] = int(result[attach.id] or 0) - return result def _data_set(self, cr, uid, id, name, value, arg, context=None): diff --git a/openerp/osv/fields.py b/openerp/osv/fields.py index b2f71142638..5ef82ed2683 100644 --- a/openerp/osv/fields.py +++ b/openerp/osv/fields.py @@ -1069,6 +1069,8 @@ def get_nice_size(value): size = value elif value: # this is supposed to be a string size = len(value) + if size < 12: # suppose human size + return value return tools.human_size(size) # See http://www.w3.org/TR/2000/REC-xml-20001006#NT-Char