mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Validate uploaded BOM file with tablib
- Check to see if a bom_file file object was uploaded - Pass off to the BOM file validity checker - Only a valid tabulated dataset will be accepted
This commit is contained in:
parent
8b207d0d1d
commit
86695cf2bb
@ -5,6 +5,9 @@ Django views for interacting with Part app
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
import tablib
|
||||||
|
import os
|
||||||
|
|
||||||
from django.shortcuts import get_object_or_404
|
from django.shortcuts import get_object_or_404
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from django.urls import reverse_lazy
|
from django.urls import reverse_lazy
|
||||||
@ -656,6 +659,8 @@ class BomUpload(AjaxView):
|
|||||||
|
|
||||||
Initially returns a form for file upload """
|
Initially returns a form for file upload """
|
||||||
|
|
||||||
|
self.request = request
|
||||||
|
|
||||||
# A valid Part object must be supplied. This is the 'parent' part for the BOM
|
# A valid Part object must be supplied. This is the 'parent' part for the BOM
|
||||||
part = get_object_or_404(Part, pk=self.kwargs['pk'])
|
part = get_object_or_404(Part, pk=self.kwargs['pk'])
|
||||||
|
|
||||||
@ -663,17 +668,56 @@ class BomUpload(AjaxView):
|
|||||||
|
|
||||||
return self.renderJsonResponse(request, form)
|
return self.renderJsonResponse(request, form)
|
||||||
|
|
||||||
|
def handleBomFileUpload(self, bom_file):
|
||||||
|
|
||||||
|
form = part_forms.BomImportForm()
|
||||||
|
|
||||||
|
data = {
|
||||||
|
# TODO - Validate the form if there isn't actually an error!
|
||||||
|
'form_valid': False
|
||||||
|
}
|
||||||
|
|
||||||
|
# Extract the file format data
|
||||||
|
ext = os.path.splitext(bom_file.name)[-1].lower()
|
||||||
|
|
||||||
|
if ext in ['.csv', '.tsv', ]:
|
||||||
|
# These file formats need string decoding
|
||||||
|
raw_data = bom_file.read().decode('utf-8')
|
||||||
|
elif ext in ['.xls', '.xlsx']:
|
||||||
|
raw_data = bom_file.read()
|
||||||
|
else:
|
||||||
|
form.errors['bom_file'] = ['Unsupported file format: ' + ext]
|
||||||
|
return self.renderJsonResponse(self.request, form, data)
|
||||||
|
|
||||||
|
# Now try to read the data
|
||||||
|
try:
|
||||||
|
bom_data = tablib.Dataset().load(raw_data)
|
||||||
|
print(bom_data)
|
||||||
|
except tablib.UnsupportedFormat:
|
||||||
|
valid = False
|
||||||
|
form.errors['bom_file'] = [
|
||||||
|
"Error reading '{f}' (Unsupported file format)".format(f=str(bom_file)),
|
||||||
|
]
|
||||||
|
|
||||||
|
return self.renderJsonResponse(self.request, form, data=data)
|
||||||
|
|
||||||
def post(self, request, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
""" Perform the various 'POST' requests required.
|
""" Perform the various 'POST' requests required.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
data = {
|
self.request = request
|
||||||
'form_valid': False,
|
|
||||||
}
|
# Did the user POST a file named bom_file?
|
||||||
|
bom_file = request.FILES.get('bom_file', None)
|
||||||
|
|
||||||
form = self.get_form()
|
form = self.get_form()
|
||||||
|
|
||||||
form.errors['bom_file'] = ['Nah mate']
|
if bom_file:
|
||||||
|
return self.handleBomFileUpload(bom_file)
|
||||||
|
|
||||||
|
data = {
|
||||||
|
'form_valid': False,
|
||||||
|
}
|
||||||
|
|
||||||
return self.renderJsonResponse(request, form, data=data)
|
return self.renderJsonResponse(request, form, data=data)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user