Skip to content

Commit

Permalink
fix: generate talpa export csv synchronously (#2608)
Browse files Browse the repository at this point in the history
  • Loading branch information
rikuke authored Dec 18, 2023
1 parent 1d6cfd3 commit 05e78d4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
14 changes: 9 additions & 5 deletions backend/benefit/applications/api/v1/application_batch_views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.db import transaction
from django.forms import ValidationError
from django.http import HttpResponse, StreamingHttpResponse
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from django.utils import timezone
from django.utils.text import format_lazy
Expand Down Expand Up @@ -161,11 +161,14 @@ def export_batch(self, request, *args, **kwargs):
permission_classes=[AllowAny],
)
@transaction.atomic
def talpa_export_batch(self, request, *args, **kwargs) -> StreamingHttpResponse:
def talpa_export_batch(self, request, *args, **kwargs) -> HttpResponse:
"""
Export ApplicationBatch to CSV format for Talpa Robot
"""
skip_update = request.query_params.get("skip_update") == "1"
skip_update = (
request.query_params.get("skip_update")
and request.query_params.get("skip_update") == "1"
)

approved_batches = ApplicationBatch.objects.filter(
status=ApplicationBatchStatus.DECIDED_ACCEPTED
Expand All @@ -189,10 +192,11 @@ def talpa_export_batch(self, request, *args, **kwargs) -> StreamingHttpResponse:
date=timezone.now().strftime("%Y%m%d_%H%M%S"),
)

response = StreamingHttpResponse(
csv_service.get_csv_string_lines_generator(True, True),
response = HttpResponse(
csv_service.get_csv_string(True).encode("utf-8"),
content_type="text/csv",
)

response["Content-Disposition"] = "attachment; filename={filename}.csv".format(
filename=file_name
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import pytz
from dateutil.relativedelta import relativedelta
from django.conf import settings
from django.http import StreamingHttpResponse
from django.http import HttpResponse
from rest_framework.reverse import reverse

from applications.api.v1.serializers.application import ApplicationBatchSerializer
Expand Down Expand Up @@ -838,6 +838,6 @@ def test_application_batches_talpa_export(anonymous_client, application_batch):
assert application_batch.status == ApplicationBatchStatus.SENT_TO_TALPA
assert app_batch_2.status == ApplicationBatchStatus.SENT_TO_TALPA

assert isinstance(response, StreamingHttpResponse)
assert isinstance(response, HttpResponse)
assert response.headers["Content-Type"] == "text/csv"
assert response.status_code == 200

0 comments on commit 05e78d4

Please sign in to comment.