From f2576cf513431c56085f736f264ad10e5b094940 Mon Sep 17 00:00:00 2001 From: Hironsan Date: Thu, 31 Mar 2022 11:10:34 +0900 Subject: [PATCH] Rename imported file --- backend/data_import/views.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/backend/data_import/views.py b/backend/data_import/views.py index e012d936..c9643fc3 100644 --- a/backend/data_import/views.py +++ b/backend/data_import/views.py @@ -1,3 +1,6 @@ +import os +from pathlib import Path + from django.shortcuts import get_object_or_404 from django_drf_filepond.models import TemporaryUpload from rest_framework import status @@ -29,9 +32,18 @@ class DatasetImportAPI(APIView): upload_ids = request.data.pop("uploadIds") file_format = request.data.pop("format") + # Rename file temporary_uploads = TemporaryUpload.objects.filter(upload_id__in=upload_ids) + for tu in temporary_uploads: + p = Path(tu.get_file_path()) + new_path = Path(p.parent, tu.upload_name) + p.rename(new_path) + tu.file.name = os.path.join(new_path.parent.stem, new_path.name) + TemporaryUpload.objects.bulk_update(temporary_uploads, fields=["file"]) + file_paths = [tu.get_file_path() for tu in temporary_uploads] save_names = {tu.get_file_path(): tu.file.name for tu in temporary_uploads} + task = import_dataset.delay( user_id=request.user.id, project_id=project_id,