Skip to content

Commit

Permalink
fix selection download
Browse files Browse the repository at this point in the history
  • Loading branch information
robinpdev committed May 23, 2024
1 parent adaf04c commit 342df0c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
3 changes: 2 additions & 1 deletion backend/pigeonhole/apps/submissions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
18 changes: 10 additions & 8 deletions backend/pigeonhole/apps/submissions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand All @@ -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(
Expand Down

0 comments on commit 342df0c

Please sign in to comment.