Ticket #14349: be_localflavor.5.diff

File be_localflavor.5.diff, 13.8 KB (added by laurentluce, 14 years ago)

update docs and unit tests for phone number with dots in format as seen in use in Belgium

  • django/contrib/localflavor/be/be_regions.py

     
     1# -*- coding: utf-8 -*-
     2from django.utils.translation import ugettext_lazy as _
     3
     4# ISO codes
     5REGION_CHOICES = (
     6    ('BRU', _('Brussels Capital Region')),
     7    ('VLG', _('Flemish Region')),
     8    ('WAL', _('Wallonia'))
     9)
     10
  • django/contrib/localflavor/be/be_provinces.py

     
     1# -*- coding: utf-8 -*-
     2from django.utils.translation import ugettext_lazy as _
     3
     4# ISO codes
     5PROVINCE_CHOICES = (
     6    ('VAN', _('Antwerp')),
     7    ('BRU', _('Brussels')),
     8    ('VOV', _('East Flanders')),
     9    ('VBR', _('Flemish Brabant')),
     10    ('WHT', _('Hainaut')),
     11    ('WLG', _('Liege')),
     12    ('VLI', _('Limburg')),
     13    ('WLX', _('Luxembourg')),
     14    ('WNA', _('Namur')),
     15    ('WBR', _('Walloon Brabant')),
     16    ('VWV', _('West Flanders'))
     17)
     18
  • django/contrib/localflavor/be/forms.py

     
     1# -*- coding: utf-8 -*-
     2"""
     3Belgium-specific Form helpers
     4"""
     5
     6from django.core.validators import EMPTY_VALUES
     7from django.forms import ValidationError
     8from django.forms.fields import RegexField, Select
     9from django.utils.translation import ugettext_lazy as _
     10import re
     11
     12class BEPostalCodeField(RegexField):
     13    """
     14    A form field that validates its input as a belgium postal code.
     15   
     16    Belgium postal code is a 4 digits string. The first digit indicates
     17    the province (except for the 3ddd numbers that are shared by the
     18    eastern part of Flemish Brabant and Limburg and the and 1ddd that
     19    are shared by the Brussels Capital Region, the western part of
     20    Flemish Brabant and Walloon Brabant)
     21    """
     22    default_error_messages = {
     23        'invalid': _('Enter a valid postal code in the range and format \
     241XXX - 9XXX.'),
     25    }
     26
     27    def __init__(self, *args, **kwargs):
     28        super(BEPostalCodeField, self).__init__(
     29                r'^[1-9]\d{3}$',
     30                max_length=None, min_length=None, *args, **kwargs)
     31
     32class BEPhoneNumberField(RegexField):
     33    """
     34    A form field that validates its input as a belgium phone number.
     35
     36    Landlines have a seven-digit subscriber number and a one-digit area code,
     37    while smaller cities have a six-digit subscriber number and a two-digit
     38    area code. Cell phones have a six-digit subscriber number and a two-digit
     39    area code preceeded by the number 4.
     40    0d ddd dd dd, 0d/ddd.dd.dd, 0d.ddd.dd.dd,
     41    0dddddddd - dialling a bigger city
     42    0dd dd dd dd, 0dd/dd.dd.dd, 0dd.dd.dd.dd,
     43    0dddddddd - dialling a smaller city
     44    04dd ddd dd dd, 04dd/ddd.dd.dd,
     45    04dd.ddd.dd.dd, 04ddddddddd - dialling a mobile number
     46    """
     47    default_error_messages = {
     48        'invalid': _('Enter a valid phone number in one of the formats \
     490x xxx xx xx, 0xx xx xx xx, 04xx xx xx xx, 0x/xxx.xx.xx, 0xx/xx.xx.xx, \
     5004xx/xx.xx.xx, 0x.xxx.xx.xx, 0xx.xx.xx.xx, 04xx.xx.xx.xx, 0xxxxxxxx, \
     5104xxxxxxxx.'),
     52    }
     53
     54    def __init__(self, *args, **kwargs):
     55        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}$',
     56            max_length=None, min_length=None, *args, **kwargs)
     57
     58class BERegionSelect(Select):
     59    """
     60    A Select widget that uses a list of belgium regions as its choices.
     61    """
     62    def __init__(self, attrs=None):
     63        from be_regions import REGION_CHOICES
     64        super(BERegionSelect, self).__init__(attrs, choices=REGION_CHOICES)
     65
     66class BEProvinceSelect(Select):
     67    """
     68    A Select widget that uses a list of belgium provinces as its choices.
     69    """
     70    def __init__(self, attrs=None):
     71        from be_provinces import PROVINCE_CHOICES
     72        super(BEProvinceSelect, self).__init__(attrs, choices=PROVINCE_CHOICES)
     73
  • tests/regressiontests/forms/tests.py

     
    4040from fields import FieldsTests
    4141from validators import TestFieldWithValidators
    4242from widgets import WidgetTests, ClearableFileInputTests
     43from localflavor.be import BETests
    4344
    4445from input_formats import *
    4546
  • tests/regressiontests/forms/localflavor/be.py

     
     1# -*- coding: utf-8 -*-
     2# Tests for the contrib/localflavor/ BE form fields.
     3
     4from django.test import TestCase
     5from django.forms import *
     6
     7from django.contrib.localflavor.be.forms import BEPostalCodeField, BEPhoneNumberField, BERegionSelect, BEProvinceSelect
     8
     9class BETests(TestCase):
     10    """
     11    Test case to validate BE localflavor
     12    """
     13    def assertRaisesErrorWithMessage(self, error, message, callable, *args, **kwargs):
     14        self.assertRaises(error, callable, *args, **kwargs)
     15        try:
     16            callable(*args, **kwargs)
     17        except error, e:
     18            self.assertEqual(message, str(e))
     19
     20    def test_postal_code_field(self):
     21        f = BEPostalCodeField()
     22        self.assertEqual(u'1451', f.clean('1451'))
     23        self.assertEqual(u'2540', f.clean('2540'))
     24        err_message = "[u'Enter a valid postal code in the range and format 1XXX - 9XXX.']"
     25        self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '0287')
     26        self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '14309')
     27        self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '873')
     28        self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '35 74')
     29        self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '859A')
     30        err_message = "[u'This field is required.']"
     31        self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '')
     32        f = BEPostalCodeField(required=False)
     33        self.assertEqual(u'1451', f.clean('1451'))
     34        self.assertEqual(u'2540', f.clean('2540'))
     35        self.assertEqual(u'', f.clean(''))
     36        err_message = "[u'Enter a valid postal code in the range and format 1XXX - 9XXX.']"
     37        self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '0287')
     38        self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '14309')
     39        self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '873')
     40        self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '35 74')
     41        self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '859A')
     42
     43    def test_phone_number_field(self):
     44        f = BEPhoneNumberField()
     45        self.assertEqual(u'01 234 56 78', f.clean('01 234 56 78'))
     46        self.assertEqual(u'01/234.56.78', f.clean('01/234.56.78'))
     47        self.assertEqual(u'01.234.56.78', f.clean('01.234.56.78'))
     48        self.assertEqual(u'012 34 56 78', f.clean('012 34 56 78'))
     49        self.assertEqual(u'012/34.56.78', f.clean('012/34.56.78'))
     50        self.assertEqual(u'012.34.56.78', f.clean('012.34.56.78'))
     51        self.assertEqual(u'0412 34 56 78', f.clean('0412 34 56 78'))
     52        self.assertEqual(u'0412/34.56.78', f.clean('0412/34.56.78'))
     53        self.assertEqual(u'0412.34.56.78', f.clean('0412.34.56.78'))
     54        self.assertEqual(u'012345678', f.clean('012345678'))
     55        self.assertEqual(u'0412345678', f.clean('0412345678'))
     56        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.']"
     57        self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '01234567')
     58        self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '12/345.67.89')
     59        self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '012/345.678.90')
     60        self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '012/34.56.789')
     61        self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '0123/45.67.89')
     62        self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '012/345 678 90')
     63        self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '012/34 56 789')
     64        self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '012.34 56 789')
     65        err_message = "[u'This field is required.']"
     66        self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '')
     67        f = BEPhoneNumberField(required=False)
     68        self.assertEqual(u'01 234 56 78', f.clean('01 234 56 78'))
     69        self.assertEqual(u'01/234.56.78', f.clean('01/234.56.78'))
     70        self.assertEqual(u'01.234.56.78', f.clean('01.234.56.78'))
     71        self.assertEqual(u'012 34 56 78', f.clean('012 34 56 78'))
     72        self.assertEqual(u'012/34.56.78', f.clean('012/34.56.78'))
     73        self.assertEqual(u'012.34.56.78', f.clean('012.34.56.78'))
     74        self.assertEqual(u'0412 34 56 78', f.clean('0412 34 56 78'))
     75        self.assertEqual(u'0412/34.56.78', f.clean('0412/34.56.78'))
     76        self.assertEqual(u'0412.34.56.78', f.clean('0412.34.56.78'))
     77        self.assertEqual(u'012345678', f.clean('012345678'))
     78        self.assertEqual(u'0412345678', f.clean('0412345678'))
     79        self.assertEqual(u'', f.clean(''))
     80        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.']"
     81        self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '01234567')
     82        self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '12/345.67.89')
     83        self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '012/345.678.90')
     84        self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '012/34.56.789')
     85        self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '0123/45.67.89')
     86        self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '012/345 678 90')
     87        self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '012/34 56 789')
     88        self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '012.34 56 789')
     89
     90    def test_phone_number_field(self):
     91        w = BERegionSelect()
     92        self.assertEqual(u'<select name="regions">\n<option value="BRU">Brussels Capital Region</option>\n<option value="VLG" selected="selected">Flemish Region</option>\n<option value="WAL">Wallonia</option>\n</select>', w.render('regions', 'VLG'))
     93
     94    def test_phone_number_field(self):
     95        w = BEProvinceSelect()
     96        self.assertEqual(u'<select name="provinces">\n<option value="VAN">Antwerp</option>\n<option value="BRU">Brussels</option>\n<option value="VOV">East Flanders</option>\n<option value="VBR">Flemish Brabant</option>\n<option value="WHT">Hainaut</option>\n<option value="WLG" selected="selected">Liege</option>\n<option value="VLI">Limburg</option>\n<option value="WLX">Luxembourg</option>\n<option value="WNA">Namur</option>\n<option value="WBR">Walloon Brabant</option>\n<option value="VWV">West Flanders</option>\n</select>', w.render('provinces', 'WLG'))
     97
  • AUTHORS

     
    307307    Simon Litchfield <simon@quo.com.au>
    308308    Daniel Lindsley <polarcowz@gmail.com>
    309309    Trey Long <trey@ktrl.com>
     310    Laurent Luce <http://www.laurentluce.com>
    310311    Martin Mahner <http://www.mahner.org/>
    311312    Matt McClanahan <http://mmcc.cx/>
    312313    Stanislaus Madueke
  • docs/ref/contrib/localflavor.txt

     
    3939    * Argentina_
    4040    * Australia_
    4141    * Austria_
     42    * Belgium_
    4243    * Brazil_
    4344    * Canada_
    4445    * Chile_
     
    8586.. _Argentina: `Argentina (ar)`_
    8687.. _Australia: `Australia (au)`_
    8788.. _Austria: `Austria (at)`_
     89.. _Belgium: `Belgium (be)`_
    8890.. _Brazil: `Brazil (br)`_
    8991.. _Canada: `Canada (ca)`_
    9092.. _Chile: `Chile (cl)`_
     
    182184
    183185    A form field that validates its input as an Austrian social security number.
    184186
     187Belgium (``be``)
     188================
     189
     190.. class:: be.forms.BEPhoneNumberField
     191
     192    A form field that validates input as a Belgium phone number, with one of
     193    the formats 0x xxx xx xx, 0xx xx xx xx, 04xx xx xx xx, 0x/xxx.xx.xx,
     194    0xx/xx.xx.xx, 04xx/xx.xx.xx, 0x.xxx.xx.xx, 0xx.xx.xx.xx, 04xx.xx.xx.xx,
     195    0xxxxxxxx, 04xxxxxxxx.
     196
     197.. class:: be.forms.BEPostalCodeField
     198
     199    A form field that validates input as a Belgium postal code, in the range
     200    and format 1XXX-9XXX.
     201
     202.. class:: be.forms.BEProvinceSelect
     203
     204    A ``Select`` widget that uses a list of Belgium provinces as its
     205    choices.
     206
     207.. class:: be.forms.BERegionSelect
     208
     209    A ``Select`` widget that uses a list of Belgium regions as its
     210    choices.
     211
    185212Brazil (``br``)
    186213===============
    187214
Back to Top