Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zip xlsx #76

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions bmds_ui/analysis/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ def excel(self, request, *args, **kwargs):

return Response(response.model_dump(), content_type="application/json")

@action(detail=True, renderer_classes=(renderers.ZipRenderer,), url_path="excel-report")
def excel_report(self, request, *args, **kwargs):
instance = self.get_object()
data = renderers.BinaryFile(data=instance.to_excel_reports(), filename=instance.slug)
return Response(data)

@action(detail=True, renderer_classes=(renderers.DocxRenderer,))
def word(self, request, *args, **kwargs):
"""
Expand Down
11 changes: 11 additions & 0 deletions bmds_ui/analysis/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from copy import deepcopy
from datetime import datetime, timedelta
from io import BytesIO
from zipfile import ZipFile

import pandas as pd
import pydantic
Expand Down Expand Up @@ -115,6 +116,9 @@ def get_delete_url(self):
def get_excel_url(self):
return reverse("api:analysis-excel", args=(str(self.id),))

def get_excel_report_url(self):
return reverse("api:analysis-excel-report", args=(str(self.id),))

def get_word_url(self):
return reverse("api:analysis-word", args=(str(self.id),))

Expand Down Expand Up @@ -224,6 +228,13 @@ def to_excel(self) -> BytesIO:
df.to_excel(writer, sheet_name=name, index=False)
return f

def to_excel_reports(self) -> BytesIO:
f = BytesIO()
with ZipFile(f, "w") as zip_file:
zip_file.writestr("file1.txt", "This is the content of file1.")
zip_file.writestr("file2.txt", "This is the content of file2.")
return f

def update_selection(self, selection: validators.AnalysisSelectedSchema):
"""Given a new selection data schema; update outputs and save instance

Expand Down
1 change: 1 addition & 0 deletions bmds_ui/analysis/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ def get_context_data(self, **kwargs):
"apiUrl": self.object.get_api_url(),
"url": self.object.get_absolute_url(),
"excelUrl": self.object.get_excel_url(),
"excelReportUrl": self.object.get_excel_report_url(),
"wordUrl": self.object.get_word_url(),
"future": settings.ALWAYS_SHOW_FUTURE
or (self.request.user.is_staff and bool(self.request.GET.get("future"))),
Expand Down
13 changes: 13 additions & 0 deletions bmds_ui/common/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,16 @@ def render(self, dataset: dict | BinaryFile, media_type=None, renderer_context=N
response = renderer_context["response"]
response["Content-Disposition"] = f'attachment; filename="{dataset.filename}.docx"'
return dataset.data.getvalue()


class ZipRenderer(BaseRenderer):
media_type = "application/zip"
format = "zip"

def render(self, dataset: dict | BinaryFile, media_type=None, renderer_context=None):
if isinstance(dataset, dict):
return json.dumps(dataset).encode()

response = renderer_context["response"]
response["Content-Disposition"] = f'attachment; filename="{dataset.filename}.zip"'
return dataset.data.getvalue()
12 changes: 10 additions & 2 deletions frontend/src/components/Main/Actions/Actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,22 @@ class Actions extends Component {
className="dropdown-item"
onClick={() => mainStore.downloadReport("excelUrl")}
icon="file-excel"
text="Download data"
text="Download Data"
/>
<Button
className="dropdown-item"
onClick={mainStore.showWordReportOptionModal}
icon="file-word"
text="Download report"
text="Download Word Report"
/>
{mainStore.isFuture ? (
<Button
className="dropdown-item"
onClick={() => mainStore.downloadReport("excelReportUrl")}
icon="file-zip"
text="Download Excel Report (zip)"
/>
) : null}
<a
className="dropdown-item"
href="#"
Expand Down
3 changes: 0 additions & 3 deletions frontend/src/components/Navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,5 @@ class Navigation extends Component {
}
Navigation.propTypes = {
mainStore: PropTypes.object,
wordUrl: PropTypes.string,
ExcelUrl: PropTypes.string,
config: PropTypes.object,
};
export default Navigation;
8 changes: 8 additions & 0 deletions tests/analysis/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,14 @@ def test_excel(self, pk):
resp = client.get(url)
assert resp.status_code == 200

@pytest.mark.parametrize("pk", analyses)
def test_excel_reports(self, pk):
client = APIClient()
analysis = Analysis.objects.get(pk=pk)
url = reverse("api:analysis-excel-report", args=(analysis.id,))
resp = client.get(url)
assert resp.status_code == 200

@pytest.mark.parametrize("pk", analyses)
def test_word(self, pk):
client = APIClient()
Expand Down
Loading