Skip to content

Commit

Permalink
fix: ssec download
Browse files Browse the repository at this point in the history
* fix(settings): sensible s3 defaults

* fix: use fileresponse for downloading template

* fix: remove unused import, tests

---------

Co-authored-by: Christian Zosel <[email protected]>
  • Loading branch information
Yelinz and czosel authored Jul 19, 2024
1 parent 87926c0 commit d6e043d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 27 deletions.
20 changes: 10 additions & 10 deletions document_merge_service/api/tests/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def test_template_download(db, client, template):
template_resp = client.get(download_url)

file.seek(0)
assert file.read() == template_resp.content
assert file.read() == template_resp.getvalue()


def test_template_list_with_file(db, client, template):
Expand Down Expand Up @@ -201,7 +201,7 @@ def test_template_create(
template_link = data["template"]
response = client.get(template_link)
assert response.status_code == status.HTTP_200_OK
file_ = io.BytesIO(response.content)
file_ = io.BytesIO(response.getvalue())
if engine == "xlsx-template":
openpyxl.load_workbook(file_)
else:
Expand Down Expand Up @@ -252,7 +252,7 @@ def test_disable_validation(
template_link = data["template"]
response = admin_client.get(template_link)
assert response.status_code == status.HTTP_200_OK
Document(io.BytesIO(response.content))
Document(io.BytesIO(response.getvalue()))


@pytest.mark.parametrize(
Expand Down Expand Up @@ -501,7 +501,7 @@ def test_template_create_with_available_placeholders(
template_link = data["template"]
response = admin_client.get(template_link)
assert response.status_code == status.HTTP_200_OK
Document(io.BytesIO(response.content))
Document(io.BytesIO(response.getvalue()))


@pytest.mark.parametrize(
Expand Down Expand Up @@ -565,13 +565,13 @@ def test_template_merge_docx(
== "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
)

docx = Document(io.BytesIO(response.content))
docx = Document(io.BytesIO(response.getvalue()))
xml = etree.tostring(docx._element.body, encoding="unicode", pretty_print=True)
try:
snapshot.assert_match(xml)
except AssertionError: # pragma: no cover
with open(f"/tmp/{template.slug}.docx", "wb") as output:
output.write(response.content)
output.write(response.getvalue())
print("Template output changed. Check file at %s" % output.name)
raise

Expand Down Expand Up @@ -607,13 +607,13 @@ def test_merge_expression(
response = client.post(url, data={"data": template_content}, format="json")
assert response.status_code == status.HTTP_200_OK

docx = Document(io.BytesIO(response.content))
docx = Document(io.BytesIO(response.getvalue()))
xml = etree.tostring(docx._element.body, encoding="unicode", pretty_print=True)
try:
snapshot.assert_match(xml)
except AssertionError: # pragma: no cover
with open(f"/tmp/{template.slug}.docx", "wb") as output:
output.write(response.content)
output.write(response.getvalue())
print("Template output changed. Check file at %s" % output.name)
raise

Expand Down Expand Up @@ -690,7 +690,7 @@ def test_template_merge_jinja_extensions_docx(
== "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
)

docx = Document(io.BytesIO(response.content))
docx = Document(io.BytesIO(response.getvalue()))
xml = etree.tostring(docx._element.body, encoding="unicode", pretty_print=True)
snapshot.assert_match(xml)

Expand Down Expand Up @@ -761,7 +761,7 @@ def test_template_merge_jinja_filters_docx(
== "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
)

docx = Document(io.BytesIO(response.content))
docx = Document(io.BytesIO(response.getvalue()))
xml = etree.tostring(docx._element.body, encoding="unicode", pretty_print=True)
snapshot.assert_match(xml)

Expand Down
17 changes: 5 additions & 12 deletions document_merge_service/api/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import mimetypes
from os.path import splitext
from pathlib import Path

import jinja2
from django.http import HttpResponse
from django.utils.encoding import smart_str
from django.http import FileResponse, HttpResponse
from generic_permissions.permissions import PermissionViewMixin
from generic_permissions.visibilities import VisibilityViewMixin
from rest_framework import exceptions, viewsets
Expand Down Expand Up @@ -70,17 +70,10 @@ class DownloadTemplateView(RetrieveAPIView):
def retrieve(self, request, **kwargs):
template = self.get_object()

mime_type, _ = mimetypes.guess_type(template.template.name)
extension = mimetypes.guess_extension(mime_type)
content_type = mime_type or "application/force-download"

response = HttpResponse(content_type=content_type)
response["Content-Disposition"] = 'attachment; filename="%s"' % smart_str(
template.slug + extension
return FileResponse(
template.template.file,
filename=f"{template.slug}{Path(template.template.name).suffix}",
)
response["Content-Length"] = template.template.size
response.write(template.template.read())
return response


class ConvertView(APIView):
Expand Down
10 changes: 5 additions & 5 deletions document_merge_service/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ def parse_admins(admins):
# django-storages S3 settings
DMS_ENABLE_AT_REST_ENCRYPTION = env.bool("DMS_ENABLE_AT_REST_ENCRYPTION", False)
S3_STORAGE_OPTIONS = {
"access_key": env.str("DMS_S3_ACCESS_KEY_ID", ""),
"secret_key": env.str("DMS_S3_SECRET_ACCESS_KEY", ""),
"bucket_name": env.str("DMS_STORAGE_BUCKET_NAME", ""),
"endpoint_url": env.str("DMS_S3_ENDPOINT_URL", ""),
"region_name": env.str("DMS_S3_REGION_NAME", ""),
"access_key": env.str("DMS_S3_ACCESS_KEY_ID", "minio"),
"secret_key": env.str("DMS_S3_SECRET_ACCESS_KEY", "minio123"),
"bucket_name": env.str("DMS_STORAGE_BUCKET_NAME", "dms-media"),
"endpoint_url": env.str("DMS_S3_ENDPOINT_URL", "http://minio:9000"),
"region_name": env.str("DMS_S3_REGION_NAME", None),
"location": env.str("DMS_LOCATION", ""),
"file_overwrite": env.bool("DMS_S3_FILE_OVERWRITE", False),
"signature_version": env.str("DMS_S3_SIGNATURE_VERSION", "v2"),
Expand Down

0 comments on commit d6e043d

Please sign in to comment.