From aba6b6cff3d2cde6d427d418cc22dcc780f69560 Mon Sep 17 00:00:00 2001 From: Nicolas HOMBERG Date: Mon, 29 Jul 2024 16:56:36 +0200 Subject: [PATCH 1/2] changed submission name to add ID --- src/apps/api/views/submissions.py | 6 +++--- src/apps/competitions/tasks.py | 7 +------ 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/apps/api/views/submissions.py b/src/apps/api/views/submissions.py index 745cdb521..6c2489245 100644 --- a/src/apps/api/views/submissions.py +++ b/src/apps/api/views/submissions.py @@ -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 diff --git a/src/apps/competitions/tasks.py b/src/apps/competitions/tasks.py index 1e5544b7b..32754e55b 100644 --- a/src/apps/competitions/tasks.py +++ b/src/apps/competitions/tasks.py @@ -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) @@ -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) From f762b886ca904002f57eaa5ef17dc1978cdbd604 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Pav=C3=A3o?= Date: Tue, 20 Aug 2024 17:50:42 +0200 Subject: [PATCH 2/2] Flake8 white space fix --- src/apps/competitions/tasks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/apps/competitions/tasks.py b/src/apps/competitions/tasks.py index 32754e55b..f56696940 100644 --- a/src/apps/competitions/tasks.py +++ b/src/apps/competitions/tasks.py @@ -295,7 +295,7 @@ def zip_generator(submission_pks): 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) - short_name = "ID_" + str(submission_id)+ '_' + 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)