From 6e24d3489dcf80ef5ed4cc597818c8dd5833dd9a Mon Sep 17 00:00:00 2001 From: "Antoine (ande)" Date: Mon, 6 Nov 2023 17:29:24 +0100 Subject: [PATCH] [FIX] base_import: wrong date format MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Current behaviour: Wrong format used when importing dates Steps to reproduce: 1. Change your language to German 2. Go to Sales > Orders 3. Export to XLSX 4. Change to dates to german format (ie: 10.09.2023) 5. Favorites > import records > Upload file 6. Click on Test 7. "does not match format '%Y-%m-%d'" Cause of the issue: Only default formats were taken into account because options are empty. opw-3540715 closes odoo/odoo#146744 X-original-commit: bdc006a81f83efddd693a8d4f4ffaec92da36f4a Signed-off-by: Rémy Voet (ryv) --- addons/base_import/models/base_import.py | 5 +++- .../tests/test_base_import.py | 29 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/addons/base_import/models/base_import.py b/addons/base_import/models/base_import.py index 2a303047e13..754b1291291 100644 --- a/addons/base_import/models/base_import.py +++ b/addons/base_import/models/base_import.py @@ -560,6 +560,9 @@ class Import(models.TransientModel): val = self._remove_currency_symbol(val) if val: if options.get('float_thousand_separator') and options.get('float_decimal_separator'): + if options['float_decimal_separator'] == '.' and val.count('.') > 1: + # This is not a float so exit this try + float('a') val = val.replace(options['float_thousand_separator'], '').replace(options['float_decimal_separator'], '.') # We are now sure that this is a float, but we still need to find the # thousand and decimal separator @@ -1528,7 +1531,7 @@ class Import(models.TransientModel): return input_file_data -_SEPARATORS = [' ', '/', '-', ''] +_SEPARATORS = [' ', '/', '-', '.', ''] _PATTERN_BASELINE = [ ('%m', '%d', '%Y'), ('%d', '%m', '%Y'), diff --git a/addons/test_base_import/tests/test_base_import.py b/addons/test_base_import/tests/test_base_import.py index 345fa49ca84..1e5091b5124 100644 --- a/addons/test_base_import/tests/test_base_import.py +++ b/addons/test_base_import/tests/test_base_import.py @@ -476,6 +476,35 @@ class test_convert_import_data(TransactionCase): # if results empty, no errors self.assertItemsEqual(results['messages'], []) + def test_date_fields_no_options(self): + import_wizard = self.env['base_import.import'].with_context(lang='de_DE').create({ + 'res_model': 'res.partner', + 'file': 'name,date,create_date\n' + '"foo","15.10.2023","15.10.2023 15:15:15"\n'.encode('utf-8'), + 'file_type': 'text/csv', + }) + + opts = { + 'date_format': '', + 'datetime_format': '', + 'quoting': '"', + 'separator': ',', + 'float_decimal_separator': '.', + 'float_thousand_separator': ',', + 'has_headers': True + } + result_parse = import_wizard.parse_preview({**opts}) + + opts = result_parse['options'] + results = import_wizard.execute_import( + ['name', 'date', 'create_date'], + [], + {**opts} + ) + + # if results empty, no errors + self.assertItemsEqual(results['messages'], []) + def test_parse_relational_fields(self): """ Ensure that relational fields float and date are correctly parsed during the import call.