2007-02-15 11:37:25 +08:00
|
|
|
"""
|
|
|
|
USA-specific Form helpers
|
|
|
|
"""
|
|
|
|
|
2010-01-05 11:56:19 +08:00
|
|
|
from django.core.validators import EMPTY_VALUES
|
2008-07-19 09:22:26 +08:00
|
|
|
from django.forms import ValidationError
|
2010-01-05 11:56:19 +08:00
|
|
|
from django.forms.fields import Field, RegexField, Select, CharField
|
2007-04-04 14:34:19 +08:00
|
|
|
from django.utils.encoding import smart_unicode
|
2008-06-18 21:10:05 +08:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2007-02-21 13:54:09 +08:00
|
|
|
import re
|
|
|
|
|
|
|
|
phone_digits_re = re.compile(r'^(?:1-?)?(\d{3})[-\.]?(\d{3})[-\.]?(\d{4})$')
|
2007-04-03 11:56:17 +08:00
|
|
|
ssn_re = re.compile(r"^(?P<area>\d{3})[-\ ]?(?P<group>\d{2})[-\ ]?(?P<serial>\d{4})$")
|
2007-02-15 11:37:25 +08:00
|
|
|
|
|
|
|
class USZipCodeField(RegexField):
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
2008-06-18 21:10:05 +08:00
|
|
|
'invalid': _('Enter a zip code in the format XXXXX or XXXXX-XXXX.'),
|
2007-12-17 16:05:27 +08:00
|
|
|
}
|
|
|
|
|
2007-02-15 11:37:25 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(USZipCodeField, self).__init__(r'^\d{5}(?:-\d{4})?$',
|
2007-12-17 16:05:27 +08:00
|
|
|
max_length=None, min_length=None, *args, **kwargs)
|
2007-02-15 14:18:27 +08:00
|
|
|
|
2010-01-02 05:36:08 +08:00
|
|
|
class USPhoneNumberField(CharField):
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
2010-01-05 06:47:52 +08:00
|
|
|
'invalid': _('Phone numbers must be in XXX-XXX-XXXX format.'),
|
2007-12-17 16:05:27 +08:00
|
|
|
}
|
|
|
|
|
2007-02-21 13:54:09 +08:00
|
|
|
def clean(self, value):
|
|
|
|
super(USPhoneNumberField, self).clean(value)
|
|
|
|
if value in EMPTY_VALUES:
|
|
|
|
return u''
|
|
|
|
value = re.sub('(\(|\)|\s+)', '', smart_unicode(value))
|
|
|
|
m = phone_digits_re.search(value)
|
|
|
|
if m:
|
|
|
|
return u'%s-%s-%s' % (m.group(1), m.group(2), m.group(3))
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2007-02-21 13:54:09 +08:00
|
|
|
|
2007-04-03 11:56:17 +08:00
|
|
|
class USSocialSecurityNumberField(Field):
|
|
|
|
"""
|
|
|
|
A United States Social Security number.
|
2007-04-04 14:34:19 +08:00
|
|
|
|
2007-04-03 11:56:17 +08:00
|
|
|
Checks the following rules to determine whether the number is valid:
|
2007-04-04 14:34:19 +08:00
|
|
|
|
2007-04-03 11:56:17 +08:00
|
|
|
* Conforms to the XXX-XX-XXXX format.
|
|
|
|
* No group consists entirely of zeroes.
|
|
|
|
* The leading group is not "666" (block "666" will never be allocated).
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
* The number is not in the promotional block 987-65-4320 through
|
|
|
|
987-65-4329, which are permanently invalid.
|
2007-04-03 11:56:17 +08:00
|
|
|
* The number is not one known to be invalid due to otherwise widespread
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
promotional use or distribution (e.g., the Woolworth's number or the
|
|
|
|
1962 promotional number).
|
2007-04-03 11:56:17 +08:00
|
|
|
"""
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
2008-06-18 21:10:05 +08:00
|
|
|
'invalid': _('Enter a valid U.S. Social Security number in XXX-XX-XXXX format.'),
|
2007-12-17 16:05:27 +08:00
|
|
|
}
|
|
|
|
|
2007-04-03 11:56:17 +08:00
|
|
|
def clean(self, value):
|
|
|
|
super(USSocialSecurityNumberField, self).clean(value)
|
|
|
|
if value in EMPTY_VALUES:
|
|
|
|
return u''
|
|
|
|
match = re.match(ssn_re, value)
|
|
|
|
if not match:
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2007-04-03 11:56:17 +08:00
|
|
|
area, group, serial = match.groupdict()['area'], match.groupdict()['group'], match.groupdict()['serial']
|
2007-04-04 06:33:28 +08:00
|
|
|
|
2007-04-03 11:56:17 +08:00
|
|
|
# First pass: no blocks of all zeroes.
|
|
|
|
if area == '000' or \
|
|
|
|
group == '00' or \
|
|
|
|
serial == '0000':
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2007-04-04 06:33:28 +08:00
|
|
|
|
2007-04-03 11:56:17 +08:00
|
|
|
# Second pass: promotional and otherwise permanently invalid numbers.
|
|
|
|
if area == '666' or \
|
2007-04-04 06:33:28 +08:00
|
|
|
(area == '987' and group == '65' and 4320 <= int(serial) <= 4329) or \
|
2007-04-03 11:56:17 +08:00
|
|
|
value == '078-05-1120' or \
|
|
|
|
value == '219-09-9999':
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2007-04-03 11:56:17 +08:00
|
|
|
return u'%s-%s-%s' % (area, group, serial)
|
|
|
|
|
2007-02-15 14:18:27 +08:00
|
|
|
class USStateField(Field):
|
|
|
|
"""
|
|
|
|
A form field that validates its input is a U.S. state name or abbreviation.
|
|
|
|
It normalizes the input to the standard two-leter postal service
|
|
|
|
abbreviation for the given state.
|
|
|
|
"""
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
2010-01-05 06:47:52 +08:00
|
|
|
'invalid': _('Enter a U.S. state or territory.'),
|
2007-12-17 16:05:27 +08:00
|
|
|
}
|
|
|
|
|
2007-02-15 14:18:27 +08:00
|
|
|
def clean(self, value):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
from us_states import STATES_NORMALIZED
|
2007-02-15 14:18:27 +08:00
|
|
|
super(USStateField, self).clean(value)
|
|
|
|
if value in EMPTY_VALUES:
|
|
|
|
return u''
|
|
|
|
try:
|
|
|
|
value = value.strip().lower()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
return STATES_NORMALIZED[value.strip().lower()].decode('ascii')
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2007-02-20 08:30:22 +08:00
|
|
|
|
|
|
|
class USStateSelect(Select):
|
|
|
|
"""
|
|
|
|
A Select widget that uses a list of U.S. states/territories as its choices.
|
|
|
|
"""
|
|
|
|
def __init__(self, attrs=None):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
from us_states import STATE_CHOICES
|
2007-02-20 08:30:22 +08:00
|
|
|
super(USStateSelect, self).__init__(attrs, choices=STATE_CHOICES)
|