From 744ece1173142c2e3e3691b73084ae3a6ccb0a7b Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Fri, 10 Apr 2020 17:27:10 +0200 Subject: [PATCH 1/2] csvdatasource: keep inline newlines when parsing CSV files (#41612) --- passerelle/apps/csvdatasource/models.py | 7 ++++++- tests/test_csv_datasource.py | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/passerelle/apps/csvdatasource/models.py b/passerelle/apps/csvdatasource/models.py index 76ce4636..ff32166f 100644 --- a/passerelle/apps/csvdatasource/models.py +++ b/passerelle/apps/csvdatasource/models.py @@ -14,6 +14,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import io import os import re import csv @@ -222,7 +223,11 @@ class CsvDataSource(BaseResource): file_type = self.csv_file.name.split('.')[-1] if file_type not in ('ods', 'xls', 'xlsx'): content = self.get_content_without_bom() - reader = csv.reader(content.splitlines(), **self.dialect_options) + if six.PY2: + fd = io.BytesIO(content) + else: + fd = io.StringIO(content) + reader = csv.reader(fd, **self.dialect_options) rows = list(reader) else: diff --git a/tests/test_csv_datasource.py b/tests/test_csv_datasource.py index 96bd28e6..5a630b82 100644 --- a/tests/test_csv_datasource.py +++ b/tests/test_csv_datasource.py @@ -16,6 +16,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import io import os import time import pytest @@ -855,3 +856,23 @@ def test_csv_dst(app, setup, admin_user): app = login(app) response = app.get(csvdata.get_absolute_url()) assert 'Oct. 27, 2019, 2:20 a.m.' in response + + +def test_csv_multiline(app, setup, admin_user): + csvdata, url = setup( + 'id,text,comment', + filename='test.csv', + data=io.BytesIO(b'''1,slug1,"un +retour +chariot" +2,slug2,ok +3,slug3,ok +4,slug4,ok +5,slug5,ok''')) + + response = app.get(url) + assert response.json['data'][0] == { + 'comment': u'un\nretour\nchariot', + 'id': '1', + 'text': 'slug1', + } -- 2.24.0