Ticket #11251: 11251.au-models.diff

File 11251.au-models.diff, 8.2 KB (added by Julien Phalip, 14 years ago)

Oops, this time in diff format

  • 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  
    1212PHONE_DIGITS_RE = re.compile(r'^(\d{10})$')
    1313
    1414class 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    """
    1620    default_error_messages = {
    17         'invalid': _('Enter a 4 digit post code.'),
     21        'invalid': _('Enter a 4 digit postcode.'),
    1822    }
    1923
    2024    def __init__(self, *args, **kwargs):
     25        if 'max_length' in kwargs:
     26            kwargs.pop('max_length')
    2127        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)
    2329
     30       
    2431class AUPhoneNumberField(Field):
    2532    """Australian phone number field."""
    2633    default_error_messages = {
    class AUPhoneNumberField(Field):  
    4047            return u'%s' % phone_match.group(1)
    4148        raise ValidationError(self.error_messages['invalid'])
    4249
     50   
    4351class AUStateSelect(Select):
    4452    """
    4553    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
    - +  
     1from django.conf import settings
     2from django.utils.translation import ugettext_lazy as _
     3from django.db.models.fields import CharField
     4from django.contrib.localflavor.au.au_states import STATE_CHOICES
     5from django.contrib.localflavor.au import forms
     6
     7class 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
     17class 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       
     31class 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``)  
    202202    A ``Select`` widget that uses a list of Australian states/territories as its
    203203    choices.
    204204
     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
    205220Austria (``at``)
    206221================
    207222
  • 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
    - +  
     1from django.forms import ModelForm
     2from models import AustralianPlace
     3
     4class 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
    - +  
     1from django.db import models
     2from django.contrib.localflavor.au.models import AUStateField, AUPostCodeField
     3 
     4class 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
    - +  
     1from django.test import TestCase
     2from forms import AustralianPlaceForm
     3import re
     4 
     5SELECTED_OPTION_PATTERN = r'<option value="%s" selected="selected">'
     6BLANK_OPTION_PATTERN = r'<option value="">'
     7INPUT_VALUE_PATTERN = r'<input[^>]*value="%s"[^>]*>'
     8
     9
     10class 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  
    33
    44# just import your tests here
    55from us.tests import *
     6from au.tests import *
Back to Top