From 8e73ae8fa0ee1f8a01794d176de984978224c13b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Thu, 19 Mar 2015 19:31:56 +0100 Subject: [PATCH] Use custom object to store Binary tmpfile path Since issue4637, it is possible that the value of a Binary field is a str. So the client can no more make the difference between the value and a path. issue4653 review6181002 --- tryton/gui/window/view_form/model/field.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/tryton/gui/window/view_form/model/field.py b/tryton/gui/window/view_form/model/field.py index 0244d8be1c..a0b1164a37 100644 --- a/tryton/gui/window/view_form/model/field.py +++ b/tryton/gui/window/view_form/model/field.py @@ -837,6 +837,11 @@ class ReferenceField(Field): return screen_domain +class _FileCache(object): + def __init__(self, path): + self.path = path + + class BinaryField(Field): _default = None @@ -844,9 +849,9 @@ class BinaryField(Field): def get(self, record): result = record.value.get(self.name, self._default) - if isinstance(result, basestring): + if isinstance(result, _FileCache): try: - with open(result, 'rb') as fp: + with open(result.path, 'rb') as fp: result = self.cast(fp.read()) except IOError: result = self.get_data(record) @@ -859,7 +864,7 @@ class BinaryField(Field): _, filename = tempfile.mkstemp(prefix='tryton_') with open(filename, 'wb') as fp: fp.write(value or '') - self.set(record, filename) + self.set(record, _FileCache(filename)) record.modified_fields.setdefault(self.name) record.signal('record-modified') self.sig_changed(record) @@ -868,9 +873,9 @@ class BinaryField(Field): def get_size(self, record): result = record.value.get(self.name) or 0 - if isinstance(result, basestring): - result = os.stat(result).st_size - elif isinstance(result, (bytes, bytearray)): + if isinstance(result, _FileCache): + result = os.stat(result.path).st_size + elif isinstance(result, (basestring, bytes, bytearray)): result = len(result) return result @@ -888,7 +893,7 @@ class BinaryField(Field): _, filename = tempfile.mkstemp(prefix='tryton_') with open(filename, 'wb') as fp: fp.write(values[self.name] or '') - self.set(record, filename) + self.set(record, _FileCache(filename)) return self.get(record)