Check for if file in form & validity

This commit is contained in:
Aroy-Art 2024-02-22 12:31:18 +01:00
parent e4079e34f2
commit 600d85d7d6
Signed by: Aroy
GPG key ID: 583642324A1D2070

View file

@ -70,27 +70,36 @@ def fileUpload(request):
if request.method == 'POST': if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES) form = UploadFileForm(request.POST, request.FILES)
if form.is_valid(): if form.is_valid():
file = form.cleaned_data['file'] if 'file' in request.FILES: # Check if a file has been uploaded
file = form.cleaned_data['file']
file_name = file.name
file_hash = compute_file_hash(file) file_name = file.name
file_hash = compute_file_hash(file)
Null, file_ext = os.path.splitext(file_name)
hash_file_name = file_hash + file_ext Null, file_ext = os.path.splitext(file_name)
hash_file_name = file_hash + file_ext
new_submission_file, created = Submission_File.objects.get_or_create(file_hash=file_hash)
new_submission_file, created = Submission_File.objects.get_or_create(file_hash=file_hash)
new_submission_file.file_hash = file_hash
new_submission_file.file_name = file_name new_submission_file.file_hash = file_hash
new_submission_file.file.save(hash_file_name, file) new_submission_file.file_name = file_name
new_submission_file.file.save(hash_file_name, file)
new_submission_file.save
new_submission_file.save
return HttpResponseRedirect(f"/files/submission/{file_hash}")
return HttpResponseRedirect(f"/files/submission/{file_hash}")
else:
# No file was uploaded, add an error message to the context
error_message = 'No file was uploaded.'
return render(request, 'files/upload.html', {'form': form, 'error_message': error_message})
else:
# Form is not valid, add an error message to the context
error_message = 'There was an error with the form.'
return render(request, 'files/upload.html', {'form': form, 'error_message': error_message})
else: else:
form = UploadFileForm() form = UploadFileForm()
return render(request, 'files/upload.html', {'form': form}) return render(request, 'files/upload.html', {'form': form})