From a11e976d8a7a0301ecf144e4c11e7ced88c321a0 Mon Sep 17 00:00:00 2001 From: Emily <44536222+emi-hi@users.noreply.github.com> Date: Wed, 22 May 2024 10:57:43 -0700 Subject: [PATCH] fix: CTHUB 297 Adds success/error message to icbc vin upload (#303) * fix: adds messages to icbc vins upload in import data function, adds basic error messaging to frontend in case errors don't have our standard format * feat: adds check for csv on backend fix: updates frontend to catch keycloak logout and display message to user when attempting upload --- django/api/viewsets/upload.py | 14 +++++++++++--- frontend/src/uploads/UploadContainer.js | 13 ++++++++++--- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/django/api/viewsets/upload.py b/django/api/viewsets/upload.py index 67971719..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,11 +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": - create_vins_file(filename) - return Response({"success": True}, status=status.HTTP_200_OK) - + 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 315400aa..22528fc0 100644 --- a/frontend/src/uploads/UploadContainer.js +++ b/frontend/src/uploads/UploadContainer.js @@ -45,15 +45,21 @@ const UploadContainer = () => { const showError = (error) => { const { response: errorResponse } = error; - setAlertContent( - `${errorResponse.data.message}\n${errorResponse.data.errors ? "Errors: " + errorResponse.data.errors.join("\n") : ""}`, - ); + 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) => { @@ -67,6 +73,7 @@ const UploadContainer = () => { filename, datasetSelected, replace, + filepath, }); }); });