From 35f523c95734701719b5cf4d7e17cabb01f906b2 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Tue, 6 Mar 2018 15:11:59 +0000 Subject: [PATCH] Prevent cells containing commas breaking downloads If a cell in the original file contains a comma, it comes back as two cells in the downloaded file. The CSV writer has logic to deal with this. It seems to work a lot better that just concatenating the columns with commas ourselves. --- app/utils.py | 2 +- tests/app/test_utils.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/utils.py b/app/utils.py index 47285924e..4d108fbb9 100644 --- a/app/utils.py +++ b/app/utils.py @@ -175,7 +175,7 @@ def generate_notifications_csv(**kwargs): notification['created_at'], notification['updated_at'] ] - yield ','.join(map(str, values)) + '\n' + yield Spreadsheet.from_rows([map(str, values)]).as_csv_data if notifications_resp['links'].get('next'): kwargs['page'] += 1 diff --git a/tests/app/test_utils.py b/tests/app/test_utils.py index 8c0959554..e1090ec48 100644 --- a/tests/app/test_utils.py +++ b/tests/app/test_utils.py @@ -145,6 +145,14 @@ def test_can_create_spreadsheet_from_dict_with_filename(): ['Row number', 'phone_number', 'a', 'b', 'c', 'Template', 'Type', 'Job', 'Status', 'Time'], ['1', '07700900123', '🐜', '🐝', '🦀', 'foo', 'sms', 'bar.csv', 'Delivered', 'Thursday 19 April at 12:00'], ), + ( + """ + "phone_number", "a", "b", "c" + "07700900123","🐜,🐜","🐝,🐝","🦀" + """, + ['Row number', 'phone_number', 'a', 'b', 'c', 'Template', 'Type', 'Job', 'Status', 'Time'], + ['1', '07700900123', '🐜,🐜', '🐝,🐝', '🦀', 'foo', 'sms', 'bar.csv', 'Delivered', 'Thursday 19 April at 12:00'], + ), ]) def test_generate_notifications_csv_returns_correct_csv_file( app_,