Skip to content

Commit

Permalink
fix unicode problems
Browse files Browse the repository at this point in the history
  • Loading branch information
valdergallo committed May 31, 2016
1 parent e70309b commit ef404e2
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 7 deletions.
11 changes: 6 additions & 5 deletions data_importer/importers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
from django.db import transaction
from django.db.models.fields import FieldDoesNotExist
from django.core.exceptions import ValidationError
from django.utils.encoding import force_unicode
from django.utils.encoding import force_text
from data_importer.core.descriptor import ReadDescriptor
from data_importer.core.exceptions import StopImporter
from data_importer.core.base import objclass2dict
from data_importer.core.base import DATA_IMPORTER_EXCEL_DECODER
from data_importer.core.base import DATA_IMPORTER_DECODER
from collections import OrderedDict
from io import IOBase


ALPHABETIC = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
Expand Down Expand Up @@ -63,8 +64,8 @@ def to_unicode(bytestr):

try:
decoded = bytestr.decode(DATA_IMPORTER_EXCEL_DECODER) # default by excel csv
except UnicodeEncodeError:
decoded = force_unicode(bytestr, DATA_IMPORTER_DECODER)
except (UnicodeEncodeError, AttributeError):
decoded = force_text(bytestr, DATA_IMPORTER_DECODER)

return decoded

Expand All @@ -76,7 +77,7 @@ def source(self):
@source.setter
def source(self, source):
"""Open source to reader"""
if isinstance(source, file):
if isinstance(source, IOBase):
self._source = source
elif isinstance(source, str) and os.path.exists(source) and source.endswith('csv'):
self._source = open(source, 'rb')
Expand Down Expand Up @@ -392,7 +393,7 @@ def convert_letter_to_number(letter):

@staticmethod
def convert_list_number_to_decimal_integer(list_number):
list_number_reversed = list(reversed(list_number))
list_number_reversed = list(reversed(list(list_number)))
final_number = 0
for number, exp in zip(list_number_reversed, range(len(list_number_reversed))):
final_number += (number + DELAY) * (FACTOR ** exp)
Expand Down
1 change: 1 addition & 0 deletions data_importer/tests/test_base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import unicode_literals
from io import StringIO
from data_importer.importers import BaseImporter
from data_importer.importers import CSVImporter
Expand Down
1 change: 1 addition & 0 deletions data_importer/tests/test_foreignkey.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import unicode_literals
import os
from django.test import TestCase
from data_importer.importers import CSVImporter
Expand Down
2 changes: 1 addition & 1 deletion data_importer/tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.test import TestCase
from mock import Mock
import sys
Expand Down
3 changes: 2 additions & 1 deletion data_importer/writers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from openpyxl import Workbook
from openpyxl import styles
from io import BytesIO
from django.utils.encoding import force_text
from django.core.files import File
from django.utils.safestring import mark_safe
import unicodedata
Expand All @@ -25,7 +26,7 @@ def slugify(value):
aren't alphanumerics, underscores, or hyphens. Converts to lowercase.
Also strips leading and trailing whitespace.
"""
value = unicodedata.normalize('NFKD', unicode(value)).encode('ascii', 'ignore').decode('ascii')
value = unicodedata.normalize('NFKD', force_text(value)).encode('ascii', 'ignore').decode('ascii')
value = re.sub('[^\w\s-]', '', value).strip().lower()
return mark_safe(re.sub('[-\s]+', '_', value))

Expand Down

0 comments on commit ef404e2

Please sign in to comment.