From 342df0c001ee5d77ac1fc439513de85a16c3d9e2 Mon Sep 17 00:00:00 2001 From: robinpdev Date: Thu, 23 May 2024 21:25:41 +0200 Subject: [PATCH] fix selection download --- backend/pigeonhole/apps/submissions/models.py | 3 ++- backend/pigeonhole/apps/submissions/views.py | 18 ++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/backend/pigeonhole/apps/submissions/models.py b/backend/pigeonhole/apps/submissions/models.py index e4f1b326..1708c18f 100644 --- a/backend/pigeonhole/apps/submissions/models.py +++ b/backend/pigeonhole/apps/submissions/models.py @@ -21,9 +21,10 @@ if not registry_name: registry_name = "sel2-1.ugent.be:2002" +SUBMISSIONS_DIR = f"{str(settings.STATIC_ROOT)}/submissions" def submission_folder_path(group_id, submission_id): - return f"{str(settings.STATIC_ROOT)}/submissions/group_{group_id}/{submission_id}" + return f"{SUBMISSIONS_DIR}/group_{group_id}/{submission_id}" def artifacts_folder_path(group_id, submission_id): diff --git a/backend/pigeonhole/apps/submissions/views.py b/backend/pigeonhole/apps/submissions/views.py index a4c23f60..b354977f 100644 --- a/backend/pigeonhole/apps/submissions/views.py +++ b/backend/pigeonhole/apps/submissions/views.py @@ -25,28 +25,29 @@ ) from backend.pigeonhole.apps.submissions.permissions import CanAccessSubmission from backend.pigeonhole.filters import CustomPageNumberPagination -from .models import submission_folder_path, submission_file_path +from .models import submission_folder_path, submission_file_path, SUBMISSIONS_DIR class ZipUtilities: - def toZip(self, folderpaths, zip_path): + def toZip(self, folderpaths, zip_path, root=SUBMISSIONS_DIR): zip_file = zipfile.ZipFile(zip_path, 'w') for folder_path in folderpaths: + print(folder_path) if os.path.isfile(folder_path): - zip_file.write(folder_path) + zip_file.write(folder_path, arcname=folder_path) else: - self.addFolderToZip(zip_file, folder_path) + self.addFolderToZip(zip_file, folder_path, root) zip_file.close() - def addFolderToZip(self, zip_file, folder): + def addFolderToZip(self, zip_file, folder, root=SUBMISSIONS_DIR): for file in os.listdir(folder): full_path = os.path.join(folder, file) if os.path.isfile(full_path): - zip_file.write(full_path) + zip_file.write(full_path, arcname=os.path.relpath(folder, root) + '/' + file) elif os.path.isdir(full_path): - self.addFolderToZip(zip_file, full_path) + self.addFolderToZip(zip_file, full_path, root) class SubmissionsViewset(viewsets.ModelViewSet): @@ -222,6 +223,7 @@ def download_selection(self, request, *args, **kwargs): else: path = 'backend/downloads/submissions.zip' submission_folders = [] + print(ids) for sid in ids: submission = Submissions.objects.get(submission_id=sid) @@ -239,7 +241,7 @@ def download_selection(self, request, *args, **kwargs): utilities = ZipUtilities() filename = path - utilities.toZip(submission_folders, filename) + utilities.toZip(submission_folders, filename, SUBMISSIONS_DIR) path = realpath(path) response = FileResponse(