Skip to content

Commit

Permalink
Comma-separate list values in formatted_data
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiask committed Sep 3, 2024
1 parent 68986d7 commit 0871968
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Next version

* Changed form submissions to convert choice values back to the unslugified
variant.
* Changed ``FormSubmission.formatted_data`` to automatically comma-separate
list values instead of returning the list representation.

0.25
----
Expand Down
10 changes: 9 additions & 1 deletion form_designer/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,15 @@ def __str__(self):

def formatted_data(self, *, html=False, default="Ø"):
sd = self.form.submissions_data(submissions=[self])
data = ((field["title"], field["value"] or default) for field in sd[0]["data"])
data = (
(
field["title"],
", ".join(value)
if isinstance(value := field["value"] or default, list)
else value,
)
for field in sd[0]["data"]
)
if html:
return format_html(
"<dl>{}</dl>",
Expand Down
35 changes: 31 additions & 4 deletions tests/testapp/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def validate_honeypot(form, data, **kwargs):


class FormsTest(TestCase):
maxDiff = None

def test_forms(self):
form = Form.objects.create(
title="Test contact form",
Expand Down Expand Up @@ -48,13 +50,29 @@ def test_forms(self):
type="date",
is_required=False,
)
form.fields.create(
ordering=6,
title="Multiple Choice",
name="multiple-choice",
type="multiple-select",
choices="Choice A,Choice B,Choice C",
is_required=False,
)

form_class = form.form_class()
form_instance = form_class()

self.assertListEqual(
[field.name for field in form_instance],
["subject", "email", "body", "please-call-me", "radio", "date"],
[
"subject",
"email",
"body",
"please-call-me",
"radio",
"date",
"multiple-choice",
],
)

page = Page.objects.create(override_url="/", title="")
Expand All @@ -72,7 +90,7 @@ def test_forms(self):
self.assertContains(
response,
"<input",
10, # csrf, subject, email, checkbox, _formcontent, submit, radio*3, date
13, # csrf, subject, email, checkbox, _formcontent, submit, radio*3, date, three multiple choice
)
self.assertContains(response, "<textarea", 1)
self.assertContains(
Expand Down Expand Up @@ -104,8 +122,8 @@ def test_forms(self):

self.assertContains(
response,
"Enter a valid e",
1, # Django 1.4 has e-mail, 1.5 and up email
"Enter a valid email",
1,
)

response = self.client.post(
Expand All @@ -117,6 +135,7 @@ def test_forms(self):
f"fc{form.id}-body": "Hello World",
f"fc{form.id}-radio": "two-what",
f"fc{form.id}-date": "2022-10-02",
f"fc{form.id}-multiple-choice": ["choice-a", "choice-c"],
},
)

Expand Down Expand Up @@ -160,11 +179,19 @@ def test_forms(self):
},
{"name": "radio", "title": "Radio Select", "value": "two what"},
{"name": "date", "title": "Date", "value": "2022-10-02"},
{
"name": "multiple-choice",
"title": "Multiple Choice",
"value": ["Choice A", "Choice C"],
},
],
}
],
)

data = submission.formatted_data()
self.assertIn("Multiple Choice:\nChoice A, Choice C", data)

# Export the submission
User.objects.create_superuser("admin", "[email protected]", "password")
self.client.login(username="admin", password="password")
Expand Down

0 comments on commit 0871968

Please sign in to comment.