Skip to content

Commit 0871968

Browse files
committed
Comma-separate list values in formatted_data
1 parent 68986d7 commit 0871968

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Next version
99

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

1315
0.25
1416
----

form_designer/models.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,15 @@ def __str__(self):
397397

398398
def formatted_data(self, *, html=False, default="Ø"):
399399
sd = self.form.submissions_data(submissions=[self])
400-
data = ((field["title"], field["value"] or default) for field in sd[0]["data"])
400+
data = (
401+
(
402+
field["title"],
403+
", ".join(value)
404+
if isinstance(value := field["value"] or default, list)
405+
else value,
406+
)
407+
for field in sd[0]["data"]
408+
)
401409
if html:
402410
return format_html(
403411
"<dl>{}</dl>",

tests/testapp/test_forms.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ def validate_honeypot(form, data, **kwargs):
1818

1919

2020
class FormsTest(TestCase):
21+
maxDiff = None
22+
2123
def test_forms(self):
2224
form = Form.objects.create(
2325
title="Test contact form",
@@ -48,13 +50,29 @@ def test_forms(self):
4850
type="date",
4951
is_required=False,
5052
)
53+
form.fields.create(
54+
ordering=6,
55+
title="Multiple Choice",
56+
name="multiple-choice",
57+
type="multiple-select",
58+
choices="Choice A,Choice B,Choice C",
59+
is_required=False,
60+
)
5161

5262
form_class = form.form_class()
5363
form_instance = form_class()
5464

5565
self.assertListEqual(
5666
[field.name for field in form_instance],
57-
["subject", "email", "body", "please-call-me", "radio", "date"],
67+
[
68+
"subject",
69+
"email",
70+
"body",
71+
"please-call-me",
72+
"radio",
73+
"date",
74+
"multiple-choice",
75+
],
5876
)
5977

6078
page = Page.objects.create(override_url="/", title="")
@@ -72,7 +90,7 @@ def test_forms(self):
7290
self.assertContains(
7391
response,
7492
"<input",
75-
10, # csrf, subject, email, checkbox, _formcontent, submit, radio*3, date
93+
13, # csrf, subject, email, checkbox, _formcontent, submit, radio*3, date, three multiple choice
7694
)
7795
self.assertContains(response, "<textarea", 1)
7896
self.assertContains(
@@ -104,8 +122,8 @@ def test_forms(self):
104122

105123
self.assertContains(
106124
response,
107-
"Enter a valid e",
108-
1, # Django 1.4 has e-mail, 1.5 and up email
125+
"Enter a valid email",
126+
1,
109127
)
110128

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

@@ -160,11 +179,19 @@ def test_forms(self):
160179
},
161180
{"name": "radio", "title": "Radio Select", "value": "two what"},
162181
{"name": "date", "title": "Date", "value": "2022-10-02"},
182+
{
183+
"name": "multiple-choice",
184+
"title": "Multiple Choice",
185+
"value": ["Choice A", "Choice C"],
186+
},
163187
],
164188
}
165189
],
166190
)
167191

192+
data = submission.formatted_data()
193+
self.assertIn("Multiple Choice:\nChoice A, Choice C", data)
194+
168195
# Export the submission
169196
User.objects.create_superuser("admin", "[email protected]", "password")
170197
self.client.login(username="admin", password="password")

0 commit comments

Comments
 (0)