0001-backoffice-detect-CSV-with-wrong-number-of-columns-i.patch
tests/test_backoffice_pages.py | ||
---|---|---|
5685 | 5685 |
assert card2.data['5'].tm_mday == 3 |
5686 | 5686 | |
5687 | 5687 | |
5688 |
def test_backoffice_cards_import_data_csv_invalid_columns(pub): |
|
5689 |
user = create_user(pub) |
|
5690 | ||
5691 |
CardDef.wipe() |
|
5692 |
carddef = CardDef() |
|
5693 |
carddef.workflow_roles = {'_editor': user.roles[0]} |
|
5694 |
carddef.backoffice_submission_roles = user.roles |
|
5695 |
carddef.name = 'test' |
|
5696 |
carddef.fields = [ |
|
5697 |
fields.StringField(id='1', label='String1'), |
|
5698 |
fields.StringField(id='2', label='String2'), |
|
5699 |
fields.TextField(id='3', label='Text'), |
|
5700 |
] |
|
5701 |
carddef.store() |
|
5702 | ||
5703 |
app = login(get_app(pub)) |
|
5704 |
resp = app.get(carddef.get_url()) |
|
5705 |
resp = resp.click('Import data from a CSV file') |
|
5706 | ||
5707 |
csv_data = '''String1,String2,Text |
|
5708 |
1,2,3 |
|
5709 |
4,5,6 |
|
5710 |
7, |
|
5711 |
8,9,10,11 |
|
5712 |
12,13,14 |
|
5713 | ||
5714 |
''' |
|
5715 |
resp.forms[0]['file'] = Upload('test.csv', csv_data.encode('utf-8'), 'text/csv') |
|
5716 |
resp = resp.forms[0].submit() |
|
5717 |
assert 'CSV file contains lines with wrong number of columns.' in resp.text |
|
5718 |
assert '(line numbers 4, 5, 7)' in resp.text |
|
5719 | ||
5720 |
csv_data += '\n' * 10 |
|
5721 |
resp.forms[0]['file'] = Upload('test.csv', csv_data.encode('utf-8'), 'text/csv') |
|
5722 |
resp = resp.forms[0].submit() |
|
5723 |
assert 'CSV file contains lines with wrong number of columns.' in resp.text |
|
5724 |
assert '(line numbers 4, 5, 7, 8, 9 and more)' in resp.text |
|
5725 | ||
5726 | ||
5688 | 5727 |
def test_backoffice_cards_wscall_failure_display(http_requests, pub, studio): |
5689 | 5728 |
LoggedError.wipe() |
5690 | 5729 |
user = create_user(pub) |
wcs/backoffice/data_management.py | ||
---|---|---|
226 | 226 |
raise ValueError(_('CSV file contains less columns than card fields.')) |
227 | 227 | |
228 | 228 |
data_lines = [] |
229 |
for csv_line in reader: |
|
229 |
incomplete_lines = [] |
|
230 |
for line_no, csv_line in enumerate(reader): |
|
230 | 231 |
data_line = {} |
232 |
if len(csv_line) != len(carddef_fields): |
|
233 |
# +2 because header and counting from 1. |
|
234 |
incomplete_lines.append(str(line_no + 2)) |
|
235 |
continue |
|
231 | 236 |
for i, field in enumerate(carddef_fields): |
232 | 237 |
value = csv_line[i] |
233 | 238 |
# skip empty values |
... | ... | |
250 | 255 |
data_line['%s_structured' % field_id] = structured_value |
251 | 256 |
data_lines.append(data_line) |
252 | 257 | |
258 |
if incomplete_lines: |
|
259 |
error_message = _('CSV file contains lines with wrong number of columns.') |
|
260 |
if len(incomplete_lines) < 5: |
|
261 |
error_message += ' ' + _('(line numbers %s)') % ', '.join(incomplete_lines) |
|
262 |
else: |
|
263 |
error_message += ' ' + _('(line numbers %s and more)') % ', '.join(incomplete_lines[:5]) |
|
264 |
raise ValueError(error_message) |
|
265 | ||
253 | 266 |
class ImportAction: |
254 | 267 |
def __init__(self, data_class, lines): |
255 | 268 |
self.data_class = data_class |
256 |
- |