From 6ddfe269324ab4220b9e20ea86b6cc0197ab1fc2 Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Mon, 11 Oct 2010 23:35:23 +0000 Subject: [PATCH] Fixed #14349 -- Added Belgium localflavor. Thanks for the report and patch, Laurent Luce. git-svn-id: http://code.djangoproject.com/svn/django/trunk@14160 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/localflavor/be/__init__.py | 0 django/contrib/localflavor/be/be_provinces.py | 16 ++++ django/contrib/localflavor/be/be_regions.py | 8 ++ django/contrib/localflavor/be/forms.py | 71 ++++++++++++++ docs/ref/contrib/localflavor.txt | 29 ++++++ tests/regressiontests/forms/localflavor/be.py | 94 +++++++++++++++++++ tests/regressiontests/forms/tests.py | 1 + 7 files changed, 219 insertions(+) create mode 100644 django/contrib/localflavor/be/__init__.py create mode 100644 django/contrib/localflavor/be/be_provinces.py create mode 100644 django/contrib/localflavor/be/be_regions.py create mode 100644 django/contrib/localflavor/be/forms.py create mode 100644 tests/regressiontests/forms/localflavor/be.py diff --git a/django/contrib/localflavor/be/__init__.py b/django/contrib/localflavor/be/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/django/contrib/localflavor/be/be_provinces.py b/django/contrib/localflavor/be/be_provinces.py new file mode 100644 index 0000000000..fa537ec22a --- /dev/null +++ b/django/contrib/localflavor/be/be_provinces.py @@ -0,0 +1,16 @@ +from django.utils.translation import ugettext_lazy as _ + +# ISO codes +PROVINCE_CHOICES = ( + ('VAN', _('Antwerp')), + ('BRU', _('Brussels')), + ('VOV', _('East Flanders')), + ('VBR', _('Flemish Brabant')), + ('WHT', _('Hainaut')), + ('WLG', _('Liege')), + ('VLI', _('Limburg')), + ('WLX', _('Luxembourg')), + ('WNA', _('Namur')), + ('WBR', _('Walloon Brabant')), + ('VWV', _('West Flanders')) +) diff --git a/django/contrib/localflavor/be/be_regions.py b/django/contrib/localflavor/be/be_regions.py new file mode 100644 index 0000000000..2c19d4d4d3 --- /dev/null +++ b/django/contrib/localflavor/be/be_regions.py @@ -0,0 +1,8 @@ +from django.utils.translation import ugettext_lazy as _ + +# ISO codes +REGION_CHOICES = ( + ('BRU', _('Brussels Capital Region')), + ('VLG', _('Flemish Region')), + ('WAL', _('Wallonia')) +) diff --git a/django/contrib/localflavor/be/forms.py b/django/contrib/localflavor/be/forms.py new file mode 100644 index 0000000000..3b27f84252 --- /dev/null +++ b/django/contrib/localflavor/be/forms.py @@ -0,0 +1,71 @@ +""" +Belgium-specific Form helpers +""" +import re + +from django.core.validators import EMPTY_VALUES +from django.forms import ValidationError +from django.forms.fields import RegexField, Select +from django.utils.translation import ugettext_lazy as _ + +class BEPostalCodeField(RegexField): + """ + A form field that validates its input as a belgium postal code. + + Belgium postal code is a 4 digits string. The first digit indicates + the province (except for the 3ddd numbers that are shared by the + eastern part of Flemish Brabant and Limburg and the and 1ddd that + are shared by the Brussels Capital Region, the western part of + Flemish Brabant and Walloon Brabant) + """ + default_error_messages = { + 'invalid': _( + 'Enter a valid postal code in the range and format 1XXX - 9XXX.'), + } + + def __init__(self, *args, **kwargs): + super(BEPostalCodeField, self).__init__(r'^[1-9]\d{3}$', + max_length=None, min_length=None, *args, **kwargs) + +class BEPhoneNumberField(RegexField): + """ + A form field that validates its input as a belgium phone number. + + Landlines have a seven-digit subscriber number and a one-digit area code, + while smaller cities have a six-digit subscriber number and a two-digit + area code. Cell phones have a six-digit subscriber number and a two-digit + area code preceeded by the number 4. + 0d ddd dd dd, 0d/ddd.dd.dd, 0d.ddd.dd.dd, + 0dddddddd - dialling a bigger city + 0dd dd dd dd, 0dd/dd.dd.dd, 0dd.dd.dd.dd, + 0dddddddd - dialling a smaller city + 04dd ddd dd dd, 04dd/ddd.dd.dd, + 04dd.ddd.dd.dd, 04ddddddddd - dialling a mobile number + """ + default_error_messages = { + 'invalid': _('Enter a valid phone number in one of the formats ' + '0x xxx xx xx, 0xx xx xx xx, 04xx xx xx xx, ' + '0x/xxx.xx.xx, 0xx/xx.xx.xx, 04xx/xx.xx.xx, ' + '0x.xxx.xx.xx, 0xx.xx.xx.xx, 04xx.xx.xx.xx, ' + '0xxxxxxxx or 04xxxxxxxx.'), + } + + def __init__(self, *args, **kwargs): + super(BEPhoneNumberField, self).__init__(r'^[0]\d{1}[/. ]?\d{3}[. ]\d{2}[. ]?\d{2}$|^[0]\d{2}[/. ]?\d{2}[. ]?\d{2}[. ]?\d{2}$|^[0][4]\d{2}[/. ]?\d{2}[. ]?\d{2}[. ]?\d{2}$', + max_length=None, min_length=None, *args, **kwargs) + +class BERegionSelect(Select): + """ + A Select widget that uses a list of belgium regions as its choices. + """ + def __init__(self, attrs=None): + from be_regions import REGION_CHOICES + super(BERegionSelect, self).__init__(attrs, choices=REGION_CHOICES) + +class BEProvinceSelect(Select): + """ + A Select widget that uses a list of belgium provinces as its choices. + """ + def __init__(self, attrs=None): + from be_provinces import PROVINCE_CHOICES + super(BEProvinceSelect, self).__init__(attrs, choices=PROVINCE_CHOICES) diff --git a/docs/ref/contrib/localflavor.txt b/docs/ref/contrib/localflavor.txt index 48cfa6340a..ae81248f24 100644 --- a/docs/ref/contrib/localflavor.txt +++ b/docs/ref/contrib/localflavor.txt @@ -39,6 +39,7 @@ Countries currently supported by :mod:`~django.contrib.localflavor` are: * Argentina_ * Australia_ * Austria_ + * Belgium_ * Brazil_ * Canada_ * Chile_ @@ -85,6 +86,7 @@ Here's an example of how to use them:: .. _Argentina: `Argentina (ar)`_ .. _Australia: `Australia (au)`_ .. _Austria: `Austria (at)`_ +.. _Belgium: `Belgium (be)`_ .. _Brazil: `Brazil (br)`_ .. _Canada: `Canada (ca)`_ .. _Chile: `Chile (cl)`_ @@ -182,6 +184,33 @@ Austria (``at``) A form field that validates its input as an Austrian social security number. +Belgium (``be``) +================ + +.. versionadded:: 1.3 + +.. class:: be.forms.BEPhoneNumberField + + A form field that validates input as a Belgium phone number, with one of + the formats 0x xxx xx xx, 0xx xx xx xx, 04xx xx xx xx, 0x/xxx.xx.xx, + 0xx/xx.xx.xx, 04xx/xx.xx.xx, 0x.xxx.xx.xx, 0xx.xx.xx.xx, 04xx.xx.xx.xx, + 0xxxxxxxx or 04xxxxxxxx. + +.. class:: be.forms.BEPostalCodeField + + A form field that validates input as a Belgium postal code, in the range + and format 1XXX-9XXX. + +.. class:: be.forms.BEProvinceSelect + + A ``Select`` widget that uses a list of Belgium provinces as its + choices. + +.. class:: be.forms.BERegionSelect + + A ``Select`` widget that uses a list of Belgium regions as its + choices. + Brazil (``br``) =============== diff --git a/tests/regressiontests/forms/localflavor/be.py b/tests/regressiontests/forms/localflavor/be.py new file mode 100644 index 0000000000..8789d9f89d --- /dev/null +++ b/tests/regressiontests/forms/localflavor/be.py @@ -0,0 +1,94 @@ +from django.test import TestCase +from django.forms import * + +from django.contrib.localflavor.be.forms import (BEPostalCodeField, + BEPhoneNumberField, BERegionSelect, BEProvinceSelect) + +class BETests(TestCase): + """ + Test case to validate BE localflavor + """ + def assertRaisesErrorWithMessage(self, error, message, callable, *args, **kwargs): + self.assertRaises(error, callable, *args, **kwargs) + try: + callable(*args, **kwargs) + except error, e: + self.assertEqual(message, str(e)) + + def test_postal_code_field(self): + f = BEPostalCodeField() + self.assertEqual(u'1451', f.clean('1451')) + self.assertEqual(u'2540', f.clean('2540')) + err_message = "[u'Enter a valid postal code in the range and format 1XXX - 9XXX.']" + self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '0287') + self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '14309') + self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '873') + self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '35 74') + self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '859A') + err_message = "[u'This field is required.']" + self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '') + f = BEPostalCodeField(required=False) + self.assertEqual(u'1451', f.clean('1451')) + self.assertEqual(u'2540', f.clean('2540')) + self.assertEqual(u'', f.clean('')) + err_message = "[u'Enter a valid postal code in the range and format 1XXX - 9XXX.']" + self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '0287') + self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '14309') + self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '873') + self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '35 74') + self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '859A') + + def test_phone_number_field(self): + f = BEPhoneNumberField() + self.assertEqual(u'01 234 56 78', f.clean('01 234 56 78')) + self.assertEqual(u'01/234.56.78', f.clean('01/234.56.78')) + self.assertEqual(u'01.234.56.78', f.clean('01.234.56.78')) + self.assertEqual(u'012 34 56 78', f.clean('012 34 56 78')) + self.assertEqual(u'012/34.56.78', f.clean('012/34.56.78')) + self.assertEqual(u'012.34.56.78', f.clean('012.34.56.78')) + self.assertEqual(u'0412 34 56 78', f.clean('0412 34 56 78')) + self.assertEqual(u'0412/34.56.78', f.clean('0412/34.56.78')) + self.assertEqual(u'0412.34.56.78', f.clean('0412.34.56.78')) + self.assertEqual(u'012345678', f.clean('012345678')) + self.assertEqual(u'0412345678', f.clean('0412345678')) + err_message = "[u'Enter a valid phone number in one of the formats 0x xxx xx xx, 0xx xx xx xx, 04xx xx xx xx, 0x/xxx.xx.xx, 0xx/xx.xx.xx, 04xx/xx.xx.xx, 0xxxxxxxx, 04xxxxxxxx, 0x.xxx.xx.xx, 0xx.xx.xx.xx, 04xx.xx.xx.xx.']" + self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '01234567') + self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '12/345.67.89') + self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '012/345.678.90') + self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '012/34.56.789') + self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '0123/45.67.89') + self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '012/345 678 90') + self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '012/34 56 789') + self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '012.34 56 789') + err_message = "[u'This field is required.']" + self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '') + f = BEPhoneNumberField(required=False) + self.assertEqual(u'01 234 56 78', f.clean('01 234 56 78')) + self.assertEqual(u'01/234.56.78', f.clean('01/234.56.78')) + self.assertEqual(u'01.234.56.78', f.clean('01.234.56.78')) + self.assertEqual(u'012 34 56 78', f.clean('012 34 56 78')) + self.assertEqual(u'012/34.56.78', f.clean('012/34.56.78')) + self.assertEqual(u'012.34.56.78', f.clean('012.34.56.78')) + self.assertEqual(u'0412 34 56 78', f.clean('0412 34 56 78')) + self.assertEqual(u'0412/34.56.78', f.clean('0412/34.56.78')) + self.assertEqual(u'0412.34.56.78', f.clean('0412.34.56.78')) + self.assertEqual(u'012345678', f.clean('012345678')) + self.assertEqual(u'0412345678', f.clean('0412345678')) + self.assertEqual(u'', f.clean('')) + err_message = "[u'Enter a valid phone number in one of the formats 0x xxx xx xx, 0xx xx xx xx, 04xx xx xx xx, 0x/xxx.xx.xx, 0xx/xx.xx.xx, 04xx/xx.xx.xx, 0xxxxxxxx, 04xxxxxxxx, 0x.xxx.xx.xx, 0xx.xx.xx.xx, 04xx.xx.xx.xx.']" + self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '01234567') + self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '12/345.67.89') + self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '012/345.678.90') + self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '012/34.56.789') + self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '0123/45.67.89') + self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '012/345 678 90') + self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '012/34 56 789') + self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '012.34 56 789') + + def test_phone_number_field(self): + w = BERegionSelect() + self.assertEqual(u'', w.render('regions', 'VLG')) + + def test_phone_number_field(self): + w = BEProvinceSelect() + self.assertEqual(u'', w.render('provinces', 'WLG')) diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py index 1871d4f89a..fb41501545 100644 --- a/tests/regressiontests/forms/tests.py +++ b/tests/regressiontests/forms/tests.py @@ -40,6 +40,7 @@ from media import media_tests from fields import FieldsTests from validators import TestFieldWithValidators from widgets import WidgetTests, ClearableFileInputTests +from localflavor.be import BETests from input_formats import *