Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed submission name to add ID when downloading many submissions #1565

Merged
merged 3 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/apps/api/views/submissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,18 +312,18 @@ def re_run_many_submissions(self, request):
submission.re_run()
return Response({})

# New methods impleted!
@action(detail=False, methods=['get'])
def download_many(self, request):
pks = request.query_params.get('pks')
if pks:
pks = json.loads(pks) # Convert JSON string to list

# Doing a local import here to avoid circular imports
from competitions.tasks import stream_batch_download
# Call the task and get the result (stream)

# in_memory_zip = stream_batch_download.apply_async((pks,)).get()
in_memory_zip = stream_batch_download(pks)
# Stream the response

response = StreamingHttpResponse(in_memory_zip, content_type='application/zip')
response['Content-Disposition'] = 'attachment; filename="bulk_submissions.zip"'
return response
Expand Down
7 changes: 1 addition & 6 deletions src/apps/competitions/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,10 @@ def retrieve_data(url, data=None):

def zip_generator(submission_pks):
in_memory_zip = BytesIO()
# logger.info("IN zip generator")
with zipfile.ZipFile(in_memory_zip, 'w', zipfile.ZIP_DEFLATED) as zip_file:
for submission_id in submission_pks:
submission = Submission.objects.get(id=submission_id)
# logger.info(submission.data.data_file)

short_name = submission.data.data_file.name.split('/')[-1]
short_name = "ID_" + str(submission_id) + '_' + submission.data.data_file.name.split('/')[-1]
url = make_url_sassy(path=submission.data.data_file.name)
for block in retrieve_data(url):
zip_file.writestr(short_name, block)
Expand All @@ -310,8 +307,6 @@ def zip_generator(submission_pks):

@app.task(queue='site-worker', soft_time_limit=60 * 60)
def stream_batch_download(submission_pks):
# logger.info("In stream_batch_download")
# logger.info(submission_pks)
return zip_generator(submission_pks)


Expand Down