Ticket #17864: django.contrib.localflavor.hk.diff

File django.contrib.localflavor.hk.diff, 3.2 KB (added by KS Chan, 13 years ago)

git-diff

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

    diff --git a/django/contrib/localflavor/hk/models.py b/django/contrib/localflavor/hk/models.py
    new file mode 100644
    index 0000000..4366996
    - +  
     1from django.db import models
     2
     3
     4class PhoneNumberField(models.CharField):
     5    description = 'Phone number'
     6
     7    def __init__(self, *args, **kwargs):
     8        kwargs['max_length'] = 9  # e.g. XXXX-XXXX
     9        super(PhoneNumberField, self).__init__(*args, **kwargs)
     10
     11    def formfield(self, **kwargs):
     12        from common.forms import HKPhoneNumberField
     13        defaults = {'form_class': HKPhoneNumberField}
     14        defaults.update(kwargs)
     15        return super(PhoneNumberField, self).formfield(**defaults)
Back to Top