Projet

Général

Profil

0001-manager-allow-CSV-file-to-be-iso-8859-15-25984.patch

Frédéric Péters, 25 mars 2019 18:17

Télécharger (3,5 ko)

Voir les différences:

Subject: [PATCH] manager: allow CSV file to be iso-8859-15 (#25984)

 chrono/manager/forms.py | 20 +++++++++++++++++---
 tests/test_manager.py   | 22 ++++++++++++++++++++++
 2 files changed, 39 insertions(+), 3 deletions(-)
chrono/manager/forms.py
169 169
        if b'\0' in content:
170 170
            raise ValidationError(_('Invalid file format.'))
171 171

  
172
        if six.PY3:
173
            content = content.decode('utf-8')
172
        for charset in ('utf-8', 'iso-8859-15'):
173
            try:
174
                content = content.decode(charset)
175
                break
176
            except UnicodeDecodeError:
177
                continue
178
        # all byte-sequences are ok for iso-8859-15 so we will always reach
179
        # this line with content being a unicode string.
180

  
174 181
        try:
175 182
            dialect = csv.Sniffer().sniff(content)
176 183
        except csv.Error:
177 184
            dialect = None
178 185

  
186
        if six.PY3:
187
            utf_8_encoder = lambda x: x
188
        else:
189
            def utf_8_encoder(unicode_csv_data):
190
                for line in unicode_csv_data:
191
                    yield line.encode('utf-8')
192

  
179 193
        events = []
180
        for i, csvline in enumerate(csv.reader(content.splitlines(), dialect=dialect)):
194
        for i, csvline in enumerate(csv.reader(utf_8_encoder(content.splitlines()), dialect=dialect)):
181 195
            if not csvline:
182 196
                continue
183 197
            if len(csvline) < 3:
tests/test_manager.py
581 581
    assert Event.objects.all()[0].label == u'éléphant'
582 582
    Event.objects.all().delete()
583 583

  
584
    resp = app.get('/manage/agendas/%s/import-events' % agenda.id, status=200)
585
    resp.form['events_csv_file'] = Upload('t.csv',
586
            u'2016-09-16,18:00,10,5,éléphant'.encode('iso-8859-15'), 'text/csv')
587
    resp = resp.form.submit(status=302)
588
    assert Event.objects.count() == 1
589
    assert Event.objects.all()[0].start_datetime == make_aware(datetime.datetime(2016, 9, 16, 18, 0))
590
    assert Event.objects.all()[0].places == 10
591
    assert Event.objects.all()[0].waiting_list_places == 5
592
    assert Event.objects.all()[0].label == u'éléphant'
593
    Event.objects.all().delete()
594

  
595
    resp = app.get('/manage/agendas/%s/import-events' % agenda.id, status=200)
596
    resp.form['events_csv_file'] = Upload('t.csv',
597
            u'2016-09-16,18:00,10,5,éléphant'.encode('eucjp'), 'text/csv')
598
    resp = resp.form.submit(status=302)
599
    assert Event.objects.count() == 1
600
    assert Event.objects.all()[0].start_datetime == make_aware(datetime.datetime(2016, 9, 16, 18, 0))
601
    assert Event.objects.all()[0].places == 10
602
    assert Event.objects.all()[0].waiting_list_places == 5
603
    assert Event.objects.all()[0].label == u'\x8f«±l\x8f«±phant'  # eucjp interpreted as iso-8859-15
604
    Event.objects.all().delete()
605

  
584 606
    resp = app.get('/manage/agendas/%s/import-events' % agenda.id, status=200)
585 607
    resp.form['events_csv_file'] = Upload('t.csv', b'date,time,etc.\n'
586 608
                                                   b'2016-09-16,18:00,10,5,bla bla bla\n'
587
-