From 78a2e8ae968ebd156b3cabe557e115245ae07813 Mon Sep 17 00:00:00 2001 From: Christine Ho Date: Wed, 22 Mar 2017 16:27:47 +0100 Subject: [PATCH] Display a comma separated string for fields returning content as lists --- CHANGELOG.txt | 1 + docs/releases/1.10.rst | 1 + wagtail/wagtailforms/tests/test_views.py | 11 ++++++++--- wagtail/wagtailforms/views.py | 12 ++++++++++-- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 3585f4d50d..3ff85ef475 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -43,6 +43,7 @@ Changelog * Fix: Page links within rich text now respect custom URLs defined on specific page models (Gary Krige, Huub Bouma) * Fix: Default avatar no longer visible when using a transparent gravatar image (Thijs Kramer) * Fix: Scrolling within the datetime picker is now usable again for touchpads (Ralph Jacobs) + * Fix: List-based fields within form builder form submissions are now displayed as comma-separated strings rather than as Python lists (Christine Ho, Matt Westcott) 1.9 (16.02.2017) diff --git a/docs/releases/1.10.rst b/docs/releases/1.10.rst index ac74d0ff13..cf25b7eb60 100644 --- a/docs/releases/1.10.rst +++ b/docs/releases/1.10.rst @@ -55,6 +55,7 @@ Bug fixes * Page links within rich text now respect custom URLs defined on specific page models (Gary Krige, Huub Bouma) * Default avatar no longer visible when using a transparent gravatar image (Thijs Kramer) * Scrolling within the datetime picker is now usable again for touchpads (Ralph Jacobs) + * List-based fields within form builder form submissions are now displayed as comma-separated strings rather than as Python lists (Christine Ho, Matt Westcott) Upgrade considerations diff --git a/wagtail/wagtailforms/tests/test_views.py b/wagtail/wagtailforms/tests/test_views.py index fffa56e550..6cac4edf4d 100644 --- a/wagtail/wagtailforms/tests/test_views.py +++ b/wagtail/wagtailforms/tests/test_views.py @@ -170,6 +170,7 @@ class TestFormsSubmissionsList(TestCase, WagtailTestUtils): form_data=json.dumps({ 'your-email': "new@example.com", 'your-message': "this is a fairly new message", + 'your-choices': ['foo', 'baz'], }), ) new_form_submission.submit_time = '2014-01-01T12:00:00.000Z' @@ -209,6 +210,9 @@ class TestFormsSubmissionsList(TestCase, WagtailTestUtils): self.assertTemplateUsed(response, 'wagtailforms/index_submissions.html') self.assertEqual(len(response.context['data_rows']), 2) + # check display of list values within form submissions + self.assertContains(response, 'foo, baz') + def test_list_submissions_after_filter_form_submissions_for_user_hook(self): # Hook forbids to delete form submissions for everyone def construct_forms_for_user(user, queryset): @@ -308,6 +312,7 @@ class TestFormsSubmissionsExport(TestCase, WagtailTestUtils): form_data=json.dumps({ 'your-email': "old@example.com", 'your-message': "this is a really old message", + 'your-choices': ['foo', 'baz'], }), ) old_form_submission.submit_time = '2013-01-01T12:00:00.000Z' @@ -337,7 +342,7 @@ class TestFormsSubmissionsExport(TestCase, WagtailTestUtils): data_lines = response.content.decode().split("\n") self.assertEqual(data_lines[0], 'Submission date,Your email,Your message,Your choices\r') - self.assertEqual(data_lines[1], '2013-01-01 12:00:00+00:00,old@example.com,this is a really old message,None\r') + self.assertEqual(data_lines[1], '2013-01-01 12:00:00+00:00,old@example.com,this is a really old message,"foo, baz"\r') self.assertEqual(data_lines[2], '2014-01-01 12:00:00+00:00,new@example.com,this is a fairly new message,None\r') def test_list_submissions_csv_export_after_filter_form_submissions_for_user_hook(self): @@ -355,7 +360,7 @@ class TestFormsSubmissionsExport(TestCase, WagtailTestUtils): data_lines = response.content.decode().split("\n") self.assertEqual(data_lines[0], 'Submission date,Your email,Your message,Your choices\r') - self.assertEqual(data_lines[1], '2013-01-01 12:00:00+00:00,old@example.com,this is a really old message,None\r') + self.assertEqual(data_lines[1], '2013-01-01 12:00:00+00:00,old@example.com,this is a really old message,"foo, baz"\r') self.assertEqual(data_lines[2], '2014-01-01 12:00:00+00:00,new@example.com,this is a fairly new message,None\r') with self.register_hook('filter_form_submissions_for_user', construct_forms_for_user): @@ -391,7 +396,7 @@ class TestFormsSubmissionsExport(TestCase, WagtailTestUtils): data_lines = response.content.decode().split("\n") self.assertEqual(data_lines[0], 'Submission date,Your email,Your message,Your choices\r') - self.assertEqual(data_lines[1], '2013-01-01 12:00:00+00:00,old@example.com,this is a really old message,None\r') + self.assertEqual(data_lines[1], '2013-01-01 12:00:00+00:00,old@example.com,this is a really old message,"foo, baz"\r') def test_list_submissions_csv_export_with_range_filtering(self): response = self.client.get( diff --git a/wagtail/wagtailforms/views.py b/wagtail/wagtailforms/views.py index 0ee5b06c97..c75fdd767d 100644 --- a/wagtail/wagtailforms/views.py +++ b/wagtail/wagtailforms/views.py @@ -100,7 +100,10 @@ def list_submissions(request, page_id): data_row = [] form_data = s.get_data() for name, label in data_fields: - data_row.append(smart_str(form_data.get(name))) + val = form_data.get(name) + if isinstance(val, list): + val = ', '.join(val) + data_row.append(smart_str(val)) writer.writerow(data_row) return response @@ -109,7 +112,12 @@ def list_submissions(request, page_id): data_rows = [] for s in submissions: form_data = s.get_data() - data_row = [form_data.get(name) for name, label in data_fields] + data_row = [] + for name, label in data_fields: + val = form_data.get(name) + if isinstance(val, list): + val = ', '.join(val) + data_row.append(val) data_rows.append({ "model_id": s.id, "fields": data_row