Skip to content

Commit

Permalink
fix : Remove import errors (#424)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnanthanCJ authored Jul 24, 2024
1 parent b15d89a commit 53550f8
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
from asset.models import Asset, AssetType, BusinessUnit, Employee, Location, Memory, AssetLog
from django.forms import model_to_dict
import json
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
import base64

def clean_field(value):
if pd.isna(value) or value == "nan" or value == "":
Expand Down Expand Up @@ -198,10 +202,10 @@ def parse_and_add_assets(file_content, user, file_type):
memory_id=memory.id if memory else None,
)


new_assets.append(asset)
added_assets_count += 1


created_assets = Asset.objects.bulk_create(new_assets)
create_asset_logs(created_assets)

Expand Down Expand Up @@ -249,17 +253,6 @@ def generate_missing_fields_csv(missing_fields_assets):

return output

@staticmethod
def generate_missing_fields_xlsx(missing_fields_assets):
if missing_fields_assets:
df = pd.DataFrame(missing_fields_assets)
output = io.BytesIO()
df.to_excel(output, index=False)
output.seek(0)
return output
else:
return io.BytesIO()

@staticmethod
def generate_zip_file_csv(csv_file_one, csv_file_two):
zip_content = io.BytesIO()
Expand All @@ -270,12 +263,35 @@ def generate_zip_file_csv(csv_file_one, csv_file_two):
zip_content.seek(0)
return zip_content

@staticmethod
def generate_zip_file_xlsx(xlsx_file_one, xlsx_file_two):
zip_content = io.BytesIO()
with zipfile.ZipFile(zip_content, "w", zipfile.ZIP_DEFLATED, allowZip64=True) as zf:
zf.writestr("skipped_fields.xlsx", xlsx_file_one.getvalue())
zf.writestr("missing_fields.xlsx", xlsx_file_two.getvalue())
class AssetImportView(APIView):
def post(self, request, *args, **kwargs):
file_obj = request.FILES.get('file')
file_type = request.GET.get('file_type')

if not file_obj:
return Response({'error': 'No file uploaded'}, status=status.HTTP_400_BAD_REQUEST)

zip_content.seek(0)
return zip_content
try:
file_content = file_obj.read()
result = AssetImportService.parse_and_add_assets(file_content, request.user, file_type)

# Generate CSV files
skipped_fields_csv = AssetImportService.generate_missing_fields_csv(result['skipped_fields_assets'])
missing_fields_csv = AssetImportService.generate_missing_fields_csv(result['missing_fields_assets'])

# Generate zip file
zip_content = AssetImportService.generate_zip_file_csv(skipped_fields_csv, missing_fields_csv)

# Encode zip content to base64
zip_base64 = base64.b64encode(zip_content.getvalue()).decode('utf-8')

response_data = {
'message': result['message'],
'added_assets_count': result['added_assets_count'],
'skipped_assets_count': result['skipped_assets_count'],
'zip_file': zip_base64
}

return Response(response_data, status=status.HTTP_200_OK)
except Exception as e:
return Response({'error': str(e)}, status=status.HTTP_400_BAD_REQUEST)
82 changes: 62 additions & 20 deletions exam_frontend/src/components/Upload/UploadComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ const UploadComponent: React.FC = () => {
const storedToken = localStorage.getItem("jwt");
if (storedToken) {
setToken(storedToken);
console.log("Token retrieved from localStorage");
} else {
console.warn("No token found in localStorage");
}
}, []);

Expand All @@ -29,6 +32,7 @@ const UploadComponent: React.FC = () => {
setFileList(info.fileList);
},
onDrop: (e) => {
console.log("Dropped files", e.dataTransfer.files);
},
};

Expand All @@ -38,6 +42,11 @@ const UploadComponent: React.FC = () => {
return;
}

if (!token || token.trim() === "") {
message.error("Invalid authentication token. Please log in again.");
return;
}

setUploading(true);
const formData = new FormData();

Expand All @@ -47,11 +56,10 @@ const UploadComponent: React.FC = () => {
}
});

try {
if (!token) {
throw new Error("Authentication token not available. Please log in again.");
}
console.log("Submitting files:", fileList);
console.log("File type:", getFileExtension());

try {
const response = await axiosInstance.post("/asset/import-csv/", formData, {
headers: {
"Content-Type": "multipart/form-data",
Expand All @@ -62,12 +70,22 @@ const UploadComponent: React.FC = () => {
},
});

if (response.status === 200) {
message.success("Files successfully submitted.");
downloadZipFile(response.data);
console.log("Submission response:", response);

if (response.status >= 200 && response.status < 300) {
message.success(response.data.message);
console.log("Response data:", response.data);

if (response.data.zip_file) {
downloadZipFile(response.data.zip_file);
} else {
console.warn("No zip file data received");
}

setFileList([]);
} else {
throw new Error("Unexpected response status");
console.warn("Unexpected response status:", response.status);
throw new Error(`Unexpected response status: ${response.status}`);
}
} catch (error) {
handleUploadError(error);
Expand All @@ -79,39 +97,63 @@ const UploadComponent: React.FC = () => {
const getFileExtension = () => {
const firstFile = fileList[0];
if (!firstFile || !firstFile.originFileObj) {
console.warn("No file selected or file object missing");
return "";
}
const fileName = firstFile.originFileObj.name;
return fileName.split(".").pop() || "";
const extension = fileName.split(".").pop() || "";
console.log("File extension:", extension);
return extension;
};

const handleUploadError = (error: any) => {
console.error("Error submitting files:", error);
if (axios.isAxiosError(error)) {
const axiosError = error as AxiosError;
const axiosError = error as AxiosError<ErrorResponse>;
if (axiosError.response) {
const responseData = axiosError.response.data as ErrorResponse;
console.error("Response data:", axiosError.response.data);
console.error("Response status:", axiosError.response.status);
const responseData = axiosError.response.data;
message.error(`Error: ${responseData.message || 'Failed to submit files. Please try again.'}`);
} else if (axiosError.request) {
console.error("Request error:", axiosError.request);
message.error("Network error. Please check your connection and try again.");
} else {
console.error("Error message:", axiosError.message);
message.error("An unexpected error occurred. Please try again.");
}
} else {
console.error("Non-Axios error:", error);
message.error("An unexpected error occurred. Please try again.");
}
};

const downloadZipFile = (data: string) => {
const byteCharacters = atob(data);
const byteNumbers = new Array(byteCharacters.length);
for (let i = 0; i < byteCharacters.length; i++) {
byteNumbers[i] = byteCharacters.charCodeAt(i);
if (!data) {
console.error("No data received for zip file download");
message.error("Failed to generate download file. Please try again.");
return;
}

try {
const binaryData = atob(data);
const byteNumbers = new Array(binaryData.length);
for (let i = 0; i < binaryData.length; i++) {
byteNumbers[i] = binaryData.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
const blob = new Blob([byteArray], { type: "application/zip" });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = 'import_results.zip';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
} catch (error) {
console.error("Error processing zip file data:", error);
message.error("Failed to process download file. Please try again.");
}
const byteArray = new Uint8Array(byteNumbers);
const blob = new Blob([byteArray], { type: "application/zip" });
const url = URL.createObjectURL(blob);
window.open(url, "_blank");
};

return (
Expand All @@ -135,4 +177,4 @@ const UploadComponent: React.FC = () => {
);
};

export default UploadComponent;
export default UploadComponent;

0 comments on commit 53550f8

Please sign in to comment.