Projet

Général

Profil

0001-manager-report-errors-in-CSV-import-using-event-inde.patch

Valentin Deniaud, 17 octobre 2022 17:11

Télécharger (5,54 ko)

Voir les différences:

Subject: [PATCH] manager: report errors in CSV import using event indexes
 (#66184)

 chrono/manager/forms.py     | 21 +++++++++++++++------
 tests/manager/test_event.py |  9 ++++++++-
 2 files changed, 23 insertions(+), 7 deletions(-)
chrono/manager/forms.py
27 27
from django import forms
28 28
from django.conf import settings
29 29
from django.contrib.auth.models import Group
30
from django.contrib.humanize.templatetags.humanize import ordinal
30 31
from django.core.exceptions import FieldDoesNotExist
31 32
from django.core.validators import URLValidator
32 33
from django.db import transaction
......
35 36
from django.template import Context, Template, TemplateSyntaxError, VariableDoesNotExist
36 37
from django.utils.encoding import force_str
37 38
from django.utils.formats import date_format
39
from django.utils.html import format_html, mark_safe
38 40
from django.utils.timezone import localtime, make_aware, now
39 41
from django.utils.translation import gettext_lazy as _
40 42

  
......
1060 1062
        super().__init__(**kwargs)
1061 1063

  
1062 1064
    def clean_events_csv_file(self):
1065
        class ValidationErrorWithOrdinal(ValidationError):
1066
            def __init__(self, message, event_no):
1067
                super().__init__(message)
1068
                self.message = format_html(message, event_no=mark_safe(ordinal(event_no + 1)))
1069

  
1063 1070
        content = self.cleaned_data['events_csv_file'].read()
1064 1071
        if b'\0' in content:
1065 1072
            raise ValidationError(_('Invalid file format.'))
......
1092 1099
            if not csvline:
1093 1100
                continue
1094 1101
            if len(csvline) < 3:
1095
                raise ValidationError(_('Invalid file format. (line %d)') % (i + 1))
1102
                raise ValidationErrorWithOrdinal(_('Invalid file format. ({event_no} event)'), i)
1096 1103
            if i == 0 and csvline[0].strip('#') in ('date', 'Date', _('date'), _('Date')):
1097 1104
                continue
1098 1105

  
......
1146 1153
                event.start_datetime = event_datetime
1147 1154
                break
1148 1155
            else:
1149
                raise ValidationError(_('Invalid file format. (date/time format, line %d)') % (i + 1))
1156
                raise ValidationErrorWithOrdinal(
1157
                    _('Invalid file format. (date/time format, {event_no} event)'), i
1158
                )
1150 1159
            try:
1151 1160
                event.places = int(csvline[2])
1152 1161
            except ValueError:
1153
                raise ValidationError(_('Invalid file format. (number of places, line %d)') % (i + 1))
1162
                raise ValidationError(_('Invalid file format. (number of places, {event_no} event)'), i)
1154 1163
            if len(csvline) >= 4:
1155 1164
                try:
1156 1165
                    event.waiting_list_places = int(csvline[3])
1157 1166
                except ValueError:
1158 1167
                    raise ValidationError(
1159
                        _('Invalid file format. (number of places in waiting list, line %d)') % (i + 1)
1168
                        _('Invalid file format. (number of places in waiting list, {event_no} event)'), i
1160 1169
                    )
1161 1170

  
1162 1171
            column_index = 7
......
1183 1192
                    except ValueError:
1184 1193
                        continue
1185 1194
                else:
1186
                    raise ValidationError(_('Invalid file format. (date/time format, line %d)') % (i + 1))
1195
                    raise ValidationError(_('Invalid file format. (date/time format, {event_no} event)'), i)
1187 1196

  
1188 1197
            if len(csvline) >= 11 and csvline[10]:  # duration is optional
1189 1198
                try:
1190 1199
                    event.duration = int(csvline[10])
1191 1200
                except ValueError:
1192
                    raise ValidationError(_('Invalid file format. (duration, line %d)') % (i + 1))
1201
                    raise ValidationError(_('Invalid file format. (duration, {event_no} event)'), i)
1193 1202

  
1194 1203
            try:
1195 1204
                event.full_clean(exclude=['desk', 'meeting_type', 'primary_event'])
tests/manager/test_event.py
6 6
import requests
7 7
from django.core.management import call_command
8 8
from django.db import connection
9
from django.test import override_settings
9 10
from django.test.utils import CaptureQueriesContext
10 11
from django.utils.timezone import localtime, make_aware, now
11 12
from webtest import Upload
......
784 785

  
785 786
    resp.form['events_csv_file'] = Upload('t.csv', b'2016-14-16,18:00,10', 'text/csv')
786 787
    resp = resp.form.submit(status=200)
787
    assert 'Invalid file format. (date/time format' in resp.text
788
    assert 'Invalid file format. (date/time format, 1st event)' in resp.text
789

  
790
    with override_settings(LANGUAGE_CODE='fr-fr'):
791
        resp.form['events_csv_file'] = Upload('t.csv', b'2016-14-16,18:00,10', 'text/csv')
792
        resp = resp.form.submit(status=200)
793
        # ensure <sup> tag is not escaped
794
        assert '1<sup>er</sup>' in resp.text
788 795

  
789 796
    resp.form['events_csv_file'] = Upload('t.csv', b'2016-09-16,18:00,blah', 'text/csv')
790 797
    resp = resp.form.submit(status=200)
791
-