diff --git a/chamber/__init__.py b/chamber/__init__.py index e69de29..4123fe3 100644 --- a/chamber/__init__.py +++ b/chamber/__init__.py @@ -0,0 +1,10 @@ +# Apply patch only if django is installed +try: + from django.core.exceptions import ImproperlyConfigured + try: + #from django.db import models # NOQA + from chamber.patch import * # NOQA + except ImproperlyConfigured: + pass +except ImportError as ex: + pass diff --git a/chamber/models/__init__.py b/chamber/models/__init__.py index 38313d3..60693a1 100644 --- a/chamber/models/__init__.py +++ b/chamber/models/__init__.py @@ -14,8 +14,7 @@ from chamber.exceptions import PersistenceException from chamber.patch import Options -from .fields import * # NOQA exposing classes and functions as a module API -from .patch import * # NOQA +from .fields import * # NOQA exposing classes and functions as a module API def many_to_many_field_to_dict(field, instance): diff --git a/chamber/models/patch.py b/chamber/models/patch.py deleted file mode 100644 index b2d0ed1..0000000 --- a/chamber/models/patch.py +++ /dev/null @@ -1,23 +0,0 @@ -from __future__ import unicode_literals - -from django.db.models.fields import Field - - -def field_init(self, *args, **kwargs): - """ - Patches a Django Field's `__init__` method for easier usage of optional `kwargs`. It defines a `humanized` attribute - on a field for better display of its value. - """ - humanize_func = kwargs.pop('humanized', None) - if humanize_func: - def humanize(val, inst, *args, **kwargs): - return humanize_func(val, inst, field=self, *args, **kwargs) - self.humanized = humanize - else: - self.humanized = self.default_humanized - getattr(self, '_init_chamber_patch_')(*args, **kwargs) - - -Field.default_humanized = None -Field._init_chamber_patch_ = Field.__init__ # pylint: disable=W0212 -Field.__init__ = field_init diff --git a/chamber/patch.py b/chamber/patch.py index 698d74d..40c60c5 100644 --- a/chamber/patch.py +++ b/chamber/patch.py @@ -2,7 +2,8 @@ import six -from django.db import models +from django.db.models import Model +from django.db.models.fields import Field class OptionsLazy(object): @@ -40,7 +41,7 @@ def _get_attributes(self, model): return self.attributes def _getattr(self, name, default_value): - meta_models = [b for b in self.model.__mro__ if issubclass(b, models.Model)] + meta_models = [b for b in self.model.__mro__ if issubclass(b, Model)] for model in meta_models: meta = getattr(model, self.meta_class_name, None) if meta: @@ -48,3 +49,23 @@ def _getattr(self, name, default_value): if value is not None: return value return default_value + + +def field_init(self, *args, **kwargs): + """ + Patches a Django Field's `__init__` method for easier usage of optional `kwargs`. It defines a `humanized` attribute + on a field for better display of its value. + """ + humanize_func = kwargs.pop('humanized', None) + if humanize_func: + def humanize(val, inst, *args, **kwargs): + return humanize_func(val, inst, field=self, *args, **kwargs) + self.humanized = humanize + else: + self.humanized = self.default_humanized + getattr(self, '_init_chamber_patch_')(*args, **kwargs) + + +Field.default_humanized = None +Field._init_chamber_patch_ = Field.__init__ # pylint: disable=W0212 +Field.__init__ = field_init diff --git a/example/dj/apps/test_chamber/tests/importers.py b/example/dj/apps/test_chamber/tests/importers.py index 4a2af95..18ab7a1 100644 --- a/example/dj/apps/test_chamber/tests/importers.py +++ b/example/dj/apps/test_chamber/tests/importers.py @@ -1,5 +1,7 @@ from __future__ import unicode_literals +from six import StringIO + from django.core.management import call_command from django.test import TestCase @@ -39,14 +41,14 @@ def test_records_should_be_imported_without_optional_fields_should_be_imported_f def test_records_should_be_bulk_imported_from_csv_with_command(self): assert_equal(CSVRecord.objects.count(), 0) - call_command('bulk_csv_import') + call_command('bulk_csv_import', stdout=StringIO(), stderr=StringIO()) assert_equal(CSVRecord.objects.count(), 7) assert_equal(CSVRecord.objects.last().name, 'Geordi LaForge') # Ensure correct value is stored assert_equal(CSVRecord.objects.last().number, 888) # Ensure clean methods work def test_records_should_be_imported_without_optional_fields_should_be_bulk_imported_from_csv_with_command(self): assert_equal(CSVRecord.objects.count(), 0) - call_command('csv_import') + call_command('csv_import', stdout=StringIO(), stderr=StringIO()) assert_equal(CSVRecord.objects.count(), 7) assert_equal(CSVRecord.objects.last().name, 'Geordi LaForge') # Ensure correct value is stored assert_equal(CSVRecord.objects.last().number, 888) # Ensure clean methods work