app/django/contrib/localflavor/us/forms.py
changeset 54 03e267d67478
child 323 ff1a9aa48cfd
equal deleted inserted replaced
53:57b4279d8c4e 54:03e267d67478
       
     1 """
       
     2 USA-specific Form helpers
       
     3 """
       
     4 
       
     5 from django.newforms import ValidationError
       
     6 from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES
       
     7 from django.utils.encoding import smart_unicode
       
     8 from django.utils.translation import ugettext
       
     9 import re
       
    10 
       
    11 phone_digits_re = re.compile(r'^(?:1-?)?(\d{3})[-\.]?(\d{3})[-\.]?(\d{4})$')
       
    12 ssn_re = re.compile(r"^(?P<area>\d{3})[-\ ]?(?P<group>\d{2})[-\ ]?(?P<serial>\d{4})$")
       
    13 
       
    14 class USZipCodeField(RegexField):
       
    15     default_error_messages = {
       
    16         'invalid': ugettext('Enter a zip code in the format XXXXX or XXXXX-XXXX.'),
       
    17     }
       
    18 
       
    19     def __init__(self, *args, **kwargs):
       
    20         super(USZipCodeField, self).__init__(r'^\d{5}(?:-\d{4})?$',
       
    21             max_length=None, min_length=None, *args, **kwargs)
       
    22 
       
    23 class USPhoneNumberField(Field):
       
    24     default_error_messages = {
       
    25         'invalid': u'Phone numbers must be in XXX-XXX-XXXX format.',
       
    26     }
       
    27 
       
    28     def clean(self, value):
       
    29         super(USPhoneNumberField, self).clean(value)
       
    30         if value in EMPTY_VALUES:
       
    31             return u''
       
    32         value = re.sub('(\(|\)|\s+)', '', smart_unicode(value))
       
    33         m = phone_digits_re.search(value)
       
    34         if m:
       
    35             return u'%s-%s-%s' % (m.group(1), m.group(2), m.group(3))
       
    36         raise ValidationError(self.error_messages['invalid'])
       
    37 
       
    38 class USSocialSecurityNumberField(Field):
       
    39     """
       
    40     A United States Social Security number.
       
    41 
       
    42     Checks the following rules to determine whether the number is valid:
       
    43 
       
    44         * Conforms to the XXX-XX-XXXX format.
       
    45         * No group consists entirely of zeroes.
       
    46         * The leading group is not "666" (block "666" will never be allocated).
       
    47         * The number is not in the promotional block 987-65-4320 through
       
    48           987-65-4329, which are permanently invalid.
       
    49         * The number is not one known to be invalid due to otherwise widespread
       
    50           promotional use or distribution (e.g., the Woolworth's number or the
       
    51           1962 promotional number).
       
    52     """
       
    53     default_error_messages = {
       
    54         'invalid': ugettext('Enter a valid U.S. Social Security number in XXX-XX-XXXX format.'),
       
    55     }
       
    56 
       
    57     def clean(self, value):
       
    58         super(USSocialSecurityNumberField, self).clean(value)
       
    59         if value in EMPTY_VALUES:
       
    60             return u''
       
    61         match = re.match(ssn_re, value)
       
    62         if not match:
       
    63             raise ValidationError(self.error_messages['invalid'])
       
    64         area, group, serial = match.groupdict()['area'], match.groupdict()['group'], match.groupdict()['serial']
       
    65 
       
    66         # First pass: no blocks of all zeroes.
       
    67         if area == '000' or \
       
    68            group == '00' or \
       
    69            serial == '0000':
       
    70             raise ValidationError(self.error_messages['invalid'])
       
    71 
       
    72         # Second pass: promotional and otherwise permanently invalid numbers.
       
    73         if area == '666' or \
       
    74            (area == '987' and group == '65' and 4320 <= int(serial) <= 4329) or \
       
    75            value == '078-05-1120' or \
       
    76            value == '219-09-9999':
       
    77             raise ValidationError(self.error_messages['invalid'])
       
    78         return u'%s-%s-%s' % (area, group, serial)
       
    79 
       
    80 class USStateField(Field):
       
    81     """
       
    82     A form field that validates its input is a U.S. state name or abbreviation.
       
    83     It normalizes the input to the standard two-leter postal service
       
    84     abbreviation for the given state.
       
    85     """
       
    86     default_error_messages = {
       
    87         'invalid': u'Enter a U.S. state or territory.',
       
    88     }
       
    89 
       
    90     def clean(self, value):
       
    91         from us_states import STATES_NORMALIZED
       
    92         super(USStateField, self).clean(value)
       
    93         if value in EMPTY_VALUES:
       
    94             return u''
       
    95         try:
       
    96             value = value.strip().lower()
       
    97         except AttributeError:
       
    98             pass
       
    99         else:
       
   100             try:
       
   101                 return STATES_NORMALIZED[value.strip().lower()].decode('ascii')
       
   102             except KeyError:
       
   103                 pass
       
   104         raise ValidationError(self.error_messages['invalid'])
       
   105 
       
   106 class USStateSelect(Select):
       
   107     """
       
   108     A Select widget that uses a list of U.S. states/territories as its choices.
       
   109     """
       
   110     def __init__(self, attrs=None):
       
   111         from us_states import STATE_CHOICES
       
   112         super(USStateSelect, self).__init__(attrs, choices=STATE_CHOICES)