Ticket #14608: 14608.IndianPhoneNumber-LocalFlavor.diff

File 14608.IndianPhoneNumber-LocalFlavor.diff, 5.5 KB (added by Julien Phalip, 13 years ago)

Patch updated to latest trunk

  • django/contrib/localflavor/in_/forms.py

    diff --git a/django/contrib/localflavor/in_/forms.py b/django/contrib/localflavor/in_/forms.py
    index afc5534..3bdd481 100644
    a b import re  
    55
    66from django.core.validators import EMPTY_VALUES
    77from django.forms import ValidationError
    8 from django.forms.fields import Field, RegexField, Select
     8from django.forms.fields import Field, RegexField, CharField, Select
    99from django.utils.encoding import smart_unicode
    1010from django.utils.translation import ugettext_lazy as _
    1111
    class INStateSelect(Select):  
    6262        from in_states import STATE_CHOICES
    6363        super(INStateSelect, self).__init__(attrs, choices=STATE_CHOICES)
    6464
     65phone_digits_re = re.compile(r"""
     66     (
     67         (?P<std_code>                   # the std-code group
     68             ^0                          # all std-codes start with 0
     69             (
     70                 (?P<twodigit>\d{2})   | # either two, three or four digits
     71                 (?P<threedigit>\d{3}) | # following the 0
     72                 (?P<fourdigit>\d{4})
     73             )
     74         )
     75         [-\s]                           # space or -
     76         (?P<phone_no>                   # the phone number group
     77             [1-6]                       # first digit of phone number
     78             (
     79                 (?(twodigit)\d{7})   |  # 7 more phone digits for 3 digit stdcode
     80                 (?(threedigit)\d{6}) |  # 6 more phone digits for 4 digit stdcode
     81                 (?(fourdigit)\d{5})     # 5 more phone digits for 5 digit stdcode
     82             )
     83         )
     84     )$""", re.VERBOSE)
     85
     86class INPhoneNumberField(CharField):
     87    """
     88        INPhoneNumberField validates that the data is a valid Indian phone number,
     89        including the STD code. It's normalised to 0XXX-XXXXXXX or 0XXX XXXXXXX format. The first
     90        string is the STD code which is a '0' followed by 2-4 digits. The second string
     91        is 8 digits if the STD code is 3 digits, 7 digits if the STD code is 4 digits and
     92        6 digits if the STD code is 5 digits. The second string will start with numbers
     93        between 1 and 6. The separator is either a space or a hyphen.
     94        """
     95    default_error_messages = {
     96        'invalid': _('Phone numbers must be in 02X-8X or 03X-7X or 04X-6X format.'),
     97    }
     98
     99    def clean(self, value):
     100        super(INPhoneNumberField, self).clean(value)
     101        if value in EMPTY_VALUES:
     102            return u''
     103        value = smart_unicode(value)
     104        m = phone_digits_re.match(value)
     105        if m:
     106            return u'%s' % (value)
     107        raise ValidationError(self.error_messages['invalid'])
     108
  • docs/ref/contrib/localflavor.txt

    diff --git a/docs/ref/contrib/localflavor.txt b/docs/ref/contrib/localflavor.txt
    index 3ad5899..80aa8f7 100644
    a b Iceland (``is_``)  
    576576India (``in_``)
    577577===============
    578578
    579 .. class:: in.forms.INStateField
     579.. class:: in_.forms.INStateField
    580580
    581581    A form field that validates input as an Indian state/territory name or
    582582    abbreviation. Input is normalized to the standard two-letter vehicle
    583583    registration abbreviation for the given state or territory.
    584584
    585 .. class:: in.forms.INZipCodeField
     585.. class:: in_.forms.INZipCodeField
    586586
    587587    A form field that validates input as an Indian zip code, with the
    588588    format XXXXXXX.
    589589
    590 .. class:: in.forms.INStateSelect
     590.. class:: in_.forms.INStateSelect
    591591
    592592    A ``Select`` widget that uses a list of Indian states/territories as its
    593593    choices.
     594   
     595.. class:: in_.forms.INPhoneNumberField
     596
     597    A form field that validates that the data is a valid Indian phone number,
     598    including the STD code. It's normalised to 0XXX-XXXXXXX or 0XXX XXXXXXX format. The first
     599    string is the STD code which is a '0' followed by 2-4 digits. The second string
     600    is 8 digits if the STD code is 3 digits, 7 digits if the STD code is 4 digits and
     601    6 digits if the STD code is 5 digits. The second string will start with numbers
     602    between 1 and 6. The separator is either a space or a hyphen.
    594603
    595604Ireland (``ie``)
    596605================
  • tests/regressiontests/forms/localflavor/in_.py

    diff --git a/tests/regressiontests/forms/localflavor/in_.py b/tests/regressiontests/forms/localflavor/in_.py
    index 1755366..5a04fd1 100644
    a b  
    11import warnings
    22
    33from django.contrib.localflavor.in_.forms import (INZipCodeField,
    4                                                   INStateField, INStateSelect)
     4    INStateField, INStateSelect, INPhoneNumberField)
    55
    66from utils import LocalFlavorTestCase
    77
    88
     9
    910class INLocalFlavorTests(LocalFlavorTestCase):
     11    def test_INPhoneNumberField(self):
     12        error_format = [u'Phone numbers must be in 02X-8X or 03X-7X or 04X-6X format.']
     13        valid = {
     14            '0423-2443667': '0423-2443667',
     15            '0423 2443667': '0423 2443667',
     16            '04236-244366': '04236-244366',
     17            '040-24436678': '040-24436678',
     18        }
     19        invalid = {
     20            '04-2443667': error_format,
     21            '423-2443667': error_format,
     22            '0423-9442667': error_format,
     23            '0423-0443667': error_format,
     24            '0423-244366': error_format,
     25            '04232442667': error_format,
     26            '0423DJANGO': error_format,
     27        }
     28        self.assertFieldOutput(INPhoneNumberField, valid, invalid)
     29
    1030    def test_INPStateSelect(self):
    1131        f = INStateSelect()
    1232        out = u'''<select name="state">
Back to Top