Skip to content

Commit

Permalink
Stream organisation document from API
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincarrogan committed Feb 6, 2024
1 parent 5278652 commit e540a88
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
5 changes: 5 additions & 0 deletions exporter/organisation/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ def update_document_on_organisation(request, organisation_id, document_id, data)
response = client.put(request, f"/organisations/{organisation_id}/document/{document_id}/", data)
response.raise_for_status()
return response.json(), response.status_code


def stream_document_on_organisation(request, organisation_id, document_id):
response = client.get(request, f"/organisations/{organisation_id}/document/{document_id}/stream/", stream=True)
return response, response.status_code
34 changes: 28 additions & 6 deletions exporter/organisation/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from http import HTTPStatus

from django.http import StreamingHttpResponse
from django.shortcuts import render
from django.urls import reverse, reverse_lazy
from django.views.generic import FormView, TemplateView, RedirectView, View
Expand All @@ -7,15 +10,19 @@

from core.auth.views import LoginRequiredMixin
from core.constants import OrganisationDocumentType
from core.decorators import expect_status
from core.helpers import get_document_data
from core.file_handler import download_document_from_s3

from exporter.core.constants import Permissions
from exporter.core.objects import Tab
from exporter.core.services import get_organisation
from exporter.organisation.roles.services import get_user_permissions
from exporter.organisation import forms
from exporter.organisation.services import post_document_on_organisation, get_document_on_organisation
from exporter.organisation.services import (
get_document_on_organisation,
post_document_on_organisation,
stream_document_on_organisation,
)


class OrganisationView(TemplateView):
Expand Down Expand Up @@ -61,16 +68,31 @@ class Details(LoginRequiredMixin, OrganisationView):


class DocumentOnOrganisation(LoginRequiredMixin, View):
@expect_status(
HTTPStatus.OK,
"Error downloading document",
"Unexpected error downloading document",
)
def stream_document_on_organisation(self, request, organisation_id, pk):
return stream_document_on_organisation(request, organisation_id, pk)

def get(self, request, pk):
organisation_id = str(self.request.session["organisation"])
response = get_document_on_organisation(request=self.request, organisation_id=organisation_id, document_id=pk)
document_on_organisation = response.json()
document = document_on_organisation["document"]

return download_document_from_s3(
document["s3_key"],
document["name"],
api_response, _ = self.stream_document_on_organisation(
request,
organisation_id,
document_on_organisation["id"],
)
response = StreamingHttpResponse(api_response.iter_content())
for header_to_copy in [
"Content-Type",
"Content-Disposition",
]:
response.headers[header_to_copy] = api_response.headers[header_to_copy]
return response


class AbstractOrganisationUpload(LoginRequiredMixin, FormView):
Expand Down
24 changes: 17 additions & 7 deletions unit_tests/exporter/organisation/test_views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import io
import uuid

from bs4 import BeautifulSoup
from unittest import mock

from django.http import StreamingHttpResponse
from django.urls import reverse
from django.core.files.uploadedfile import SimpleUploadedFile
import pytest

from core import client
from unit_tests.helpers import mocked_now
Expand Down Expand Up @@ -50,15 +52,23 @@ def test_download_document_on_organisation(
authorized_client,
requests_mock,
organisation_pk,
mock_s3_files,
):
mock_s3_files(
("123", b"test", {"ContentType": "application/doc"}),
)

requests_mock.get(
client._build_absolute_uri(f"/organisations/{organisation_pk}/document/00acebbf-2077-4b80-8b95-37ff7f46c6d0/"),
json={"document": {"s3_key": "123", "name": "fakefile.doc"}},
json={
"id": "00acebbf-2077-4b80-8b95-37ff7f46c6d0",
"document": {"id": str(uuid.uuid4()), "s3_key": "123", "name": "fakefile.doc"},
},
)
requests_mock.get(
client._build_absolute_uri(
f"/organisations/{organisation_pk}/document/00acebbf-2077-4b80-8b95-37ff7f46c6d0/stream/"
),
body=io.BytesIO(b"test"),
headers={
"Content-Type": "application/doc",
"Content-Disposition": 'attachment; filename="fakefile.doc"',
},
)

url = reverse("organisation:document", kwargs={"pk": "00acebbf-2077-4b80-8b95-37ff7f46c6d0"})
Expand Down

0 comments on commit e540a88

Please sign in to comment.