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

Issue #575 - Select boxes rendering #583

Merged
merged 3 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 32 additions & 21 deletions src/openforms/submissions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from collections import defaultdict
from dataclasses import dataclass
from datetime import date, timedelta
from typing import Any, List, Mapping, Optional, Tuple
from typing import Any, Dict, List, Mapping, Optional, Tuple

from django.contrib.postgres.fields import JSONField
from django.core.files.base import ContentFile, File
Expand Down Expand Up @@ -220,9 +220,11 @@ def get_merged_data_with_component_type(self) -> dict:
):
components = step.form_step.form_definition.configuration["components"]
flattened_components = get_flattened_components(components)
component_key_to_type = dict()
for component in flattened_components:
component_key_to_type[component["key"]] = component["type"]
component_key_to_type = {
component["key"]: component["type"]
for component in flattened_components
}

for key, value in step.data.items():
if key in merged_data:
logger.warning(
Expand Down Expand Up @@ -253,6 +255,30 @@ def get_merged_data(self) -> dict:

return merged_data

def get_printable_data(self) -> Dict[str, str]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine for now, but if we keep expanding on this, we might have to just register a display/print function which takes the info argument and have a mapping of type: print_function so that can be tested/maintained in a bit more isolated way.

printable_data = dict()
attachment_data = self.get_merged_attachments()

for key, info in self.get_merged_data_with_component_type().items():
if info["type"] == "file":
files = attachment_data.get(key)
if files:
printable_data[key] = _("attachment: %s") % (
", ".join(file.get_display_name() for file in files)
)
else:
printable_data[key] = _("attachment")
elif info["type"] == "selectboxes":
formatted_select_boxes = ", ".join(
[label for label, selected in info["value"].items() if selected]
)
printable_data[key] = formatted_select_boxes
else:
# more here? like getComponentValue() in the SDK?
printable_data[key] = info["value"]

return printable_data

data = property(get_merged_data)
data_with_component_type = property(get_merged_data_with_component_type)

Expand Down Expand Up @@ -393,29 +419,14 @@ def __str__(self):

def generate_submission_report_pdf(self) -> None:
form = self.submission.form

submission_data = dict()
attachment_data = self.submission.get_merged_attachments()

for key, info in self.submission.get_merged_data_with_component_type().items():
if info["type"] == "file":
files = attachment_data.get(key)
if files:
submission_data[key] = _("attachment: %s") % (
", ".join(file.get_display_name() for file in files)
)
else:
submission_data[key] = _("attachment")
else:
# more here? like getComponentValue() in the SDK?
submission_data[key] = info["value"]
printable_data = self.submission.get_printable_data()

html_report = render(
request=None,
template_name="report/submission_report.html",
context={
"form": form,
"submission_data": submission_data,
"submission_data": printable_data,
"submission": self.submission,
},
).content.decode("utf8")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
table {
border: 1px solid black;
border-collapse: collapse;
table-layout: auto;
table-layout: fixed;
width: 100%;
}
.table-cell-key {
Expand Down
31 changes: 31 additions & 0 deletions src/openforms/submissions/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,34 @@ def test_get_merged_data_with_component_types(self):
"key5": {"type": "textfield", "value": "this is some inner text"},
},
)

def test_get_printable_data_with_selectboxes(self):
form_definition = FormDefinitionFactory.create(
configuration={
"display": "form",
"components": [
{
"key": "testSelectBoxes",
"type": "selectboxes",
"values": [
{"values": "test1", "label": "test1", "shortcut": ""},
{"values": "test2", "label": "test2", "shortcut": ""},
{"values": "test3", "label": "test3", "shortcut": ""},
],
},
],
}
)
submission = SubmissionFactory.create()
SubmissionStepFactory.create(
submission=submission,
data={"testSelectBoxes": {"test1": True, "test2": True, "test3": False}},
form_step=FormStepFactory.create(form_definition=form_definition),
)

printable_data = submission.get_printable_data()

self.assertEqual(
printable_data["testSelectBoxes"],
"test1, test2",
)