Ticket #14349: be_localflavor.diff

File be_localflavor.diff, 15.1 KB (added by laurentluce, 4 years ago)
  • 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 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)
     17    """
     18    default_error_messages = {
     19        'invalid': _('Enter a valid postal code in the range and format 1XXX - 9XXX.'),
     20    }
     21
     22    def __init__(self, *args, **kwargs):
     23        super(BEPostalCodeField, self).__init__(
     24                r'^[1-9]\d{3}$',
     25                max_length=None, min_length=None, *args, **kwargs)
     26
     27class BEPhoneNumberField(RegexField):
     28    """
     29    A form field that validates its input as a belgium phone number.
     30
     31    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.
     32    0d ddd dd dd, 0d/ddd.dd.dd - dialling a bigger city
     33    0dd dd dd dd, 0dd/dd.dd.dd - dialling a smaller city
     34    04dd ddd dd dd, 04dd/ddd.dd.dd - dialling a mobile number
     35
     36    """
     37    default_error_messages = {
     38        '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.'),
     39    }
     40
     41    def __init__(self, *args, **kwargs):
     42      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}$',
     43                max_length=None, min_length=None, *args, **kwargs)
     44
     45class BERegionSelect(Select):
     46    """
     47    A Select widget that uses a list of belgium regions as its choices.
     48    """
     49    def __init__(self, attrs=None):
     50        from be_regions import REGION_CHOICES
     51        super(BERegionSelect, self).__init__(attrs, choices=REGION_CHOICES)
     52
     53class BEProvinceSelect(Select):
     54    """
     55    A Select widget that uses a list of belgium provinces as its choices.
     56    """
     57    def __init__(self, attrs=None):
     58        from be_provinces import PROVINCE_CHOICES
     59        super(BEProvinceSelect, self).__init__(attrs, choices=PROVINCE_CHOICES)
     60
  • tests/regressiontests/forms/tests.py

     
    55from localflavor.ar import tests as localflavor_ar_tests
    66from localflavor.at import tests as localflavor_at_tests
    77from localflavor.au import tests as localflavor_au_tests
     8from localflavor.be import tests as localflavor_be_tests
    89from localflavor.br import tests as localflavor_br_tests
    910from localflavor.ca import tests as localflavor_ca_tests
    1011from localflavor.ch import tests as localflavor_ch_tests
     
    5051    'localflavor_ar_tests': localflavor_ar_tests,
    5152    'localflavor_at_tests': localflavor_at_tests,
    5253    'localflavor_au_tests': localflavor_au_tests,
     54    'localflavor_be_tests': localflavor_be_tests,
    5355    'localflavor_br_tests': localflavor_br_tests,
    5456    'localflavor_ca_tests': localflavor_ca_tests,
    5557    'localflavor_ch_tests': localflavor_ch_tests,
  • tests/regressiontests/forms/localflavor/be.py

     
     1# -*- coding: utf-8 -*-
     2# Tests for the contrib/localflavor/ BE form fields.
     3
     4tests = r"""
     5# BEPostalCodeField ##############################################################
     6
     7BEPostalCodeField validates that data is a four-digit belgium postal code following the regex ^[1-9](/d){3}$.
     8>>> from django.contrib.localflavor.be.forms import BEPostalCodeField
     9>>> f = BEPostalCodeField()
     10>>> f.clean('1451')
     11u'1451'
     12>>> f.clean('2540')
     13u'2540'
     14>>> f.clean('0287')
     15Traceback (most recent call last):
     16...
     17ValidationError: [u'Enter a valid postal code in the range and format 1XXX - 9XXX.']
     18>>> f.clean('14309')
     19Traceback (most recent call last):
     20...
     21ValidationError: [u'Enter a valid postal code in the range and format 1XXX - 9XXX.']
     22>>> f.clean('873')
     23Traceback (most recent call last):
     24...
     25ValidationError: [u'Enter a valid postal code in the range and format 1XXX - 9XXX.']
     26>>> f.clean('35 74')
     27Traceback (most recent call last):
     28...
     29ValidationError: [u'Enter a valid postal code in the range and format 1XXX - 9XXX.']
     30>>> f.clean('859A')
     31Traceback (most recent call last):
     32...
     33ValidationError: [u'Enter a valid postal code in the range and format 1XXX - 9XXX.']
     34>>> f.clean('')
     35Traceback (most recent call last):
     36...
     37ValidationError: [u'This field is required.']
     38
     39>>> f = BEPostalCodeField(required=False)
     40>>> f.clean('1451')
     41u'1451'
     42>>> f.clean('2540')
     43u'2540'
     44>>> f.clean('0287')
     45Traceback (most recent call last):
     46...
     47ValidationError: [u'Enter a valid postal code in the range and format 1XXX - 9XXX.']
     48>>> f.clean('14309')
     49Traceback (most recent call last):
     50...
     51ValidationError: [u'Enter a valid postal code in the range and format 1XXX - 9XXX.']
     52>>> f.clean('873')
     53Traceback (most recent call last):
     54...
     55ValidationError: [u'Enter a valid postal code in the range and format 1XXX - 9XXX.']
     56>>> f.clean('35 74')
     57Traceback (most recent call last):
     58...
     59ValidationError: [u'Enter a valid postal code in the range and format 1XXX - 9XXX.']
     60>>> f.clean('859A')
     61Traceback (most recent call last):
     62...
     63ValidationError: [u'Enter a valid postal code in the range and format 1XXX - 9XXX.']
     64>>> f.clean('')
     65u''
     66
     67# BEPhoneNumberField ##############################################################
     68
     69BEPhoneNumberField validates that data follows: 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.
     700d ddd dd dd, 0d/ddd.dd.dd - dialling a bigger city
     710dd dd dd dd, 0dd/dd.dd.dd - dialling a smaller city
     7204dd dd dd dd, 04dd/dd.dd.dd - dialling a mobile number
     73
     74>>> from django.contrib.localflavor.be.forms import BEPhoneNumberField
     75>>> f = BEPhoneNumberField()
     76>>> f.clean('01 234 56 78')
     77u'01 234 56 78'
     78>>> f.clean('01/234.56.78')
     79u'01/234.56.78'
     80>>> f.clean('012 34 56 78')
     81u'012 34 56 78'
     82>>> f.clean('012/34.56.78')
     83u'012/34.56.78'
     84>>> f.clean('0412 34 56 78')
     85u'0412 34 56 78'
     86>>> f.clean('0412/34.56.78')
     87u'0412/34.56.78'
     88>>> f.clean('012345678')
     89Traceback (most recent call last):
     90...
     91ValidationError: [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.']
     92>>> f.clean('1234567')
     93Traceback (most recent call last):
     94...
     95ValidationError: [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.']
     96>>> f.clean('12/345.67.89')
     97Traceback (most recent call last):
     98...
     99ValidationError: [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.']
     100>>> f.clean('012/345.678.90')
     101Traceback (most recent call last):
     102...
     103ValidationError: [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.']
     104>>> f.clean('012/34.56.789')
     105Traceback (most recent call last):
     106...
     107ValidationError: [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.']
     108>>> f.clean('0123/45.67.89')
     109Traceback (most recent call last):
     110...
     111ValidationError: [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.']
     112>>> f.clean('012/345 678 90')
     113Traceback (most recent call last):
     114...
     115ValidationError: [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.']
     116>>> f.clean('012/34 56 789')
     117Traceback (most recent call last):
     118...
     119ValidationError: [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.']
     120>>> f.clean('')
     121Traceback (most recent call last):
     122...
     123ValidationError: [u'This field is required.']
     124
     125>>> f = BEPhoneNumberField(required=False)
     126>>> f.clean('01 234 56 78')
     127u'01 234 56 78'
     128>>> f.clean('01/234.56.78')
     129u'01/234.56.78'
     130>>> f.clean('012 34 56 78')
     131u'012 34 56 78'
     132>>> f.clean('012/34.56.78')
     133u'012/34.56.78'
     134>>> f.clean('0412 34 56 78')
     135u'0412 34 56 78'
     136>>> f.clean('0412/34.56.78')
     137u'0412/34.56.78'
     138>>> f.clean('012345678')
     139Traceback (most recent call last):
     140...
     141ValidationError: [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.']
     142>>> f.clean('1234567')
     143Traceback (most recent call last):
     144...
     145ValidationError: [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.']
     146>>> f.clean('12/345.67.89')
     147Traceback (most recent call last):
     148...
     149ValidationError: [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.']
     150>>> f.clean('012/345.678.90')
     151Traceback (most recent call last):
     152...
     153ValidationError: [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.']
     154>>> f.clean('012/34.56.789')
     155Traceback (most recent call last):
     156...
     157ValidationError: [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.']
     158>>> f.clean('0123/45.67.89')
     159Traceback (most recent call last):
     160...
     161ValidationError: [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.']
     162>>> f.clean('012/345 678 90')
     163Traceback (most recent call last):
     164...
     165ValidationError: [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.']
     166>>> f.clean('012/34 56 789')
     167Traceback (most recent call last):
     168...
     169ValidationError: [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.']
     170>>> f.clean('')
     171u''
     172
     173# BERegionSelect ##############################################################
     174
     175BERegionSelect is a Select widget that uses a list of belgium regions as its choices.
     176>>> from django.contrib.localflavor.be.forms import BERegionSelect
     177>>> w = BERegionSelect()
     178>>> w.render('regions', 'VLG')
     179u'<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>'
     180
     181# BEProvinceSelect ##############################################################
     182
     183BEProvinceSelect is a Select widget that uses a list of Belgium provinces as its choices.
     184>>> from django.contrib.localflavor.be.forms import BEProvinceSelect
     185>>> w = BEProvinceSelect()
     186>>> w.render('provinces', 'WLG')
     187u'<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>'
     188
     189"""
     190
  • 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.
     195
     196.. class:: be.forms.BEPostalCodeField
     197
     198    A form field that validates input as a Belgium postal code, in the range
     199    and format 1XXX-9XXX.
     200
     201.. class:: be.forms.BEProvinceSelect
     202
     203    A ``Select`` widget that uses a list of Belgium provinces as its
     204    choices.
     205
     206.. class:: be.forms.BERegionSelect
     207
     208    A ``Select`` widget that uses a list of Belgium regions as its
     209    choices.
     210
    185211Brazil (``br``)
    186212===============
    187213
Back to Top