Commit 6ddfe269 authored by Jannis Leidel's avatar Jannis Leidel
Browse files

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
parent d7397370
Loading
Loading
Loading
Loading
+0 −0

Empty file added.

+16 −0
Original line number Diff line number Diff line
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'))
)
+8 −0
Original line number Diff line number Diff line
from django.utils.translation import ugettext_lazy as _

# ISO codes
REGION_CHOICES = (
    ('BRU', _('Brussels Capital Region')),
    ('VLG', _('Flemish Region')),
    ('WAL', _('Wallonia'))
)
+71 −0
Original line number Diff line number Diff line
"""
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)
+29 −0
Original line number Diff line number Diff line
@@ -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``)
===============

Loading