Code

Ticket #11251: 11251.au-models.diff

File 11251.au-models.diff, 8.2 KB (added by julien, 3 years ago)

Oops, this time in diff format

Line 
1diff --git a/django/contrib/localflavor/au/forms.py b/django/contrib/localflavor/au/forms.py
2index 87e0438..1b094e7 100644
3--- a/django/contrib/localflavor/au/forms.py
4+++ b/django/contrib/localflavor/au/forms.py
5@@ -12,15 +12,22 @@ import re
6 PHONE_DIGITS_RE = re.compile(r'^(\d{10})$')
7 
8 class AUPostCodeField(RegexField):
9-    """Australian post code field."""
10+    """ Australian post code field.
11+
12+    Assumed to be 4 digits.
13+    Northern Territory 3-digit postcodes should have leading zero.
14+    """
15     default_error_messages = {
16-        'invalid': _('Enter a 4 digit post code.'),
17+        'invalid': _('Enter a 4 digit postcode.'),
18     }
19 
20     def __init__(self, *args, **kwargs):
21+        if 'max_length' in kwargs:
22+            kwargs.pop('max_length')
23         super(AUPostCodeField, self).__init__(r'^\d{4}$',
24-            max_length=None, min_length=None, *args, **kwargs)
25+            max_length=4, min_length=None, *args, **kwargs)
26 
27+       
28 class AUPhoneNumberField(Field):
29     """Australian phone number field."""
30     default_error_messages = {
31@@ -40,6 +47,7 @@ class AUPhoneNumberField(Field):
32             return u'%s' % phone_match.group(1)
33         raise ValidationError(self.error_messages['invalid'])
34 
35+   
36 class AUStateSelect(Select):
37     """
38     A Select widget that uses a list of Australian states/territories as its
39diff --git a/django/contrib/localflavor/au/models.py b/django/contrib/localflavor/au/models.py
40new file mode 100644
41index 0000000..f2f6e47
42--- /dev/null
43+++ b/django/contrib/localflavor/au/models.py
44@@ -0,0 +1,43 @@
45+from django.conf import settings
46+from django.utils.translation import ugettext_lazy as _
47+from django.db.models.fields import CharField
48+from django.contrib.localflavor.au.au_states import STATE_CHOICES
49+from django.contrib.localflavor.au import forms
50+
51+class AUStateField(CharField):
52+
53+    description = _("Australian State")
54+
55+    def __init__(self, *args, **kwargs):
56+        kwargs['choices'] = STATE_CHOICES
57+        kwargs['max_length'] = 3
58+        super(AUStateField, self).__init__(*args, **kwargs)
59+
60+
61+class AUPostCodeField(CharField):
62+
63+    description = _("Australian Postcode")
64+
65+    def __init__(self, *args, **kwargs):
66+        kwargs['max_length'] = 4
67+        super(AUPostCodeField, self).__init__(*args, **kwargs)
68+
69+    def formfield(self, **kwargs):
70+        defaults = {'form_class': forms.AUPostCodeField}
71+        defaults.update(kwargs)
72+        return super(AUPostCodeField, self).formfield(**defaults)
73+       
74+       
75+class AUPhoneNumberField(CharField):
76+
77+    description = _("Australian Phone number")
78+
79+    def __init__(self, *args, **kwargs):
80+        kwargs['max_length'] = 20
81+        super(AUPhoneNumberField, self).__init__(*args, **kwargs)
82+
83+    def formfield(self, **kwargs):
84+        defaults = {'form_class': forms.AUPhoneNumberField}
85+        defaults.update(kwargs)
86+        return super(AUPhoneNumberField, self).formfield(**defaults)
87+   
88diff --git a/docs/ref/contrib/localflavor.txt b/docs/ref/contrib/localflavor.txt
89index f54341e..699be68 100644
90--- a/docs/ref/contrib/localflavor.txt
91+++ b/docs/ref/contrib/localflavor.txt
92@@ -202,6 +202,21 @@ Australia (``au``)
93     A ``Select`` widget that uses a list of Australian states/territories as its
94     choices.
95 
96+.. class:: au.models.AUPhoneNumberField
97+
98+    A model field that checks that the value is a valid Australian phone
99+    number (ten digits).
100+
101+.. class:: au.models.AUStateField
102+
103+    A model field that forms represent as a ``forms.AUStateField`` field and
104+    stores the three-letter Australian state abbreviation in the database.
105+
106+.. class:: au.models.AUPostCodeField
107+
108+    A model field that forms represent as a ``forms.AUPostCodeField`` field
109+    and stores the four-digit Australian postcode in the database.
110+
111 Austria (``at``)
112 ================
113 
114diff --git a/tests/regressiontests/localflavor/au/__init__.py b/tests/regressiontests/localflavor/au/__init__.py
115new file mode 100644
116index 0000000..e69de29
117diff --git a/tests/regressiontests/localflavor/au/forms.py b/tests/regressiontests/localflavor/au/forms.py
118new file mode 100644
119index 0000000..e65b924
120--- /dev/null
121+++ b/tests/regressiontests/localflavor/au/forms.py
122@@ -0,0 +1,7 @@
123+from django.forms import ModelForm
124+from models import AustralianPlace
125+
126+class AustralianPlaceForm(ModelForm):
127+    """ Form for storing an Australian place. """
128+    class Meta:
129+        model = AustralianPlace
130diff --git a/tests/regressiontests/localflavor/au/models.py b/tests/regressiontests/localflavor/au/models.py
131new file mode 100644
132index 0000000..8b8b103
133--- /dev/null
134+++ b/tests/regressiontests/localflavor/au/models.py
135@@ -0,0 +1,14 @@
136+from django.db import models
137+from django.contrib.localflavor.au.models import AUStateField, AUPostCodeField
138+
139+class AustralianPlace(models.Model):
140+    state = AUStateField(blank=True)
141+    state_required = AUStateField()
142+    state_default = AUStateField(default="NSW", blank=True)
143+    postcode = AUPostCodeField(blank=True)
144+    postcode_required = AUPostCodeField()
145+    postcode_default = AUPostCodeField(default="2500", blank=True)
146+    name = models.CharField(max_length=20)
147+   
148+    class Meta:
149+        app_label = 'localflavor'
150diff --git a/tests/regressiontests/localflavor/au/tests.py b/tests/regressiontests/localflavor/au/tests.py
151new file mode 100644
152index 0000000..05301ea
153--- /dev/null
154+++ b/tests/regressiontests/localflavor/au/tests.py
155@@ -0,0 +1,57 @@
156+from django.test import TestCase
157+from forms import AustralianPlaceForm
158+import re
159+
160+SELECTED_OPTION_PATTERN = r'<option value="%s" selected="selected">'
161+BLANK_OPTION_PATTERN = r'<option value="">'
162+INPUT_VALUE_PATTERN = r'<input[^>]*value="%s"[^>]*>'
163+
164+
165+class AULocalflavorTests(TestCase):
166+    def setUp(self):
167+        self.form = AustralianPlaceForm(
168+            {'state':'WA',
169+             'state_required':'QLD',
170+             'name':'dummy',
171+             'postcode':'1234',
172+             'postcode_required':'4321',
173+             })
174+       
175+    def test_get_display_methods(self):
176+        """ Ensure get_*_display() methods are added to model instances. """
177+        place = self.form.save()
178+        self.assertEqual(place.get_state_display(), 'Western Australia')
179+        self.assertEqual(place.get_state_required_display(), 'Queensland')
180+
181+    def test_default_values(self):
182+        """ Ensure that default values are selected in forms. """
183+        form = AustralianPlaceForm()
184+        self.assertTrue(re.search(SELECTED_OPTION_PATTERN % 'NSW',
185+                                  str(form['state_default'])))
186+        self.assertTrue(re.search(INPUT_VALUE_PATTERN % '2500',
187+                                  str(form['postcode_default'])))
188+       
189+    def test_required(self):
190+        """ Test that required AUStateFields throw appropriate errors. """
191+        form = AustralianPlaceForm({'state':'NSW', 'name':'Wollongong'})
192+        self.assertFalse(form.is_valid())
193+        self.assertEqual(
194+            form.errors['state_required'], [u'This field is required.'])
195+        self.assertEqual(
196+            form.errors['postcode_required'], [u'This field is required.'])       
197+   
198+    def test_field_blank_option(self):
199+        """ Test that the empty option is there. """
200+        self.assertTrue(re.search(BLANK_OPTION_PATTERN,
201+                                  str(self.form['state'])))
202+
203+    def test_selected_values(self):
204+        """ Ensure selected states match the initial values provided. """
205+        self.assertTrue(re.search(SELECTED_OPTION_PATTERN % 'WA',
206+                                  str(self.form['state'])))
207+        self.assertTrue(re.search(SELECTED_OPTION_PATTERN % 'QLD',
208+                                  str(self.form['state_required'])))
209+        self.assertTrue(re.search(INPUT_VALUE_PATTERN % '1234',
210+                                  str(self.form['postcode'])))
211+        self.assertTrue(re.search(INPUT_VALUE_PATTERN % '4321',
212+                                  str(self.form['postcode_required'])))
213diff --git a/tests/regressiontests/localflavor/tests.py b/tests/regressiontests/localflavor/tests.py
214index 85e91a0..e22fc0f 100644
215--- a/tests/regressiontests/localflavor/tests.py
216+++ b/tests/regressiontests/localflavor/tests.py
217@@ -3,3 +3,4 @@ from django.utils import unittest
218 
219 # just import your tests here
220 from us.tests import *
221+from au.tests import *