From bd33d6231894fb7d4a96fabe27bf7a3af67cb3fb Mon Sep 17 00:00:00 2001 From: kittiu Date: Sun, 15 Mar 2020 15:44:39 +0700 Subject: [PATCH] [12.0][FIX] excel_import_export, problem on import with datetime field Can't import if the import file has date time field --- excel_import_export/models/common.py | 9 ++++----- excel_import_export/models/xlsx_import.py | 13 +++++++++---- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/excel_import_export/models/common.py b/excel_import_export/models/common.py index 3f5fc50fe..5e70f0d95 100644 --- a/excel_import_export/models/common.py +++ b/excel_import_export/models/common.py @@ -268,13 +268,12 @@ def _get_cell_value(cell, field_type=False): datemode = 0 # From book.datemode, but we fix it for simplicity if field_type in ['date', 'datetime']: ctype = xlrd.sheet.ctype_text.get(cell.ctype, 'unknown type') - if ctype == 'number': + if ctype in ('xldate', 'number'): + is_datetime = cell.value % 1 != 0.0 time_tuple = xlrd.xldate_as_tuple(cell.value, datemode) date = dt(*time_tuple) - if field_type == 'date': - value = date.strftime("%Y-%m-%d") - elif field_type == 'datetime': - value = date.strftime("%Y-%m-%d %H:%M:%S") + value = (date.strftime("%Y-%m-%d %H:%M:%S") + if is_datetime else date.strftime("%Y-%m-%d")) else: value = cell.value elif field_type in ['integer', 'float']: diff --git a/excel_import_export/models/xlsx_import.py b/excel_import_export/models/xlsx_import.py index 933d86149..389a0a544 100644 --- a/excel_import_export/models/xlsx_import.py +++ b/excel_import_export/models/xlsx_import.py @@ -203,14 +203,19 @@ class XLSXImport(models.AbstractModel): 'encoding': '', 'separator': '', 'quoting': '"', - 'date_style': '', - 'datetime_style': '%Y-%m-%d %H:%M:%S', + 'date_format': '%Y-%m-%d', + 'datetime_format': '%Y-%m-%d %H:%M:%S', 'float_thousand_separator': ',', 'float_decimal_separator': '.', 'fields': []}) if errors.get('messages'): - message = errors['messages']['message'].encode('utf-8') - raise ValidationError(message) + message = _('Error importing data') + messages = errors['messages'] + if isinstance(messages, dict): + message = messages['message'] + if isinstance(messages, list): + message = ', '.join([x['message'] for x in messages]) + raise ValidationError(message.encode('utf-8')) return self.env.ref(xml_id) except xlrd.XLRDError: raise ValidationError(