Ticket #17864: hkphonenumber.diff
File hkphonenumber.diff, 5.9 KB (added by , 13 years ago) |
---|
-
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 """ 2 Hong Kong specific Form helpers 3 """ 4 from __future__ import absolute_import 5 6 import re 7 8 from django.core.validators import EMPTY_VALUES 9 from django.forms import CharField 10 from django.forms import ValidationError 11 from django.utils.encoding import smart_unicode 12 from django.utils.translation import ugettext_lazy as _ 13 14 15 hk_phone_digits_re = re.compile(r'^(?:852-?)?(\d{4})[-\.]?(\d{4})$') 16 hk_special_numbers = ('999', '992', '112') 17 hk_phone_prefixes = ('2', '3', '5', '6', '8', '9') 18 19 20 class 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: 51 51 * Finland_ 52 52 * France_ 53 53 * Germany_ 54 * `Hong Kong`_ 54 55 * Iceland_ 55 56 * India_ 56 57 * Indonesia_ … … Here's an example of how to use them:: 108 109 .. _Finland: `Finland (fi)`_ 109 110 .. _France: `France (fr)`_ 110 111 .. _Germany: `Germany (de)`_ 112 .. _Hong Kong: `Hong Kong (hk)`_ 111 113 .. _The Netherlands: `The Netherlands (nl)`_ 112 114 .. _Iceland: `Iceland (is\_)`_ 113 115 .. _India: `India (in\_)`_ … … Germany (``de``) 560 562 561 563 A ``Select`` widget that uses a list of German states as its choices. 562 564 565 Hong 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 563 573 The Netherlands (``nl``) 564 574 ======================== 565 575 -
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
- + 1 from __future__ import absolute_import 2 3 from django.forms import ModelForm 4 5 from .models import HKPlace 6 7 8 class 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
- + 1 from __future__ import absolute_import 2 3 from django.contrib.localflavor.hk.forms import HKPhoneNumberField 4 from django.test import SimpleTestCase 5 6 7 class 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 18 18 from .fr.tests import FRLocalFlavorTests 19 19 from .gb.tests import GBLocalFlavorTests 20 20 from .generic.tests import GenericLocalFlavorTests 21 from .hk.tests import HKLocalFlavorTests 21 22 from .hr.tests import HRLocalFlavorTests 22 23 from .id.tests import IDLocalFlavorTests 23 24 from .ie.tests import IELocalFlavorTests