Ticket #3876: localflavor_au.diff

File localflavor_au.diff, 7.6 KB (added by Matthew Flanagan <mattimustang@…>, 8 years ago)

localflavor.au 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

    Property changes on: django/contrib/localflavor/au/au_states.py
    ___________________________________________________________________
    Name: svn:eol-style
       + native
    
    
    Property changes on: django/contrib/localflavor/au/__init__.py
    ___________________________________________________________________
    Name: svn:eol-style
       + native
    
     
     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."""
     25        super(AUPhoneNumberField, self).clean(value)
     26        if value in EMPTY_VALUES:
     27            return u''
     28        value = re.sub('(\(|\)|\s+)', '', smart_unicode(value))
     29        phone_match = PHONE_DIGITS_RE.search(value)
     30        if phone_match:
     31            return u'%s' % phone_match.group(1)
     32        raise ValidationError(u'Phone numbers must be in XXXXXXXXXX format.')
     33
     34class AUStateSelect(Select):
     35    """
     36    A Select widget that uses a list of Australian states/territories as its
     37    choices.
     38    """
     39    def __init__(self, attrs=None):
     40        from au_states import STATE_CHOICES # relative import
     41        super(AUStateSelect, self).__init__(attrs, choices=STATE_CHOICES)
  • tests/regressiontests/forms/tests.py

    Property changes on: django/contrib/localflavor/au/forms.py
    ___________________________________________________________________
    Name: svn:eol-style
       + native
    
     
    38793879</select>
    38803880
    38813881
     3882## AUPostCodeField ##########################################################
     3883
     3884A field that accepts a four digit Australian post code.
     3885
     3886>>> from django.contrib.localflavor.au.forms import AUPostCodeField
     3887>>> f = AUPostCodeField()
     3888>>> f.clean('1234')
     3889u'1234'
     3890>>> f.clean('2000')
     3891u'2000'
     3892>>> f.clean('abcd')
     3893Traceback (most recent call last):
     3894...
     3895django.newforms.util.ValidationError: [u'Enter a post code in the format XXXX.']
     3896>>> f.clean('20001')
     3897Traceback (most recent call last):
     3898...
     3899django.newforms.util.ValidationError: [u'Enter a post code in the format XXXX.']
     3900>>> f.clean(None)
     3901Traceback (most recent call last):
     3902...
     3903django.newforms.util.ValidationError: [u'This field is required.']
     3904>>> f.clean('')
     3905Traceback (most recent call last):
     3906...
     3907django.newforms.util.ValidationError: [u'This field is required.']
     3908
     3909>>> f = AUPostCodeField(required=False)
     3910>>> f.clean('1234')
     3911u'1234'
     3912>>> f.clean('2000')
     3913u'2000'
     3914>>> f.clean('abcd')
     3915Traceback (most recent call last):
     3916...
     3917django.newforms.util.ValidationError: [u'Enter a post code in the format XXXX.']
     3918>>> f.clean('20001')
     3919Traceback (most recent call last):
     3920...
     3921django.newforms.util.ValidationError: [u'Enter a post code in the format XXXX.']
     3922>>> f.clean(None)
     3923u''
     3924>>> f.clean('')
     3925u''
     3926
     3927## AUPhoneNumberField ########################################################
     3928
     3929A field that accepts a 10 digit Australian phone number.
     3930llows spaces and parentheses around area code.
     3931
     3932>>> from django.contrib.localflavor.au.forms import AUPhoneNumberField
     3933>>> f = AUPhoneNumberField()
     3934>>> f.clean('1234567890')
     3935u'1234567890'
     3936>>> f.clean('0213456789')
     3937u'0213456789'
     3938>>> f.clean('02 13 45 67 89')
     3939u'0213456789'
     3940>>> f.clean('(02) 1345 6789')
     3941u'0213456789'
     3942>>> f.clean('123')
     3943Traceback (most recent call last):
     3944...
     3945django.newforms.util.ValidationError: [u'Phone numbers must be in XXXXXXXXXX format.']
     3946>>> f.clean('1800DJANGO')
     3947Traceback (most recent call last):
     3948...
     3949django.newforms.util.ValidationError: [u'Phone numbers must be in XXXXXXXXXX format.']
     3950>>> f.clean(None)
     3951Traceback (most recent call last):
     3952...
     3953django.newforms.util.ValidationError: [u'This field is required.']
     3954>>> f.clean('')
     3955Traceback (most recent call last):
     3956...
     3957django.newforms.util.ValidationError: [u'This field is required.']
     3958
     3959>>> f = AUPhoneNumberField(required=False)
     3960>>> f.clean('1234567890')
     3961u'1234567890'
     3962>>> f.clean('0213456789')
     3963u'0213456789'
     3964>>> f.clean('02 13 45 67 89')
     3965u'0213456789'
     3966>>> f.clean('(02) 1345 6789')
     3967u'0213456789'
     3968>>> f.clean('123')
     3969Traceback (most recent call last):
     3970...
     3971django.newforms.util.ValidationError: [u'Phone numbers must be in XXXXXXXXXX format.']
     3972>>> f.clean('1800DJANGO')
     3973Traceback (most recent call last):
     3974...
     3975django.newforms.util.ValidationError: [u'Phone numbers must be in XXXXXXXXXX format.']
     3976>>> f.clean(None)
     3977u''
     3978>>> f.clean('')
     3979u''
     3980
     3981## AUStateSelect #############################################################
     3982
     3983AUStateSelect is a Select widget that uses a list of Australian
     3984states/territories as its choices.
     3985
     3986>>> from django.contrib.localflavor.au.forms import AUStateSelect
     3987>>> f = AUStateSelect()
     3988>>> print f.render('state', 'NSW')
     3989<select name="state">
     3990<option value="ACT">Australian Capital Territory</option>
     3991<option value="NSW" selected="selected">New South Wales</option>
     3992<option value="NT">Northern Territory</option>
     3993<option value="QLD">Queensland</option>
     3994<option value="SA">South Australia</option>
     3995<option value="TAS">Tasmania</option>
     3996<option value="VIC">Victoria</option>
     3997<option value="WA">Western Australia</option>
     3998</select>
     3999
    38824000#################################
    38834001# Tests of underlying functions #
    38844002#################################
  • AUTHORS

     
    8585    Dirk Eschler <dirk.eschler@gmx.net>
    8686    Marc Fargas <telenieko@telenieko.com>
    8787    favo@exoweb.net
     88    Matthew Flanagan <http://wadofstuff.blogspot.com>
    8889    Eric Floehr <eric@intellovations.com>
    8990    Jorge Gajon <gajon@gajon.org>
    9091    gandalf@owca.info
Back to Top