summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@proxience.com>2015-06-18 03:03:49 +0200
committerÉtienne Loks <etienne.loks@proxience.com>2015-06-18 03:03:49 +0200
commitbc7a7480fefdcc4a5259fa3444ea9fcd4900ed34 (patch)
tree688e1f0b50596c14fdb42f12f05fb20b116d9b03
parent1770e38eb5cba8d8f3959d2a67384b49c9292f5f (diff)
downloadIshtar-bc7a7480fefdcc4a5259fa3444ea9fcd4900ed34.tar.bz2
Ishtar-bc7a7480fefdcc4a5259fa3444ea9fcd4900ed34.zip
Import: try different encoding if the specified doesn't work
-rw-r--r--archaeological_operations/tests.py6
-rw-r--r--ishtar_common/models.py16
2 files changed, 13 insertions, 9 deletions
diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py
index ccb67cc03..73f462895 100644
--- a/archaeological_operations/tests.py
+++ b/archaeological_operations/tests.py
@@ -62,7 +62,8 @@ class ImportOperationTest(TestCase):
'../archaeological_operations/tests/MCC-operations-example.csv', 'rb')
file_dict = {'imported_file': SimpleUploadedFile(mcc_operation_file.name,
mcc_operation_file.read())}
- post_dict = {'importer_type':MCC_OPERATION.pk, 'skip_lines':1}
+ post_dict = {'importer_type':MCC_OPERATION.pk, 'skip_lines':1,
+ "encoding":'utf-8'}
form = forms_common.NewImportForm(data=post_dict, files=file_dict,
instance=None)
form.is_valid()
@@ -133,7 +134,8 @@ class ImportOperationTest(TestCase):
'../archaeological_operations/tests/MCC-parcelles-example.csv', 'rb')
file_dict = {'imported_file': SimpleUploadedFile(mcc_file.name,
mcc_file.read())}
- post_dict = {'importer_type':MCC_PARCEL.pk, 'skip_lines':1}
+ post_dict = {'importer_type':MCC_PARCEL.pk, 'skip_lines':1,
+ "encoding":'utf-8'}
form = forms_common.NewImportForm(data=post_dict, files=file_dict,
instance=None)
form.is_valid()
diff --git a/ishtar_common/models.py b/ishtar_common/models.py
index 1f654fdf4..c1ee90df6 100644
--- a/ishtar_common/models.py
+++ b/ishtar_common/models.py
@@ -1426,13 +1426,15 @@ class Import(models.Model):
@property
def data_table(self):
with open(self.imported_file.path) as csv_file:
- try:
- return [line for line in unicodecsv.reader(csv_file,
- encoding=self.encoding)]
- except UnicodeDecodeError:
- if encoding != encodings[-1]:
- csv_file.seek(0)
- return []
+ encodings = [self.encoding]
+ encodings += [coding for coding, c in ENCODINGS]
+ for encoding in encodings:
+ try:
+ return [line for line in unicodecsv.reader(csv_file,
+ encoding=encoding)]
+ except UnicodeDecodeError:
+ if encoding != encodings[-1]:
+ csv_file.seek(0)
return []
def initialize(self):