Ticket #12146: ca_localflavor_with_tests.diff

File ca_localflavor_with_tests.diff, 2.8 KB (added by Mark Lavin, 15 years ago)

New patch with tests.

  • django/contrib/localflavor/ca/forms.py

     
    1212sin_re = re.compile(r"^(\d{3})-(\d{3})-(\d{3})$")
    1313
    1414class CAPostalCodeField(RegexField):
    15     """Canadian postal code field."""
     15    """
     16    Canadian postal code field.
     17
     18    Validates against known invalid characters: D, F, I, O, Q, U
     19    Additionally the first character cannot be Z or W.
     20    For more info see:
     21    http://www.canadapost.ca/tools/pg/manual/PGaddress-e.asp#1402170
     22    """
    1623    default_error_messages = {
    1724        'invalid': _(u'Enter a postal code in the format XXX XXX.'),
    1825    }
    1926
    2027    def __init__(self, *args, **kwargs):
    21         super(CAPostalCodeField, self).__init__(r'^[ABCEGHJKLMNPRSTVXYZ]\d[A-Z] \d[A-Z]\d$',
     28         super(CAPostalCodeField, self).__init__(r'^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ] \d[ABCEGHJKLMNPRSTVWXYZ]\d$',
    2229            max_length=None, min_length=None, *args, **kwargs)
    2330
    2431class CAPhoneNumberField(Field):
  • tests/regressiontests/forms/localflavor/ca.py

     
    6060u''
    6161>>> f.clean('')
    6262u''
     63>>> f.clean('W2S 2H3')
     64Traceback (most recent call last):
     65...
     66ValidationError: [u'Enter a postal code in the format XXX XXX.']
     67>>> f.clean('T2W 2H7')
     68u'T2W 2H7'
     69>>> f.clean('T2S 2W7')
     70u'T2S 2W7'
     71>>> f.clean('Z2S 2H3')
     72Traceback (most recent call last):
     73...
     74ValidationError: [u'Enter a postal code in the format XXX XXX.']
     75>>> f.clean('T2Z 2H7')
     76u'T2Z 2H7'
     77>>> f.clean('T2S 2Z7')
     78u'T2S 2Z7'
     79>>> f.clean('F2S 2H3')
     80Traceback (most recent call last):
     81...
     82ValidationError: [u'Enter a postal code in the format XXX XXX.']
     83>>> f.clean('A2S 2D3')
     84Traceback (most recent call last):
     85...
     86ValidationError: [u'Enter a postal code in the format XXX XXX.']
     87>>> f.clean('A2I 2R3')
     88Traceback (most recent call last):
     89...
     90ValidationError: [u'Enter a postal code in the format XXX XXX.']
     91>>> f.clean('A2I 2R3')
     92Traceback (most recent call last):
     93...
     94ValidationError: [u'Enter a postal code in the format XXX XXX.']
     95>>> f.clean('A2Q 2R3')
     96Traceback (most recent call last):
     97...
     98ValidationError: [u'Enter a postal code in the format XXX XXX.']
     99>>> f.clean('U2B 2R3')
     100Traceback (most recent call last):
     101...
     102ValidationError: [u'Enter a postal code in the format XXX XXX.']
     103>>> f.clean('O2B 2R3')
     104Traceback (most recent call last):
     105...
     106ValidationError: [u'Enter a postal code in the format XXX XXX.']
    63107
    64108# CAPhoneNumberField ##########################################################
Back to Top