Ticket #11251: 11251.au-models.diff
File 11251.au-models.diff, 8.2 KB (added by , 14 years ago) |
---|
-
django/contrib/localflavor/au/forms.py
diff --git a/django/contrib/localflavor/au/forms.py b/django/contrib/localflavor/au/forms.py index 87e0438..1b094e7 100644
a b import re 12 12 PHONE_DIGITS_RE = re.compile(r'^(\d{10})$') 13 13 14 14 class AUPostCodeField(RegexField): 15 """Australian post code field.""" 15 """ Australian post code field. 16 17 Assumed to be 4 digits. 18 Northern Territory 3-digit postcodes should have leading zero. 19 """ 16 20 default_error_messages = { 17 'invalid': _('Enter a 4 digit post 21 'invalid': _('Enter a 4 digit postcode.'), 18 22 } 19 23 20 24 def __init__(self, *args, **kwargs): 25 if 'max_length' in kwargs: 26 kwargs.pop('max_length') 21 27 super(AUPostCodeField, self).__init__(r'^\d{4}$', 22 max_length= None, min_length=None, *args, **kwargs)28 max_length=4, min_length=None, *args, **kwargs) 23 29 30 24 31 class AUPhoneNumberField(Field): 25 32 """Australian phone number field.""" 26 33 default_error_messages = { … … class AUPhoneNumberField(Field): 40 47 return u'%s' % phone_match.group(1) 41 48 raise ValidationError(self.error_messages['invalid']) 42 49 50 43 51 class AUStateSelect(Select): 44 52 """ 45 53 A Select widget that uses a list of Australian states/territories as its -
new file django/contrib/localflavor/au/models.py
diff --git a/django/contrib/localflavor/au/models.py b/django/contrib/localflavor/au/models.py new file mode 100644 index 0000000..f2f6e47
- + 1 from django.conf import settings 2 from django.utils.translation import ugettext_lazy as _ 3 from django.db.models.fields import CharField 4 from django.contrib.localflavor.au.au_states import STATE_CHOICES 5 from django.contrib.localflavor.au import forms 6 7 class AUStateField(CharField): 8 9 description = _("Australian State") 10 11 def __init__(self, *args, **kwargs): 12 kwargs['choices'] = STATE_CHOICES 13 kwargs['max_length'] = 3 14 super(AUStateField, self).__init__(*args, **kwargs) 15 16 17 class AUPostCodeField(CharField): 18 19 description = _("Australian Postcode") 20 21 def __init__(self, *args, **kwargs): 22 kwargs['max_length'] = 4 23 super(AUPostCodeField, self).__init__(*args, **kwargs) 24 25 def formfield(self, **kwargs): 26 defaults = {'form_class': forms.AUPostCodeField} 27 defaults.update(kwargs) 28 return super(AUPostCodeField, self).formfield(**defaults) 29 30 31 class AUPhoneNumberField(CharField): 32 33 description = _("Australian Phone number") 34 35 def __init__(self, *args, **kwargs): 36 kwargs['max_length'] = 20 37 super(AUPhoneNumberField, self).__init__(*args, **kwargs) 38 39 def formfield(self, **kwargs): 40 defaults = {'form_class': forms.AUPhoneNumberField} 41 defaults.update(kwargs) 42 return super(AUPhoneNumberField, self).formfield(**defaults) 43 -
docs/ref/contrib/localflavor.txt
diff --git a/docs/ref/contrib/localflavor.txt b/docs/ref/contrib/localflavor.txt index f54341e..699be68 100644
a b Australia (``au``) 202 202 A ``Select`` widget that uses a list of Australian states/territories as its 203 203 choices. 204 204 205 .. class:: au.models.AUPhoneNumberField 206 207 A model field that checks that the value is a valid Australian phone 208 number (ten digits). 209 210 .. class:: au.models.AUStateField 211 212 A model field that forms represent as a ``forms.AUStateField`` field and 213 stores the three-letter Australian state abbreviation in the database. 214 215 .. class:: au.models.AUPostCodeField 216 217 A model field that forms represent as a ``forms.AUPostCodeField`` field 218 and stores the four-digit Australian postcode in the database. 219 205 220 Austria (``at``) 206 221 ================ 207 222 -
new file tests/regressiontests/localflavor/au/forms.py
diff --git a/tests/regressiontests/localflavor/au/__init__.py b/tests/regressiontests/localflavor/au/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/regressiontests/localflavor/au/forms.py b/tests/regressiontests/localflavor/au/forms.py new file mode 100644 index 0000000..e65b924
- + 1 from django.forms import ModelForm 2 from models import AustralianPlace 3 4 class AustralianPlaceForm(ModelForm): 5 """ Form for storing an Australian place. """ 6 class Meta: 7 model = AustralianPlace -
new file tests/regressiontests/localflavor/au/models.py
diff --git a/tests/regressiontests/localflavor/au/models.py b/tests/regressiontests/localflavor/au/models.py new file mode 100644 index 0000000..8b8b103
- + 1 from django.db import models 2 from django.contrib.localflavor.au.models import AUStateField, AUPostCodeField 3 4 class AustralianPlace(models.Model): 5 state = AUStateField(blank=True) 6 state_required = AUStateField() 7 state_default = AUStateField(default="NSW", blank=True) 8 postcode = AUPostCodeField(blank=True) 9 postcode_required = AUPostCodeField() 10 postcode_default = AUPostCodeField(default="2500", blank=True) 11 name = models.CharField(max_length=20) 12 13 class Meta: 14 app_label = 'localflavor' -
new file tests/regressiontests/localflavor/au/tests.py
diff --git a/tests/regressiontests/localflavor/au/tests.py b/tests/regressiontests/localflavor/au/tests.py new file mode 100644 index 0000000..05301ea
- + 1 from django.test import TestCase 2 from forms import AustralianPlaceForm 3 import re 4 5 SELECTED_OPTION_PATTERN = r'<option value="%s" selected="selected">' 6 BLANK_OPTION_PATTERN = r'<option value="">' 7 INPUT_VALUE_PATTERN = r'<input[^>]*value="%s"[^>]*>' 8 9 10 class AULocalflavorTests(TestCase): 11 def setUp(self): 12 self.form = AustralianPlaceForm( 13 {'state':'WA', 14 'state_required':'QLD', 15 'name':'dummy', 16 'postcode':'1234', 17 'postcode_required':'4321', 18 }) 19 20 def test_get_display_methods(self): 21 """ Ensure get_*_display() methods are added to model instances. """ 22 place = self.form.save() 23 self.assertEqual(place.get_state_display(), 'Western Australia') 24 self.assertEqual(place.get_state_required_display(), 'Queensland') 25 26 def test_default_values(self): 27 """ Ensure that default values are selected in forms. """ 28 form = AustralianPlaceForm() 29 self.assertTrue(re.search(SELECTED_OPTION_PATTERN % 'NSW', 30 str(form['state_default']))) 31 self.assertTrue(re.search(INPUT_VALUE_PATTERN % '2500', 32 str(form['postcode_default']))) 33 34 def test_required(self): 35 """ Test that required AUStateFields throw appropriate errors. """ 36 form = AustralianPlaceForm({'state':'NSW', 'name':'Wollongong'}) 37 self.assertFalse(form.is_valid()) 38 self.assertEqual( 39 form.errors['state_required'], [u'This field is required.']) 40 self.assertEqual( 41 form.errors['postcode_required'], [u'This field is required.']) 42 43 def test_field_blank_option(self): 44 """ Test that the empty option is there. """ 45 self.assertTrue(re.search(BLANK_OPTION_PATTERN, 46 str(self.form['state']))) 47 48 def test_selected_values(self): 49 """ Ensure selected states match the initial values provided. """ 50 self.assertTrue(re.search(SELECTED_OPTION_PATTERN % 'WA', 51 str(self.form['state']))) 52 self.assertTrue(re.search(SELECTED_OPTION_PATTERN % 'QLD', 53 str(self.form['state_required']))) 54 self.assertTrue(re.search(INPUT_VALUE_PATTERN % '1234', 55 str(self.form['postcode']))) 56 self.assertTrue(re.search(INPUT_VALUE_PATTERN % '4321', 57 str(self.form['postcode_required']))) -
tests/regressiontests/localflavor/tests.py
diff --git a/tests/regressiontests/localflavor/tests.py b/tests/regressiontests/localflavor/tests.py index 85e91a0..e22fc0f 100644
a b from django.utils import unittest 3 3 4 4 # just import your tests here 5 5 from us.tests import * 6 from au.tests import *