From a36a82f28ee691b420978e1a5b1dfd9b21580c6c Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Thu, 18 Jun 2015 01:13:40 +0200 Subject: Import: manage different encoding for source file --- ishtar_common/models.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'ishtar_common/models.py') diff --git a/ishtar_common/models.py b/ishtar_common/models.py index a23808c73..1f654fdf4 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -1352,12 +1352,17 @@ IMPORT_STATE = (("C", _(u"Created")), ) IMPORT_STATE_DCT = dict(IMPORT_STATE) +ENCODINGS = [(settings.ENCODING, settings.ENCODING), + (settings.ALT_ENCODING, settings.ALT_ENCODING), + ('utf-8', 'utf-8')] class Import(models.Model): user = models.ForeignKey('IshtarUser') importer_type = models.ForeignKey(ImporterType) imported_file = models.FileField(_(u"Imported file"), upload_to="upload/imports/") + encoding = models.CharField(_(u"Encoding"), choices=ENCODINGS, + default='utf-8', max_length=15) skip_lines = models.IntegerField(_(u"Skip lines"), default=1) error_file = models.FileField(_(u"Error file"), upload_to="upload/imports/", @@ -1420,16 +1425,14 @@ class Import(models.Model): @property def data_table(self): - encodings = [settings.ENCODING, settings.ALT_ENCODING, 'utf-8'] with open(self.imported_file.path) as csv_file: - 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) - continue + try: + return [line for line in unicodecsv.reader(csv_file, + encoding=self.encoding)] + except UnicodeDecodeError: + if encoding != encodings[-1]: + csv_file.seek(0) + return [] return [] def initialize(self): -- cgit v1.2.3