Ticket #3876: localflavor_au2.diff

File localflavor_au2.diff, 6.9 KB (added by mattimustang@…, 8 years ago)

updated patch with better ValidationError message and moved tests into localflavor.py module

  • django/contrib/localflavor/au/au_states.py

     
     1"""
     2An alphabetical list of states for use as `choices` in a formfield.
     3
     4This exists in this standalone file so that it's only imported into memory
     5when explicitly needed.
     6"""
     7
     8STATE_CHOICES = (
     9    ('ACT', 'Australian Capital Territory'),
     10    ('NSW', 'New South Wales'),
     11    ('NT', 'Northern Territory'),
     12    ('QLD', 'Queensland'),
     13    ('SA', 'South Australia'),
     14    ('TAS', 'Tasmania'),
     15    ('VIC', 'Victoria'),
     16    ('WA', 'Western Australia'),
     17)
  • django/contrib/localflavor/au/forms.py

     
     1"""
     2Australian-specific Form helpers
     3"""
     4
     5from django.newforms import ValidationError
     6from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES
     7from django.newforms.util import smart_unicode
     8from django.utils.translation import gettext
     9import re
     10
     11PHONE_DIGITS_RE = re.compile(r'^(\d{10})$')
     12
     13class AUPostCodeField(RegexField):
     14    """Australian post code field."""
     15    def __init__(self, *args, **kwargs):
     16        super(AUPostCodeField, self).__init__(r'^\d{4}$',
     17            max_length=None, min_length=None,
     18            error_message=gettext(u'Enter a post code in the format XXXX.'),
     19            *args, **kwargs)
     20
     21class AUPhoneNumberField(Field):
     22    """Australian phone number field."""
     23    def clean(self, value):
     24        """Validate a phone number. Strips parentheses, whitespace and
     25        hyphens.
     26        """
     27        super(AUPhoneNumberField, self).clean(value)
     28        if value in EMPTY_VALUES:
     29            return u''
     30        value = re.sub('(\(|\)|\s+|-)', '', smart_unicode(value))
     31        phone_match = PHONE_DIGITS_RE.search(value)
     32        if phone_match:
     33            return u'%s' % phone_match.group(1)
     34        raise ValidationError(u'Phone numbers must consist of 10 digits.' \
     35            u' Parentheses, white space and hyphens are allowed.')
     36
     37class AUStateSelect(Select):
     38    """
     39    A Select widget that uses a list of Australian states/territories as its
     40    choices.
     41    """
     42    def __init__(self, attrs=None):
     43        from au_states import STATE_CHOICES # relative import
     44        super(AUStateSelect, self).__init__(attrs, choices=STATE_CHOICES)
  • tests/regressiontests/forms/localflavor.py

     
    882882Traceback (most recent call last):
    883883...
    884884ValidationError: [u'Enter a valid German identity card number in XXXXXXXXXXX-XXXXXXX-XXXXXXX-X format.']
     885
     886## AUPostCodeField ##########################################################
     887
     888A field that accepts a four digit Australian post code.
     889
     890>>> from django.contrib.localflavor.au.forms import AUPostCodeField
     891>>> f = AUPostCodeField()
     892>>> f.clean('1234')
     893u'1234'
     894>>> f.clean('2000')
     895u'2000'
     896>>> f.clean('abcd')
     897Traceback (most recent call last):
     898...
     899ValidationError: [u'Enter a post code in the format XXXX.']
     900>>> f.clean('20001')
     901Traceback (most recent call last):
     902...
     903ValidationError: [u'Enter a post code in the format XXXX.']
     904>>> f.clean(None)
     905Traceback (most recent call last):
     906...
     907ValidationError: [u'This field is required.']
     908>>> f.clean('')
     909Traceback (most recent call last):
     910...
     911ValidationError: [u'This field is required.']
     912
     913>>> f = AUPostCodeField(required=False)
     914>>> f.clean('1234')
     915u'1234'
     916>>> f.clean('2000')
     917u'2000'
     918>>> f.clean('abcd')
     919Traceback (most recent call last):
     920...
     921ValidationError: [u'Enter a post code in the format XXXX.']
     922>>> f.clean('20001')
     923Traceback (most recent call last):
     924...
     925ValidationError: [u'Enter a post code in the format XXXX.']
     926>>> f.clean(None)
     927u''
     928>>> f.clean('')
     929u''
     930
     931## AUPhoneNumberField ########################################################
     932
     933A field that accepts a 10 digit Australian phone number.
     934llows spaces and parentheses around area code.
     935
     936>>> from django.contrib.localflavor.au.forms import AUPhoneNumberField
     937>>> f = AUPhoneNumberField()
     938>>> f.clean('1234567890')
     939u'1234567890'
     940>>> f.clean('0213456789')
     941u'0213456789'
     942>>> f.clean('02 13 45 67 89')
     943u'0213456789'
     944>>> f.clean('(02) 1345 6789')
     945u'0213456789'
     946>>> f.clean('123')
     947Traceback (most recent call last):
     948...
     949ValidationError: [u'Phone numbers must consist of 10 digits. Parentheses, white space and hyphens are allowed.']
     950>>> f.clean('1800DJANGO')
     951Traceback (most recent call last):
     952...
     953ValidationError: [u'Phone numbers must consist of 10 digits. Parentheses, white space and hyphens are allowed.']
     954>>> f.clean(None)
     955Traceback (most recent call last):
     956...
     957ValidationError: [u'This field is required.']
     958>>> f.clean('')
     959Traceback (most recent call last):
     960...
     961ValidationError: [u'This field is required.']
     962
     963>>> f = AUPhoneNumberField(required=False)
     964>>> f.clean('1234567890')
     965u'1234567890'
     966>>> f.clean('0213456789')
     967u'0213456789'
     968>>> f.clean('02 13 45 67 89')
     969u'0213456789'
     970>>> f.clean('(02) 1345 6789')
     971u'0213456789'
     972>>> f.clean('123')
     973Traceback (most recent call last):
     974...
     975ValidationError: [u'Phone numbers must consist of 10 digits. Parentheses, white space and hyphens are allowed.']
     976>>> f.clean('1800DJANGO')
     977Traceback (most recent call last):
     978...
     979ValidationError: [u'Phone numbers must consist of 10 digits. Parentheses, white space and hyphens are allowed.']
     980>>> f.clean(None)
     981u''
     982>>> f.clean('')
     983u''
     984
     985## AUStateSelect #############################################################
     986
     987AUStateSelect is a Select widget that uses a list of Australian
     988states/territories as its choices.
     989
     990>>> from django.contrib.localflavor.au.forms import AUStateSelect
     991>>> f = AUStateSelect()
     992>>> print f.render('state', 'NSW')
     993<select name="state">
     994<option value="ACT">Australian Capital Territory</option>
     995<option value="NSW" selected="selected">New South Wales</option>
     996<option value="NT">Northern Territory</option>
     997<option value="QLD">Queensland</option>
     998<option value="SA">South Australia</option>
     999<option value="TAS">Tasmania</option>
     1000<option value="VIC">Victoria</option>
     1001<option value="WA">Western Australia</option>
     1002</select>
    8851003"""
  • AUTHORS

     
    8888    Dirk Eschler <dirk.eschler@gmx.net>
    8989    Marc Fargas <telenieko@telenieko.com>
    9090    favo@exoweb.net
     91    Matthew Flanagan <http://wadofstuff.blogspot.com>
    9192    Eric Floehr <eric@intellovations.com>
    9293    Jorge Gajon <gajon@gajon.org>
    9394    gandalf@owca.info
Back to Top