2007-04-08 21:22:48 +08:00
|
|
|
"""
|
|
|
|
Australian-specific Form helpers
|
|
|
|
"""
|
|
|
|
|
2008-07-19 09:22:26 +08:00
|
|
|
from django.forms import ValidationError
|
|
|
|
from django.forms.fields import Field, RegexField, Select, EMPTY_VALUES
|
|
|
|
from django.forms.util import smart_unicode
|
2008-06-18 21:10:05 +08:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2007-04-08 21:22:48 +08:00
|
|
|
import re
|
|
|
|
|
|
|
|
PHONE_DIGITS_RE = re.compile(r'^(\d{10})$')
|
|
|
|
|
|
|
|
class AUPostCodeField(RegexField):
|
|
|
|
"""Australian post code field."""
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
2008-06-18 21:10:05 +08:00
|
|
|
'invalid': _('Enter a 4 digit post code.'),
|
2007-12-17 16:05:27 +08:00
|
|
|
}
|
|
|
|
|
2007-04-08 21:22:48 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(AUPostCodeField, self).__init__(r'^\d{4}$',
|
2007-12-17 16:05:27 +08:00
|
|
|
max_length=None, min_length=None, *args, **kwargs)
|
2007-04-08 21:22:48 +08:00
|
|
|
|
|
|
|
class AUPhoneNumberField(Field):
|
|
|
|
"""Australian phone number field."""
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
|
|
|
'invalid': u'Phone numbers must contain 10 digits.',
|
|
|
|
}
|
|
|
|
|
2007-04-08 21:22:48 +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
|
|
|
"""
|
|
|
|
Validate a phone number. Strips parentheses, whitespace and hyphens.
|
2007-04-08 21:22:48 +08:00
|
|
|
"""
|
|
|
|
super(AUPhoneNumberField, self).clean(value)
|
|
|
|
if value in EMPTY_VALUES:
|
|
|
|
return u''
|
|
|
|
value = re.sub('(\(|\)|\s+|-)', '', smart_unicode(value))
|
|
|
|
phone_match = PHONE_DIGITS_RE.search(value)
|
|
|
|
if phone_match:
|
|
|
|
return u'%s' % phone_match.group(1)
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2007-04-08 21:22:48 +08:00
|
|
|
|
|
|
|
class AUStateSelect(Select):
|
|
|
|
"""
|
|
|
|
A Select widget that uses a list of Australian 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 au_states import STATE_CHOICES
|
2007-04-08 21:22:48 +08:00
|
|
|
super(AUStateSelect, self).__init__(attrs, choices=STATE_CHOICES)
|