From 59d1f82634456851592857a41100c572637642ad Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Fri, 22 Apr 2011 12:01:59 +0000 Subject: [PATCH] Fixed #11251 -- Extended Australian localflavor to ship a few model fields additionally. Thanks, Simon Meers and Julien Phalip. git-svn-id: http://code.djangoproject.com/svn/django/trunk@16066 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/localflavor/au/forms.py | 16 +++-- django/contrib/localflavor/au/models.py | 44 ++++++++++++++ docs/ref/contrib/localflavor.txt | 15 +++++ .../localflavor/au/__init__.py | 0 tests/regressiontests/localflavor/au/forms.py | 7 +++ .../regressiontests/localflavor/au/models.py | 14 +++++ tests/regressiontests/localflavor/au/tests.py | 58 +++++++++++++++++++ tests/regressiontests/localflavor/tests.py | 1 + 8 files changed, 151 insertions(+), 4 deletions(-) create mode 100644 django/contrib/localflavor/au/models.py create mode 100644 tests/regressiontests/localflavor/au/__init__.py create mode 100644 tests/regressiontests/localflavor/au/forms.py create mode 100644 tests/regressiontests/localflavor/au/models.py create mode 100644 tests/regressiontests/localflavor/au/tests.py diff --git a/django/contrib/localflavor/au/forms.py b/django/contrib/localflavor/au/forms.py index 87e04387f5..12911ea8a3 100644 --- a/django/contrib/localflavor/au/forms.py +++ b/django/contrib/localflavor/au/forms.py @@ -1,25 +1,32 @@ """ Australian-specific Form helpers """ +import re from django.core.validators import EMPTY_VALUES from django.forms import ValidationError from django.forms.fields import Field, RegexField, Select from django.utils.encoding import smart_unicode from django.utils.translation import ugettext_lazy as _ -import re PHONE_DIGITS_RE = re.compile(r'^(\d{10})$') class AUPostCodeField(RegexField): - """Australian post code field.""" + """ Australian post code field. + + Assumed to be 4 digits. + Northern Territory 3-digit postcodes should have leading zero. + """ default_error_messages = { - 'invalid': _('Enter a 4 digit post code.'), + 'invalid': _('Enter a 4 digit postcode.'), } def __init__(self, *args, **kwargs): + if 'max_length' in kwargs: + kwargs.pop('max_length') super(AUPostCodeField, self).__init__(r'^\d{4}$', - max_length=None, min_length=None, *args, **kwargs) + max_length=4, min_length=None, *args, **kwargs) + class AUPhoneNumberField(Field): """Australian phone number field.""" @@ -40,6 +47,7 @@ class AUPhoneNumberField(Field): return u'%s' % phone_match.group(1) raise ValidationError(self.error_messages['invalid']) + class AUStateSelect(Select): """ A Select widget that uses a list of Australian states/territories as its diff --git a/django/contrib/localflavor/au/models.py b/django/contrib/localflavor/au/models.py new file mode 100644 index 0000000000..e2b897e4df --- /dev/null +++ b/django/contrib/localflavor/au/models.py @@ -0,0 +1,44 @@ +from django.conf import settings +from django.utils.translation import ugettext_lazy as _ +from django.db.models.fields import CharField + +from django.contrib.localflavor.au.au_states import STATE_CHOICES +from django.contrib.localflavor.au import forms + +class AUStateField(CharField): + + description = _("Australian State") + + def __init__(self, *args, **kwargs): + kwargs['choices'] = STATE_CHOICES + kwargs['max_length'] = 3 + super(AUStateField, self).__init__(*args, **kwargs) + + +class AUPostCodeField(CharField): + + description = _("Australian Postcode") + + def __init__(self, *args, **kwargs): + kwargs['max_length'] = 4 + super(AUPostCodeField, self).__init__(*args, **kwargs) + + def formfield(self, **kwargs): + defaults = {'form_class': forms.AUPostCodeField} + defaults.update(kwargs) + return super(AUPostCodeField, self).formfield(**defaults) + + +class AUPhoneNumberField(CharField): + + description = _("Australian Phone number") + + def __init__(self, *args, **kwargs): + kwargs['max_length'] = 20 + super(AUPhoneNumberField, self).__init__(*args, **kwargs) + + def formfield(self, **kwargs): + defaults = {'form_class': forms.AUPhoneNumberField} + defaults.update(kwargs) + return super(AUPhoneNumberField, self).formfield(**defaults) + diff --git a/docs/ref/contrib/localflavor.txt b/docs/ref/contrib/localflavor.txt index f54341ee6e..e2515e3e08 100644 --- a/docs/ref/contrib/localflavor.txt +++ b/docs/ref/contrib/localflavor.txt @@ -202,6 +202,21 @@ Australia (``au``) A ``Select`` widget that uses a list of Australian states/territories as its choices. +.. class:: au.models.AUPhoneNumberField + + A model field that checks that the value is a valid Australian phone + number (ten digits). + +.. class:: au.models.AUStateField + + A model field that forms represent as a ``forms.AUStateField`` field and + stores the three-letter Australian state abbreviation in the database. + +.. class:: au.models.AUPostCodeField + + A model field that forms represent as a ``forms.AUPostCodeField`` field + and stores the four-digit Australian postcode in the database. + Austria (``at``) ================ diff --git a/tests/regressiontests/localflavor/au/__init__.py b/tests/regressiontests/localflavor/au/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/regressiontests/localflavor/au/forms.py b/tests/regressiontests/localflavor/au/forms.py new file mode 100644 index 0000000000..e65b92438e --- /dev/null +++ b/tests/regressiontests/localflavor/au/forms.py @@ -0,0 +1,7 @@ +from django.forms import ModelForm +from models import AustralianPlace + +class AustralianPlaceForm(ModelForm): + """ Form for storing an Australian place. """ + class Meta: + model = AustralianPlace diff --git a/tests/regressiontests/localflavor/au/models.py b/tests/regressiontests/localflavor/au/models.py new file mode 100644 index 0000000000..8adfa1f6f5 --- /dev/null +++ b/tests/regressiontests/localflavor/au/models.py @@ -0,0 +1,14 @@ +from django.db import models +from django.contrib.localflavor.au.models import AUStateField, AUPostCodeField + +class AustralianPlace(models.Model): + state = AUStateField(blank=True) + state_required = AUStateField() + state_default = AUStateField(default="NSW", blank=True) + postcode = AUPostCodeField(blank=True) + postcode_required = AUPostCodeField() + postcode_default = AUPostCodeField(default="2500", blank=True) + name = models.CharField(max_length=20) + + class Meta: + app_label = 'localflavor' diff --git a/tests/regressiontests/localflavor/au/tests.py b/tests/regressiontests/localflavor/au/tests.py new file mode 100644 index 0000000000..1f462d579f --- /dev/null +++ b/tests/regressiontests/localflavor/au/tests.py @@ -0,0 +1,58 @@ +import re + +from django.test import TestCase +from forms import AustralianPlaceForm + +SELECTED_OPTION_PATTERN = r'