-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed #3876 -- Added Australian local flavour. Thanks, Matthew Flanagan.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4955 bcc190cf-cafb-0310-a4f2-bffc1f526a37
- Loading branch information
1 parent
1245b5c
commit 8fafee4
Showing
5 changed files
with
191 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
""" | ||
An alphabetical list of states for use as `choices` in a formfield. | ||
This exists in this standalone file so that it's only imported into memory | ||
when explicitly needed. | ||
""" | ||
|
||
STATE_CHOICES = ( | ||
('ACT', 'Australian Capital Territory'), | ||
('NSW', 'New South Wales'), | ||
('NT', 'Northern Territory'), | ||
('QLD', 'Queensland'), | ||
('SA', 'South Australia'), | ||
('TAS', 'Tasmania'), | ||
('VIC', 'Victoria'), | ||
('WA', 'Western Australia'), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
""" | ||
Australian-specific Form helpers | ||
""" | ||
|
||
from django.newforms import ValidationError | ||
from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES | ||
from django.newforms.util import smart_unicode | ||
from django.utils.translation import gettext | ||
import re | ||
|
||
PHONE_DIGITS_RE = re.compile(r'^(\d{10})$') | ||
|
||
class AUPostCodeField(RegexField): | ||
"""Australian post code field.""" | ||
def __init__(self, *args, **kwargs): | ||
super(AUPostCodeField, self).__init__(r'^\d{4}$', | ||
max_length=None, min_length=None, | ||
error_message=gettext(u'Enter a 4 digit post code.'), | ||
*args, **kwargs) | ||
|
||
class AUPhoneNumberField(Field): | ||
"""Australian phone number field.""" | ||
def clean(self, value): | ||
"""Validate a phone number. Strips parentheses, whitespace and | ||
hyphens. | ||
""" | ||
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) | ||
raise ValidationError(u'Phone numbers must contain 10 digits.') | ||
|
||
class AUStateSelect(Select): | ||
""" | ||
A Select widget that uses a list of Australian states/territories as its | ||
choices. | ||
""" | ||
def __init__(self, attrs=None): | ||
from au_states import STATE_CHOICES # relative import | ||
super(AUStateSelect, self).__init__(attrs, choices=STATE_CHOICES) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters