Skip to content

Commit b325234

Browse files
committed
Extra attributes problem, fixes #202
1 parent 31c350c commit b325234

File tree

8 files changed

+48
-1
lines changed

8 files changed

+48
-1
lines changed

docs/authors.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Authors
6262
* Trey Hunner
6363
* Tyler Ball
6464
* Venelin Stoykov
65+
* Vladimir Nani
6566
* baffolobill
6667
* d.merc
6768
* luyikei

docs/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Modifications to existing flavors:
2525
- Updated IBANField to support the latest additions to the IBAN Registry (version 64 / March 2016).
2626
- Fix bug with MXRFCField where some incorrect values would validate correctly.
2727
(`gh-204 <https://github.com/django/django-localflavor/issues/204>`_).
28+
- Fixed bug with IBANFormField validation.
29+
(`gh-215 <https://github.com/django/django-localflavor/pull/215>`_).
2830
- Update regex in DEZipCodeField to prohibit invalid postal codes.
2931
(`gh-216 <https://github.com/django/django-localflavor/pull/216>`_).
3032

localflavor/generic/models.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class MyModel(models.Model):
3838
def __init__(self, use_nordea_extensions=False, include_countries=None, *args, **kwargs):
3939
kwargs.setdefault('max_length', 34)
4040
super(IBANField, self).__init__(*args, **kwargs)
41+
self.use_nordea_extensions = use_nordea_extensions
42+
self.include_countries = include_countries
4143
self.validators.append(IBANValidator(use_nordea_extensions, include_countries))
4244

4345
def to_python(self, value):
@@ -47,7 +49,11 @@ def to_python(self, value):
4749
return value
4850

4951
def formfield(self, **kwargs):
50-
defaults = {'form_class': IBANFormField}
52+
defaults = {
53+
'use_nordea_extensions': self.use_nordea_extensions,
54+
'include_countries': self.include_countries,
55+
'form_class': IBANFormField,
56+
}
5157
defaults.update(kwargs)
5258
return super(IBANField, self).formfield(**defaults)
5359

tests/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
'tests.test_mx',
1616
'tests.test_us',
1717
'tests.test_pk',
18+
'tests.test_generic',
1819
]
1920

2021
SECRET_KEY = 'spam-spam-spam-spam'

tests/test_generic/__init__.py

Whitespace-only changes.

tests/test_generic/forms.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from django.forms import ModelForm
2+
3+
from .models import UseNordeaExtensionsModel, UseIncludedCountriesModel
4+
5+
6+
class UseNordeaExtensionsForm(ModelForm):
7+
class Meta:
8+
model = UseNordeaExtensionsModel
9+
fields = ('iban',)
10+
11+
12+
class UseIncludedCountriesForm(ModelForm):
13+
class Meta:
14+
model = UseIncludedCountriesModel
15+
fields = ('iban',)
16+

tests/test_generic/models.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from django.db import models
2+
3+
from localflavor.generic.models import IBANField
4+
5+
6+
class UseNordeaExtensionsModel(models.Model):
7+
iban = IBANField(use_nordea_extensions=True)
8+
9+
10+
class UseIncludedCountriesModel(models.Model):
11+
iban = IBANField(include_countries=['NL'])
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from localflavor.generic.models import BICField, IBANField
1010
from localflavor.generic.validators import BICValidator, IBANValidator, EANValidator
1111
from localflavor.generic.forms import DateField, DateTimeField, SplitDateTimeField, BICFormField, IBANFormField
12+
from .forms import UseNordeaExtensionsForm, UseIncludedCountriesForm
1213

1314

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

198+
def test_use_nordea_extensions_formfield(self):
199+
form = UseNordeaExtensionsForm({'iban': 'EG1100006001880800100014553'})
200+
self.assertFalse(form.errors)
201+
202+
def test_include_countries_formfield(self):
203+
valid_not_included = 'CH9300762011623852957'
204+
form = UseIncludedCountriesForm({'iban': valid_not_included})
205+
self.assertRaises(ValidationError, form.fields['iban'].run_validators, valid_not_included)
206+
197207
def test_form_field_formatting(self):
198208
iban_form_field = IBANFormField()
199209
self.assertEqual(iban_form_field.prepare_value('NL02ABNA0123456789'), 'NL02 ABNA 0123 4567 89')

0 commit comments

Comments
 (0)