2007-04-08 21:22:48 +08:00
|
|
|
"""
|
|
|
|
Australian-specific Form helpers
|
|
|
|
"""
|
2011-10-18 08:47:49 +08:00
|
|
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2011-04-22 20:01:59 +08:00
|
|
|
import re
|
2007-04-08 21:22:48 +08:00
|
|
|
|
2011-10-18 08:47:49 +08:00
|
|
|
from django.contrib.localflavor.au.au_states import STATE_CHOICES
|
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
|
|
|
|
from django.utils.encoding 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
|
|
|
|
2011-10-18 08:47:49 +08:00
|
|
|
|
2007-04-08 21:22:48 +08:00
|
|
|
PHONE_DIGITS_RE = re.compile(r'^(\d{10})$')
|
|
|
|
|
|
|
|
class AUPostCodeField(RegexField):
|
2011-04-22 20:01:59 +08:00
|
|
|
""" Australian post code field.
|
|
|
|
|
|
|
|
Assumed to be 4 digits.
|
|
|
|
Northern Territory 3-digit postcodes should have leading zero.
|
|
|
|
"""
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
2011-04-22 20:01:59 +08:00
|
|
|
'invalid': _('Enter a 4 digit postcode.'),
|
2007-12-17 16:05:27 +08:00
|
|
|
}
|
|
|
|
|
2011-05-03 19:52:04 +08:00
|
|
|
def __init__(self, max_length=4, min_length=None, *args, **kwargs):
|
2007-04-08 21:22:48 +08:00
|
|
|
super(AUPostCodeField, self).__init__(r'^\d{4}$',
|
2011-05-03 19:52:04 +08:00
|
|
|
max_length, min_length, *args, **kwargs)
|
2011-04-22 20:01:59 +08:00
|
|
|
|
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
|
|
|
|
2011-04-22 20:01:59 +08:00
|
|
|
|
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):
|
|
|
|
super(AUStateSelect, self).__init__(attrs, choices=STATE_CHOICES)
|