Skip to content

Commit

Permalink
Field patch was not inicialized
Browse files Browse the repository at this point in the history
  • Loading branch information
matllubos committed Feb 23, 2017
1 parent d333680 commit b2fe421
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 29 deletions.
10 changes: 10 additions & 0 deletions 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
3 changes: 1 addition & 2 deletions chamber/models/__init__.py
Expand Up @@ -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):
Expand Down
23 changes: 0 additions & 23 deletions chamber/models/patch.py

This file was deleted.

25 changes: 23 additions & 2 deletions chamber/patch.py
Expand Up @@ -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):
Expand Down Expand Up @@ -40,11 +41,31 @@ 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:
value = getattr(meta, name, None)
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
6 changes: 4 additions & 2 deletions 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

Expand Down Expand Up @@ -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

0 comments on commit b2fe421

Please sign in to comment.