Projet

Général

Profil

0001-csvdatasource-keep-inline-newlines-when-parsing-CSV-.patch

Benjamin Dauvergne, 10 avril 2020 17:27

Télécharger (2,55 ko)

Voir les différences:

Subject: [PATCH] csvdatasource: keep inline newlines when parsing CSV files
 (#41612)

 passerelle/apps/csvdatasource/models.py |  7 ++++++-
 tests/test_csv_datasource.py            | 28 +++++++++++++++++++++++++
 2 files changed, 34 insertions(+), 1 deletion(-)
passerelle/apps/csvdatasource/models.py
14 14
# You should have received a copy of the GNU Affero General Public License
15 15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 16

  
17
import io
17 18
import os
18 19
import re
19 20
import csv
......
222 223
        file_type = self.csv_file.name.split('.')[-1]
223 224
        if file_type not in ('ods', 'xls', 'xlsx'):
224 225
            content = self.get_content_without_bom()
225
            reader = csv.reader(content.splitlines(), **self.dialect_options)
226
            if six.PY2:
227
                fd = io.BytesIO(content)
228
            else:
229
                fd = io.StringIO(content)
230
            reader = csv.reader(fd, **self.dialect_options)
226 231
            rows = list(reader)
227 232

  
228 233
        else:
tests/test_csv_datasource.py
16 16
# You should have received a copy of the GNU Affero General Public License
17 17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 18

  
19
import io
19 20
import os
20 21
import time
21 22
import pytest
......
855 856
        app = login(app)
856 857
        response = app.get(csvdata.get_absolute_url())
857 858
        assert 'Oct. 27, 2019, 2:20 a.m.' in response
859

  
860

  
861
def test_csv_multiline(app, setup, admin_user):
862
    csvdata, url = setup(
863
        'id,text,comment',
864
        filename='test.csv',
865
        data=io.BytesIO(b'''1,slug1,"un
866
retour
867
chariot"
868
2,slug2,ok'''))
869

  
870
    response = app.get(url)
871
    assert response.json == {
872
        'data': [
873
            {
874
                'comment': u'un\nretour\nchariot',
875
                'id': '1',
876
                'text': 'slug1'
877
             },
878
            {
879
                'comment': 'ok',
880
                'id': '2',
881
                'text': u'slug2'
882
            }
883
        ],
884
        u'err': 0
885
    }
858
-