Ticket #13733: nz-localflavor.diff

File nz-localflavor.diff, 8.9 KB (added by Matthew Schinckel, 14 years ago)
  • django/contrib/localflavor/nz/nz_provincial_districts.py

     
     1from django.utils.translation import gettext_lazy as _
     2
     3PROVINCIAL_DISTRICT_CHOICES = (
     4    ('Auckland', _('Auckland Province')),
     5    ('Canterbury', _('Canterbury')),
     6    ('Chatham', _('Chatham Islands')),
     7    ('HawkesBay', _("Hawke's Bay")),
     8    ('Marlborough', _('Marlborough')),
     9    ('Nelson', _('Nelson')),
     10    ('Northland', _('Northland')),
     11    ('Otago', _('Otago Province')),
     12    ('SouthCanterbury', _('South Canterbury')),
     13    ('Southland', _('Southland')),
     14    ('Taranaki', _('Taranaki (New Plymouth)')),
     15    ('Wellington', _('Wellington Province')),
     16    ('Westland', _('Westland')),
     17
     18)
  • django/contrib/localflavor/nz/forms.py

     
     1"""
     2New Zealand-specific Form helpers
     3"""
     4
     5from django.core.validators import EMPTY_VALUES
     6from django.forms import ValidationError
     7from django.forms.fields import Field, RegexField, Select
     8from django.utils.encoding import smart_unicode
     9from django.utils.translation import ugettext_lazy as _
     10import re
     11
     12PHONE_DIGITS_RE = re.compile(r'^((0\d{8})|(02\d{7,9}))$')
     13
     14class NZPostCodeField(RegexField):
     15    """New Zealand post code field."""
     16    default_error_messages = {
     17        'invalid': _('Enter a 4 digit post code.'),
     18    }
     19
     20    def __init__(self, *args, **kwargs):
     21        super(NZPostCodeField, self).__init__(r'^\d{4}$',
     22            max_length=None, min_length=None, *args, **kwargs)
     23
     24class NZPhoneNumberField(Field):
     25    """New Zealand phone number field."""
     26    default_error_messages = {
     27        'invalid': u'Phone numbers must contain 9 digits (including area code),'
     28        ' unless they are mobile numbers (02, then 7-9 digits).',
     29    }
     30
     31    def clean(self, value):
     32        """
     33        Validate a phone number. Strips parentheses, whitespace and hyphens.
     34        """
     35        super(NZPhoneNumberField, self).clean(value)
     36        if value in EMPTY_VALUES:
     37            return u''
     38        value = re.sub('(\(|\)|\s+|-)', '', smart_unicode(value))
     39        phone_match = PHONE_DIGITS_RE.search(value)
     40        if phone_match:
     41            return u'%s' % phone_match.group(1)
     42        raise ValidationError(self.error_messages['invalid'])
     43
     44class NZProvincialDistrictSelect(Select):
     45    """
     46    A Select widget that uses a list of Australian states/territories as its
     47    choices.
     48    """
     49    def __init__(self, attrs=None):
     50        from nz_provincial_districts import PROVINCIAL_DISTRICT_CHOICES
     51        super(NZProvincialDistrictSelect, self).__init__(attrs, choices=PROVINCIAL_DISTRICT_CHOICES)
  • tests/regressiontests/forms/localflavor/nz.py

     
     1# -*- coding: utf-8 -*-
     2# Tests for the contrib/localflavor/ NZ form fields.
     3
     4tests = r"""
     5## NZPostCodeField ##########################################################
     6
     7A field that accepts a four digit New Zealand post code.
     8
     9>>> from django.contrib.localflavor.nz.forms import NZPostCodeField
     10>>> f = NZPostCodeField()
     11>>> f.clean('1234')
     12u'1234'
     13>>> f.clean('2000')
     14u'2000'
     15>>> f.clean('abcd')
     16Traceback (most recent call last):
     17...
     18ValidationError: [u'Enter a 4 digit post code.']
     19>>> f.clean('20001')
     20Traceback (most recent call last):
     21...
     22ValidationError: [u'Enter a 4 digit post code.']
     23>>> f.clean(None)
     24Traceback (most recent call last):
     25...
     26ValidationError: [u'This field is required.']
     27>>> f.clean('')
     28Traceback (most recent call last):
     29...
     30ValidationError: [u'This field is required.']
     31
     32>>> f = NZPostCodeField(required=False)
     33>>> f.clean('1234')
     34u'1234'
     35>>> f.clean('2000')
     36u'2000'
     37>>> f.clean('abcd')
     38Traceback (most recent call last):
     39...
     40ValidationError: [u'Enter a 4 digit post code.']
     41>>> f.clean('20001')
     42Traceback (most recent call last):
     43...
     44ValidationError: [u'Enter a 4 digit post code.']
     45>>> f.clean(None)
     46u''
     47>>> f.clean('')
     48u''
     49
     50## NZPhoneNumberField ########################################################
     51
     52A field that accepts a 9 digit New Zealand phone number, or a 8-10 digit
     53New Zealand mobile phone number (starting with 02).
     54Allows spaces and parentheses around area code.
     55
     56>>> from django.contrib.localflavor.nz.forms import NZPhoneNumberField
     57>>> f = NZPhoneNumberField()
     58>>> f.clean('012345678')
     59u'012345678'
     60>>> f.clean('0213456789')
     61u'0213456789'
     62>>> f.clean('02 13 45 67 8')
     63u'021345678'
     64>>> f.clean('(02) 1345 6789')
     65u'0213456789'
     66>>> f.clean('(02) 1345-6789')
     67u'0213456789'
     68>>> f.clean('(02)1345-6789')
     69u'0213456789'
     70>>> f.clean('0228 123 45')
     71u'022812345'
     72>>> f.clean('123')
     73Traceback (most recent call last):
     74...
     75ValidationError: [u'Phone numbers must contain 9 digits (including area code), unless they are mobile numbers (02, then 7-9 digits).']
     76>>> f.clean('1800DJANGO')
     77Traceback (most recent call last):
     78...
     79ValidationError: [u'Phone numbers must contain 9 digits (including area code), unless they are mobile numbers (02, then 7-9 digits).']
     80>>> f.clean(None)
     81Traceback (most recent call last):
     82...
     83ValidationError: [u'This field is required.']
     84>>> f.clean('')
     85Traceback (most recent call last):
     86...
     87ValidationError: [u'This field is required.']
     88
     89>>> f = NZPhoneNumberField(required=False)
     90>>> f.clean('1234567890')
     91Traceback (most recent call last):
     92...
     93ValidationError: [u'Phone numbers must contain 9 digits (including area code), unless they are mobile numbers (02, then 7-9 digits).']
     94>>> f.clean('0213456789')
     95u'0213456789'
     96>>> f.clean('02 13 45 67 89')
     97u'0213456789'
     98>>> f.clean('(02) 1345 6789')
     99u'0213456789'
     100>>> f.clean('(02) 1345-6789')
     101u'0213456789'
     102>>> f.clean('(02)1345-6789')
     103u'0213456789'
     104>>> f.clean('0408 123 456')
     105Traceback (most recent call last):
     106...
     107ValidationError: [u'Phone numbers must contain 9 digits (including area code), unless they are mobile numbers (02, then 7-9 digits).']
     108
     109>>> f.clean('123')
     110Traceback (most recent call last):
     111...
     112ValidationError: [u'Phone numbers must contain 9 digits (including area code), unless they are mobile numbers (02, then 7-9 digits).']
     113>>> f.clean('1800DJANGO')
     114Traceback (most recent call last):
     115...
     116ValidationError: [u'Phone numbers must contain 9 digits (including area code), unless they are mobile numbers (02, then 7-9 digits).']
     117>>> f.clean(None)
     118u''
     119>>> f.clean('')
     120u''
     121
     122
     123## NZStateSelect #############################################################
     124
     125NZProvincialDistrictSelect is a Select widget that uses a list of New Zealand
     126provincial districts as its choices.
     127
     128>>> from django.contrib.localflavor.nz.forms import NZProvincialDistrictSelect
     129>>> f = NZProvincialDistrictSelect()
     130>>> print f.render('provincial_district', 'Wellington')
     131<select name="provincial_district">
     132<option value="Auckland">Auckland Province</option>
     133<option value="Canterbury">Canterbury</option>
     134<option value="Chatham">Chatham Islands</option>
     135<option value="HawkesBay">Hawke's Bay</option>
     136<option value="Marlborough">Marlborough</option>
     137<option value="Nelson">Nelson</option>
     138<option value="Northland">Northland</option>
     139<option value="Otago">Otago Province</option>
     140<option value="SouthCanterbury">South Canterbury</option>
     141<option value="Southland">Southland</option>
     142<option value="Taranaki">Taranaki (New Plymouth)</option>
     143<option value="Wellington" selected="selected">Wellington Province</option>
     144<option value="Westland">Westland</option>
     145</select>
     146
     147"""
     148 No newline at end of file
  • tests/regressiontests/forms/tests.py

     
    2222from localflavor.jp import tests as localflavor_jp_tests
    2323from localflavor.kw import tests as localflavor_kw_tests
    2424from localflavor.nl import tests as localflavor_nl_tests
     25from localflavor.nz import tests as localflavor_nz_tests
    2526from localflavor.pl import tests as localflavor_pl_tests
    2627from localflavor.pt import tests as localflavor_pt_tests
    2728from localflavor.ro import tests as localflavor_ro_tests
     
    6566    'localflavor_jp_tests': localflavor_jp_tests,
    6667    'localflavor_kw_tests': localflavor_kw_tests,
    6768    'localflavor_nl_tests': localflavor_nl_tests,
     69    'localflavor_nz_tests': localflavor_nz_tests,
    6870    'localflavor_pl_tests': localflavor_pl_tests,
    6971    'localflavor_pt_tests': localflavor_pt_tests,
    7072    'localflavor_ro_tests': localflavor_ro_tests,
Back to Top