From ab2ad3bd0e920bcf6579db9b7ce19e5aa5fdf384 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sat, 16 Feb 2008 16:36:03 +0100 Subject: [PATCH] Improve webdav size compute --- trytond/ir/attachment.py | 14 +++++++++++--- trytond/webdav/webdav.py | 4 ++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/trytond/ir/attachment.py b/trytond/ir/attachment.py index 8376473b0e..0e4d5fb7d6 100644 --- a/trytond/ir/attachment.py +++ b/trytond/ir/attachment.py @@ -20,6 +20,8 @@ class Attachment(OSV): 'link': fields.Char('Link', size=256), 'digest': fields.Char('Digest', size=32), 'collision': fields.Integer('Collision'), + 'datas_size': fields.Function('datas', type='integer', + string='Datas size'), } _defaults = { 'collision': lambda *a: 0, @@ -35,15 +37,21 @@ class Attachment(OSV): db_name = cursor.dbname for attachment in self.browse(cursor, user, ids, context=context): value = False + if name == 'datas_size': + value = 0 if attachment.digest: filename = attachment.digest if attachment.collision: filename = filename + '-' + str(attachment.collision) filename = os.path.join(CONFIG['data_path'], db_name, filename[0:2], filename[2:4], filename) - file_p = file(filename, 'rb') - value = base64.encodestring(file_p.read()) - file_p.close() + if name == 'datas_size': + statinfo = os.stat(filename) + value = statinfo.st_size + else: + file_p = file(filename, 'rb') + value = base64.encodestring(file_p.read()) + file_p.close() res[attachment.id] = value return res diff --git a/trytond/webdav/webdav.py b/trytond/webdav/webdav.py index 6a11b7ced3..35de65245c 100644 --- a/trytond/webdav/webdav.py +++ b/trytond/webdav/webdav.py @@ -140,8 +140,8 @@ class Directory(OSV): attachment_obj = self.pool.get('ir.attachment') attachment = attachment_obj.browse(cursor, user, object_id, context=context) - if attachment.datas: - return str(len(attachment.datas)) + if attachment.datas_size: + return str(attachment.datas_size) return '0' def get_contenttype(self, cursor, user, uri, context=None):