From ba561d6d194d4fab0c9aacc876b217b330b604d5 Mon Sep 17 00:00:00 2001 From: eeintech Date: Tue, 11 May 2021 14:06:38 -0400 Subject: [PATCH] Fixed import for excel formats --- InvenTree/common/files.py | 13 +++++++------ requirements.txt | 3 ++- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/InvenTree/common/files.py b/InvenTree/common/files.py index 976daf992d..377120f44d 100644 --- a/InvenTree/common/files.py +++ b/InvenTree/common/files.py @@ -49,28 +49,27 @@ class FileManager: cleaned_data = None - ext = os.path.splitext(file.name)[-1].lower() + ext = os.path.splitext(file.name)[-1].lower().replace('.', '') - if ext in ['.csv', '.tsv', ]: + if ext in ['csv', 'tsv', ]: # These file formats need string decoding raw_data = file.read().decode('utf-8') # Reset stream position to beginning of file file.seek(0) - elif ext in ['.xls', '.xlsx', '.json', '.yaml', ]: + elif ext in ['xls', 'xlsx', 'json', 'yaml', ]: raw_data = file.read() # Reset stream position to beginning of file file.seek(0) else: - raise ValidationError(_(f'Unsupported file format: {ext}')) + raise ValidationError(_(f'Unsupported file format: {ext.upper()}')) try: - cleaned_data = tablib.Dataset().load(raw_data) + cleaned_data = tablib.Dataset().load(raw_data, format=ext) except tablib.UnsupportedFormat: raise ValidationError(_('Error reading file (invalid format)')) except tablib.core.InvalidDimensions: raise ValidationError(_('Error reading file (incorrect dimension)')) except KeyError: - # TODO: Find fix for XLSX format as it keeps on returning a KeyError raise ValidationError(_('Error reading file (data could be corrupted)')) return cleaned_data @@ -209,6 +208,8 @@ class FileManager: data[idx] = int(item) except ValueError: pass + except TypeError: + data[idx] = '' # Skip empty rows if empty: diff --git a/requirements.txt b/requirements.txt index beaa30c4fe..0f5f518217 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,9 +11,10 @@ django-markdownx==3.0.1 # Markdown form fields django-markdownify==0.8.0 # Markdown rendering coreapi==2.3.0 # API documentation pygments==2.7.4 # Syntax highlighting -tablib==0.13.0 # Import / export data files +# tablib==0.13.0 # Import / export data files (installed as dependency of django-import-export package) django-crispy-forms==1.11.2 # Form helpers django-import-export==2.0.0 # Data import / export for admin interface +tablib[xls,xlsx] # Support for XLS and XLSX formats django-cleanup==5.1.0 # Manage deletion of old / unused uploaded files flake8==3.8.3 # PEP checking pep8-naming==0.11.1 # PEP naming convention extension