diff --git a/django/api/viewsets/upload.py b/django/api/viewsets/upload.py index f854edaf..13704861 100644 --- a/django/api/viewsets/upload.py +++ b/django/api/viewsets/upload.py @@ -5,6 +5,7 @@ from rest_framework.permissions import AllowAny from rest_framework.response import Response from rest_framework.viewsets import GenericViewSet +import pathlib from django.http import HttpResponse from django.core.exceptions import ValidationError from django.utils.decorators import method_decorator @@ -50,13 +51,18 @@ def import_data(self, request): filename = request.data.get("filename") dataset_selected = request.data.get("datasetSelected") replace_data = request.data.get("replace", False) + filepath = request.data.get("filepath") if dataset_selected == "ICBC Vins": - try: - create_vins_file(filename) - return Response({"success": True, "message": "ICBC data successfully uploaded!"}, status=status.HTTP_200_OK) - except Exception as error: - return Response({"success": False, "errors": str(error)}) + file_extension = pathlib.Path(filepath).suffix + if file_extension == '.csv': + try: + create_vins_file(filename) + return Response({"success": True, "message": "File successfully uploaded!"}, status=status.HTTP_200_OK) + except Exception as error: + return Response({"success": False, "message": str(error)}) + else: + return Response({"success": False, "message": "File must be a csv."}, status=status.HTTP_400_BAD_REQUEST) try: url = minio_get_object(filename) urllib.request.urlretrieve(url, filename) diff --git a/frontend/src/uploads/UploadContainer.js b/frontend/src/uploads/UploadContainer.js index 521723d1..22528fc0 100644 --- a/frontend/src/uploads/UploadContainer.js +++ b/frontend/src/uploads/UploadContainer.js @@ -45,18 +45,21 @@ const UploadContainer = () => { const showError = (error) => { const { response: errorResponse } = error; - setAlertContent("There was an error uploading the file! Please refresh the page.") - errorResponse && errorResponse.data && ( + setAlertContent("There was an issue uploading the file.") + if (errorResponse && errorResponse.data && errorResponse.data.message) { setAlertContent( `${errorResponse.data.message}\n${errorResponse.data.errors ? "Errors: " + errorResponse.data.errors.join("\n") : ""}`, ) - ) + } else if (errorResponse && errorResponse.data && errorResponse.status === 403) { + setAlertContent("There was an error. Please refresh page and ensure you are logged in.") + } setAlertSeverity("error"); setAlert(true); }; const doUpload = () => uploadFiles.forEach((file) => { + let filepath = file.path; setLoading(true); const uploadPromises = uploadFiles.map((file) => { return axios.get(ROUTES_UPLOAD.MINIO_URL).then((response) => { @@ -70,6 +73,7 @@ const UploadContainer = () => { filename, datasetSelected, replace, + filepath, }); }); }); @@ -95,7 +99,6 @@ const UploadContainer = () => { setUploadFiles([]); }) .catch((error) => { - console.log(error) showError(error); }) .finally(() => {