From 7757a63fc24cbe84016cb68ad8b1a8ca54006fd4 Mon Sep 17 00:00:00 2001 From: "jerome hanke (jhk)" Date: Thu, 26 Dec 2019 12:43:36 +0000 Subject: [PATCH] [FIX] base_import: handling of scientific notation Steps to reproduce: -install sales -try to import a product file with volumes set to scientific notation (9.2e-05 for example) Previous behavior: scientific notation is not recognized by the base_import module and raises a small warning Current behavior: scientific notation is converted to decimal notation on the fly when possible opw-2162353 closes odoo/odoo#42591 X-original-commit: 9acd76c5d116abd07207af3b548658ad983ff82d Signed-off-by: Jorge Pinna Puissant (jpp) --- addons/base_import/models/base_import.py | 10 ++++++++++ addons/base_import/tests/test_base_import.py | 21 ++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/addons/base_import/models/base_import.py b/addons/base_import/models/base_import.py index 81f1ffa89f4..041d5ef2dd1 100644 --- a/addons/base_import/models/base_import.py +++ b/addons/base_import/models/base_import.py @@ -723,6 +723,16 @@ class Import(models.TransientModel): if not line[index]: continue thousand_separator, decimal_separator = self._infer_separators(line[index], options) + + if 'E' in line[index] or 'e' in line[index]: + tmp_value = line[index].replace(thousand_separator, '.') + try: + tmp_value = '{:f}'.format(float(tmp_value)) + line[index] = tmp_value + thousand_separator = ' ' + except Exception: + pass + line[index] = line[index].replace(thousand_separator, '').replace(decimal_separator, '.') old_value = line[index] line[index] = self._remove_currency_symbol(line[index]) diff --git a/addons/base_import/tests/test_base_import.py b/addons/base_import/tests/test_base_import.py index 3fe12d4b7bb..d4cbbbd866c 100644 --- a/addons/base_import/tests/test_base_import.py +++ b/addons/base_import/tests/test_base_import.py @@ -468,6 +468,27 @@ class test_convert_import_data(TransactionCase): self.assertEqual(float(result[0][-1]), 5.69) self.assertEqual(str(result[0][-2]), '2017-10-12') + def test_parse_scientific_notation(self): + """ Ensure that scientific notation is correctly converted to decimal """ + import_wizard = self.env['base_import.import'] + + test_options = {} + test_data = [ + ["1E+05"], + ["1.20E-05"], + ["1,9e5"], + ["9,5e-5"], + ] + expected_result = [ + ["100000.000000"], + ["0.000012"], + ["190000.000000"], + ["0.000095"], + ] + + import_wizard._parse_float_from_data(test_data, 0, 'test-name', test_options) + self.assertEqual(test_data, expected_result) + def test_filtered(self): """ If ``False`` is provided as field mapping for a column, that column should be removed from importable data