Ticket #14608: INPhoneNumberField.diff

File INPhoneNumberField.diff, 7.5 KB (added by Kenneth Gonsalves, 14 years ago)

fresh patch that fixes typo in the error message

  • django-mine/docs/ref/contrib/localflavor.txt

     
    440440India (``in_``)
    441441===============
    442442
    443 .. class:: in.forms.INStateField
     443.. class:: in_.forms.INStateField
    444444
    445445    A form field that validates input as an Indian state/territory name or
    446446    abbreviation. Input is normalized to the standard two-letter vehicle
    447447    registration abbreviation for the given state or territory.
    448448
    449 .. class:: in.forms.INZipCodeField
     449.. class:: in_.forms.INZipCodeField
    450450
    451451    A form field that validates input as an Indian zip code, with the
    452452    format XXXXXXX.
    453453
    454 .. class:: in.forms.INStateSelect
     454.. class:: in_.forms.INStateSelect
    455455
    456456    A ``Select`` widget that uses a list of Indian states/territories as its
    457457    choices.
     458   
     459.. class:: in_.forms.INPhoneNumberField
    458460
     461    A form field that validates that the data is a valid Indian phone number,
     462    including the STD code. It's normalised to 0XXX-XXXXXXX or 0XXX XXXXXXX format. The first
     463    string is the STD code which is a '0' followed by 2-4 digits. The second string
     464    is 8 digits if the STD code is 3 digits, 7 digits if the STD code is 4 digits and
     465    6 digits if the STD code is 5 digits. The second string will start with numbers
     466    between 1 and 6. The separator is either a space or a hyphen.
     467
    459468Ireland (``ie``)
    460469================
    461470
  • django-mine/django/contrib/localflavor/in_/forms.py

     
    44
    55from django.core.validators import EMPTY_VALUES
    66from django.forms import ValidationError
    7 from django.forms.fields import Field, RegexField, Select
     7from django.forms.fields import Field, RegexField, Select, CharField
    88from django.utils.encoding import smart_unicode
    9 from django.utils.translation import gettext
     9from django.utils.translation import ugettext_lazy as _
    1010import re
    1111
    1212
    1313class INZipCodeField(RegexField):
    1414    default_error_messages = {
    15         'invalid': gettext(u'Enter a zip code in the format XXXXXXX.'),
     15        'invalid': _(u'Enter a zip code in the format XXXXXXX.'),
    1616    }
    1717
    1818    def __init__(self, *args, **kwargs):
     
    5454        from in_states import STATE_CHOICES
    5555        super(INStateSelect, self).__init__(attrs, choices=STATE_CHOICES)
    5656
     57phone_digits_re = re.compile(r"""
     58     (
     59         (?P<std_code>                   # the std-code group
     60             ^0                          # all std-codes start with 0
     61             (
     62                 (?P<twodigit>\d{2})   | # either two, three or four digits
     63                 (?P<threedigit>\d{3}) | # following the 0
     64                 (?P<fourdigit>\d{4})
     65             )
     66         )
     67         [-\s]                           # space or -
     68         (?P<phone_no>                   # the phone number group
     69             [1-6]                       # first digit of phone number
     70             (
     71                 (?(twodigit)\d{7})   |  # 7 more phone digits for 3 digit stdcode
     72                 (?(threedigit)\d{6}) |  # 6 more phone digits for 4 digit stdcode
     73                 (?(fourdigit)\d{5})     # 5 more phone digits for 5 digit stdcode
     74             )
     75         )
     76     )$""", re.VERBOSE)
     77       
     78class INPhoneNumberField(CharField):
     79    """
     80        INPhoneNumberField validates that the data is a valid Indian phone number,
     81        including the STD code. It's normalised to 0XXX-XXXXXXX or 0XXX XXXXXXX format. The first
     82        string is the STD code which is a '0' followed by 2-4 digits. The second string
     83        is 8 digits if the STD code is 3 digits, 7 digits if the STD code is 4 digits and
     84        6 digits if the STD code is 5 digits. The second string will start with numbers
     85        between 1 and 6. The separator is either a space or a hyphen.
     86        """
     87    default_error_messages = {
     88        'invalid': _('Phone numbers must be in 02X-8X or 03X-7X or 04X-6X format.'),
     89    }
     90
     91    def clean(self, value):
     92        super(INPhoneNumberField, self).clean(value)
     93        if value in EMPTY_VALUES:
     94            return u''
     95        value = smart_unicode(value)
     96        m = phone_digits_re.match(value)
     97        if m:
     98            return u'%s' % (value)
     99        raise ValidationError(self.error_messages['invalid'])
     100
  • django-mine/tests/regressiontests/forms/tests/__init__.py

     
    1717    CHLocalFlavorTests, CLLocalFlavorTests, CZLocalFlavorTests,
    1818    DELocalFlavorTests, ESLocalFlavorTests, FILocalFlavorTests,
    1919    FRLocalFlavorTests, GenericLocalFlavorTests, IDLocalFlavorTests,
    20     IELocalFlavorTests, ILLocalFlavorTests, ISLocalFlavorTests,
    21     ITLocalFlavorTests, JPLocalFlavorTests, KWLocalFlavorTests,
    22     NLLocalFlavorTests, PLLocalFlavorTests, PTLocalFlavorTests,
    23     ROLocalFlavorTests, SELocalFlavorTests, SKLocalFlavorTests,
    24     TRLocalFlavorTests, UKLocalFlavorTests, USLocalFlavorTests,
    25     UYLocalFlavorTests, ZALocalFlavorTests
     20    IELocalFlavorTests, ILLocalFlavorTests, INLocalFlavorTests,
     21    ISLocalFlavorTests, ITLocalFlavorTests, JPLocalFlavorTests,
     22    KWLocalFlavorTests, NLLocalFlavorTests, PLLocalFlavorTests,
     23    PTLocalFlavorTests, ROLocalFlavorTests, SELocalFlavorTests,
     24    SKLocalFlavorTests, TRLocalFlavorTests, UKLocalFlavorTests,
     25    USLocalFlavorTests, UYLocalFlavorTests, ZALocalFlavorTests
    2626)
  • django-mine/tests/regressiontests/forms/localflavor/in_.py

     
     1from django.contrib.localflavor.in_.forms import INPhoneNumberField
     2   
     3from utils import LocalFlavorTestCase
     4   
     5   
     6class INLocalFlavorTests(LocalFlavorTestCase):
     7    def test_INPhoneNumberField(self):
     8        error_format = [u'Phone numbers must be in 02X-8X or 03X-7X or 04X-6X format.']
     9        valid = {
     10            '0423-2443667': '0423-2443667',
     11            '0423 2443667': '0423 2443667',
     12            '04236-244366': '04236-244366',
     13            '040-24436678': '040-24436678',
     14        }
     15        invalid = {
     16            '04-2443667': error_format,
     17            '423-2443667': error_format,
     18            '0423-9442667': error_format,
     19            '0423-0443667': error_format,
     20            '0423-244366': error_format,
     21            '04232442667': error_format,
     22            '0423DJANGO': error_format,
     23        }
     24        self.assertFieldOutput(INPhoneNumberField, valid, invalid)
  • django-mine/tests/regressiontests/forms/localflavortests.py

     
    1515from localflavor.id import IDLocalFlavorTests
    1616from localflavor.ie import IELocalFlavorTests
    1717from localflavor.il import ILLocalFlavorTests
     18from localflavor.in_ import INLocalFlavorTests
    1819from localflavor.is_ import ISLocalFlavorTests
    1920from localflavor.it import ITLocalFlavorTests
    2021from localflavor.jp import JPLocalFlavorTests
Back to Top