0001-report-failure-to-detect-CSV-dialect-fixes-26580.patch
passerelle/apps/csvdatasource/models.py | ||
---|---|---|
124 | 124 |
file_type = self.csv_file.name.split('.')[-1] |
125 | 125 |
if file_type in ('ods', 'xls', 'xlsx') and not self.sheet_name: |
126 | 126 |
raise ValidationError(_('You must specify a sheet name')) |
127 |
if file_type not in ('ods', 'xls', 'xlsx'): |
|
128 |
try: |
|
129 |
self._detect_dialect_options() |
|
130 |
except Exception as e: |
|
131 |
raise ValidationError(_('Could not detect CSV dialect: %s') % e) |
|
127 | 132 |
return super(CsvDataSource, self).clean(*args, **kwargs) |
128 | 133 | |
134 |
def _detect_dialect_options(self): |
|
135 |
content = self.get_content_without_bom() |
|
136 |
dialect = csv.Sniffer().sniff(content) |
|
137 |
self.dialect_options = { |
|
138 |
k: v for k, v in vars(dialect).items() if not k.startswith('_') |
|
139 |
} |
|
140 | ||
129 | 141 |
def save(self, *args, **kwargs): |
130 |
file_type = self.csv_file.name.split('.')[-1] |
|
131 |
if file_type not in ('ods', 'xls', 'xlsx'): |
|
132 |
content = self.get_content_without_bom() |
|
133 |
dialect = csv.Sniffer().sniff(content) |
|
134 |
self.dialect_options = { |
|
135 |
k: v for k, v in vars(dialect).items() if not k.startswith('_') |
|
136 |
} |
|
137 | 142 |
result = super(CsvDataSource, self).save(*args, **kwargs) |
138 | 143 |
self.cache_data() |
139 | 144 |
return result |
tests/test_csv_datasource.py | ||
---|---|---|
13 | 13 |
from passerelle.base.models import ApiUser, AccessRight |
14 | 14 |
from test_manager import login, admin_user |
15 | 15 | |
16 |
import webtest |
|
17 | ||
16 | 18 |
data = """121;69981;DELANOUE;Eliot;H |
17 | 19 |
525;6;DANIEL WILLIAMS;Shanone;F |
18 | 20 |
253;67742;MARTIN;Sandra;F |
... | ... | |
659 | 661 |
resp = resp.form.submit('delete') |
660 | 662 |
resp = resp.follow() |
661 | 663 |
assert 'No query are defined.' in resp.body |
664 | ||
665 | ||
666 |
def test_csv_sniffer(admin_user, app): |
|
667 |
app = login(app) |
|
668 |
resp = app.get('/manage/csvdatasource/add') |
|
669 |
form = resp.form |
|
670 |
form.set('title', 'Title') |
|
671 |
form.set('description', 'Description') |
|
672 |
form.set('csv_file', webtest.Upload('test.csv', '\n', 'application/octet-stream')) |
|
673 |
resp = form.submit() |
|
674 |
assert 'Could not detect CSV dialect' in resp |
|
662 |
- |