Fixed import for excel formats

This commit is contained in:
eeintech 2021-05-11 14:06:38 -04:00
parent a093118856
commit ba561d6d19
2 changed files with 9 additions and 7 deletions

View File

@ -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:

View File

@ -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