Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extra attributes problem, fixes #202 #215

Merged
merged 1 commit into from
Apr 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/authors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Authors
* Trey Hunner
* Tyler Ball
* Venelin Stoykov
* Vladimir Nani
* baffolobill
* d.merc
* luyikei
Expand Down
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Modifications to existing flavors:
- Updated IBANField to support the latest additions to the IBAN Registry (version 64 / March 2016).
- Fix bug with MXRFCField where some incorrect values would validate correctly.
(`gh-204 <https://github.com/django/django-localflavor/issues/204>`_).
- Fixed bug with IBANFormField validation.
(`gh-215 <https://github.com/django/django-localflavor/pull/215>`_).
- Update regex in DEZipCodeField to prohibit invalid postal codes.
(`gh-216 <https://github.com/django/django-localflavor/pull/216>`_).

Expand Down
8 changes: 7 additions & 1 deletion localflavor/generic/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class MyModel(models.Model):
def __init__(self, use_nordea_extensions=False, include_countries=None, *args, **kwargs):
kwargs.setdefault('max_length', 34)
super(IBANField, self).__init__(*args, **kwargs)
self.use_nordea_extensions = use_nordea_extensions
self.include_countries = include_countries
self.validators.append(IBANValidator(use_nordea_extensions, include_countries))

def to_python(self, value):
Expand All @@ -47,7 +49,11 @@ def to_python(self, value):
return value

def formfield(self, **kwargs):
defaults = {'form_class': IBANFormField}
defaults = {
'use_nordea_extensions': self.use_nordea_extensions,
'include_countries': self.include_countries,
'form_class': IBANFormField,
}
defaults.update(kwargs)
return super(IBANField, self).formfield(**defaults)

Expand Down
1 change: 1 addition & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
'tests.test_mx',
'tests.test_us',
'tests.test_pk',
'tests.test_generic',
]

SECRET_KEY = 'spam-spam-spam-spam'
Expand Down
Empty file added tests/test_generic/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions tests/test_generic/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.forms import ModelForm

from .models import UseNordeaExtensionsModel, UseIncludedCountriesModel


class UseNordeaExtensionsForm(ModelForm):
class Meta:
model = UseNordeaExtensionsModel
fields = ('iban',)


class UseIncludedCountriesForm(ModelForm):
class Meta:
model = UseIncludedCountriesModel
fields = ('iban',)

11 changes: 11 additions & 0 deletions tests/test_generic/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.db import models

from localflavor.generic.models import IBANField


class UseNordeaExtensionsModel(models.Model):
iban = IBANField(use_nordea_extensions=True)


class UseIncludedCountriesModel(models.Model):
iban = IBANField(include_countries=['NL'])
10 changes: 10 additions & 0 deletions tests/test_generic.py → tests/test_generic/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from localflavor.generic.models import BICField, IBANField
from localflavor.generic.validators import BICValidator, IBANValidator, EANValidator
from localflavor.generic.forms import DateField, DateTimeField, SplitDateTimeField, BICFormField, IBANFormField
from .forms import UseNordeaExtensionsForm, UseIncludedCountriesForm


class DateTimeFieldTestCase(SimpleTestCase):
Expand Down Expand Up @@ -194,6 +195,15 @@ def test_nordea_extensions(self):
# Run the validator to ensure there are no ValidationErrors raised.
iban_validator('Eg1100006001880800100014553')

def test_use_nordea_extensions_formfield(self):
form = UseNordeaExtensionsForm({'iban': 'EG1100006001880800100014553'})
self.assertFalse(form.errors)

def test_include_countries_formfield(self):
valid_not_included = 'CH9300762011623852957'
form = UseIncludedCountriesForm({'iban': valid_not_included})
self.assertRaises(ValidationError, form.fields['iban'].run_validators, valid_not_included)

def test_form_field_formatting(self):
iban_form_field = IBANFormField()
self.assertEqual(iban_form_field.prepare_value('NL02ABNA0123456789'), 'NL02 ABNA 0123 4567 89')
Expand Down