Skip to content

Commit

Permalink
feat: adds check for csv on backend
Browse files Browse the repository at this point in the history
fix: updates frontend to catch keycloak logout and display message to user when attempting upload
  • Loading branch information
emi-hi committed May 22, 2024
1 parent 551626f commit f04879f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
16 changes: 11 additions & 5 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,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)})

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
11 changes: 7 additions & 4 deletions frontend/src/uploads/UploadContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -70,6 +73,7 @@ const UploadContainer = () => {
filename,
datasetSelected,
replace,
filepath,
});
});
});
Expand All @@ -95,7 +99,6 @@ const UploadContainer = () => {
setUploadFiles([]);
})
.catch((error) => {
console.log(error)
showError(error);
})
.finally(() => {
Expand Down

0 comments on commit f04879f

Please sign in to comment.