changeset 0b7759ed0706 in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset&node=0b7759ed0706
description:
Support Binary field in CSV import/export
issue10350
review346101002
diffstat:
CHANGELOG | 1 +
trytond/model/modelstorage.py | 4 ++++
trytond/tests/import_data.py | 7 +++++++
trytond/tests/test_importdata.py | 20 ++++++++++++++++++++
4 files changed, 32 insertions(+), 0 deletions(-)
diffs (82 lines):
diff -r fae3761b96da -r 0b7759ed0706 CHANGELOG
--- a/CHANGELOG Sun May 16 18:09:28 2021 +0200
+++ b/CHANGELOG Tue May 18 22:08:59 2021 +0200
@@ -1,3 +1,4 @@
+* Support base64 encoded data in ModelStorage.import_data
* Add BOOL_AND and BOOL_OR to SQLite backend
Version 6.0.0 - 2021-05-03
diff -r fae3761b96da -r 0b7759ed0706 trytond/model/modelstorage.py
--- a/trytond/model/modelstorage.py Sun May 16 18:09:28 2021 +0200
+++ b/trytond/model/modelstorage.py Tue May 18 22:08:59 2021 +0200
@@ -1,6 +1,7 @@
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
+import base64
import datetime
import time
import csv
@@ -929,6 +930,9 @@
res = get_one2one(this_field_def['relation'], value)
elif field_type == 'reference':
res = get_reference(value, '/'.join(field))
+ elif (field_type == 'binary'
+ and not isinstance(value, bytes)):
+ res = base64.b64decode(value)
else:
res = value or None
row[field[-1]] = res
diff -r fae3761b96da -r 0b7759ed0706 trytond/tests/import_data.py
--- a/trytond/tests/import_data.py Sun May 16 18:09:28 2021 +0200
+++ b/trytond/tests/import_data.py Tue May 18 22:08:59 2021 +0200
@@ -135,6 +135,12 @@
one2many = fields.Many2One('test.import_data.one2many', 'One2Many')
+class ImportDataBinary(ModelSQL):
+ "Import Data Binary"
+ __name__ = 'test.import_data.binary'
+ data = fields.Binary("Data")
+
+
class ImportDataReferenceSelection(ModelSQL):
"Import Data Reference Selection"
__name__ = 'test.import_data.reference.selection'
@@ -180,5 +186,6 @@
ImportDataOne2ManyTarget,
ImportDataReferenceSelection,
ImportDataReference,
+ ImportDataBinary,
ImportDataUpdate,
module=module, type_='model')
diff -r fae3761b96da -r 0b7759ed0706 trytond/tests/test_importdata.py
--- a/trytond/tests/test_importdata.py Sun May 16 18:09:28 2021 +0200
+++ b/trytond/tests/test_importdata.py Tue May 18 22:08:59 2021 +0200
@@ -564,6 +564,26 @@
transaction.rollback()
@with_transaction()
+ def test_binary_bytes(self):
+ "Test binary bytes"
+ pool = Pool()
+ Binary = pool.get('test.import_data.binary')
+
+ self.assertEqual(Binary.import_data(['data'], [[b'data']]), 1)
+ record, = Binary.search([])
+ self.assertEqual(record.data, b'data')
+
+ @with_transaction()
+ def test_binary_base64(self):
+ "Test binary base64"
+ pool = Pool()
+ Binary = pool.get('test.import_data.binary')
+
+ self.assertEqual(Binary.import_data(['data'], [['ZGF0YQ==']]), 1)
+ record, = Binary.search([])
+ self.assertEqual(record.data, b'data')
+
+ @with_transaction()
def test_update_id(self):
"Test update with ID"
pool = Pool()