Browse Source

Rename imported file

pull/1779/head
Hironsan 2 years ago
parent
commit
f2576cf513
1 changed files with 12 additions and 0 deletions
  1. 12
      backend/data_import/views.py

12
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.shortcuts import get_object_or_404
from django_drf_filepond.models import TemporaryUpload from django_drf_filepond.models import TemporaryUpload
from rest_framework import status from rest_framework import status
@ -29,9 +32,18 @@ class DatasetImportAPI(APIView):
upload_ids = request.data.pop("uploadIds") upload_ids = request.data.pop("uploadIds")
file_format = request.data.pop("format") file_format = request.data.pop("format")
# Rename file
temporary_uploads = TemporaryUpload.objects.filter(upload_id__in=upload_ids) 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] 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} save_names = {tu.get_file_path(): tu.file.name for tu in temporary_uploads}
task = import_dataset.delay( task = import_dataset.delay(
user_id=request.user.id, user_id=request.user.id,
project_id=project_id, project_id=project_id,

Loading…
Cancel
Save