From 7ddcaac4cdab6c9eee47b53f5e15c4f1c8437d06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Czy=C5=BC?= Date: Wed, 13 Dec 2023 16:59:57 +0100 Subject: [PATCH] BulkDownloadView supports zipping and merging of documents --- src/documents/views.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/documents/views.py b/src/documents/views.py index e8c6db0de..98cc8dd88 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -59,6 +59,7 @@ from rest_framework.viewsets import ViewSet from documents import bulk_edit from documents.bulk_download import ArchiveOnlyStrategy +from documents.bulk_download import MergedPdfFile from documents.bulk_download import OriginalAndArchiveStrategy from documents.bulk_download import OriginalsOnlyStrategy from documents.classifier import load_classifier @@ -1076,6 +1077,7 @@ class BulkDownloadView(GenericAPIView): compression = serializer.validated_data.get("compression") content = serializer.validated_data.get("content") follow_filename_format = serializer.validated_data.get("follow_formatting") + single_file = serializer.validated_data.get("single_file") os.makedirs(settings.SCRATCH_DIR, exist_ok=True) temp = tempfile.NamedTemporaryFile( @@ -1091,18 +1093,21 @@ class BulkDownloadView(GenericAPIView): else: strategy_class = ArchiveOnlyStrategy - with zipfile.ZipFile(temp.name, "w", compression) as zipf: - strategy = strategy_class(zipf, follow_filename_format) + def _get_bulk_document_writer(): + if single_file: + return MergedPdfFile(temp.name) + else: + return zipfile.ZipFile(temp.name, "w", compression) + + with _get_bulk_document_writer() as writer: + strategy = strategy_class(writer, follow_filename_format) for id in ids: doc = Document.objects.get(id=id) strategy.add_document(doc) with open(temp.name, "rb") as f: response = HttpResponse(f, content_type="application/zip") - response["Content-Disposition"] = '{}; filename="{}"'.format( - "attachment", - "documents.zip", - ) + response["Content-Disposition"] = "attachment" return response