mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-29 12:06:44 +00:00
Fixed import for excel formats
This commit is contained in:
parent
a093118856
commit
ba561d6d19
@ -49,28 +49,27 @@ class FileManager:
|
|||||||
|
|
||||||
cleaned_data = None
|
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
|
# These file formats need string decoding
|
||||||
raw_data = file.read().decode('utf-8')
|
raw_data = file.read().decode('utf-8')
|
||||||
# Reset stream position to beginning of file
|
# Reset stream position to beginning of file
|
||||||
file.seek(0)
|
file.seek(0)
|
||||||
elif ext in ['.xls', '.xlsx', '.json', '.yaml', ]:
|
elif ext in ['xls', 'xlsx', 'json', 'yaml', ]:
|
||||||
raw_data = file.read()
|
raw_data = file.read()
|
||||||
# Reset stream position to beginning of file
|
# Reset stream position to beginning of file
|
||||||
file.seek(0)
|
file.seek(0)
|
||||||
else:
|
else:
|
||||||
raise ValidationError(_(f'Unsupported file format: {ext}'))
|
raise ValidationError(_(f'Unsupported file format: {ext.upper()}'))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
cleaned_data = tablib.Dataset().load(raw_data)
|
cleaned_data = tablib.Dataset().load(raw_data, format=ext)
|
||||||
except tablib.UnsupportedFormat:
|
except tablib.UnsupportedFormat:
|
||||||
raise ValidationError(_('Error reading file (invalid format)'))
|
raise ValidationError(_('Error reading file (invalid format)'))
|
||||||
except tablib.core.InvalidDimensions:
|
except tablib.core.InvalidDimensions:
|
||||||
raise ValidationError(_('Error reading file (incorrect dimension)'))
|
raise ValidationError(_('Error reading file (incorrect dimension)'))
|
||||||
except KeyError:
|
except KeyError:
|
||||||
# TODO: Find fix for XLSX format as it keeps on returning a KeyError
|
|
||||||
raise ValidationError(_('Error reading file (data could be corrupted)'))
|
raise ValidationError(_('Error reading file (data could be corrupted)'))
|
||||||
|
|
||||||
return cleaned_data
|
return cleaned_data
|
||||||
@ -209,6 +208,8 @@ class FileManager:
|
|||||||
data[idx] = int(item)
|
data[idx] = int(item)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
|
except TypeError:
|
||||||
|
data[idx] = ''
|
||||||
|
|
||||||
# Skip empty rows
|
# Skip empty rows
|
||||||
if empty:
|
if empty:
|
||||||
|
@ -11,9 +11,10 @@ django-markdownx==3.0.1 # Markdown form fields
|
|||||||
django-markdownify==0.8.0 # Markdown rendering
|
django-markdownify==0.8.0 # Markdown rendering
|
||||||
coreapi==2.3.0 # API documentation
|
coreapi==2.3.0 # API documentation
|
||||||
pygments==2.7.4 # Syntax highlighting
|
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-crispy-forms==1.11.2 # Form helpers
|
||||||
django-import-export==2.0.0 # Data import / export for admin interface
|
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
|
django-cleanup==5.1.0 # Manage deletion of old / unused uploaded files
|
||||||
flake8==3.8.3 # PEP checking
|
flake8==3.8.3 # PEP checking
|
||||||
pep8-naming==0.11.1 # PEP naming convention extension
|
pep8-naming==0.11.1 # PEP naming convention extension
|
||||||
|
Loading…
x
Reference in New Issue
Block a user