diff --git a/.travis.yml b/.travis.yml index b129497..29e4b51 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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: diff --git a/data_importer/__init__.py b/data_importer/__init__.py index 973c9e4..0471fd2 100644 --- a/data_importer/__init__.py +++ b/data_importer/__init__.py @@ -1,4 +1,4 @@ # encoding: utf-8 __doc__ = 'Data Importer' -__version__ = '3.0.0' +__version__ = '3.0.1' __author__ = 'Valder Gallo ' diff --git a/data_importer/importers/base.py b/data_importer/importers/base.py index a812268..36b735a 100644 --- a/data_importer/importers/base.py +++ b/data_importer/importers/base.py @@ -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 @@ -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 @@ -177,7 +182,7 @@ 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) @@ -185,7 +190,12 @@ def clean_field(self, field_name, value): 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): diff --git a/setup.py b/setup.py index 6d17079..283e21d 100644 --- a/setup.py +++ b/setup.py @@ -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): @@ -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', diff --git a/tests/test_base.py b/tests/test_base.py index 7609615..aaac276 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -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]) diff --git a/tests/test_csv_importer.py b/tests/test_csv_importer.py index 4eae028..64a056a 100644 --- a/tests/test_csv_importer.py +++ b/tests/test_csv_importer.py @@ -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]) diff --git a/tests/test_dict_fields.py b/tests/test_dict_fields.py index 62dcbb3..4a75b39 100644 --- a/tests/test_dict_fields.py +++ b/tests/test_dict_fields.py @@ -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])