I am trying to add some validation for user uploaded files. This requires running through a custom script I made called "sumpin", which only takes a filepath as a variable and sends back JSON data that will verify. Everything inside my script is working independently, putting it together where the error occurs.
Since this is file validation, I decided to expand my file_extension validator that was already working.
from allauthdemo.fileuploadapp.slic3rcheck import sumpin
def user_directory_path_files(instance, filename):
return os.path.join('uploads', str(instance.objectid), filename)
ext = os.path.splitext(value.name)
valid_extensions = ['.stl','.STL']
if not ext in valid_extensions:
raise ValidationError(u'Please upload a .stl file type only')
data = sumpin(value.path)
STL = models.FileField(_('STL Upload'),
I've found my own answer, but I'll post it here in case someone runs across this issue in the future.
I was incorrect, a validator wouldn't actually download the file. I need to use a file upload handler, which is shown below.
import os from django.core.files.storage import default_storage from allauthdemo.fileuploadapp.slic3rcheck import sumpin def handle_uploaded_file(f): with open(default_storage.path('tmp/'+f.name), 'wb+') as destination: for chunk in f.chunks(): destination.write(chunk) data = sumpin(default_storage.path('tmp/'+f.name)) os.remove(default_storage.path('tmp/'+f.name)) return data
then I call this inside my views.py.
from allauthdemo.fileuploadapp.uploadhandler import handle_uploaded_file @login_required def STLupload(request): # Handle file upload if request.method == 'POST': formA = ObjectUp(request.POST, request.FILES) if formA is_valid(): data = handle_uploaded_file(request.FILES['STL'])
This will return whatever I called to return within handle_upload_file, which worked perfect for my issues. Hopefully someone will find this useful the future.