Skip to content

Commit

Permalink
bugfix read excel csv
Browse files Browse the repository at this point in the history
  • Loading branch information
valdergallo committed Oct 9, 2016
1 parent 33e324c commit 796091f
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ env:
install:
- export PYTHONPATH=$PWD;
- export DJANGO_SETTINGS_MODULE=django_test_settings;
- pip install -q Django==$DJANGO -r requirements.txt
- pip install -q Django==$DJANGO

branches:
only:
Expand Down
2 changes: 1 addition & 1 deletion data_importer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# encoding: utf-8
__doc__ = 'Data Importer'
__version__ = '3.0.0'
__version__ = '3.0.1'
__author__ = 'Valder Gallo <[email protected]>'
16 changes: 13 additions & 3 deletions data_importer/importers/base.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# encoding: utf-8
from __future__ import unicode_literals
import os
import sys
import re
import io
import six
import codecs
from django.db import transaction
from django.db.models.fields import FieldDoesNotExist
from django.core.exceptions import ValidationError
Expand Down Expand Up @@ -79,7 +81,10 @@ def source(self, source=None, encoding="cp1252"):
if isinstance(source, io.IOBase):
self._source = source
elif isinstance(source, six.string_types) and os.path.exists(source) and source.endswith('csv'):
self._source = io.open(source, 'rb')
if sys.version_info >= (3,0):
self._source = codecs.open(source, 'rb', encoding=encoding)
else:
self._source = codecs.open(source, 'rb')
elif isinstance(source, list):
self._source = source
elif hasattr(source, 'file_upload'): # for FileHistory instances
Expand Down Expand Up @@ -177,15 +182,20 @@ def clean_field(self, field_name, value):
field.clean(value, field)
except FieldDoesNotExist:
pass # do nothing if not find this field in model
except ValidationError as msg:
except Exception as msg:
default_msg = msg.messages[0].replace('This field', '')
new_msg = 'Field ({0!s}) {1!s}'.format(field.name, default_msg)
raise ValidationError(new_msg)

clean_function = getattr(self, 'clean_{0!s}'.format(field_name), False)

if clean_function:
return clean_function(value)
try:
return clean_function(value)
except Exception as msg:
default_msg = str(msg).replace('This field', '')
new_msg = 'Field ({0!s}) {1!s}'.format(field_name, default_msg)
raise ValidationError(new_msg)
return value

def process_row(self, row, values):
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def readme():
class PyTest(TestCommand):
def finalize_options(self):
TestCommand.finalize_options(self)
self.test_args = ['data_importer', 'tests', '--cov=data_importer', '-vrsx', '-x']
self.test_args = ['data_importer', 'tests', '--cov=data_importer', '-vrsx']
self.test_suite = True

def run_tests(self):
Expand Down Expand Up @@ -65,7 +65,7 @@ def run_tests(self):
],
version=data_importer.__version__,
install_requires=install_requires,
tests_requires=tests_requires,
tests_require=tests_requires,
cmdclass={'test': PyTest},
zip_safe=False,
platforms='any',
Expand Down
2 changes: 1 addition & 1 deletion tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def clean_test(self, value):
# test get row
self.assertEqual(importer_error.errors[0][0], 1)
# test get error type
self.assertEqual(importer_error.errors[0][1], 'AttributeError')
self.assertEqual(importer_error.errors[0][1], 'ValidationError')
# test get error message
self.assertIn('object has no attribute coisa', importer_error.errors[0][2])

Expand Down
2 changes: 1 addition & 1 deletion tests/test_csv_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def clean_test(self, value):
# test get row
self.assertEqual(importer_error.errors[0][0], 1)
# test get error type
self.assertEqual(importer_error.errors[0][1], 'AttributeError')
self.assertEqual(importer_error.errors[0][1], 'ValidationError')
# test get error message
self.assertIn('object has no attribute coisa', importer_error.errors[0][2])

Expand Down
2 changes: 1 addition & 1 deletion tests/test_dict_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def clean_test(self, value):
# test get row
self.assertEqual(importer_error.errors[0][0], 1)
# test get error type
self.assertEqual(importer_error.errors[0][1], 'AttributeError')
self.assertEqual(importer_error.errors[0][1], 'ValidationError')
# test get error message
self.assertIn('object has no attribute coisa', importer_error.errors[0][2])

Expand Down

0 comments on commit 796091f

Please sign in to comment.