Ticket #17864: hkphonenumber.diff

File hkphonenumber.diff, 5.9 KB (added by Adrien Lemaire, 12 years ago)

Field fixed + Tests + translatable strings + Doc updated

  • new file django/contrib/localflavor/hk/forms.py

    diff --git a/django/contrib/localflavor/hk/__init__.py b/django/contrib/localflavor/hk/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/django/contrib/localflavor/hk/forms.py b/django/contrib/localflavor/hk/forms.py
    new file mode 100644
    index 0000000..4b53e2b
    - +  
     1"""
     2Hong Kong specific Form helpers
     3"""
     4from __future__ import absolute_import
     5
     6import re
     7
     8from django.core.validators import EMPTY_VALUES
     9from django.forms import CharField
     10from django.forms import ValidationError
     11from django.utils.encoding import smart_unicode
     12from django.utils.translation import ugettext_lazy as _
     13
     14
     15hk_phone_digits_re = re.compile(r'^(?:852-?)?(\d{4})[-\.]?(\d{4})$')
     16hk_special_numbers = ('999', '992', '112')
     17hk_phone_prefixes = ('2', '3', '5', '6', '8', '9')
     18
     19
     20class HKPhoneNumberField(CharField):
     21    """
     22    Validate Hong Kong phone number.
     23    The input format can be either one of the followings:
     24    'XXXX-XXXX', '852-XXXX-XXXX', '(+852) XXXX-XXXX',
     25    'XXXX XXXX', or 'XXXXXXXX'.
     26    The output format is 'XXXX-XXXX'.
     27
     28    Note: The phone number shall not start with 999, 992, or 112.
     29          And, it should start with either 2, 3, 5, 6, 8, or 9.
     30
     31    Ref - http://en.wikipedia.org/wiki/Telephone_numbers_in_Hong_Kong
     32    """
     33    default_error_messages = {
     34        'disguise': _('Phone number should not start with ' \
     35                    'one of the followings: %s.' % \
     36                    ', '.join(hk_special_numbers)),
     37        'invalid': _('Phone number must be in XXXX-XXXX format.'),
     38        'prefix': _('Phone number should start with ' \
     39                  'one of the followings: %s.' % \
     40                  ', '.join(hk_phone_prefixes)),
     41    }
     42
     43    def __init__(self, *args, **kwargs):
     44        super(HKPhoneNumberField, self).__init__(*args, **kwargs)
     45
     46    def clean(self, value):
     47        super(HKPhoneNumberField, self).clean(value)
     48
     49        if value in EMPTY_VALUES:
     50            return u''
     51
     52        value = re.sub('(\(|\)|\s+|\+)', '', smart_unicode(value))
     53        m = hk_phone_digits_re.search(value)
     54        if not m:
     55            raise ValidationError(self.error_messages['invalid'])
     56
     57        value = u'%s-%s' % (m.group(1), m.group(2))
     58        for special in hk_special_numbers:
     59            if value.startswith(special):
     60                raise ValidationError(self.error_messages['disguise'])
     61
     62        prefix_found = map(lambda prefix: value.startswith(prefix),
     63                           hk_phone_prefixes)
     64        if not any(prefix_found):
     65            raise ValidationError(self.error_messages['prefix'])
     66
     67        return value
  • docs/ref/contrib/localflavor.txt

    diff --git a/docs/ref/contrib/localflavor.txt b/docs/ref/contrib/localflavor.txt
    index 1593598..ad8c921 100644
    a b Countries currently supported by :mod:`~django.contrib.localflavor` are:  
    5151* Finland_
    5252* France_
    5353* Germany_
     54* `Hong Kong`_
    5455* Iceland_
    5556* India_
    5657* Indonesia_
    Here's an example of how to use them::  
    108109.. _Finland: `Finland (fi)`_
    109110.. _France: `France (fr)`_
    110111.. _Germany: `Germany (de)`_
     112.. _Hong Kong: `Hong Kong (hk)`_
    111113.. _The Netherlands: `The Netherlands (nl)`_
    112114.. _Iceland: `Iceland (is\_)`_
    113115.. _India: `India (in\_)`_
    Germany (``de``)  
    560562
    561563    A ``Select`` widget that uses a list of German states as its choices.
    562564
     565Hong Kong (``hk``)
     566=================================
     567
     568.. class:: hk.forms.HKPhoneNumberField
     569
     570    A form field that validates input as a Hong Kong phone number.
     571
     572
    563573The Netherlands (``nl``)
    564574========================
    565575
  • new file tests/regressiontests/localflavor/hk/forms.py

    diff --git a/tests/regressiontests/localflavor/hk/__init__.py b/tests/regressiontests/localflavor/hk/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/localflavor/hk/forms.py b/tests/regressiontests/localflavor/hk/forms.py
    new file mode 100644
    index 0000000..a6233d4
    - +  
     1from __future__ import absolute_import
     2
     3from django.forms import ModelForm
     4
     5from .models import HKPlace
     6
     7
     8class HKPlaceForm(ModelForm):
     9
     10    class Meta:
     11        model = HKPlace
  • new file tests/regressiontests/localflavor/hk/tests.py

    diff --git a/tests/regressiontests/localflavor/hk/tests.py b/tests/regressiontests/localflavor/hk/tests.py
    new file mode 100644
    index 0000000..de4ea37
    - +  
     1from __future__ import absolute_import
     2
     3from django.contrib.localflavor.hk.forms import HKPhoneNumberField
     4from django.test import SimpleTestCase
     5
     6
     7class HKLocalFlavorTests(SimpleTestCase):
     8    """Tests for Hong Kong Local Flavors"""
     9
     10    def test_HKPhoneNumberField(self):
     11        error_msgs = HKPhoneNumberField.default_error_messages
     12        valid = {
     13            '2111-1111': '2111-1111',
     14            '3111 1111': '3111-1111',
     15            '51111111': '5111-1111',
     16            '852-6111-1111': '6111-1111',
     17            '(+852) 8111-1111': '8111-1111',
     18            '(+852) 9111-1111': '9111-1111',
     19        }
     20        invalid = {
     21            '9991-1111': [error_msgs['disguise'], ],
     22            '9921-1111': [error_msgs['disguise'], ],
     23            '1121-1111': [error_msgs['disguise'], ],
     24            '1': [error_msgs['invalid'], ],
     25            '1111 1111': [error_msgs['prefix'], ],
     26        }
     27        self.assertFieldOutput(HKPhoneNumberField, valid, invalid)
  • tests/regressiontests/localflavor/tests.py

    diff --git a/tests/regressiontests/localflavor/tests.py b/tests/regressiontests/localflavor/tests.py
    index 0062a4f..856e518 100644
    a b from .fi.tests import FILocalFlavorTests  
    1818from .fr.tests import FRLocalFlavorTests
    1919from .gb.tests import GBLocalFlavorTests
    2020from .generic.tests import GenericLocalFlavorTests
     21from .hk.tests import HKLocalFlavorTests
    2122from .hr.tests import HRLocalFlavorTests
    2223from .id.tests import IDLocalFlavorTests
    2324from .ie.tests import IELocalFlavorTests
Back to Top