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

Fix 584 #656

Closed
wants to merge 1 commit into from
Closed
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
14 changes: 14 additions & 0 deletions wagtail/tests/fixtures/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,20 @@
"page": 8
}
},
{
"pk": 3,
"model": "tests.formfield",
"fields": {
"sort_order": 3,
"label": "Your choices",
"field_type": "checkboxes",
"required": false,
"choices": "foo,bar,baz",
"default_value": "",
"help_text": "",
"page": 8
}
},

{
"pk": 10,
Expand Down
12 changes: 10 additions & 2 deletions wagtail/wagtailforms/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,15 @@ class FormSubmission(models.Model):
submit_time = models.DateTimeField(auto_now_add=True)

def get_data(self):
return json.loads(self.form_data)
# Convert sequences to strings.
# This check is required because form values used to be stored
# as strings and our now stored as lists, so form data may
# contain a mix of lists and strings.
data = json.loads(self.form_data)
for k, v in data.iteritems():
if hasattr(v, '__iter__'):
data[k] = ", ".join(data[k])
Copy link
Contributor

Choose a reason for hiding this comment

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

This breaks the CSV export. Strings are iterable and they are being printed out as "H", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d"

return data

def __str__(self):
return self.form_data
Expand Down Expand Up @@ -133,7 +141,7 @@ def get_form_parameters(self):
def process_form_submission(self, form):
# remove csrf_token from form.data
form_data = dict(
i for i in form.data.items()
i for i in form.data.iterlists()
if i[0] != 'csrfmiddlewaretoken'
)

Expand Down
31 changes: 28 additions & 3 deletions wagtail/wagtailforms/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ def test_get_form(self):

def test_post_invalid_form(self):
response = self.client.post('/contact-us/', {
'your-email': 'bob', 'your-message': 'hello world'
'your-email': 'bob',
'your-message': 'hello world',
'your-choices': ''
})

# Check response
Expand All @@ -34,7 +36,9 @@ def test_post_invalid_form(self):

def test_post_valid_form(self):
response = self.client.post('/contact-us/', {
'your-email': 'bob@example.com', 'your-message': 'hello world'
'your-email': 'bob@example.com',
'your-message': 'hello world',
'your-choices': {'foo': '', 'bar': '', 'baz': ''}
})

# Check response
Expand All @@ -53,6 +57,27 @@ def test_post_valid_form(self):
form_page = Page.objects.get(url_path='/home/contact-us/')
self.assertTrue(FormSubmission.objects.filter(page=form_page, form_data__contains='hello world').exists())

def test_post_multiple_values(self):
response = self.client.post('/contact-us/', {
'your-email': 'bob@example.com',
'your-message': 'hello world',
'your-choices': {'foo': 'on', 'bar': 'on', 'baz': 'on'}
})

# Check response
self.assertContains(response, "Thank you for your feedback.")
self.assertTemplateNotUsed(response, 'tests/form_page.html')
self.assertTemplateUsed(response, 'tests/form_page_landing.html')

# Check that the three checkbox values were saved correctly
form_page = Page.objects.get(url_path='/home/contact-us/')
submission = FormSubmission.objects.filter(
page=form_page, form_data__contains='hello world'
)
self.assertIn("foo", submission[0].form_data)
self.assertIn("bar", submission[0].form_data)
self.assertIn("baz", submission[0].form_data)


class TestPageModes(TestCase):
fixtures = ['test.json']
Expand Down Expand Up @@ -89,7 +114,7 @@ def test_fields(self):
This tests that all fields were added to the form with the correct types
"""
form_class = self.fb.get_form_class()

self.assertTrue('your-email' in form_class.base_fields.keys())
self.assertTrue('your-message' in form_class.base_fields.keys())

Expand Down