Skip to content

Commit

Permalink
fix: CTHUB 297 Adds success/error message to icbc vin upload (#303)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
emi-hi authored May 22, 2024
1 parent ff66bab commit a11e976
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
14 changes: 11 additions & 3 deletions django/api/viewsets/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)})

Check warning

Code scanning / CodeQL

Information exposure through an exception Medium

Stack trace information
flows to this location and may be exposed to an external user.
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)
Expand Down
13 changes: 10 additions & 3 deletions frontend/src/uploads/UploadContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -67,6 +73,7 @@ const UploadContainer = () => {
filename,
datasetSelected,
replace,
filepath,
});
});
});
Expand Down

0 comments on commit a11e976

Please sign in to comment.