diff options
author | Étienne Loks <etienne.loks@proxience.com> | 2015-06-18 01:13:40 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@proxience.com> | 2015-06-18 01:13:40 +0200 |
commit | 353f779f15a2b3fbee066eb7a7151002f19e2256 (patch) | |
tree | bf74cc67a89ef0c93fa213e58d9aed1ac0517f83 /ishtar_common/models.py | |
parent | 8a7b2bbf395e3c90b4feed4a5631bc55848c2408 (diff) | |
download | Ishtar-353f779f15a2b3fbee066eb7a7151002f19e2256.tar.bz2 Ishtar-353f779f15a2b3fbee066eb7a7151002f19e2256.zip |
Import: manage different encoding for source file
Diffstat (limited to 'ishtar_common/models.py')
-rw-r--r-- | ishtar_common/models.py | 21 |
1 files changed, 12 insertions, 9 deletions
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): |